internal bool LoadModDll() { if (Instance != null) { throw new Exception("Error! Mod has already been loaded!"); } try { ModAssembly = Assembly.Load(GetDllBytes()); if (ModAssembly.GetTypes().Count(x => x.BaseType == typeof(Mod)) > 0) { var type = ModAssembly.GetTypes().First(x => x.BaseType == typeof(Mod)); Mod instance = (Mod)ModAssembly.CreateInstance(type.ToString()); if (instance != null) { instance.ModSettings = this; instance.Entry(); } Instance = instance; Log.Verbose($"Loaded mod dll: {Name}"); } else { var types = ModAssembly.GetTypes(); foreach (var layer in ModLoader.CompatibilityLayers) { if (!layer.ContainsOurModType(types)) { continue; } Instance = layer.LoadMod(ModAssembly, types, this); Log.Verbose($"Loaded mod dll: {Name}"); break; } } if (Instance == null) { throw new Exception("Invalid Mod DLL"); } } catch (Exception ex) { var exception = ex as ReflectionTypeLoadException; if (exception != null) { foreach (var e in exception.LoaderExceptions) { Log.Exception("loaderexceptions entry: " + $"{e.Message} ${e.Source} ${e.TargetSite} ${e.StackTrace} ${e.Data}", e); } } Log.Exception("Error loading mod DLL", ex); //throw new Exception(string.Format($"Failed to load mod '{modDllPath}'\n\t-{ex.Message}\n\t\t-{ex.StackTrace}"), ex); } return(Instance != null); }
internal bool LoadModDll() { if (Instance != null) { throw new Exception("Error! Mod has already been loaded!"); } var modDllPath = $"{ModDirectory}\\{ModDll}"; if (!modDllPath.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase)) { modDllPath += ".dll"; } try { ModAssembly = Assembly.LoadFrom(modDllPath); if (ModAssembly.GetTypes().Count(x => x.BaseType == typeof(Farmhand.Mod)) > 0) { var type = ModAssembly.GetTypes().First(x => x.BaseType == typeof(Farmhand.Mod)); Mod instance = (Mod)ModAssembly.CreateInstance(type.ToString()); if (instance != null) { instance.ModSettings = this; instance.Entry(); } Instance = instance; Log.Verbose($"Loaded mod dll: {Name}"); } else { var types = ModAssembly.GetTypes(); foreach (var layer in ModLoader.CompatibilityLayers) { if (!layer.ContainsOurModType(types)) { continue; } Instance = layer.LoadMod(ModAssembly, types, this); Log.Verbose($"Loaded mod dll: {Name}"); break; } } if (Instance == null) { throw new Exception("Invalid Mod DLL"); } } catch (Exception ex) { Log.Exception("Error loading mod DLL", ex); //throw new Exception(string.Format($"Failed to load mod '{modDllPath}'\n\t-{ex.Message}\n\t\t-{ex.StackTrace}"), ex); } return(Instance != null); }
internal void Initialize() { string path = Path.Combine(FileIO.DataDirectory, InternalName, InternalName + ".dll"); ModAssembly = Assembly.LoadFrom(path); Type mainType = ModAssembly.GetTypes().First(t => typeof(IModEntryPoint).IsAssignableFrom(t)); object obj = Activator.CreateInstance(mainType); ((IModEntryPoint)obj).Main(); IsLoaded = true; }
internal static void Initialize(string[] args = null) { if (args != null) { for (int i = 0; i < args.Length; i++) { if (args[i] == "--mod" && Directory.Exists(Path.Combine(Application.StartupPath, "mods", args[i + 1]))) { ModName = args[i + 1]; } if (args[i] == "--visisble3d") //this opens a form and renders the gameplay in realtime(will certainly make it slower, well, unsure. Since otherwise it would be rendered in memory) { new FrmMain().Show(); //requires client zips } } } EventRegistry = new GameEventRegistry(); BF2Engine = new BF2Engine(); if (string.IsNullOrEmpty(ModName)) { Console.WriteLine("No mod was defined, which mod would you like to load?"); var directories = Directory.EnumerateDirectories(Path.Combine(Application.StartupPath, "mods")).Select(d => new DirectoryInfo(d).Name); for (int i = 0; i < directories.Count(); i++) { Console.WriteLine("[" + i + "] " + directories.ToArray()[i]); } Console.Write(">"); var mod = Console.ReadLine(); if (!directories.Contains(mod)) { Console.WriteLine("Invalid mod! Press any key to exit..."); Console.ReadKey(); Environment.Exit(-1); } ModName = "mods/" + mod; ModAssembly = Assembly.Load(File.ReadAllBytes(Path.Combine(Application.StartupPath, ModName, "phoenixmod.dll"))); ModPath = Path.Combine(Application.StartupPath, ModName); LoadedMod = (IMod)Activator.CreateInstance(ModAssembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IMod))).First()); ConFileProcessor = LoadedMod.GetConFileProcessor(); } LoadedMod.Initialize(BF2Engine, EventRegistry); Console.WriteLine("Loaded " + LoadedMod.Name); BF2Engine.InitEngine(); BF2Engine.LoadServerArchives(); }
/// <summary> /// Scans the mod to see if it has loaded the assembly in to app domain. /// </summary> /// /// <param name="mod">The <see cref="PluginInfo"/> to scan.</param> /// <param name="assemblyName">The name of the assembly to search for.</param> /// <param name="details">If found, the <see cref="ModAssembly"/> struct containing details.</param> /// /// <returns>Returns <c>true</c> if successful, otherwise <c>false</c>.</returns> private static bool TryGetLoadedAssembly(PluginInfo mod, string assemblyName, out ModAssembly details) { List <Assembly> assemblies = mod.GetAssemblies(); foreach (Assembly asm in assemblies) { if (asm.GetName().Name == assemblyName) { string hash = string.Empty; if (TryGetAssemblyPath(mod, assemblyName, out string fullPath)) { if (Md5.TryGetHash(fullPath, out string md5Hash)) { hash = md5Hash; } } details = new ModAssembly { ModPath = mod.modPath, ModName = GetName(mod), ModEnabled = mod.isEnabled, Folder = Path.GetFileName(mod.modPath), AsmDetails = asm.GetName(), AsmLoaded = true, AsmMD5Hash = hash, }; return(true); } } details = default; return(false); }
/// <summary> /// Scans the mod folder to see if contains an unloaded copy of the assembly. /// </summary> /// /// <param name="mod">The <see cref="PluginInfo"/> to scan.</param> /// <param name="assemblyName">The name of the assembly to search for.</param> /// <param name="details">If found, the <see cref="ModAssembly"/> struct containing details.</param> /// /// <returns>Returns <c>true</c> if successful, otherwise <c>false</c>.</returns> private static bool TryGetUnloadedAssembly(PluginInfo mod, string assemblyName, out ModAssembly details) { details = default; // we're only interested in Harmony for now... if (assemblyName != "0Harmony") { return(false); } if (TryGetAssemblyPath(mod, assemblyName, out string fullPath)) { details = new ModAssembly { ModPath = mod.modPath, ModName = GetName(mod), ModEnabled = mod.isEnabled, Folder = Path.GetFileName(mod.modPath), AsmLoaded = false, }; if (Md5.TryGetHash(fullPath, out string md5Hash)) { details.AsmMD5Hash = md5Hash; } if (Assemblies.TryGetAssemblyDetailsWithoutLoading(fullPath, out AssemblyName asmDetails)) { details.AsmDetails = asmDetails; } return(true); } return(false); }
/// <summary> /// Exported hook. Do not use. /// </summary> public static void SetupMomLoader() { Utils.Log("Setup MomLoader"); if (OneTimeSetup) { return; } Utils.Log("Starting to load assemblies"); //Get a list of all the Assemblies to load in mods/assemblies string fileDir = ModsManager.instance.ModsFolder + "/Assemblies"; string[] filePaths = Directory.GetFiles(fileDir, "*.dll", SearchOption.TopDirectoryOnly); Utils.Log($"MomLoader loading Assemblies: {filePaths.Length}"); OneTimeSetup = true; if (filePaths == null) { return; } if (filePaths.Length == 0) { return; } object ModInstance; Assembly ModAssembly; Type ModType; string typeName; foreach (string dll in filePaths) { Utils.Log($"Loading and instancing {dll}"); ModAssembly = Assembly.LoadFile(dll); //Trim and retrieve typename from filename typeName = dll.Replace(fileDir, ""); typeName = typeName.Substring(1); typeName = typeName.Remove(typeName.Length - 4) + ".ModMain"; Utils.Log($"TypeName for this assembly: {typeName}"); ModType = ModAssembly.GetType(typeName); if (ModType == null) { Utils.Log("Skipping this DLL. No ModMain"); continue; } ModInstance = Activator.CreateInstance(ModType); if (ModInstance == null) { continue; } //Update the ModList ((ModContainer)ModInstance).GetModInfo.AssemblyName = typeName.Replace(".ModMain", ""); AllMods.Add((ModContainer)ModInstance); } }