From 3927b069b725d4a5797e0c7f13b55a048a857c67 Mon Sep 17 00:00:00 2001 From: Dregu Date: Thu, 28 Sep 2023 22:11:52 +0300 Subject: [PATCH 1/3] don't inject OL twice, don't apply patches twice --- src/game_api/state.cpp | 22 ++++++++++++++++------ src/injector/injector.cpp | 25 +++++++++++++++++++++++++ src/injector/injector.h | 1 + src/injector/main.cpp | 10 ++++++++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/game_api/state.cpp b/src/game_api/state.cpp index 3d773134c..8da7d4fb0 100644 --- a/src/game_api/state.cpp +++ b/src/game_api/state.cpp @@ -290,13 +290,23 @@ State& State::get() strings_init(); init_state_update_hook(); - // game patches - patch_tiamat_kill_crash(); - patch_orbs_limit(); - patch_olmec_kill_crash(); - patch_liquid_OOB(); + auto mem = Memory::get(); + auto watermark_offset = get_address("destroy_game_manager") - 8; // pulled this out of a hat, its just a random place with some CCCC hopefully + auto watermark = memory_read(watermark_offset); + if (watermark != 0x4C4F4C4F) + { + write_mem_prot(watermark_offset, "\x4F\x4C\x4F\x4C", true); + DEBUG("Applying patches"); + patch_tiamat_kill_crash(); + patch_orbs_limit(); + patch_olmec_kill_crash(); + patch_liquid_OOB(); + } + else + { + DEBUG("Not applying patches, someone has already done it"); + } } - get_is_init() = true; } return STATE; diff --git a/src/injector/injector.cpp b/src/injector/injector.cpp index 693a45907..26a56f916 100644 --- a/src/injector/injector.cpp +++ b/src/injector/injector.cpp @@ -113,6 +113,31 @@ void call(const Process& proc, LPTHREAD_START_ROUTINE addr, LPVOID args) WaitForSingleObject(handle, INFINITE); } +bool find_dll_in_process(DWORD pid, const std::string& name) +{ + HMODULE hMods[1024]; + HANDLE hProcess; + DWORD cbNeeded; + unsigned int i; + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); + if (NULL == hProcess) + return false; + if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) + { + for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) + { + TCHAR szModName[MAX_PATH]; + if (GetModuleFileNameEx(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) + { + auto modName = std::string(szModName); + if (modName.ends_with(name)) + return true; + } + } + } + return false; +} + void inject_dll(const Process& proc, const std::string& name) { auto str = alloc_str(proc, name); diff --git a/src/injector/injector.h b/src/injector/injector.h index 73aaa583b..6fd5a918c 100644 --- a/src/injector/injector.h +++ b/src/injector/injector.h @@ -27,3 +27,4 @@ void inject_dll(const Process& proc, const std::string& name); LPTHREAD_START_ROUTINE find_function(const Process& proc, const std::string& library, const std::string& function); void call(const Process& proc, LPTHREAD_START_ROUTINE addr, LPVOID args); std::optional find_process(std::string name); +bool find_dll_in_process(DWORD pid, const std::string& name); diff --git a/src/injector/main.cpp b/src/injector/main.cpp index f65d69098..d65dfab0d 100644 --- a/src/injector/main.cpp +++ b/src/injector/main.cpp @@ -349,6 +349,11 @@ bool inject_search(fs::path overlunky_path) } SetConsoleTitle("Overlunky"); INFO("Found Spel2.exe PID: {}", proc.info.pid); + if (find_dll_in_process(proc.info.pid, "Overlunky.dll")) + { + INFO("Already injected, let's not do that again. If you want to inject multiple game processes, use the --launch_game parameter."); + return false; + } inject_dll(proc, overlunky_path.string()); INFO("DLL injected"); wait(); @@ -406,6 +411,11 @@ bool launch(fs::path exe_path, fs::path overlunky_path, bool& do_inject) { auto proc = Process{pi.hProcess, {g_exe, pi.dwProcessId}}; INFO("Game launched, injecting DLL..."); + if (find_dll_in_process(proc.info.pid, "Overlunky.dll")) + { + INFO("Already injected, let's not do that again. If you want to inject multiple game processes, use the --launch_game parameter."); + return false; + } inject_dll(proc, overlunky_path.string()); INFO("DLL injected"); wait(); From 1092639273b4650c3640841946e6de5b3348c8e2 Mon Sep 17 00:00:00 2001 From: Dregu Date: Thu, 28 Sep 2023 22:13:52 +0300 Subject: [PATCH 2/3] remove old debugging --- src/game_api/layer.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/game_api/layer.cpp b/src/game_api/layer.cpp index df0a1cc1b..68e52051d 100644 --- a/src/game_api/layer.cpp +++ b/src/game_api/layer.cpp @@ -126,13 +126,11 @@ Entity* Layer::spawn_door(float x, float y, uint8_t w, uint8_t l, uint8_t t) { case 11: { - DEBUG("In camp, spawning starting exit"); door = spawn_entity(to_id("ENT_TYPE_FLOOR_DOOR_STARTING_EXIT"), round(x), round(y), false, 0.0, 0.0, true); break; } case 12: { - DEBUG("In game, spawning regular exit"); door = spawn_entity(to_id("ENT_TYPE_FLOOR_DOOR_EXIT"), round(x), round(y), false, 0.0, 0.0, true); break; } From eeda5375a3730f0d1082e97985df6dde2466523b Mon Sep 17 00:00:00 2001 From: Dregu Date: Thu, 28 Sep 2023 22:34:32 +0300 Subject: [PATCH 3/3] thank you clang --- src/game_api/state.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/game_api/state.cpp b/src/game_api/state.cpp index 8da7d4fb0..7a4dd4dc6 100644 --- a/src/game_api/state.cpp +++ b/src/game_api/state.cpp @@ -290,7 +290,6 @@ State& State::get() strings_init(); init_state_update_hook(); - auto mem = Memory::get(); auto watermark_offset = get_address("destroy_game_manager") - 8; // pulled this out of a hat, its just a random place with some CCCC hopefully auto watermark = memory_read(watermark_offset); if (watermark != 0x4C4F4C4F)