/// <summary> /// Invokes a method in every mod. /// </summary> /// <param name="methodName">Method name of the method to call.</param> /// <param name="args">Arguments to pass - null for none.</param> public static void Invoke(string methodName, object[] args = null) { Type[] argsTypes = null; if (args == null) { args = _EmptyObjectArray; argsTypes = _EmptyTypeArray; } for (int i = 0; i < _ModuleTypes.Count; i++) { GameMod mod = Mods[i]; IDictionary <string, MethodInfo> moduleMethods = _ModuleMethods[i]; MethodInfo method; if (moduleMethods.TryGetValue(methodName, out method)) { method?.GetDelegate()?.Invoke(Mods[i], args); continue; } if (argsTypes == null) { argsTypes = Type.GetTypeArray(args); } method = _ModuleTypes[i].GetMethod(methodName, argsTypes); moduleMethods[methodName] = method; method?.GetDelegate()?.Invoke(Mods[i], args); } }
public static void LoadMod(GameModMetadata meta, Assembly asm) { ModContent.Crawl(null, asm); Type[] types; try { types = asm.GetTypes(); } catch (Exception e) { e.LogDetailed(); ModLogger.Log("loader", $"Failed reading assembly: {e}"); return; } for (int i = 0; i < types.Length; i++) { Type type = types[i]; if (!typeof(GameMod).IsAssignableFrom(type) || type.IsAbstract) { continue; } GameMod mod = (GameMod)type.GetConstructor(_EmptyTypeArray).Invoke(_EmptyObjectArray); mod.Metadata = meta; Mods.Add(mod); _ModuleTypes.Add(type); _ModuleMethods.Add(new FastDictionary <string, MethodInfo>()); } ModLogger.Log("loader", $"Mod {meta} initialized."); }