From 704e46b937d11e751f8d80ae6612d82e1718ca7b Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Mon, 1 Jan 2024 15:13:40 +0100 Subject: [PATCH] SporeModManager: use GetEnvironmentVariableW() on win32 --- .../SporeModManagerHelpers/Path.cpp | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/SporeModManager/SporeModManagerHelpers/Path.cpp b/SporeModManager/SporeModManagerHelpers/Path.cpp index 9b2db1a..479c44e 100644 --- a/SporeModManager/SporeModManagerHelpers/Path.cpp +++ b/SporeModManager/SporeModManagerHelpers/Path.cpp @@ -7,7 +7,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -#define _CRT_SECURE_NO_WARNINGS // TODO for getenv() for msvc #include "SporeModManagerHelpers.hpp" #include @@ -200,12 +199,30 @@ std::filesystem::path Path::GetFullInstallPath(SporeMod::InstallLocation install std::filesystem::path Path::GetConfigFilePath(void) { - static const char* configFile = std::getenv("SPOREMODMANAGER_CONFIGFILE"); + static std::filesystem::path cachedConfigFilePath; + if (!cachedConfigFilePath.empty()) + { + return cachedConfigFilePath; + } + +#ifdef _WIN32 + wchar_t envBuffer[MAX_PATH] = {0}; + if (GetEnvironmentVariableW(L"SPOREMODMANAGER_CONFIGFILE", envBuffer, MAX_PATH) != 0) + { + cachedConfigFilePath = envBuffer; + } +#else + const char* configFile = std::getenv("SPOREMODMANAGER_CONFIGFILE"); if (configFile != nullptr) { - return configFile; + cachedConfigFilePath = configFile; + } +#endif + if (cachedConfigFilePath.empty()) + { + cachedConfigFilePath = Path::Combine({ Path::GetCurrentExecutablePath(), "SporeModManager.xml" }); } - return Path::Combine({ Path::GetCurrentExecutablePath(), "SporeModManager.xml" });; + return cachedConfigFilePath; } std::filesystem::path Path::GetCoreLibsPath(void)