From 01b87374887d7a848bd876ffad7006beb157ade4 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 30 Mar 2024 17:41:34 +0100 Subject: [PATCH 1/3] fix command line arguments for alterware-launcher.exe --- src/Components/Modules/QuickPatch.cpp | 2 +- src/Utils/Library.cpp | 26 +++++++++++++------------- src/Utils/Library.hpp | 2 +- src/Utils/Utils.cpp | 22 ++++++++++++++++++++++ src/Utils/Utils.hpp | 2 ++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index e8a7ded9..ef455f9a 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -249,7 +249,7 @@ namespace Components const std::string command = binary == "iw4x-sp.exe" ? "iw4x-sp" : "iw4x"; SetEnvironmentVariableA("MW2_INSTALL", workingDir.data()); - Utils::Library::LaunchProcess(binary, std::format("{} --pass \"{}\"", command, GetCommandLineA()), workingDir); + Utils::Library::LaunchProcess(Utils::String::Convert(binary), std::format(L"{} --pass \"{}\"", Utils::String::Convert(command), Utils::GetLaunchParameters()), workingDir); } __declspec(naked) void QuickPatch::SND_GetAliasOffset_Stub() diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp index 70d661de..fae1bbcc 100644 --- a/src/Utils/Library.cpp +++ b/src/Utils/Library.cpp @@ -102,22 +102,22 @@ namespace Utils this->module_ = nullptr; } - void Library::LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir) + void Library::LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir) { - STARTUPINFOA startupInfo; - PROCESS_INFORMATION processInfo; + STARTUPINFOW startup_info; + PROCESS_INFORMATION process_info; - ZeroMemory(&startupInfo, sizeof(startupInfo)); - ZeroMemory(&processInfo, sizeof(processInfo)); - startupInfo.cb = sizeof(startupInfo); + ZeroMemory(&startup_info, sizeof(startup_info)); + ZeroMemory(&process_info, sizeof(process_info)); + startup_info.cb = sizeof(startup_info); - CreateProcessA(process.data(), const_cast(commandLine.data()), nullptr, - nullptr, false, NULL, nullptr, currentDir.data(), - &startupInfo, &processInfo); + CreateProcessW(process.data(), const_cast(commandLine.data()), nullptr, + nullptr, false, NULL, nullptr, currentDir.wstring().data(), + &startup_info, &process_info); - if (processInfo.hThread && processInfo.hThread != INVALID_HANDLE_VALUE) - CloseHandle(processInfo.hThread); - if (processInfo.hProcess && processInfo.hProcess != INVALID_HANDLE_VALUE) - CloseHandle(processInfo.hProcess); + if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) + CloseHandle(process_info.hThread); + if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) + CloseHandle(process_info.hProcess); } } diff --git a/src/Utils/Library.hpp b/src/Utils/Library.hpp index 367d9d08..3ff72663 100644 --- a/src/Utils/Library.hpp +++ b/src/Utils/Library.hpp @@ -65,7 +65,7 @@ namespace Utils return T(); } - static void LaunchProcess(const std::string& process, const std::string& commandLine, const std::string& currentDir); + static void LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir); private: HMODULE module_; diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 141a0fcc..60e06a5f 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -163,6 +163,28 @@ namespace Utils } } + std::wstring GetLaunchParameters() + { + std::wstring args; + + int numArgs; + auto* const argv = CommandLineToArgvW(GetCommandLineW(), &numArgs); + + if (argv) + { + for (auto i = 1; i < numArgs; ++i) + { + std::wstring arg(argv[i]); + args.append(arg); + args.append(L" "); + } + + LocalFree(argv); + } + + return args; + } + HMODULE GetNTDLL() { return GetModuleHandleA("ntdll.dll"); diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index f864a670..9ec8664b 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -24,6 +24,8 @@ namespace Utils void OpenUrl(const std::string& url); + std::wstring GetLaunchParameters(); + bool HasIntersection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2); template From 0801553556548a2d7dda4b64a52d2ddc7c38c362 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sun, 31 Mar 2024 22:05:20 +0200 Subject: [PATCH 2/3] Fix commandLine for CreateProcessW Remove unnecessary ternary (binary is never iw4x-sp.exe) --- src/Components/Modules/QuickPatch.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Modules/QuickPatch.cpp b/src/Components/Modules/QuickPatch.cpp index ef455f9a..ca8dfc9d 100644 --- a/src/Components/Modules/QuickPatch.cpp +++ b/src/Components/Modules/QuickPatch.cpp @@ -244,12 +244,12 @@ namespace Components return; } - auto workingDir = std::filesystem::current_path().string(); - const std::string binary = *Game::sys_exitCmdLine; - const std::string command = binary == "iw4x-sp.exe" ? "iw4x-sp" : "iw4x"; + const std::filesystem::path workingDir = std::filesystem::current_path(); + const std::wstring binary = Utils::String::Convert(*Game::sys_exitCmdLine); + const std::wstring commandLine = std::format(L"\"{}\" iw4x --pass \"{}\"", (workingDir / binary).wstring(), Utils::GetLaunchParameters()); - SetEnvironmentVariableA("MW2_INSTALL", workingDir.data()); - Utils::Library::LaunchProcess(Utils::String::Convert(binary), std::format(L"{} --pass \"{}\"", Utils::String::Convert(command), Utils::GetLaunchParameters()), workingDir); + SetEnvironmentVariableA("MW2_INSTALL", workingDir.string().data()); + Utils::Library::LaunchProcess(binary, commandLine, workingDir); } __declspec(naked) void QuickPatch::SND_GetAliasOffset_Stub() From a07253fff47892819d89352fd142fb90252d3ac2 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:13:37 +0200 Subject: [PATCH 3/3] format according to review --- src/Utils/Library.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Utils/Library.cpp b/src/Utils/Library.cpp index fae1bbcc..d5220666 100644 --- a/src/Utils/Library.cpp +++ b/src/Utils/Library.cpp @@ -104,20 +104,20 @@ namespace Utils void Library::LaunchProcess(const std::wstring& process, const std::wstring& commandLine, const std::filesystem::path& currentDir) { - STARTUPINFOW startup_info; - PROCESS_INFORMATION process_info; + STARTUPINFOW startupInfo; + PROCESS_INFORMATION processInfo; - ZeroMemory(&startup_info, sizeof(startup_info)); - ZeroMemory(&process_info, sizeof(process_info)); - startup_info.cb = sizeof(startup_info); + ZeroMemory(&startupInfo, sizeof(startupInfo)); + ZeroMemory(&processInfo, sizeof(processInfo)); + startupInfo.cb = sizeof(startupInfo); CreateProcessW(process.data(), const_cast(commandLine.data()), nullptr, nullptr, false, NULL, nullptr, currentDir.wstring().data(), - &startup_info, &process_info); + &startupInfo, &processInfo); - if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hThread); - if (process_info.hProcess && process_info.hProcess != INVALID_HANDLE_VALUE) - CloseHandle(process_info.hProcess); + if (processInfo.hThread && processInfo.hThread != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hThread); + if (processInfo.hProcess && processInfo.hProcess != INVALID_HANDLE_VALUE) + CloseHandle(processInfo.hProcess); } }