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); }
public HookInjector(Injector injector, MethodDefinition originalMethod, MethodReference customMethodReference, MethodHookType hookType, MethodHookFlags hookFlags) { Injector = injector; OriginalMethod = originalMethod; CustomMethodReference = customMethodReference; HookType = hookType; HookFlags = hookFlags; GenericArguments = new TypeReference[originalMethod.GenericParameters.Count]; if (originalMethod.HasGenericParameters) { //throw new NotImplementedException("Hooking generic instances? Never tested it yet. Contact creator, pls!"); for (var i = 0; i < originalMethod.GenericParameters.Count; i++) { GenericArguments[i] = originalMethod.GenericParameters[i]; } } originalMethod.Body.SimplifyMacros(); ILGen = OriginalMethod.Body.GetILProcessor(); }
public CustomLoadArgsHookInjector(Injector inj, List<Tuple<OpCode, byte?>> instructionData, MethodDefinition methodBase, MethodReference methodInfo, MethodHookType methodHookType, MethodHookFlags methodHookFlags) : base(inj, methodBase, methodInfo, methodHookType, methodHookFlags) { this.instructionData = instructionData; }