From b8c034f8c5a220df572e2e9d8e8b1af1290d35e5 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 1 Aug 2022 14:51:08 +0200 Subject: [PATCH] sys: Prevent portable Windows version from writing to AppData Fixes #627 --- .github/workflows/build.yml | 1 + lib/libimhex/include/hex/api/imhex_api.hpp | 4 ++++ lib/libimhex/source/api/imhex_api.cpp | 9 +++++++++ lib/libimhex/source/helpers/fs.cpp | 22 +++++++++++++--------- main/source/init/tasks.cpp | 11 +++++++++++ 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7d03e84c6..70ea4f45a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -100,6 +100,7 @@ jobs: .. mingw32-make -j4 install cpack + touch $PWD/install/PORTABLE - name: ⬆️ Upload Portable ZIP uses: actions/upload-artifact@v3 diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 4d8ba79fe..2a559bb82 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -165,6 +165,8 @@ namespace hex { void setFontSize(float size); void setGPUVendor(const std::string &vendor); + + void setPortableVersion(bool enabled); } struct ProgramArguments { @@ -208,6 +210,8 @@ namespace hex { void setAdditionalFolderPaths(const std::vector &paths); const std::string &getGPUVendor(); + + bool isPortableVersion(); } } diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 5a855a313..30bf5c3b0 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -354,6 +354,11 @@ namespace hex { s_gpuVendor = vendor; } + static bool s_portableVersion = false; + void setPortableVersion(bool enabled) { + s_portableVersion = enabled; + } + } @@ -451,6 +456,10 @@ namespace hex { const std::string &getGPUVendor() { return impl::s_gpuVendor; } + + bool isPortableVersion() { + return impl::s_portableVersion; + } } } diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index 392237af2..64f2ae6f6 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -111,18 +111,22 @@ namespace hex::fs { }; #if defined(OS_WINDOWS) - std::fs::path appDataDir; - { - PWSTR wAppDataPath = nullptr; - if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) - throw std::runtime_error("Failed to get APPDATA folder path"); + std::vector paths; - appDataDir = std::wstring(wAppDataPath); - CoTaskMemFree(wAppDataPath); + if (!ImHexApi::System::isPortableVersion()) { + std::fs::path appDataDir; + { + PWSTR wAppDataPath = nullptr; + if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) + throw std::runtime_error("Failed to get APPDATA folder path"); + + appDataDir = std::wstring(wAppDataPath); + CoTaskMemFree(wAppDataPath); + } + + paths.push_back(appDataDir / "imhex"); } - std::vector paths = { appDataDir / "imhex" }; - if (exePath) paths.push_back(exePath->parent_path()); diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 5a11a2aeb..e20d1f13f 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -72,6 +72,17 @@ namespace hex::init { fs::ImHexPath::Logs }; + // Check if ImHex is installed in portable mode + { + if (const auto executablePath = fs::getExecutablePath(); executablePath.has_value()) { + const auto flagFile = executablePath.value() / "PORTABLE"; + + if (fs::exists(flagFile) && fs::isRegularFile(flagFile)) + ImHexApi::System::impl::setPortableVersion(true); + } + } + + // Create all folders for (auto path : paths) { for (auto &folder : fs::getDefaultPaths(path, true)) { try {