Ejemplo n.º 1
0
        public static void Uninstall()
        {
            if (beginSampeOnly != null)
            {
                beginSampeOnly.Uninstall();
                beginSampeOnly = null;
            }
            if (beginObjetSample != null)
            {
                beginObjetSample.Uninstall();
                beginObjetSample = null;
            }
            if (endSample != null)
            {
                endSample.Uninstall();
                endSample = null;
            }
            if (tostringHook != null)
            {
                tostringHook.Uninstall();
                tostringHook = null;
            }
            if (loaderHook != null)
            {
                loaderHook.Uninstall();
                loaderHook = null;
            }

            m_hooked = false;
        }
Ejemplo n.º 2
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");

        MethodHooker hooker = new MethodHooker(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);
    }
Ejemplo n.º 3
0
        public static void HookNewLuaEnv()
        {
            if (hookNewLuaEnv == null)
            {
                Type       envReplace   = typeof(LuaEnvReplace);
                Type       typeEnv      = typeof(LuaState);
                var        clickFun     = typeEnv.GetMethod("Start");
                MethodInfo clickReplace = envReplace.GetMethod("Start");
                MethodInfo clickProxy   = envReplace.GetMethod("Proxy", BindingFlags.Public | BindingFlags.Static);
                hookNewLuaEnv = new MethodHooker(clickFun, clickReplace, clickProxy);
                hookNewLuaEnv.Install();

                Type typeDll     = typeof(LuaDLL);
                var  newstateFun = typeDll.GetMethod("luaL_newstate");
                clickReplace  = envReplace.GetMethod("luaL_newstate");
                clickProxy    = envReplace.GetMethod("ProxyNewstate", BindingFlags.Public | BindingFlags.Static);
                hookNewLuaEnv = new MethodHooker(newstateFun, clickReplace, clickProxy);
                hookNewLuaEnv.Install();

                newstateFun   = typeDll.GetMethod("lua_close");
                clickReplace  = envReplace.GetMethod("lua_close");
                clickProxy    = envReplace.GetMethod("ProxyClose", BindingFlags.Public | BindingFlags.Static);
                hookNewLuaEnv = new MethodHooker(newstateFun, clickReplace, clickProxy);
                hookNewLuaEnv.Install();
            }
        }
Ejemplo n.º 4
0
        private static void Install()
        {
            if (m_hooked)
            {
                return;
            }
            if (tostringHook == null)
            {
                Type typeLogReplace = typeof(LuaDLLReplace);
                Type typeLog        = typeof(LuaDLL);
#if XLUA
                MethodInfo tostringFun = typeLog.GetMethod("xluaL_loadbuffer");
#else
                MethodInfo tostringFun = typeLog.GetMethod("luaL_loadbuffer", new Type[] { typeof(IntPtr), typeof(byte[]), typeof(int), typeof(string) });
#endif
                MethodInfo tostringReplace = typeLogReplace.GetMethod("luaL_loadbuffer");
                MethodInfo tostringProxy   = typeLogReplace.GetMethod("ProxyLoadbuffer");
                tostringHook = new MethodHooker(tostringFun, tostringReplace, tostringProxy);
                tostringHook.Install();

                tostringFun     = typeLog.GetMethod("lua_tostring");
                tostringReplace = typeLogReplace.GetMethod("lua_tostring");
                tostringProxy   = typeLogReplace.GetMethod("PoxyToString");
                tostringHook    = new MethodHooker(tostringFun, tostringReplace, tostringProxy);
                tostringHook.Install();
            }

            m_hooked = true;
        }
Ejemplo n.º 5
0
        static HookLuaUtil()
        {
#if XLUA || TOLUA || SLUA
            if (hookNewLuaEnv == null)
            {
                Type envReplace = typeof(LuaEnvReplace);

                Type typeDll      = typeof(LuaDLL);
                var  newstateFun  = typeDll.GetMethod("luaL_newstate");
                var  clickReplace = envReplace.GetMethod("luaL_newstate");
                var  clickProxy   = envReplace.GetMethod("ProxyNewstate", BindingFlags.Public | BindingFlags.Static);
                hookNewLuaEnv = new MethodHooker(newstateFun, clickReplace, clickProxy);
                hookNewLuaEnv.Install();

                newstateFun   = typeDll.GetMethod("lua_close");
                clickReplace  = envReplace.GetMethod("lua_close");
                clickProxy    = envReplace.GetMethod("ProxyClose", BindingFlags.Public | BindingFlags.Static);
                hookNewLuaEnv = new MethodHooker(newstateFun, clickReplace, clickProxy);
                hookNewLuaEnv.Install();

                if (LuaDeepProfilerSetting.Instance.isDeepProfiler && LuaDeepProfilerSetting.Instance.profilerMono)
                {
                    InjectMethods.InjectAllMethods();
                }

                EditorApplication.update += () =>
                {
                    isPlaying  = Application.isPlaying;
                    frameCount = Time.frameCount;
                };
            }
            if (LuaDeepProfilerSetting.Instance.isInited)
            {
                return;
            }
#endif
            string[] paths = Directory.GetFiles(Application.dataPath, "*.dll", SearchOption.AllDirectories);
            foreach (var item in paths)
            {
                string fileName = Path.GetFileName(item);
                if (fileName == "slua.dll")
                {
                    AppendMacro("#define SLUA");
                }

                if (fileName == "xlua.dll")
                {
                    AppendMacro("#define XLUA");
                    break;
                }

                if (fileName == "tolua.dll")
                {
                    AppendMacro("#define TOLUA");
                    break;
                }
            }
            LuaDeepProfilerSetting.Instance.isInited = true;
        }
