From 8be6efcfc28d426dfefc1357416c302bcdcdcca3 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Wed, 8 May 2024 00:30:23 +0300 Subject: [PATCH] allow loading `.ini` file with the same name as the cold client loader exe name --- CHANGELOG.md | 6 +++ helpers/pe_helpers.cpp | 54 +++++++++++++++---- helpers/pe_helpers/pe_helpers.hpp | 7 ++- .../win/ColdClientLoader.cpp | 16 ++++-- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae1d94e..c435e9ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +* for Windows ColdClientLoader: allow loading `.ini` file with the same name as the loader + ex: if the loader is named `game_cold_loader.exe`, then it will first try to load `game_cold_loader.ini`, + if that doesn't exist, it will fallback to `ColdClientLoader.ini` + +--- + ## 2024/5/5 * **[Clompress]** update Turkish translation diff --git a/helpers/pe_helpers.cpp b/helpers/pe_helpers.cpp index c8eeb4f2..4a6e960a 100644 --- a/helpers/pe_helpers.cpp +++ b/helpers/pe_helpers.cpp @@ -383,6 +383,11 @@ size_t pe_helpers::get_pe_size(HMODULE hModule) static std::wstring path_w{}; static std::string path_a{}; + +static std::wstring modulename_w{}; +static std::string modulename_a{}; + + const std::string& pe_helpers::get_current_exe_path() { if (path_a.empty()) { @@ -409,14 +414,27 @@ const std::wstring& pe_helpers::get_current_exe_path_w() } if ((read_chars < path_w.size()) && path_w[0]) { - path_w = path_w.substr(0, path_w.find_last_of(L"\\/") + 1); + auto modulename_idx = path_w.find_last_of(L"\\/") + 1; + modulename_w = path_w.substr(modulename_idx, read_chars - modulename_idx); + path_w = path_w.substr(0, modulename_idx); - auto cvt_state = std::mbstate_t(); - const wchar_t* src = &path_w[0]; - size_t conversion_bytes = std::wcsrtombs(nullptr, &src, 0, &cvt_state); - path_a.resize(conversion_bytes + 1); - std::wcsrtombs(&path_a[0], &src, path_a.size(), &cvt_state); - path_a = path_a.substr(0, conversion_bytes); + { + auto cvt_state = std::mbstate_t(); + const wchar_t* src = &path_w[0]; + size_t conversion_bytes = std::wcsrtombs(nullptr, &src, 0, &cvt_state); + path_a.resize(conversion_bytes + 1); + std::wcsrtombs(&path_a[0], &src, path_a.size(), &cvt_state); + path_a = path_a.substr(0, conversion_bytes); + } + + { + auto cvt_state = std::mbstate_t(); + const wchar_t* src = &modulename_w[0]; + size_t conversion_bytes = std::wcsrtombs(nullptr, &src, 0, &cvt_state); + modulename_a.resize(conversion_bytes + 1); + std::wcsrtombs(&modulename_a[0], &src, modulename_a.size(), &cvt_state); + modulename_a = modulename_a.substr(0, conversion_bytes); + } } else { path_w.clear(); } @@ -428,10 +446,28 @@ const std::wstring& pe_helpers::get_current_exe_path_w() return path_w; } -bool pe_helpers::ends_with_i(PUNICODE_STRING target, const std::wstring &query) +const std::string& pe_helpers::get_current_exe_name() +{ + if (modulename_a.empty()) { + get_current_exe_path_w(); + } + + return modulename_a; +} + +const std::wstring& pe_helpers::get_current_exe_name_w() +{ + if (modulename_w.empty()) { + get_current_exe_path_w(); + } + + return modulename_w; +} + +bool pe_helpers::ends_with_i(PUNICODE_STRING target, const std::wstring_view &query) { return common_helpers::ends_with_i( - std::wstring(target->Buffer, (PWSTR)((char*)target->Buffer + target->Length)), + std::wstring_view( target->Buffer, target->Length / sizeof(target->Buffer[0]) ), query ); } diff --git a/helpers/pe_helpers/pe_helpers.hpp b/helpers/pe_helpers/pe_helpers.hpp index 649702e2..b8c14ec8 100644 --- a/helpers/pe_helpers/pe_helpers.hpp +++ b/helpers/pe_helpers/pe_helpers.hpp @@ -5,6 +5,7 @@ #include #include +#include namespace pe_helpers { @@ -44,7 +45,11 @@ const std::string& get_current_exe_path(); const std::wstring& get_current_exe_path_w(); -bool ends_with_i(PUNICODE_STRING target, const std::wstring &query); +const std::string& get_current_exe_name(); + +const std::wstring& get_current_exe_name_w(); + +bool ends_with_i(PUNICODE_STRING target, const std::wstring_view &query); MEMORY_BASIC_INFORMATION get_mem_page_details(const void* mem); diff --git a/tools/steamclient_loader/win/ColdClientLoader.cpp b/tools/steamclient_loader/win/ColdClientLoader.cpp index 0f93d637..e2845d76 100644 --- a/tools/steamclient_loader/win/ColdClientLoader.cpp +++ b/tools/steamclient_loader/win/ColdClientLoader.cpp @@ -12,12 +12,13 @@ #include #include #include +#include #include #include -static const std::wstring IniFile = pe_helpers::get_current_exe_path_w() + L"ColdClientLoader.ini"; -static const std::wstring dbg_file = pe_helpers::get_current_exe_path_w() + L"COLD_LDR_LOG.txt"; +static std::wstring IniFile{}; +static const std::wstring dbg_file = pe_helpers::get_current_exe_path_w() + pe_helpers::get_current_exe_name_w() + L".log"; constexpr static const char STEAM_UNIVERSE[] = "Public"; constexpr static const char STEAM_URL_PROTOCOL[] = "URL:steam protocol"; @@ -339,9 +340,16 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance { dbg_log::init(dbg_file.c_str()); + IniFile = pe_helpers::get_current_exe_path_w() + std::filesystem::path(pe_helpers::get_current_exe_name_w()).stem().wstring() + L".ini"; + dbg_log::write(L"Searching for configuration file: " + IniFile); if (!common_helpers::file_exist(IniFile)) { - dbg_log::write(L"Couldn't find the configuration file: " + dbg_file); - MessageBoxA(NULL, "Couldn't find the configuration file ColdClientLoader.ini.", "ColdClientLoader", MB_ICONERROR); + IniFile = pe_helpers::get_current_exe_path_w() + L"ColdClientLoader.ini"; + dbg_log::write(L"Searching for configuration file: " + IniFile); + } + + if (!common_helpers::file_exist(IniFile)) { + dbg_log::write(L"Couldn't find the configuration file"); + MessageBoxA(NULL, "Couldn't find the configuration file.", "ColdClientLoader", MB_ICONERROR); dbg_log::close(); return 1; }