From 7850811b5b693ae961de92fdc7eed08a127dd98e Mon Sep 17 00:00:00 2001 From: thexai <58434170+thexai@users.noreply.github.com> Date: Tue, 16 Jul 2024 09:00:40 +0200 Subject: [Windows] Modernize code to obtain system AppData folder - 'SHGetFolderPathW' is deprecated, updated to 'SHGetKnownFolderPath' - Allows use LocaAppData when RoamingAppData is in UNC path --- xbmc/platform/win32/WIN32Util.cpp | 56 ++++++++++++++++++++++++--------------- xbmc/platform/win32/WIN32Util.h | 2 +- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/xbmc/platform/win32/WIN32Util.cpp b/xbmc/platform/win32/WIN32Util.cpp index 45fb259007..b5c42a1d82 100644 --- a/xbmc/platform/win32/WIN32Util.cpp +++ b/xbmc/platform/win32/WIN32Util.cpp @@ -349,27 +349,6 @@ size_t CWIN32Util::GetSystemMemorySize() #endif } -#ifdef TARGET_WINDOWS_DESKTOP -std::string CWIN32Util::GetSpecialFolder(int csidl) -{ - std::string strProfilePath; - static const int bufSize = MAX_PATH; - WCHAR* buf = new WCHAR[bufSize]; - - if(SUCCEEDED(SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, buf))) - { - buf[bufSize-1] = 0; - g_charsetConverter.wToUTF8(buf, strProfilePath); - strProfilePath = UncToSmb(strProfilePath); - } - else - strProfilePath = ""; - - delete[] buf; - return strProfilePath; -} -#endif - std::string CWIN32Util::GetProfilePath(const bool platformDirectories) { std::string strProfilePath; @@ -380,7 +359,7 @@ std::string CWIN32Util::GetProfilePath(const bool platformDirectories) std::string strHomePath = CUtil::GetHomePath(); if (platformDirectories) - strProfilePath = URIUtils::AddFileToFolder(GetSpecialFolder(CSIDL_APPDATA|CSIDL_FLAG_CREATE), CCompileInfo::GetAppName()); + strProfilePath = URIUtils::AddFileToFolder(GetAppDataFolder(), CCompileInfo::GetAppName()); else strProfilePath = URIUtils::AddFileToFolder(strHomePath , "portable_data"); @@ -392,6 +371,39 @@ std::string CWIN32Util::GetProfilePath(const bool platformDirectories) return strProfilePath; } +#ifdef TARGET_WINDOWS_DESKTOP +std::string CWIN32Util::GetAppDataFolder() +{ + std::string profilePath; + WCHAR* path = nullptr; + + // First get the roaming appdata location. + // All current users use this folder, must not break their setup. + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &path))) + { + g_charsetConverter.wToUTF8(path, profilePath); + // We do not support appdata on a UNC path. + if (profilePath.starts_with("\\\\")) + profilePath.clear(); + } + + // Must always free, even if failed. This handles NULL, no need to check. + CoTaskMemFree(path); + path = nullptr; + + // If we still do not have the data folder, get the local appdata path. + // This will only happen for new users with redirected roaming appdata. + if (profilePath.empty()) + { + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &path))) + g_charsetConverter.wToUTF8(path, profilePath); + CoTaskMemFree(path); + } + + return profilePath; +} +#endif + std::string CWIN32Util::UncToSmb(const std::string &strPath) { std::string strRetPath(strPath); diff --git a/xbmc/platform/win32/WIN32Util.h b/xbmc/platform/win32/WIN32Util.h index db4921b2d2..5e948a39af 100644 --- a/xbmc/platform/win32/WIN32Util.h +++ b/xbmc/platform/win32/WIN32Util.h @@ -67,7 +67,7 @@ public: static BOOL IsCurrentUserLocalAdministrator(); #ifdef TARGET_WINDOWS_DESKTOP - static std::string GetSpecialFolder(int csidl); + static std::string GetAppDataFolder(); static LONG UtilRegGetValue( const HKEY hKey, const char *const pcKey, DWORD *const pdwType, char **const ppcBuffer, DWORD *const pdwSizeBuff, const DWORD dwSizeAdd ); static bool UtilRegOpenKeyEx( const HKEY hKeyParent, const char *const pcKey, const REGSAM rsAccessRights, HKEY *hKey, const bool bReadX64= false ); static bool GetFocussedProcess(std::string &strProcessFile); -- cgit v1.2.3