From 60a5d74edae9638ed50f1a232d69129c67f93926 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 21 Apr 2023 16:49:22 -0400 Subject: [PATCH] improve config loading and prevent crashes --- texthook/engine/engine.cc | 8 ++++---- texthook/engine/engine.h | 2 +- texthook/engine/match.cc | 12 ++++++------ texthook/engine/match64.cc | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/texthook/engine/engine.cc b/texthook/engine/engine.cc index 61d5140..e8a464e 100644 --- a/texthook/engine/engine.cc +++ b/texthook/engine/engine.cc @@ -17001,7 +17001,7 @@ void InsertMonoHook(HMODULE h) if (!getDomain || !getName || !getJitInfo) goto failed; static auto domain = getDomain(); if (!domain) goto failed; - ConsoleOutput("Textractor: Mono Dynamic ENTER (hooks = %s)", loadedConfig ? loadedConfig : "brute force"); + ConsoleOutput("Textractor: Mono Dynamic ENTER (hooks = %s)", *loadedConfig ? loadedConfig : "brute force"); const BYTE prolog[] = { 0x55, 0x8b, 0xec }; for (auto addr : Util::SearchMemory(prolog, sizeof(prolog), PAGE_EXECUTE_READWRITE)) { @@ -17016,7 +17016,7 @@ void InsertMonoHook(HMODULE h) HookParam hp = {}; hp.address = addr; hp.type = USING_UNICODE | FULL_STRING; - if (!loadedConfig) hp.type |= KNOWN_UNSTABLE; + if (!*loadedConfig) hp.type |= KNOWN_UNSTABLE; hp.offset = 4; char nameForUser[HOOK_NAME_SIZE] = {}; strncpy_s(nameForUser, name + 1, HOOK_NAME_SIZE - 1); @@ -17034,7 +17034,7 @@ void InsertMonoHook(HMODULE h) __except (EXCEPTION_EXECUTE_HANDLER) {} }(addr); } - if (!loadedConfig) ConsoleOutput("Textractor: Mono Dynamic used brute force: if performance issues arise, please specify the correct hook in the game configuration"); + if (!*loadedConfig) ConsoleOutput("Textractor: Mono Dynamic used brute force: if performance issues arise, please specify the correct hook in the game configuration"); return true; failed: ConsoleOutput("Textractor: Mono Dynamic failed"); @@ -17091,7 +17091,7 @@ bool InsertMonoHooks() if (FARPROC addr = ::GetProcAddress(h, func.functionName)) { hp.address = (DWORD)addr; hp.type = func.hookType; - if (loadedConfig) hp.type |= HOOK_EMPTY; + if (*loadedConfig) hp.type |= HOOK_EMPTY; hp.filter_fun = NoAsciiFilter; hp.offset = func.textIndex * 4; hp.length_offset = func.lengthIndex * 4; diff --git a/texthook/engine/engine.h b/texthook/engine/engine.h index e8e401e..5e167b2 100644 --- a/texthook/engine/engine.h +++ b/texthook/engine/engine.h @@ -14,7 +14,7 @@ namespace Engine { // Global variables extern wchar_t *processName, // cached processPath[MAX_PATH]; // cached -inline const char *requestedEngine = nullptr, *loadedConfig = nullptr; +inline const char *requestedEngine = "", * loadedConfig = ""; bool InsertMonoHooks(); // Mono diff --git a/texthook/engine/match.cc b/texthook/engine/match.cc index 6158f20..badb7d1 100644 --- a/texthook/engine/match.cc +++ b/texthook/engine/match.cc @@ -45,13 +45,13 @@ namespace Engine if (AutoHandle<> configFile = CreateFileW(configFilename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) { ReadFile(configFile, configFileData, sizeof(configFileData) - 1, DUMMY, nullptr); - if (strncmp(configFileData, "Engine:", 7) == 0) + if (strnicmp(configFileData, "engine:", 7) == 0) { - if (loadedConfig = strchr(configFileData, '\n')) *(char*)loadedConfig++ = 0; - ConsoleOutput("Textractor: Engine = %s", requestedEngine = configFileData + 7); + if (const char* config = strchr(configFileData, '\n')) *(char*)(loadedConfig = config)++ = 0; + ConsoleOutput("Textractor: Engine = %s", requestedEngine = strlwr(configFileData + 7)); } else loadedConfig = configFileData; - if ((loadedConfig && !*loadedConfig) || strstr(configFileData, "https://")) loadedConfig = nullptr; + if (!*loadedConfig || strstr(configFileData, "https://")) loadedConfig = ""; else ConsoleOutput("Textractor: game configuration loaded"); } @@ -67,14 +67,14 @@ namespace Engine spDefault.maxAddress = processStopAddress; ConsoleOutput("Textractor: hijacking process located from 0x%p to 0x%p", processStartAddress, processStopAddress); - DetermineEngineType(); + if (!strstr(requestedEngine, "none")) DetermineEngineType(); if (processStartAddress + 0x40000 > processStopAddress) ConsoleOutput("Textractor: WARNING injected process is very small, possibly a dummy!"); }(), 0); } bool ShouldMonoHook(const char* name) { - if (!loadedConfig) return strstr(name, "string:") && !strstr(name, "string:mem"); + if (!*loadedConfig) return strstr(name, "string:") && !strstr(name, "string:mem"); for (const char* hook = loadedConfig; hook; hook = strchr(hook + 1, '\t')) for (auto start = name; *start; ++start) for (int i = 0; ; ++i) diff --git a/texthook/engine/match64.cc b/texthook/engine/match64.cc index 9ab8545..859f746 100644 --- a/texthook/engine/match64.cc +++ b/texthook/engine/match64.cc @@ -80,7 +80,7 @@ namespace Engine if (!getDomain || !getName || !getJitInfo) goto failed; static auto domain = getDomain(); if (!domain) goto failed; - ConsoleOutput("Textractor: Mono Dynamic ENTER (hooks = %s)", loadedConfig ? loadedConfig : "brute force"); + ConsoleOutput("Textractor: Mono Dynamic ENTER (hooks = %s)", *loadedConfig ? loadedConfig : "brute force"); const BYTE prolog1[] = { 0x55, 0x48, 0x8b, 0xec }; const BYTE prolog2[] = { 0x48, 0x83, 0xec }; for (auto [prolog, size] : Array{ { prolog1, sizeof(prolog1) }, { prolog2, sizeof(prolog2) } }) @@ -97,7 +97,7 @@ namespace Engine HookParam hp = {}; hp.address = addr; hp.type = USING_STRING | USING_UNICODE | FULL_STRING; - if (!loadedConfig) hp.type |= KNOWN_UNSTABLE; + if (!*loadedConfig) hp.type |= KNOWN_UNSTABLE; hp.offset = -0x20; // rcx hp.padding = 20; char nameForUser[HOOK_NAME_SIZE] = {}; @@ -119,7 +119,7 @@ namespace Engine }(addr); } - if (!loadedConfig) ConsoleOutput("Textractor: Mono Dynamic used brute force: if performance issues arise, please specify the correct hook in the game configuration"); + if (!*loadedConfig) ConsoleOutput("Textractor: Mono Dynamic used brute force: if performance issues arise, please specify the correct hook in the game configuration"); return true; failed: ConsoleOutput("Textractor: Mono Dynamic failed");