I'm making a modding system for my game in Godot, 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, (the Godot init method, that the modders should also name their init method), 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,methodandmod.Instanceare all not null. That is not the problem.
_Readymethod and which specific statement it failed in.