Skip to content

Commit

Permalink
Merge pull request #338 from spelunky-fyi/NoSeconds
Browse files Browse the repository at this point in the history
Don't inject or patch things twice
  • Loading branch information
Dregu authored Sep 29, 2023
2 parents 15088bc + eeda537 commit 3993aa3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/game_api/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 15 additions & 6 deletions src/game_api/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,22 @@ 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 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<uint32_t>(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;
Expand Down
25 changes: 25 additions & 0 deletions src/injector/injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/injector/injector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Process> find_process(std::string name);
bool find_dll_in_process(DWORD pid, const std::string& name);
10 changes: 10 additions & 0 deletions src/injector/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3993aa3

Please sign in to comment.