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}"); } }
void LogEngineSymbols(SymbolResolver engineResolver) { var engineSymbols = engineResolver.FindSymbolsMatching(new Regex("lua*")); File.WriteAllLines("engine_symbols.txt", engineSymbols); Util.LoadExtenderLibrary("LuaHelper.dll"); var helperModule = Util.GetModule("LuaHelper.dll"); var helperResolver = new ExportResolver(helperModule); var symbols = helperResolver.FindSymbolsMatching(new Regex("lua*")); var missingSymbols = symbols. Where(name => !engineResolver.TryResolve(name, out var _)); File.WriteAllLines("missing_symbols.txt", missingSymbols); }