public ScriptManager(SymbolResolver resolver, bool enableDebug)
 {
     DebugEnabled = enableDebug;
     Console.WriteLine("Initializing ScriptManager");
     Resolver          = resolver;
     luaInterface      = resolver.Resolve <LuaInterface>("?LUA_INTERFACE@ScriptManager@sgg@@2ULua@@A");
     enableMessageHook = resolver.Resolve <bool>("?EnableLuaMessageHook@ConfigOptions@sgg@@2_NA");
     l_msghandler      = resolver.Resolve("l_msghandler");
     l_panic           = resolver.Resolve("l_panic");
     lua = new Lua(resolver, luaInterface);
     if (CustomLuaRuntime)
     {
         customRuntime = new CustomLuaRuntimeManager();
         customRuntime.Init(resolver, luaInterface, l_msghandler, l_panic, enableMessageHook);
     }
 }
Example #2
0
        private void HookLuaApi(SymbolResolver resolver)
        {
            Kernel32.LoadLibrary(Path.Combine(Util.ExtenderDirectory, "Lua.dll"));
            var luaModule   = Util.GetModule("Lua.dll");
            var luaResolver = new DiaSymbolResolver(luaModule);

            luaL_newstate   = luaResolver.ResolveFunction <LuaLNewStateDelegate>("luaL_newstate");
            luaopen_package = luaResolver.Resolve("luaopen_package");
            luaopen_io      = luaResolver.Resolve("luaopen_io");
            luaopen_os      = luaResolver.Resolve("luaopen_os");
            db_sethook      = luaResolver.Resolve("db_sethook");

            using var sw = new StreamWriter("PatchLog.txt");

            /* do not hook luaL_openlibs so that the engine will load its own implementation of
             * luaopen_debug and luaopen_utf8
             */
            var ignoreSymbols = new string[] {
                "luaopen_debug",
                "luaopen_utf8",
                "luaL_openlibs"
            };
            var symbols = resolver.FindSymbolsMatching(new Regex("lua*"))
                          .Concat(resolver.FindSymbolsMatching(new Regex(@"\?lua*")))
                          .Where(symbol => !ignoreSymbols.Contains(symbol));

            foreach (var symbol in symbols)
            {
                var source = resolver.Resolve(symbol);
                if (!luaResolver.TryResolve(symbol, out var target))
                {
                    RegisterErrorHook(symbol, source);
                    sw.WriteLine($"Could not find symbol {symbol} in lua.dll");
                    continue;
                }
                var asm = new string[] {
                    $"use64",
                    Utilities.GetAbsoluteJumpMnemonics(target, is64bit: true)
                };
                var hook = new AsmHook(asm, source.ToInt64(), AsmHookBehaviour.DoNotExecuteOriginal).Activate();
                luahooks[symbol] = hook;
                sw.WriteLine($"hooked lua function {symbol}. 0x{source.ToInt64():X8} -> 0x{target.ToInt64():X8}");
            }
        }