Skip to content

Commit

Permalink
GetFindableID -> GetID, split up Extensions, refactor DMD gens
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ade committed Jul 18, 2019
1 parent bfa8124 commit a70072c
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 317 deletions.
2 changes: 1 addition & 1 deletion MonoMod.DebugIL/DebugILGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static void Generate(MonoModder modder)
writer.Write("// Method: (");
writer.Write(method.Attributes);
writer.Write(") ");
writer.WriteLine(method.GetFindableID()); Line++;
writer.WriteLine(method.GetID()); Line++;
writer.WriteLine(); Line++;

// TODO: [DbgILGen] Other method metadata?
Expand Down
6 changes: 3 additions & 3 deletions MonoMod.RuntimeDetour/Detour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class Detour : ISortableDetour {
get => _ID;
set {
if (string.IsNullOrEmpty(value))
value = Target.GetFindableID(simple: true);
value = Target.GetID(simple: true);
if (_ID == value)
return;
_ID = value;
Expand Down Expand Up @@ -144,7 +144,7 @@ public class Detour : ISortableDetour {
}

using (DynamicMethodDefinition dmd = new DynamicMethodDefinition(
$"Chain<{Method.GetFindableID(simple: true)}>?{GetHashCode()}",
$"Chain<{Method.GetID(simple: true)}>?{GetHashCode()}",
(Method as MethodInfo)?.ReturnType ?? typeof(void), argTypes
))
_ChainedTrampoline = dmd.StubCriticalDetour().Generate().Pin();
Expand Down Expand Up @@ -338,7 +338,7 @@ public Detour(Expression<Action> from, Expression<Action> to)
argTypes[i] = args[i].ParameterType;

using (DynamicMethodDefinition dmd = new DynamicMethodDefinition(
$"Trampoline<{Method.GetFindableID(simple: true)}>?{GetHashCode()}",
$"Trampoline<{Method.GetID(simple: true)}>?{GetHashCode()}",
returnType, argTypes
)) {
ILProcessor il = dmd.GetILProcessor();
Expand Down
2 changes: 1 addition & 1 deletion MonoMod.RuntimeDetour/DetourContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public sealed class DetourContext : IDisposable {
Creator = caller;
break;
}
_FallbackID = Creator?.DeclaringType?.Assembly?.GetName().Name ?? Creator?.GetFindableID(simple: true);
_FallbackID = Creator?.DeclaringType?.Assembly?.GetName().Name ?? Creator?.GetID(simple: true);

Last = this;
Contexts.Add(this);
Expand Down
4 changes: 2 additions & 2 deletions MonoMod.RuntimeDetour/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class Hook : IDetour {
ILProcessor il;

using (dmd = new DynamicMethodDefinition(
$"Hook<{Method.GetFindableID(simple: true)}>?{GetHashCode()}",
$"Hook<{Method.GetID(simple: true)}>?{GetHashCode()}",
(Method as MethodInfo)?.ReturnType ?? typeof(void), argTypes
)) {
il = dmd.GetILProcessor();
Expand Down Expand Up @@ -121,7 +121,7 @@ public class Hook : IDetour {
origArgTypes[i] = origArgs[i].ParameterType;

using (dmd = new DynamicMethodDefinition(
$"Chain:TMP<{Method.GetFindableID(simple: true)}>?{GetHashCode()}",
$"Chain:TMP<{Method.GetID(simple: true)}>?{GetHashCode()}",
(origInvoke as MethodInfo)?.ReturnType ?? typeof(void), origArgTypes
)) {
il = dmd.GetILProcessor();
Expand Down
9 changes: 0 additions & 9 deletions MonoMod.RuntimeDetour/HookGen/HookEndpointManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ public static class HookEndpointManager {
private static readonly Dictionary<MethodBase, object> HookEndpointMap = new Dictionary<MethodBase, object>();
private static readonly Dictionary<object, List<HookEntry>> OwnedHookLists = new Dictionary<object, List<HookEntry>>();

[Obsolete("Use the ILHook counterpart instead")]
public static event Func<AssemblyName, ModuleDefinition> OnGenerateCecilModule {
add => ILHook.OnGenerateCecilModule += value;
remove => ILHook.OnGenerateCecilModule += value;
}
[Obsolete("Use the ILHook counterpart instead")]
public static ModuleDefinition GenerateCecilModule(AssemblyName name)
=> ILHook.GenerateCecilModule(name);

public static event Func<Delegate, object> OnGetOwner;
public static object GetOwner(Delegate hook)
=> (OnGetOwner ?? DefaultOnGetOwner).InvokeWhileNull<object>(hook);
Expand Down
8 changes: 2 additions & 6 deletions MonoMod.RuntimeDetour/ILHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public class ILHook : ISortableDetour {

private Context _Ctx => _Map.TryGetValue(Method, out Context ctx) ? ctx : null;

public static event Func<AssemblyName, ModuleDefinition> OnGenerateCecilModule;
public static ModuleDefinition GenerateCecilModule(AssemblyName name)
=> OnGenerateCecilModule?.InvokeWhileNull<ModuleDefinition>(name);

public bool IsValid => Index != -1;
public bool IsApplied { get; private set; }

Expand All @@ -62,7 +58,7 @@ public static ModuleDefinition GenerateCecilModule(AssemblyName name)
get => _ID;
set {
if (string.IsNullOrEmpty(value))
value = Manipulator.Method?.GetFindableID(simple: true) ?? GetHashCode().ToString();
value = Manipulator.Method?.GetID(simple: true) ?? GetHashCode().ToString();
if (_ID == value)
return;
_ID = value;
Expand Down Expand Up @@ -243,7 +239,7 @@ private class Context {
DetourSorter<ILHook>.Sort(hooks);

MethodBase dm;
using (DynamicMethodDefinition dmd = new DynamicMethodDefinition(Method, GenerateCecilModule)) {
using (DynamicMethodDefinition dmd = new DynamicMethodDefinition(Method)) {
MethodDefinition def = dmd.Definition;
foreach (ILHook cb in hooks)
InvokeManipulator(def, cb.Manipulator);
Expand Down
2 changes: 1 addition & 1 deletion MonoMod.RuntimeDetour/NativeDetour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public NativeDetour(Delegate from, Delegate to)
argTypes[i] = args[i].ParameterType;

using (DynamicMethodDefinition dmd = new DynamicMethodDefinition(
$"Trampoline:Native<{Method?.GetFindableID(simple: true) ?? ((long) Data.Method).ToString("X16")}>?{GetHashCode()}",
$"Trampoline:Native<{Method?.GetID(simple: true) ?? ((long) Data.Method).ToString("X16")}>?{GetHashCode()}",
returnType, argTypes
)) {
ILProcessor il = dmd.GetILProcessor();
Expand Down
15 changes: 0 additions & 15 deletions MonoMod.Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,5 @@ public static string ToHexadecimalString(this byte[] data)
}
}

public static bool IsCompatible(this Type type, Type other)
=> _IsCompatible(type, other) || _IsCompatible(other, type);
public static bool _IsCompatible(this Type type, Type other) {
if (type == other)
return true;

if (type.IsAssignableFrom(other))
return true;

if (other.IsEnum && IsCompatible(type, Enum.GetUnderlyingType(other)))
return true;

return false;
}

}
}
4 changes: 2 additions & 2 deletions MonoMod.Utils/FastReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class FastReflectionHelper {
private static readonly MethodInfo m_object_GetType = typeof(object).GetMethod("GetType");

public static FastReflectionDelegate CreateFastDelegate(this MethodBase method, bool directBoxValueAccess = true) {
DynamicMethod dynam = new DynamicMethod($"FastReflection<{method.GetFindableID(simple: true)}>", typeof(object), _DynamicMethodDelegateArgs, typeof(FastReflectionHelper).Module, true);
DynamicMethod dynam = new DynamicMethod($"FastReflection<{method.GetID(simple: true)}>", typeof(object), _DynamicMethodDelegateArgs, typeof(FastReflectionHelper).Module, true);
ILGenerator il = dynam.GetILGenerator();

ParameterInfo[] args = method.GetParameters();
Expand Down Expand Up @@ -114,7 +114,7 @@ public static class FastReflectionHelper {
for (int i = 0; i < args.Length; i++)
argTypes[i] = args[i].ParameterType;

DynamicMethod dynam = new DynamicMethod($"FastReflection:JMP<{method.GetFindableID(simple: true)}>", invoke.ReturnType, argTypes, typeof(FastReflectionHelper).Module, true);
DynamicMethod dynam = new DynamicMethod($"FastReflection:JMP<{method.GetID(simple: true)}>", invoke.ReturnType, argTypes, typeof(FastReflectionHelper).Module, true);
ILGenerator il = dynam.GetILGenerator();

il.Emit(OpCodes.Jmp, (MethodInfo) method);
Expand Down

0 comments on commit a70072c

Please sign in to comment.