I have a modloader developed that a previous question I have asked was based upon. I have encountered a mysterious issue that doesn't print any errors or raise any eyebrows, just quietly fails and I have been debugging for a week no with no result.
This is my code for the modloader and the problematic function:
public class ModeLoader
{
public ModeLoader(string directory)
{
_directory = directory;
}
private readonly string _directory;
internal Dictionary<string, Assembly> loadedModes = new Dictionary<string, Assembly>();
public void Load(string name)
{
if (name.EndsWith("PantixApi.dll")) return;
loadedModes.Add(name.Replace(".dll", ""), Assembly.LoadFrom($"{_directory}/{name}"));
name = name.Replace(".dll", "");
Type t = loadedModes[name].GetType($"{name}.PantixMode");
t.GetMethod("Load").Invoke(null, null);
}
public void SelectLoadMode(string name)
{
foreach (KeyValuePair<string, Assembly> i in loadedModes)
{
if (i.Value.GetType($"{i.Key}.ExtraMode").GetMember(name) != null)
{
i.Value.GetType($"{i.Key}.PantixMode").GetMethod("OnModeLoaded").Invoke(null, null);
return;
}
}
return;
}
}
This is the code for the invoked function in question:
// when the user selects the mode
public static void OnModeLoaded()
{
Console.ForegroundColor = ConsoleColor.Green;
PantixConsole.WriteSlow("[PantixModelMode] ", 5);
Console.ForegroundColor = ConsoleColor.White;
PantixConsole.WriteSlow("Good morning ", 10);
Console.ForegroundColor = ConsoleColor.DarkYellow;
PantixConsole.WriteSlow("starshine", 30);
Console.ForegroundColor = ConsoleColor.White;
PantixConsole.WriteSlow(", the world says ", 10);
Console.ForegroundColor = ConsoleColor.Cyan;
PantixConsole.WriteSlow("hello", 10);
Console.ForegroundColor = ConsoleColor.White;
PantixConsole.WriteLineSlow("!", 10);
}
This is the code for the function calling the reflection function:
dynamic ChangeMode(string[] args)
{
if (args.Length != 2)
{
InvalidArgs(1);
return null;
}
switch (args[1])
{
case "pantix":
mode = DefaultMode.pantix;
break;
case "dbot":
mode = DefaultMode.dbot;
break;
case "net":
mode = DefaultMode.net;
break;
default:
try { modelo.SelectLoadMode(args[1]); }
catch { InvalidArgs(1); }
break;
}
return null;
}
This is the code for ExtraMode
:
public class ExtraMode : CurrentMode
{
public ExtraMode(string name) : base(name) { }
public static CurrentMode model = new ExtraMode("model");
}
This is the command I am running:
cm model
When debugging, GetMethod
does return the function for OnModeLoaded
. Invoke
returns null
, which doesn't seem out of the ordinary considering that OnModeLoaded
returns void
. I have also check for exceptions by removing the try block, with no avail.
ModeLoader.Load
works perfectly fine as expected even though the code is near identical.
I have been banging my head on the desk for a while now and this is seriously stunting my development.
Sorry if I supplied too much code...
Aucun commentaire:
Enregistrer un commentaire