static void HookMain(void * retAddr) { if(hookInstalled) return; else hookInstalled = true; _MESSAGE("HookMain: thread = %d retaddr = %016I64X", GetCurrentThreadId(), retAddr); std::string runtimePath = GetRuntimePath(); _MESSAGE("runtimePath = %s", runtimePath.c_str()); bool isEditor = false; // check version etc std::string dllSuffix; ProcHookInfo procHookInfo; if(!IdentifyEXE(runtimePath.c_str(), isEditor, &dllSuffix, &procHookInfo)) { _ERROR("unknown exe"); return; } const char * dllPrefix = (isEditor == false) ? "\\skse64_" : "\\skse64_editor_"; g_dllPath = GetRuntimeDirectory() + dllPrefix + dllSuffix + ".dll"; _MESSAGE("dll = %s", g_dllPath.c_str()); LoadLibrary(g_dllPath.c_str()); }
void InstallHook(void * retaddr, UInt32 hookSrc) { if(hookInstalled) return; else hookInstalled = true; _MESSAGE("InstallHook: thread = %d retaddr = %08X hookSrc = %d", GetCurrentThreadId(), retaddr, hookSrc); // DumpThreads(); std::string appPath = GetAppPath(); _MESSAGE("appPath = %s", appPath.c_str()); std::string dllSuffix; ProcHookInfo procHookInfo; bool isEditor = RunningEditor(); if(!IdentifyEXE(appPath.c_str(), isEditor, &dllSuffix, &procHookInfo)) { _ERROR("unknown exe"); return; } // build full path to our dll const char * dllPrefix = (isEditor == false) ? "\\skse_" : "\\skse_editor_"; g_dllPath = GetAppDir() + dllPrefix + dllSuffix + ".dll"; _MESSAGE("dll = %s", g_dllPath.c_str()); // hook winmain call UInt32 hookBaseAddr = procHookInfo.hookCallAddr; UInt32 hookBaseCallAddr = *((UInt32 *)(hookBaseAddr + 1)); hookBaseCallAddr += 5 + hookBaseAddr; // adjust for relcall _MESSAGE("old winmain = %08X", hookBaseCallAddr); g_hookedWinMain = (_HookedWinMain)hookBaseCallAddr; UInt32 newHookDst = ((UInt32)OnHook) - hookBaseAddr - 5; SafeWrite32(hookBaseAddr + 1, newHookDst); Hooks_Memory_PreloadCommit(); FlushInstructionCache(GetCurrentProcess(), NULL, 0); }