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

Commit

Permalink
Merge pull request #191 from soulsmods/garyttierney-patch-1
Browse files Browse the repository at this point in the history
Defer path resolution to modengine entrypoint
  • Loading branch information
garyttierney authored Mar 23, 2024
2 parents 2440fc7 + bcf9e73 commit 4162f81
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using namespace spdlog;

namespace fs = std::filesystem;

static HMODULE modengine_instance;
static fs::path modengine_path;
static fs::path game_path;

Expand All @@ -25,6 +26,29 @@ HookSet entry_hook_set;

int WINAPI modengine_entrypoint(void)
{
wchar_t dll_filename[MAX_PATH + 1];

// Grab the path to the modengine2.dll file, so we can locate the global
// configuration from here if it exists.
if (!GetModuleFileNameW(modengine_instance, dll_filename, MAX_PATH)) {
return false;
}

modengine_path = fs::path(dll_filename).parent_path();
if (modengine_path.filename() == "bin") {
modengine_path = modengine_path.parent_path();
}

wchar_t game_filename[MAX_PATH + 1];

// Also get the path to the game executable, to support legacy use-cases of putting
// mods in the game folder.
if (!GetModuleFileNameW(nullptr, game_filename, MAX_PATH)) {
return false;
}

game_path = fs::path(game_filename).parent_path();

start_crash_handler(modengine_path, game_path);

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

static bool attach(HMODULE module)
{
wchar_t dll_filename[MAX_PATH];

// Grab the path to the modengine2.dll file, so we can locate the global
// configuration from here if it exists.
if (!GetModuleFileNameW(module, dll_filename, MAX_PATH)) {
return false;
}

modengine_path = fs::path(dll_filename).parent_path();
if (modengine_path.filename() == "bin") {
modengine_path = modengine_path.parent_path();
}

wchar_t game_filename[MAX_PATH];

// Also get the path to the game executable, to support legacy use-cases of putting
// mods in the game folder.
if (!GetModuleFileNameW(nullptr, game_filename, MAX_PATH)) {
return false;
}

game_path = fs::path(game_filename).parent_path();

modengine_instance = module;

hooked_entrypoint.original = reinterpret_cast<fnEntry>(DetourGetEntryPoint(nullptr));
hooked_entrypoint.replacement = modengine_entrypoint;
entry_hook_set.install(reinterpret_cast<Hook<modengine::GenericFunctionPointer>*>(&hooked_entrypoint));
Expand Down Expand Up @@ -144,4 +147,4 @@ BOOL APIENTRY DllMain(HMODULE module, DWORD dwReason, LPVOID)
return detach();
}
return TRUE;
}
}

0 comments on commit 4162f81

Please sign in to comment.