Godot Version
4.3
Question
I’m making a modding system for my game, where the modders write their code in C# and compile it to a DLL, which the game loads and executes methods from.
The relevant part of the compile method (returns an Assembly)
string dll = //the absolute path...
if (File.Exists(dll)) {
return Assembly.LoadFrom(dll);
}
Then I invoke the method like this:
private void ExecuteModMethod(Mod mod, string methodName, object[] methodParameters)
{
if (!useMods) return;
Type type = mod.Assembly.GetType(mod.Name+".Main");
if (type == null) return;
MethodInfo method = type.GetMethod(methodName);
mod.Instance = Activator.CreateInstance(type);
if (method == null) return;
method.Invoke(mod.Instance, methodParameters);
}
When I try to invoke the _Ready
method, (modders should name their init method after Godot’s one for consistensy), I get Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
This is the only code file in the mod:
using Godot;
namespace TestMod
{
public class Main
{
//Called when the game is loaded
public void _Ready()
{
GD.Print("Hello from the mod!");
}
}
}
I don’t know much about DLLs and this sort of thing so any help is appreciated. I just need the error to go away because it stops execution of the application and so no method is invoked.
NOTES
- I know this is really dangerous and modders could include malware. I will deal with this problem, you don’t need to mention how dangerous it is, thanks.
- This is the only error thrown, the dll path,
type
,method
andmod.Instance
are all not null. That is not the problem. - I saw this Github post Godot 4 dynamically evaluating C# script then crash, Fatal error.System.AccessViolationException · Issue #92906 · godotengine/godot · GitHub but I couldn’t figure out how to make it work for my use case