diff --git a/Harmony/HookInjector.cs b/Harmony/HookInjector.cs index 6f78b4fb..a6e8dabb 100644 --- a/Harmony/HookInjector.cs +++ b/Harmony/HookInjector.cs @@ -106,7 +106,7 @@ public void Detour(byte[] payload, bool isNew) // 0 (long) // var size = prefixBytes + (1 + 1 + IntPtr.Size + 2) + sizeof(long); - memory = Platform.AllocateMemory(size); + memory = Platform.GetMemory(size); } else { diff --git a/Harmony/Platform.cs b/Harmony/Platform.cs index 34ac2d64..063c2388 100644 --- a/Harmony/Platform.cs +++ b/Harmony/Platform.cs @@ -1,5 +1,5 @@ using System; -using System.Runtime.InteropServices; +using System.Reflection.Emit; namespace Harmony { @@ -11,12 +11,6 @@ public static bool IsAnyUnix() return p == 4 || p == 6 || p == 128; } - public static unsafe long AllocateMemory(int size) - { - var monoCodeManager = mono_code_manager_new(); - return (long)mono_code_manager_reserve(monoCodeManager, size); - } - internal static unsafe long PeekJmp(long memory) { byte* p = (byte*)memory; @@ -109,42 +103,24 @@ public static unsafe long WriteLong(long memory, long value) return memory + sizeof(long); } - [DllImport("mono.dll", EntryPoint = "mono_code_manager_new")] - static extern private unsafe void* win_mono_code_manager_new(); - - [DllImport("RimWorldLinux_Data/Mono/x86_64/libmono.so", EntryPoint = "mono_code_manager_new")] - static extern private unsafe void* linux_64_mono_code_manager_new(); - - [DllImport("RimWorldLinux_Data/Mono/x86/libmono.so", EntryPoint = "mono_code_manager_new")] - static extern private unsafe void* linux_86_mono_code_manager_new(); - - public static unsafe void* mono_code_manager_new() - { - if (IsAnyUnix()) - { - if (IntPtr.Size == sizeof(long)) return linux_64_mono_code_manager_new(); - else return linux_86_mono_code_manager_new(); - } - return win_mono_code_manager_new(); - } - - [DllImport("mono.dll", EntryPoint = "mono_code_manager_reserve")] - static extern private unsafe void* win_mono_code_manager_reserve(void* MonoCodeManager, int size); - - [DllImport("RimWorldLinux_Data/Mono/x86_64/libmono.so", EntryPoint = "mono_code_manager_reserve")] - static extern private unsafe void* linux_64_mono_code_manager_reserve(void* MonoCodeManager, int size); - - [DllImport("RimWorldLinux_Data/Mono/x86/libmono.so", EntryPoint = "mono_code_manager_reserve")] - static extern private unsafe void* linux_86_mono_code_manager_reserve(void* MonoCodeManager, int size); - - public static unsafe void* mono_code_manager_reserve(void* MonoCodeManager, int size) + delegate void Dummy(); + public static long GetMemory(int size) // TODO: size ignored for now { - if (IsAnyUnix()) + var dm = new DynamicMethod("", typeof(void), new Type[] { }); + var il = dm.GetILGenerator(); + il.DeclareLocal(typeof(int)); + il.Emit(OpCodes.Ldc_I4, 0); + for (int i = 1; i <= 16; i++) { - if (IntPtr.Size == sizeof(long)) return linux_64_mono_code_manager_reserve(MonoCodeManager, size); - else return linux_86_mono_code_manager_reserve(MonoCodeManager, size); + il.Emit(OpCodes.Stloc_0); + il.Emit(OpCodes.Ldloc_0); + il.Emit(OpCodes.Ldc_I4, i); + il.Emit(OpCodes.Add); } - return win_mono_code_manager_reserve(MonoCodeManager, size); + il.Emit(OpCodes.Stloc_0); + il.Emit(OpCodes.Ret); + var m = dm.CreateDelegate(typeof(Dummy)); + return m.Method.MethodHandle.GetFunctionPointer().ToInt64(); } } } \ No newline at end of file diff --git a/Harmony/Properties/AssemblyInfo.cs b/Harmony/Properties/AssemblyInfo.cs index 6c4f1a40..c4952484 100644 --- a/Harmony/Properties/AssemblyInfo.cs +++ b/Harmony/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +[assembly: AssemblyVersion("1.0.2.0")] +[assembly: AssemblyFileVersion("1.0.2.0")]