Ejemplo n.º 6
0
        public static void Install()
        {
#if TOLUA || XLUA || SLUA
            if (m_hooked)
            {
                return;
            }

            Type oldType     = typeof(OldLuaDLL);
            Type replaceType = typeof(LuaDLL);
            if (luaL_newstate_hooker == null)
            {
                luaL_newstate_hooker = BindHook(oldType, replaceType, "luaL_newstate", "luaL_newstate_replace");
            }

            if (lua_close_hooker == null)
            {
                lua_close_hooker = BindHook(oldType, replaceType, "lua_close", "lua_close_replace");
            }

            if (lua_ref_hooker == null)
            {
                lua_ref_hooker = BindHook(oldType, replaceType, "luaL_ref", "luaL_ref_replace");
            }

            if (lua_unref_hooker == null)
            {
                lua_unref_hooker = BindHook(oldType, replaceType, "luaL_unref", "luaL_unref_replace");
            }

            if (lua_load_buffer_hooker == null)
            {
#if TOLUA
                lua_load_buffer_hooker = BindHook(oldType, replaceType, "tolua_loadbuffer", "luaL_loadbuffer_replace");
#elif XLUA
                lua_load_buffer_hooker = BindHook(oldType, replaceType, "xluaL_loadbuffer", "luaL_loadbuffer_replace");
#elif SLUA
                oldType = typeof(SLua.LuaDLLWrapper);
                lua_load_buffer_hooker = BindHook(oldType, replaceType, "luaLS_loadbuffer", "luaL_loadbuffer_replace");
#endif
            }
#if TOLUA
            if (tolua_ref_hooker == null)
            {
                tolua_ref_hooker = BindHook(oldType, replaceType, "toluaL_ref", "toluaL_ref_replace");
            }

            if (tolua_unref_hooker == null)
            {
                tolua_unref_hooker = BindHook(oldType, replaceType, "toluaL_unref", "toluaL_unref_replace");
            }
#endif
            m_hooked = true;
#endif
        }
Ejemplo n.º 7
0
        private static MethodHooker BindHook(Type oldType, Type replaceType, string oldName, string replaceName)
        {
            MethodInfo oldFun     = oldType.GetMethod(oldName);
            MethodInfo replaceFun = replaceType.GetMethod(replaceName);

            MethodHooker result = new MethodHooker(oldFun, replaceFun);

            result.Install();

            return(result);
        }
