From 572f4eab6c9fd1098d1945668bfa783bd90aa8d9 Mon Sep 17 00:00:00 2001 From: p0358 Date: Thu, 30 Dec 2021 03:26:08 +0100 Subject: Restore the functionality of arguments from command line --- LauncherInjector/main.cpp | 1 - NorthstarDedicatedTest/hooks.cpp | 54 +++++++++++++++++++++++++++++++++++++++ loader_launcher_proxy/dllmain.cpp | 1 - 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/LauncherInjector/main.cpp b/LauncherInjector/main.cpp index 4f21b200..1eca067a 100644 --- a/LauncherInjector/main.cpp +++ b/LauncherInjector/main.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include diff --git a/NorthstarDedicatedTest/hooks.cpp b/NorthstarDedicatedTest/hooks.cpp index 9d2be61c..e58b7afc 100644 --- a/NorthstarDedicatedTest/hooks.cpp +++ b/NorthstarDedicatedTest/hooks.cpp @@ -5,6 +5,12 @@ #include #include #include +#include +#include +#include + +typedef LPSTR(*GetCommandLineAType)(); +LPSTR GetCommandLineAHook(); // note that these load library callbacks only support explicitly loaded dynamic libraries @@ -20,6 +26,7 @@ HMODULE LoadLibraryExWHook(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags); typedef HMODULE(*LoadLibraryWType)(LPCWSTR lpLibFileName); HMODULE LoadLibraryWHook(LPCWSTR lpLibFileName); +GetCommandLineAType GetCommandLineAOriginal; LoadLibraryExAType LoadLibraryExAOriginal; LoadLibraryAType LoadLibraryAOriginal; LoadLibraryExWType LoadLibraryExWOriginal; @@ -31,12 +38,59 @@ void InstallInitialHooks() spdlog::error("MH_Initialize failed"); HookEnabler hook; + ENABLER_CREATEHOOK(hook, &GetCommandLineA, &GetCommandLineAHook, reinterpret_cast(&GetCommandLineAOriginal)); ENABLER_CREATEHOOK(hook, &LoadLibraryExA, &LoadLibraryExAHook, reinterpret_cast(&LoadLibraryExAOriginal)); ENABLER_CREATEHOOK(hook, &LoadLibraryA, &LoadLibraryAHook, reinterpret_cast(&LoadLibraryAOriginal)); ENABLER_CREATEHOOK(hook, &LoadLibraryExW, &LoadLibraryExWHook, reinterpret_cast(&LoadLibraryExWOriginal)); ENABLER_CREATEHOOK(hook, &LoadLibraryW, &LoadLibraryWHook, reinterpret_cast(&LoadLibraryWOriginal)); } +char* cmdlineResult; +LPSTR GetCommandLineAHook() +{ + static char* cmdlineOrg; + + if (cmdlineOrg == nullptr || cmdlineResult == nullptr) + { + cmdlineOrg = GetCommandLineAOriginal(); + bool isDedi = strstr(cmdlineOrg, "-dedicated"); // well, this one has to be a real argument + + std::string args; + std::ifstream cmdlineArgFile; + + // it looks like CommandLine() prioritizes parameters apprearing first, so we want the real commandline to take priority + // not to mention that cmdlineOrg starts with the EXE path + args.append(cmdlineOrg); + args.append(" "); + + // append those from the file + + cmdlineArgFile = std::ifstream(!isDedi ? "ns_startup_args.txt" : "ns_startup_args_dedi.txt"); + + if (cmdlineArgFile) + { + std::stringstream argBuffer; + argBuffer << cmdlineArgFile.rdbuf(); + cmdlineArgFile.close(); + + args.append(argBuffer.str()); + } + + auto len = args.length(); + cmdlineResult = reinterpret_cast(malloc(len + 1)); + if (!cmdlineResult) + { + spdlog::error("malloc failed for command line"); + return cmdlineOrg; + } + memcpy(cmdlineResult, args.c_str(), len + 1); + + spdlog::info("Command line: {}", cmdlineResult); + } + + return cmdlineResult; +} + // dll load callback stuff // this allows for code to register callbacks to be run as soon as a dll is loaded, mainly to allow for patches to be made on dll load struct DllLoadCallback diff --git a/loader_launcher_proxy/dllmain.cpp b/loader_launcher_proxy/dllmain.cpp index 6db50986..7a778208 100644 --- a/loader_launcher_proxy/dllmain.cpp +++ b/loader_launcher_proxy/dllmain.cpp @@ -102,7 +102,6 @@ bool LoadNorthstar() } } - printf("WILL CALL HOOK INIT\n"); ((bool (*)()) Hook_Init)(); return true; } -- cgit v1.2.3