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 BeforeAndAfterMethodHook(MethodBase intercepted, MethodInfo custom_before_method, MethodInfo custom_after_method) { OnBeforeHook = new MethodHook(intercepted, custom_before_method, MethodHookType.RunBefore); OnAfterHook = new MethodHook(intercepted, custom_after_method, MethodHookType.RunAfter); }
protected void InjectHook(MethodHook hook) { if (hook.IsStaticInterception) { InjectStaticHook( MethodBaseToMethodDefinition(hook.InterceptedMethod), Module.Import(hook.CustomMethod), hook.HookType, hook.HookFlags ); } else { InjectInstanceHook( MethodBaseToMethodDefinition(hook.InterceptedMethod), Module.Import(hook.CustomMethod), hook.HookType, hook.HookFlags ); } }