Ejemplo n.º 8
0
        public static void HookLuaFuns()
        {
            if (m_hooked)
            {
                return;
            }
            if (tostringHook == null)
            {
                Type       typeLogReplace  = typeof(LuaDll);
                Type       typeLog         = typeof(LuaDLL);
                MethodInfo tostringFun     = typeLog.GetMethod("lua_tostring");
                MethodInfo tostringReplace = typeLogReplace.GetMethod("lua_tostring");
                MethodInfo tostringProxy   = typeLogReplace.GetMethod("ProxyToString");

                tostringHook = new MethodHooker(tostringFun, tostringReplace, tostringProxy);
                tostringHook.Install();

                tostringFun     = typeLog.GetMethod("luaL_loadbuffer");
                tostringReplace = typeLogReplace.GetMethod("luaL_loadbuffer");
                tostringProxy   = typeLogReplace.GetMethod("ProxyLoadbuffer");

                tostringHook = new MethodHooker(tostringFun, tostringReplace, tostringProxy);
                tostringHook.Install();
            }

            if (beginSampeOnly == null)
            {
                Type typeTarget  = typeof(UnityEngine.Profiling.Profiler);
                Type typeReplace = typeof(Profiler);

                MethodInfo hookTarget  = typeTarget.GetMethod("BeginSampleOnly", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
                MethodInfo hookReplace = typeReplace.GetMethod("BeginSampleOnly");
                MethodInfo hookProxy   = typeReplace.GetMethod("ProxyBeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
                beginSampeOnly = new MethodHooker(hookTarget, hookReplace, hookProxy);
                beginSampeOnly.Install();

                hookTarget       = typeTarget.GetMethod("BeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(UnityEngine.Object) }, null);
                hookReplace      = typeReplace.GetMethod("BeginSample");
                hookProxy        = typeReplace.GetMethod("ProxyBeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(UnityEngine.Object) }, null);
                beginObjetSample = new MethodHooker(hookTarget, hookReplace, hookProxy);
                beginObjetSample.Install();

                hookTarget  = typeTarget.GetMethod("EndSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
                hookReplace = typeReplace.GetMethod("EndSample");
                hookProxy   = typeReplace.GetMethod("ProxyEndSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
                endSample   = new MethodHooker(hookTarget, hookReplace, hookProxy);
                endSample.Install();
            }

            m_hooked = true;
        }
Ejemplo n.º 9
0
    public static void AddHooker(MethodBase method, MethodHooker hooker)
    {
        MethodHooker preHooker;

        if (_hookers.TryGetValue(method, out preHooker))
        {
            preHooker.Uninstall();
            _hookers[method] = hooker;
        }
        else
        {
            _hookers.Add(method, hooker);
        }
    }
Ejemplo n.º 10
0
        public static void UnInstall()
        {
#if TOLUA || XLUA || SLUA
            if (luaL_newstate_hooker != null)
            {
                luaL_newstate_hooker.Uninstall();
                luaL_newstate_hooker = null;
            }

            if (lua_close_hooker != null)
            {
                lua_close_hooker.Uninstall();
                lua_close_hooker = null;
            }

            if (lua_load_buffer_hooker != null)
            {
                lua_load_buffer_hooker.Uninstall();
                lua_load_buffer_hooker = null;
            }

            if (lua_ref_hooker != null)
            {
                lua_ref_hooker.Uninstall();
                lua_ref_hooker = null;
            }

            if (lua_unref_hooker != null)
            {
                lua_unref_hooker.Uninstall();
                lua_unref_hooker = null;
            }

#if TOLUA
            if (tolua_ref_hooker != null)
            {
                tolua_ref_hooker.Uninstall();
                tolua_ref_hooker = null;
            }

            if (tolua_unref_hooker != null)
            {
                tolua_unref_hooker.Uninstall();
                tolua_unref_hooker = null;
            }
#endif
            m_hooked = false;
#endif
        }
Ejemplo n.º 11
0
        public static void UnInstall()
        {
            if (tostringHook != null)
            {
                tostringHook.Uninstall();
                tostringHook = null;
            }
            if (loaderHook != null)
            {
                loaderHook.Uninstall();
                loaderHook = null;
            }

            m_hooked = false;
        }
Ejemplo n.º 12
0
    public static void RunIl2CppWithArguments(object obj, List <string> arguments, Action <System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
    {
        if (il2cppHooker != null)
        {
            il2cppHooker.Dispose();
            il2cppHooker = null;
        }

        RunBinderBeforeIl2cpp(workingDirectory);

        // call the realy method
        if (obj != null)
        {
            RunIl2CppWithArgumentsWrap(obj, arguments, setupStartInfo, workingDirectory);
        }
    }
Ejemplo n.º 13
0
    public static void StripAssemblies(string managedAssemblyFolderPath, object platformProvider, object rcr, ManagedStrippingLevel managedStrippingLevel)
#endif
    {
        if (stripHooker != null)
        {
            stripHooker.Dispose();
            stripHooker = null;
        }
        RunBinderBeforeStrip(managedAssemblyFolderPath);

        // call the realy method
#if UNITY_2019_1_OR_NEWER
        StripAssembliesWrap(managedAssemblyFolderPath, unityLinkerPlatformProvider, il2cppPlatformProvider, rcr, managedStrippingLevel);
#else
        StripAssembliesWrap(managedAssemblyFolderPath, platformProvider, rcr, managedStrippingLevel);
#endif
    }
Ejemplo n.º 14
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");

        MethodHooker hooker = new MethodHooker(miAPrivateFunc, miBReplace, miBProxy);

        hooker.Install();

        PrivateTestA privateTestA = new PrivateTestA();

        privateTestA.FuncTest();
    }
Ejemplo n.º 15
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");

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

        hookder.Install();

        CtorHookTarget hookTarget = new CtorHookTarget(1);

        Debug.Assert(hookTarget.x == 2);
    }
Ejemplo n.º 16
0
    static void InstallHook()
    {
        if (_hooker == null)
        {
#if UNITY_2017 || UNITY_2018
            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 MethodHooker(miTarget, miReplacement, miProxy);
            _hooker.Install();
        }
    }
Ejemplo n.º 17
0
    private static void InsertBuildTask()
    {
        if (stripHooker == null)
        {
            var        builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.AssemblyStripper");
            MethodBase orign       = builderType.GetMethod("StripAssemblies", BindingFlags.Static | BindingFlags.NonPublic);
            MethodBase custom      = typeof(PureScriptBuilder).GetMethod("StripAssemblies");
            MethodBase wrap2Orign  = typeof(PureScriptBuilder).GetMethod("StripAssembliesWrap");
            stripHooker = new MethodHooker(orign, custom, wrap2Orign);
        }

        if (il2cppHooker == null)
        {
            var        builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.IL2CPPBuilder");
            MethodBase orign       = builderType.GetMethod("RunIl2CppWithArguments", BindingFlags.Instance | BindingFlags.NonPublic);
            MethodBase custom      = typeof(PureScriptBuilder).GetMethod("RunIl2CppWithArguments");
            MethodBase wrap2Orign  = typeof(PureScriptBuilder).GetMethod("RunIl2CppWithArgumentsWrap");
            il2cppHooker = new MethodHooker(orign, custom, wrap2Orign);
        }
    }
Ejemplo n.º 18
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");

        MethodHooker hooker = new MethodHooker(miASet, miBReplace, miBProxy);

        hooker.Install();

        PropClassA a = new PropClassA(5);

        a.X = 7;
        Debug.Assert(a.X == 8);
    }
Ejemplo n.º 19
0
        public static void HookLuaFuns()
        {
            if (m_hooked)
            {
                return;
            }
            if (tostringHook == null)
            {
                Type       typeLogReplace  = typeof(LuaDll);
                Type       typeLog         = typeof(LuaLib);
                MethodInfo tostringFun     = typeLog.GetMethod("lua_tostring");
                MethodInfo tostringReplace = typeLogReplace.GetMethod("lua_tostring");
                MethodInfo tostringProxy   = typeLogReplace.GetMethod("ProxyToString");

                tostringHook = new MethodHooker(tostringFun, tostringReplace, tostringProxy);
                tostringHook.Install();
            }

            if (loaderHook == null)
            {
                Type       typeLoadReplace = typeof(LuaLoader);
                Type       typeEnv         = typeof(XLua.LuaEnv);
                MethodInfo loaderFun       = typeEnv.GetMethod("AddLoader");
                MethodInfo loaderReplace   = typeLoadReplace.GetMethod("AddLoader");
                MethodInfo loaderProxy     = typeLoadReplace.GetMethod("Proxy");

                loaderHook = new MethodHooker(loaderFun, loaderReplace, loaderProxy);
                loaderHook.Install();

                MethodInfo searchFun     = typeEnv.GetMethod("AddSearcher", BindingFlags.NonPublic | BindingFlags.Instance);
                MethodInfo searchReplace = typeLoadReplace.GetMethod("AddSearcher", BindingFlags.Public | BindingFlags.Static);
                MethodInfo searchProxy   = typeLoadReplace.GetMethod("ProxySearcher", BindingFlags.Public | BindingFlags.Static);

                loaderHook = new MethodHooker(searchFun, searchReplace, searchProxy);
                loaderHook.Install();
            }

            if (beginSampeOnly == null)
            {
                Type typeTarget  = typeof(UnityEngine.Profiling.Profiler);
                Type typeReplace = typeof(Profiler);

                MethodInfo hookTarget  = typeTarget.GetMethod("BeginSampleOnly", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
                MethodInfo hookReplace = typeReplace.GetMethod("BeginSampleOnly");
                MethodInfo hookProxy   = typeReplace.GetMethod("ProxyBeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
                beginSampeOnly = new MethodHooker(hookTarget, hookReplace, hookProxy);
                beginSampeOnly.Install();

                hookTarget       = typeTarget.GetMethod("BeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(UnityEngine.Object) }, null);
                hookReplace      = typeReplace.GetMethod("BeginSample");
                hookProxy        = typeReplace.GetMethod("ProxyBeginSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), typeof(UnityEngine.Object) }, null);
                beginObjetSample = new MethodHooker(hookTarget, hookReplace, hookProxy);
                beginObjetSample.Install();

                hookTarget  = typeTarget.GetMethod("EndSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
                hookReplace = typeReplace.GetMethod("EndSample");
                hookProxy   = typeReplace.GetMethod("ProxyEndSample", BindingFlags.Public | BindingFlags.Static, null, new Type[] { }, null);
                endSample   = new MethodHooker(hookTarget, hookReplace, hookProxy);
                endSample.Install();
            }

            m_hooked = true;
        }