示例#1
0
    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);
    }
    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");

        if (miBProxy == null)
        {
            throw new Exception("PropXSetProxy is null");
        }

        MethodHook hook = new MethodHook(miASet, miBReplace, miBProxy);

        hook.Install();

        PropClassA a = new PropClassA(5);

        a.X = 7;
        Debug.Assert(a.X == 8);

        a.X = 100;
        Debug.Assert(a.X == 101);
    }
示例#3
0
 public static void Main1(string[] args)
 {
     MethodHook.Install();
     test();
     Console.WriteLine("end");
     //Console.ReadLine();
     //Console.ReadLine();
     //Console.ReadLine();
     //Console.ReadLine();
 }
示例#4
0
        public void ConstructorMethod()
        {
            MethodHook.Install();

            CallContext.LogicalSetData("OpenComputerConstructorHook", "1");
            var o = new Computer();

            CallContext.LogicalSetData("OpenComputerConstructorHook", null);

            Assert.AreEqual("ConstructorMethod X1", o.Name);
        }
    public static void InstallPatch()
    {
        Type typeA = typeof(A);
        Type typeB = typeof(B);

        MethodInfo miAFunc    = typeA.GetMethod("Func");
        MethodInfo miBReplace = typeB.GetMethod("FuncReplace");
        MethodInfo miBProxy   = typeB.GetMethod("FuncProxy");

        _hook = new MethodHook(miAFunc, miBReplace, miBProxy);
        _hook.Install();
    }
示例#6
0
 /// <summary>
 /// Hook 初始化
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static int Start(string msg)
 {
     try
     {
         TextHelper.LogInfo("开始" + msg);
         MethodHook.Install();
     }
     catch
     {
         return(-1);
     }
     return(1);
 }
示例#7
0
    public static void Init()
    {
        if (_hook == null)
        {
            Type       type     = typeof(GameObject).Assembly.GetType("UnityEngine.GameObject");
            MethodInfo miTarget = type.GetMethod("SetActive", BindingFlags.Instance | BindingFlags.Public);

            MethodInfo miReplacement = new Action <GameObject, bool>(SetActiveNew).Method;
            MethodInfo miProxy       = new Action <GameObject, bool>(SetActiveProxy).Method;

            _hook = new MethodHook(miTarget, miReplacement, miProxy);
            _hook.Install();
        }
    }
    static GameObject_CreatePrimitive_HookTest()
    {
        if (_hook == null)
        {
            Type       type     = typeof(AssetDatabase).Assembly.GetType("UnityEditor.GOCreationCommands");
            MethodInfo miTarget = type.GetMethod("CreateAndPlacePrimitive", BindingFlags.Static | BindingFlags.NonPublic);

            MethodInfo miReplacement = new Action <PrimitiveType, GameObject>(CreateAndPlacePrimitive).Method;
            MethodInfo miProxy       = new Action <PrimitiveType, GameObject>(CreateAndPlacePrimitiveProxy).Method;

            _hook = new MethodHook(miTarget, miReplacement, miProxy);
            _hook.Install();
        }
    }
示例#9
0
        public void InstanceMethod2()
        {
            MethodHook.Install();
            Assert.AreEqual("Hook 土豆(worked)", new Computer().Work("土豆"));

            //注意:存在SynchronizationContext时(如:HttpContext),异步方法不能直接在同步方法中调用,真发生异步行为时100%死锁
            var bak = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(null);
            try {
                Assert.AreEqual("Hook 土豆(workedAsync)", new Computer().WorkAsync("土豆").Result);
            } finally {
                SynchronizationContext.SetSynchronizationContext(bak);
            }
        }
示例#10
0
    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);
            _hook.Install();
        }
    }
示例#11
0
    public void Test()
    {
        MethodBase mbCtorA   = typeof(CtorHookTarget).GetConstructor(new Type[] { typeof(int) });
        MethodInfo mbReplace = new Action <int>(CtorTargetReplace).Method;
        MethodInfo mbProxy   = new Action <int>(CtorTargetProxy).Method;

        MethodHook hookder = new MethodHook(mbCtorA, mbReplace, mbProxy);

        hookder.Install();

        for (int i = 0; i < 2; i++)
        {
            CtorHookTarget hookTarget = new CtorHookTarget(1);
            Debug.Assert(hookTarget.x == 2, $"expect 2 but get {hookTarget.x} at i:{i}");
        }
    }
示例#12
0
    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();
    }
    static SceneHierarchyStageHandling_HookTest()
    {
        if (_hook == null)
        {
            Type type = Type.GetType("UnityEditor.SceneHierarchyStageHandling,UnityEditor.dll");
#if UNITY_2021_2_OR_NEWER
            var target = type.GetMethod("StageHeaderGUI", BindingFlags.Instance | BindingFlags.Public);
#else
            var target = type.GetMethod("PrefabStageHeaderGUI", BindingFlags.Instance | BindingFlags.Public);
#endif
            var dst = new Action <object, Rect>(PrefabStageHeaderGUINew).Method;
            var old = new Action <object, Rect>(PrefabStageHeaderGUIOld).Method;

            _hook = new MethodHook(target, dst, old);
            _hook.Install();
        }
    }
示例#14
0
    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 == 2);
    }
示例#15
0
    static PinnedLog()
    {
        if (_hook == null)
        {
#if UNITY_2017_1_OR_NEWER
            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);

            MethodInfo miReplacement = new Action(NewClearLog).Method;
            MethodInfo miProxy       = new Action(ProxyClearLog).Method;

            _hook = new MethodHook(miTarget, miReplacement, miProxy);
            _hook.Install();
        }
    }
