public string Test() { Type typeA = typeof(A); Type typeB = typeof(B); MethodInfo miAFunc = typeA.GetMethod("Func"); MethodInfo miBReplace = typeB.GetMethod("FuncReplace"); MethodInfo miBProxy = typeB.GetMethod("FuncProxy"); MethodHook hooker = new MethodHook(miAFunc, miBReplace, miBProxy); hooker.Install(); // 调用原始A的方法测试 A a = new A() { val = 5 }; int ret = a.Func(2); string info = string.Format("ret:{0}", ret); Debug.Log(info); Debug.Assert(ret == 10); return(info); }
private static void Position_Hook() { if (MethodHooker != null) { return; } Type type = typeof(Transform); var targetPro = type.GetProperty("position", (BindingFlags)(0x0fffffff)); var targetMethod = targetPro.SetMethod; type = typeof(Hook_Transform_Position); var replaceMethod = type.GetMethod("Position_Replace", (BindingFlags)(0x0fffffff)); var proxyMethod = type.GetMethod("Position_Proxy", (BindingFlags)(0x0fffffff)); Debug.Assert(targetMethod != null); Debug.Assert(replaceMethod != null); Debug.Assert(proxyMethod != null); MethodHooker = new MethodHook(targetMethod, replaceMethod, proxyMethod); MethodHooker.Install(); }
public static void AddHooker(MethodBase method, MethodHook hooker) { MethodHook preHooker; if (_hookers.TryGetValue(method, out preHooker)) { preHooker.Uninstall(); _hookers[method] = hooker; } else { _hookers.Add(method, hooker); } }
public void Test() { Type typeA = typeof(PrivateTestA); Type typeB = typeof(PrivateTestB); MethodInfo miAPrivateFunc = typeA.GetMethod("InnerFuncTest", BindingFlags.Instance | BindingFlags.NonPublic); MethodInfo miBReplace = typeB.GetMethod("FuncReplace"); MethodInfo miBProxy = typeB.GetMethod("Proxy"); MethodHook hooker = new MethodHook(miAPrivateFunc, miBReplace, miBProxy); hooker.Install(); PrivateTestA privateTestA = new PrivateTestA(); privateTestA.FuncTest(); }
public void Test() { Type typeA = typeof(CtorHookTarget); Type typeB = typeof(CtorHookTest); MethodBase mbCtorA = typeA.GetConstructor(new Type[] { typeof(int) }); MethodBase mbReplace = typeB.GetMethod("CtorTargetReplace"); MethodBase mbProxy = typeB.GetMethod("CtorTargetProxy"); MethodHook hookder = new MethodHook(mbCtorA, mbReplace, mbProxy); hookder.Install(); CtorHookTarget hookTarget = new CtorHookTarget(1); Debug.Assert(hookTarget.x == 1); }
static GameObject_SetActive_HookTest() { if (_hook == null) { Type type = typeof(GameObject).Assembly.GetType("UnityEngine.GameObject"); MethodInfo miTarget = type.GetMethod("SetActive", BindingFlags.Instance | BindingFlags.Public); type = typeof(GameObject_SetActive_HookTest); MethodInfo miReplacement = type.GetMethod("SetActiveNew", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo miProxy = type.GetMethod("SetActiveProxy", BindingFlags.Static | BindingFlags.NonPublic); _hook = new MethodHook(miTarget, miReplacement, miProxy); Debug.Log("Hooked"); _hook.Install(); } }
static PinnedLog() { if (_hooker == null) { #if UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020 Type type = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll"); #else Type type = Type.GetType("UnityEditorInternal.LogEntries,UnityEditor.dll"); #endif MethodInfo miTarget = type.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public); type = typeof(PinnedLog); MethodInfo miReplacement = type.GetMethod("NewClearLog", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo miProxy = type.GetMethod("ProxyClearLog", BindingFlags.Static | BindingFlags.NonPublic); _hooker = new MethodHook(miTarget, miReplacement, miProxy); _hooker.Install(); } }
public void Test() { Type typeA = typeof(PropClassA); Type typeB = typeof(PropClassB); PropertyInfo pi = typeA.GetProperty("X"); MethodInfo miASet = pi.GetSetMethod(); MethodInfo miBReplace = typeB.GetMethod("PropXSetReplace"); MethodInfo miBProxy = typeB.GetMethod("PropXSetProxy"); Debug.Log($"PropertyHook of miBProxy is not null {miBProxy != null}"); MethodHook hooker = new MethodHook(miASet, miBReplace, miBProxy); hooker.Install(); PropClassA a = new PropClassA(5); a.X = 7; Debug.Assert(a.X == 8); }
private static void SetActive_Hook() { if (MethodHooker != null) { return; } Type type = typeof(GameObject); MethodInfo targetMethod = type.GetMethod("SetActive", (BindingFlags)(0x0fffffff)); type = typeof(Hook_GameObject_SetActive); MethodInfo replaceMethod = type.GetMethod("SetActive_Replace", (BindingFlags)(0x0fffffff)); MethodInfo proxyMethod = type.GetMethod("SetActive_Proxy", (BindingFlags)(0x0fffffff)); Debug.Assert(targetMethod != null); Debug.Assert(replaceMethod != null); Debug.Assert(proxyMethod != null); MethodHooker = new MethodHook(targetMethod, replaceMethod, proxyMethod); MethodHooker.Install(); }