Skip to content

Commit 02f64aa

Browse files
committed
add path autodetection
1 parent a546399 commit 02f64aa

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

library/Hooks.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,36 @@
33

44
#include "df/gamest.h"
55

6+
#ifdef _WIN32
7+
# define WIN32_LEAN_AND_MEAN
8+
# include <Windows.h>
9+
# include <libloaderapi.h>
10+
#else
11+
# include <dlfcn.h>
12+
#endif
13+
614
static bool disabled = false;
715

816
DFhackCExport const int32_t dfhooks_priority = 100;
917

10-
static std::filesystem::path basepath{"./hack"};
18+
static std::filesystem::path getModulePath()
19+
{
20+
#ifdef _WIN32
21+
HMODULE module = nullptr;
22+
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)getModulePath, &module);
23+
if (!module) return std::filesystem::path(); // should never happen, but just in case, return an empty path instead of crashing
24+
25+
wchar_t path[MAX_PATH];
26+
GetModuleFileNameW(module, path, MAX_PATH);
27+
return std::filesystem::path(path);
28+
#else
29+
DL_info info;
30+
dladdr(getModulePath, &info);
31+
return std::filesystem::path(info.dli_fname);
32+
#endif
33+
}
34+
35+
static std::filesystem::path basepath{getModulePath()};
1136

1237
// called by the chainloader before the main thread is initialized and before any other hooks are called.
1338
DFhackCExport void dfhooks_preinit(std::filesystem::path dllpath)

0 commit comments

Comments
 (0)