Fixing current working directory on startup (#1047)

* 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>
This commit is contained in:
IliasHDZ 2024-09-10 18:07:09 +01:00 committed by GitHub
parent ebd65caa2b
commit 7c558ee47e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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!",