public void RegisterErrorHook(string symbol, IntPtr source) { if (OnErrorWrapper == null) { OnErrorWrapper = new Reloaded.Hooks.X64.ReverseWrapper <OnErrorDelegate>(OnError); } var asm = new string[] { $"use64", $"mov ecx, {unresolvedSymbols.Count}", Utilities.GetAbsoluteJumpMnemonics(OnErrorWrapper.WrapperPointer, is64bit: true) }; var hook = new AsmHook(asm, source.ToInt64(), AsmHookBehaviour.DoNotExecuteOriginal).Activate(); luahooks[symbol] = hook; unresolvedSymbols.Add(symbol); }
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}"); } }