Skip to content

Commit

Permalink
Made cross platform support much simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 16, 2017
1 parent 6366c10 commit 36249a4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Harmony/HookInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
56 changes: 16 additions & 40 deletions Harmony/Platform.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

namespace Harmony
{
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
4 changes: 2 additions & 2 deletions Harmony/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit 36249a4

Please sign in to comment.