Skip to content

Commit

Permalink
Fixing current working directory on startup (#1047)
Browse files Browse the repository at this point in the history
* add fix currect working directory

* oops

* change to wchar and use max_path

* using `std::array`

* Make the cwd fix use std filesystem path

---------

Co-authored-by: IliasHDZ <nassimbennamari38@gmail.com>
Co-authored-by: alk <45172705+altalk23@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent ebd65ca commit 7c558ee
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions loader/src/platform/windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,25 @@ void* mainTrampolineAddr;
#include "gdTimestampMap.hpp"
unsigned int gdTimestamp = 0;

// In case the game is launched from a different directory through command line
// this function will set the current working directory to the game's directory
// to avoid the game crashing due to not being able to find the resources
void fixCurrentWorkingDirectory() {
std::array<WCHAR, MAX_PATH> cwd;

auto size = GetModuleFileNameW(nullptr, cwd.data(), cwd.size());
if (size == cwd.size()) return;

SetCurrentDirectoryW(std::filesystem::path(cwd.data()).parent_path().wstring().c_str());
}

int WINAPI gdMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
// MessageBoxA(NULL, "Hello from gdMainHook!", "Hi", 0);

updateGeode();

fixCurrentWorkingDirectory();

if (versionToTimestamp(GEODE_STR(GEODE_GD_VERSION)) > gdTimestamp) {
console::messageBox(
"Unable to Load Geode!",
Expand Down

0 comments on commit 7c558ee

Please sign in to comment.