public HookGenerator(MonoModder modder, string name) { Modder = modder; OutputModule = ModuleDefinition.CreateModule(name, new ModuleParameters { Architecture = modder.Module.Architecture, AssemblyResolver = modder.Module.AssemblyResolver, Kind = ModuleKind.Dll, Runtime = modder.Module.Runtime }); Namespace = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE"); if (string.IsNullOrEmpty(Namespace)) { Namespace = "On"; } HookOrig = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_ORIG") == "1"; HookPrivate = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE") == "1"; modder.MapDependency(modder.Module, "MonoMod.RuntimeDetour"); if (!modder.DependencyCache.TryGetValue("MonoMod.RuntimeDetour", out md_RuntimeDetour)) { throw new FileNotFoundException("MonoMod.RuntimeDetour not found!"); } t_MulticastDelegate = OutputModule.ImportReference(modder.FindType("System.MulticastDelegate")); t_IAsyncResult = OutputModule.ImportReference(modder.FindType("System.IAsyncResult")); t_AsyncCallback = OutputModule.ImportReference(modder.FindType("System.AsyncCallback")); t_MethodBase = OutputModule.ImportReference(modder.FindType("System.Reflection.MethodBase")); t_RuntimeMethodHandle = OutputModule.ImportReference(modder.FindType("System.RuntimeMethodHandle")); TypeDefinition td_HookManager = md_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookManager"); t_HookManager = OutputModule.ImportReference(td_HookManager); m_GetMethodFromHandle = OutputModule.ImportReference( new MethodReference("GetMethodFromHandle", t_MethodBase, t_MethodBase) { Parameters = { new ParameterDefinition(t_RuntimeMethodHandle) } } ); m_Add = OutputModule.ImportReference(td_HookManager.FindMethod("Add")); m_Remove = OutputModule.ImportReference(td_HookManager.FindMethod("Remove")); }
public HookGenerator(MonoModder modder, string name) { Modder = modder; OutputModule = ModuleDefinition.CreateModule(name, new ModuleParameters { Architecture = modder.Module.Architecture, AssemblyResolver = modder.Module.AssemblyResolver, Kind = ModuleKind.Dll, Runtime = modder.Module.Runtime }); // Copy all assembly references from the input module. // Cecil + .NET Standard libraries + .NET 5.0 = weirdness. modder.MapDependencies(); OutputModule.AssemblyReferences.AddRange(modder.Module.AssemblyReferences); modder.DependencyMap[OutputModule] = new List <ModuleDefinition>(modder.DependencyMap[modder.Module]); Namespace = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE"); if (string.IsNullOrEmpty(Namespace)) { Namespace = "On"; } NamespaceIL = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE_IL"); if (string.IsNullOrEmpty(NamespaceIL)) { NamespaceIL = "IL"; } HookOrig = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_ORIG") == "1"; HookPrivate = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE") == "1"; modder.MapDependency(modder.Module, "MonoMod.RuntimeDetour"); if (!modder.DependencyCache.TryGetValue("MonoMod.RuntimeDetour", out module_RuntimeDetour)) { throw new FileNotFoundException("MonoMod.RuntimeDetour not found!"); } modder.MapDependency(modder.Module, "MonoMod.Utils"); if (!modder.DependencyCache.TryGetValue("MonoMod.Utils", out module_Utils)) { throw new FileNotFoundException("MonoMod.Utils not found!"); } t_MulticastDelegate = OutputModule.ImportReference(modder.FindType("System.MulticastDelegate")); t_IAsyncResult = OutputModule.ImportReference(modder.FindType("System.IAsyncResult")); t_AsyncCallback = OutputModule.ImportReference(modder.FindType("System.AsyncCallback")); t_MethodBase = OutputModule.ImportReference(modder.FindType("System.Reflection.MethodBase")); t_RuntimeMethodHandle = OutputModule.ImportReference(modder.FindType("System.RuntimeMethodHandle")); t_EditorBrowsableState = OutputModule.ImportReference(modder.FindType("System.ComponentModel.EditorBrowsableState")); TypeDefinition td_HookEndpointManager = module_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookGen.HookEndpointManager"); t_ILManipulator = OutputModule.ImportReference( module_Utils.GetType("MonoMod.Cil.ILContext/Manipulator") ); m_Object_ctor = OutputModule.ImportReference(modder.FindType("System.Object").Resolve().FindMethod("System.Void .ctor()")); m_ObsoleteAttribute_ctor = OutputModule.ImportReference(modder.FindType("System.ObsoleteAttribute").Resolve().FindMethod("System.Void .ctor(System.String,System.Boolean)")); m_EditorBrowsableAttribute_ctor = OutputModule.ImportReference(modder.FindType("System.ComponentModel.EditorBrowsableAttribute").Resolve().FindMethod("System.Void .ctor(System.ComponentModel.EditorBrowsableState)")); m_GetMethodFromHandle = OutputModule.ImportReference( new MethodReference("GetMethodFromHandle", t_MethodBase, t_MethodBase) { Parameters = { new ParameterDefinition(t_RuntimeMethodHandle) } } ); m_Add = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Add")); m_Remove = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Remove")); m_Modify = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Modify")); m_Unmodify = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Unmodify")); }
public HookGenerator(MonoModder modder, string name) { Modder = modder; OutputModule = ModuleDefinition.CreateModule(name, new ModuleParameters { Architecture = modder.Module.Architecture, AssemblyResolver = modder.Module.AssemblyResolver, Kind = ModuleKind.Dll, Runtime = modder.Module.Runtime }); RDRefHide = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_RDREF_HIDE") == "1"; Namespace = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE"); if (string.IsNullOrEmpty(Namespace)) { Namespace = "On"; } NamespaceIL = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE_IL"); if (string.IsNullOrEmpty(NamespaceIL)) { NamespaceIL = "IL"; } HookOrig = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_ORIG") == "1"; HookPrivate = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE") == "1"; HookExtName = Environment.GetEnvironmentVariable("MONOMOD_HOOKGEN_EXTENSIONS"); if (string.IsNullOrEmpty(HookExtName)) { HookExtName = $"ːHookExtensionsː{NameVerifyRegex.Replace(modder.Module.Assembly.Name.Name, "_")}"; } modder.MapDependency(modder.Module, "MonoMod.RuntimeDetour"); if (!modder.DependencyCache.TryGetValue("MonoMod.RuntimeDetour", out module_RuntimeDetour)) { throw new FileNotFoundException("MonoMod.RuntimeDetour not found!"); } t_MulticastDelegate = OutputModule.ImportReference(modder.FindType("System.MulticastDelegate")); t_IAsyncResult = OutputModule.ImportReference(modder.FindType("System.IAsyncResult")); t_AsyncCallback = OutputModule.ImportReference(modder.FindType("System.AsyncCallback")); t_MethodBase = OutputModule.ImportReference(modder.FindType("System.Reflection.MethodBase")); t_RuntimeMethodHandle = OutputModule.ImportReference(modder.FindType("System.RuntimeMethodHandle")); t_EditorBrowsableState = OutputModule.ImportReference(modder.FindType("System.ComponentModel.EditorBrowsableState")); TypeDefinition td_HookEndpointManager = module_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookGen.HookEndpointManager"); td_HookExtensions = module_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookGen.HookExtensions"); t_HookExtensions = OutputModule.ImportReference(td_HookExtensions); t_HookEndpointManager = OutputModule.ImportReference(td_HookEndpointManager); t_HookEndpointManager = OutputModule.ImportReference(module_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookGen.HookIL")); t_ILManipulator = OutputModule.ImportReference(module_RuntimeDetour.GetType("MonoMod.RuntimeDetour.HookGen.ILManipulator")); m_Object_ctor = OutputModule.ImportReference(modder.FindType("System.Object").Resolve().FindMethod("System.Void .ctor()")); m_ObsoleteAttribute_ctor = OutputModule.ImportReference(modder.FindType("System.ObsoleteAttribute").Resolve().FindMethod("System.Void .ctor(System.String,System.Boolean)")); m_EditorBrowsableAttribute_ctor = OutputModule.ImportReference(modder.FindType("System.ComponentModel.EditorBrowsableAttribute").Resolve().FindMethod("System.Void .ctor(System.ComponentModel.EditorBrowsableState)")); m_GetMethodFromHandle = OutputModule.ImportReference( new MethodReference("GetMethodFromHandle", t_MethodBase, t_MethodBase) { Parameters = { new ParameterDefinition(t_RuntimeMethodHandle) } } ); m_Add = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Add")); m_Remove = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Remove")); m_Modify = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Modify")); m_Unmodify = OutputModule.ImportReference(td_HookEndpointManager.FindMethod("Unmodify")); }