Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 4162f81

Browse files
authored
Merge pull request #191 from soulsmods/garyttierney-patch-1
Defer path resolution to modengine entrypoint
2 parents 2440fc7 + bcf9e73 commit 4162f81

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

Diff for: src/main.cpp

+27-24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ using namespace spdlog;
1414

1515
namespace fs = std::filesystem;
1616

17+
static HMODULE modengine_instance;
1718
static fs::path modengine_path;
1819
static fs::path game_path;
1920

@@ -25,6 +26,29 @@ HookSet entry_hook_set;
2526

2627
int WINAPI modengine_entrypoint(void)
2728
{
29+
wchar_t dll_filename[MAX_PATH + 1];
30+
31+
// Grab the path to the modengine2.dll file, so we can locate the global
32+
// configuration from here if it exists.
33+
if (!GetModuleFileNameW(modengine_instance, dll_filename, MAX_PATH)) {
34+
return false;
35+
}
36+
37+
modengine_path = fs::path(dll_filename).parent_path();
38+
if (modengine_path.filename() == "bin") {
39+
modengine_path = modengine_path.parent_path();
40+
}
41+
42+
wchar_t game_filename[MAX_PATH + 1];
43+
44+
// Also get the path to the game executable, to support legacy use-cases of putting
45+
// mods in the game folder.
46+
if (!GetModuleFileNameW(nullptr, game_filename, MAX_PATH)) {
47+
return false;
48+
}
49+
50+
game_path = fs::path(game_filename).parent_path();
51+
2852
start_crash_handler(modengine_path, game_path);
2953

3054
auto is_debugger_enabled = std::getenv("MODENGINE_DEBUG_GAME") != nullptr;
@@ -90,29 +114,8 @@ int WINAPI modengine_entrypoint(void)
90114

91115
static bool attach(HMODULE module)
92116
{
93-
wchar_t dll_filename[MAX_PATH];
94-
95-
// Grab the path to the modengine2.dll file, so we can locate the global
96-
// configuration from here if it exists.
97-
if (!GetModuleFileNameW(module, dll_filename, MAX_PATH)) {
98-
return false;
99-
}
100-
101-
modengine_path = fs::path(dll_filename).parent_path();
102-
if (modengine_path.filename() == "bin") {
103-
modengine_path = modengine_path.parent_path();
104-
}
105-
106-
wchar_t game_filename[MAX_PATH];
107-
108-
// Also get the path to the game executable, to support legacy use-cases of putting
109-
// mods in the game folder.
110-
if (!GetModuleFileNameW(nullptr, game_filename, MAX_PATH)) {
111-
return false;
112-
}
113-
114-
game_path = fs::path(game_filename).parent_path();
115-
117+
modengine_instance = module;
118+
116119
hooked_entrypoint.original = reinterpret_cast<fnEntry>(DetourGetEntryPoint(nullptr));
117120
hooked_entrypoint.replacement = modengine_entrypoint;
118121
entry_hook_set.install(reinterpret_cast<Hook<modengine::GenericFunctionPointer>*>(&hooked_entrypoint));
@@ -144,4 +147,4 @@ BOOL APIENTRY DllMain(HMODULE module, DWORD dwReason, LPVOID)
144147
return detach();
145148
}
146149
return TRUE;
147-
}
150+
}

0 commit comments

Comments
 (0)