internal ModdingEnvironmentWriter(IMod[] mods_to_use, IMod[] dependencies_to_use, bool useHiDefProfile) { config = ModdingEnvironmentConfiguration.Create(); ModEnvironment.RequestSetupDataReset(); all_mods_to_process = mods_to_use; all_possible_dependencies = dependencies_to_use; var source_exe = ModManager.GameDirectory.ContainingFile(ModManager.OriginalExecutable); //new System.IO.FileInfo(System.IO.Path.Combine(base_directoy.FullName, source_exe_name)); var modded_exe = ModManager.GameDirectory.ContainingFile(ModManager.ModdedExecutable); //new System.IO.FileInfo(System.IO.Path.Combine(base_directoy.FullName, modded_exe_name)); var source_lib = ModManager.GameDirectory.ContainingFile(ModManager.OriginalLibrary); var modded_lib = ModManager.GameDirectory.ContainingFile(ModManager.ModdedLibrary); game_injector = new GnomoriaExeInjector(source_exe); lib_injector = new Injector(source_lib); config.Hashes.SourceExecutable = source_exe.GenerateMD5Hash(); config.Hashes.SourceLibrary = source_lib.GenerateMD5Hash(); // may switch those 2 later to have it outside... game_injector.Inject_SetContentRootDirectoryToCurrentDir_InsertAtStartOfMain(); game_injector.Inject_CallTo_ModRuntimeController_Initialize_AtStartOfMain(ModManager.GameDirectory.ContainingFile(ModManager.ModController)); //game_injector.Inject_TryCatchWrapperAroundEverthingInMain_WriteCrashLog(); //game_injector.Inject_CurrentAppDomain_AddResolveEventAtStartOfMain(); game_injector.Inject_SaveLoadCalls(); //game_injector.Inject_TryCatchWrapperAroundGnomanEmpire_LoadGame(); game_injector.Debug_ManipulateStuff(); if (useHiDefProfile) { game_injector.Inject_AddHighDefXnaProfile(); } foreach (var mod in mods_to_use) { ProcessMod(mod); } var allLoadedStuff = processedMods.Select(mod => Tuple.Create(mod, mod.Dependencies.Union(mod.InitAfter.Where(befor => processedMods.Contains(befor.GetInstance()))).Select(type => type.GetInstance()))); var processedMods_sortedByDependencyAndInitAfter = DependencySort.Sort(allLoadedStuff); config.SetModReferences(processedMods_sortedByDependencyAndInitAfter.Select(mod => new ModReference(mod)).ToArray()); //Mono.Cecil.WriterParameters game_injector.Write(modded_exe); lib_injector.Write(modded_lib); config.Hashes.ModdedExecutable = modded_exe.GenerateMD5Hash(); config.Hashes.ModdedLibrary = modded_lib.GenerateMD5Hash(); }
public void ModifyGnomoria() { Reference.GnomodiaDirectory.GetFile(RuntimeModController.Log.LogfileName).Delete(); var sourceExe = Reference.GameDirectory.GetFile(Reference.OriginalExecutable); var moddedExe = Reference.GnomodiaDirectory.GetFile(Reference.ModdedExecutable); var sourceLib = Reference.GameDirectory.GetFile(Reference.OriginalLibrary); var moddedLib = Reference.GnomodiaDirectory.GetFile(Reference.ModdedLibrary); var gameInjector = new GnomoriaExeInjector(sourceExe); var libInjector = new Injector(sourceLib); gameInjector.InitializeGnomodia(Reference.GnomodiaDirectory.GetFile(Reference.GnomodiaLibrary)); gameInjector.InjectMapGenerationCalls(); gameInjector.InjectSaveLoadCalls(); //gameInjector.Debug_ManipulateStuff(); foreach (var mod in ModManager.CreateOrGetAllMods()) { var interceptedMethods = from method in mod.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) where method.GetCustomAttributes(typeof(InterceptMethodAttribute), false).Any() select new { Method = method, Attribute = method.GetCustomAttributes(typeof(InterceptMethodAttribute), false).Cast <InterceptMethodAttribute>().Single() }; foreach (var interceptedMethod in interceptedMethods) { var attribute = interceptedMethod.Attribute; IModification mh = new MethodHook(attribute.InterceptedMethod, interceptedMethod.Method, attribute.HookType, attribute.HookFlags); if (gameInjector.AssemblyContainsType(mh.TargetType)) { gameInjector.InjectModification(mh); } else if (libInjector.AssemblyContainsType(mh.TargetType)) { libInjector.InjectModification(mh); } else { throw new InvalidOperationException(string.Format("Cannot change behavior of type {0}!", mh.TargetType)); } } } gameInjector.Write(moddedExe); libInjector.Write(moddedLib); }