示例#16
0
    static ProjectWindow_DuplicateAsset_HookTest()
    {
        if (_hook == null)
        {
#if UNITY_2021_2_OR_NEWER
            Type type = typeof(UnityEditor.AssetDatabase).Assembly.GetType("UnityEditor.AssetClipboardUtility");
#else
            Type type = typeof(UnityEditor.AssetDatabase).Assembly.GetType("UnityEditor.ProjectWindowUtil");
#endif
            MethodInfo miTarget = type.GetMethod("DuplicateSelectedAssets", BindingFlags.Static | BindingFlags.NonPublic);

            MethodInfo miReplacement = new Action(DuplicateSelectedAssets).Method;
            MethodInfo miProxy       = new Action(DuplicateSelectedAssetsProxy).Method;

            _hook = new MethodHook(miTarget, miReplacement, miProxy);
            _hook.Install();
        }
    }
示例#17
0
    static SceneHierarchyStageHandling_HookTest()
    {
        if (_hooker == null)
        {
            Type type = Type.GetType("UnityEditor.SceneHierarchyStageHandling,UnityEditor.dll");
#if UNITY_2021_2_OR_NEWER
            var target = type.GetMethod("StageHeaderGUI", BindingFlags.Instance | BindingFlags.Public);
#else
            var target = type.GetMethod("PrefabStageHeaderGUI", BindingFlags.Instance | BindingFlags.Public);
#endif
            var dst = typeof(SceneHierarchyStageHandling_HookTest).GetMethod("PrefabStageHeaderGUINew",
                                                                             BindingFlags.Static | BindingFlags.NonPublic);
            var old = typeof(SceneHierarchyStageHandling_HookTest).GetMethod("PrefabStageHeaderGUIOld",
                                                                             BindingFlags.Static | BindingFlags.NonPublic);

            _hooker = new MethodHook(target, dst, old);
            _hooker.Install();
        }
    }
示例#18
0
    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();
        }
    }
示例#19
0
        public void GenericMethodMethod()
        {
            MethodHook.Install();

            Assert.AreEqual("Hook<int> 123", Computer.Any <int>(123));
            //引用类型的没法正确hook,不知道啥原因
            //Assert.AreEqual("Hook<string> str", Computer.Any<string>("str"));


            //注意:存在SynchronizationContext时(如:HttpContext),异步方法不能直接在同步方法中调用,真发生异步行为时100%死锁
            var bak = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(null);
            try {
                Assert.AreEqual("Hook<int> 123Async", Computer.AnyAsync <int>(123).Result);
            } finally {
                SynchronizationContext.SetSynchronizationContext(bak);
            }
        }
示例#20
0
        public void GenericTypeMethod()
        {
            MethodHook.Install();

            Assert.AreEqual("Hook<string> Jack", new ComputerOf <string>().ComputerIo("Jack"));

            Assert.AreEqual("Hook<object> X1", new ComputerOf <Computer>().ComputerIo(new Computer()).Name);

            Assert.AreEqual(5, new ComputerOf <int>().ComputerIo(4));

            //注意:存在SynchronizationContext时(如:HttpContext),异步方法不能直接在同步方法中调用,真发生异步行为时100%死锁
            var bak = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(null);
            try {
                Assert.AreEqual(5, new ComputerOf <int>().ComputerIoAsync(4).Result);
            } finally {
                SynchronizationContext.SetSynchronizationContext(bak);
            }
        }
示例#21
0
    public static void Register()
    {
        // MeshRendererEditor
        Type       type   = typeof(AssetDatabase).Assembly.GetType("UnityEditor.MeshRendererEditor");
        MethodInfo method = type.GetMethod("OnInspectorGUI", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        type = typeof(RendererLayerEditor);
        MethodInfo methodReplacement = type.GetMethod("SubRendererOnInspectorGUI", BindingFlags.Static | BindingFlags.NonPublic);
        MethodInfo methodProxy       = type.GetMethod("SubRendererOnInspectorGUIProxy", BindingFlags.Static | BindingFlags.NonPublic);
        MethodHook hooker            = new MethodHook(method, methodReplacement, methodProxy);

        hooker.Install();

        // SkinnedMeshRendererEditor
        type              = typeof(AssetDatabase).Assembly.GetType("UnityEditor.SkinnedMeshRendererEditor");
        method            = type.GetMethod("OnInspectorGUI", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        type              = typeof(RendererLayerEditor);
        methodReplacement = type.GetMethod("SubRendererOnInspectorGUI", BindingFlags.Static | BindingFlags.NonPublic);
        methodProxy       = type.GetMethod("SubRendererOnInspectorGUIProxyE", BindingFlags.Static | BindingFlags.NonPublic);
        hooker            = new MethodHook(method, methodReplacement, methodProxy);
        hooker.Install();
    }
示例#22
0
    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);
    }
示例#23
0
 public void PropertyMethod()
 {
     MethodHook.Install();
     Assert.AreEqual("Not Windows 10", new Computer().Os);
 }
示例#24
0
 public void InstanceMethod()
 {
     MethodHook.Install();
     Assert.AreEqual("Hook 512M", new Computer().GetRAMSize());
 }
示例#25
0
 public void InternalTypeMethod()
 {
     MethodHook.Install();
     Assert.AreEqual("InternalTypeMethod X1:off", new Computer().PowerOff());
 }
示例#26
0
 public void StaticMethod()
 {
     MethodHook.Install();
     Assert.AreEqual("Not Intel Core I7", Computer.GetCpu());
 }
示例#27
0
 public void A_DotNetSystemMethod()
 {
     MethodHook.Install();
     Assert.AreEqual("Hook My_name_is_:NetFrameworkDetour", File.ReadAllText("test"));
 }
示例#28
0
 public static void Main(string[] args)
 {
     MethodHook.Install();
     test();
 }