Address WIL todos in UICommon.cpp

This commit is contained in:
Silent 2023-02-12 15:01:21 +01:00
parent efed037c4a
commit 9f3d3e2b9c
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
1 changed files with 18 additions and 24 deletions

View File

@ -12,6 +12,8 @@
#include <sstream> #include <sstream>
#ifdef _WIN32 #ifdef _WIN32
#include <shlobj.h> // for SHGetFolderPath #include <shlobj.h> // for SHGetFolderPath
#include <wil/resource.h>
#endif #endif
#include "Common/Common.h" #include "Common/Common.h"
@ -305,51 +307,46 @@ void SetUserDirectory(std::string custom_path)
// -> Use GetExeDirectory()\User // -> Use GetExeDirectory()\User
// Get AppData path in case we need it. // Get AppData path in case we need it.
// TODO: Maybe use WIL when it's available? wil::unique_cotaskmem_string appdata;
PWSTR appdata = nullptr; bool appdata_found = SUCCEEDED(
bool appdata_found = SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, appdata.put()));
SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &appdata));
#ifndef STEAM #ifndef STEAM
// Check our registry keys // Check our registry keys
// TODO: Maybe use WIL when it's available? wil::unique_hkey hkey;
HKEY hkey;
DWORD local = 0; DWORD local = 0;
std::unique_ptr<TCHAR[]> configPath; std::unique_ptr<TCHAR[]> configPath;
if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE,
&hkey) == ERROR_SUCCESS) hkey.put()) == ERROR_SUCCESS)
{ {
DWORD size = 4; DWORD size = sizeof(local);
if (RegQueryValueEx(hkey, TEXT("LocalUserConfig"), nullptr, nullptr, if (RegQueryValueEx(hkey.get(), TEXT("LocalUserConfig"), nullptr, nullptr,
reinterpret_cast<LPBYTE>(&local), &size) != ERROR_SUCCESS) reinterpret_cast<LPBYTE>(&local), &size) != ERROR_SUCCESS)
{ {
local = 0; local = 0;
} }
size = 0; size = 0;
RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size); RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size);
configPath = std::make_unique<TCHAR[]>(size / sizeof(TCHAR)); configPath = std::make_unique<TCHAR[]>(size / sizeof(TCHAR));
if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, if (RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr,
reinterpret_cast<LPBYTE>(configPath.get()), &size) != ERROR_SUCCESS) reinterpret_cast<LPBYTE>(configPath.get()), &size) != ERROR_SUCCESS)
{ {
configPath.reset(); configPath.reset();
} }
RegCloseKey(hkey);
} }
local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt"); local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt");
// Attempt to check if the old User directory exists in My Documents. // Attempt to check if the old User directory exists in Documents.
// TODO: Maybe use WIL when it's available? wil::unique_cotaskmem_string documents;
PWSTR documents = nullptr; bool documents_found = SUCCEEDED(
bool documents_found = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, documents.put()));
SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &documents));
std::optional<std::string> old_user_folder; std::optional<std::string> old_user_folder;
if (documents_found) if (documents_found)
{ {
old_user_folder = TStrToUTF8(documents) + DIR_SEP NORMAL_USER_DIR DIR_SEP; old_user_folder = TStrToUTF8(documents.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP;
} }
if (local) // Case 1-2 if (local) // Case 1-2
@ -366,7 +363,7 @@ void SetUserDirectory(std::string custom_path)
} }
else if (appdata_found) // Case 5 else if (appdata_found) // Case 5
{ {
user_path = TStrToUTF8(appdata) + DIR_SEP NORMAL_USER_DIR DIR_SEP; user_path = TStrToUTF8(appdata.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP;
// Set the UserConfigPath value in the registry for backwards compatibility with older Dolphin // Set the UserConfigPath value in the registry for backwards compatibility with older Dolphin
// builds, which will look for the default User directory in Documents. If we set this key, // builds, which will look for the default User directory in Documents. If we set this key,
@ -381,8 +378,6 @@ void SetUserDirectory(std::string custom_path)
{ {
user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP; user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP;
} }
CoTaskMemFree(documents);
#else // ifndef STEAM #else // ifndef STEAM
if (File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt")) // Case 1 if (File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt")) // Case 1
{ {
@ -390,7 +385,7 @@ void SetUserDirectory(std::string custom_path)
} }
else if (appdata_found) // Case 2 else if (appdata_found) // Case 2
{ {
user_path = TStrToUTF8(appdata) + DIR_SEP NORMAL_USER_DIR DIR_SEP; user_path = TStrToUTF8(appdata.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP;
} }
else // Case 3 else // Case 3
{ {
@ -398,7 +393,6 @@ void SetUserDirectory(std::string custom_path)
} }
#endif #endif
CoTaskMemFree(appdata);
#else #else
if (File::IsDirectory(ROOT_DIR DIR_SEP EMBEDDED_USER_DIR)) if (File::IsDirectory(ROOT_DIR DIR_SEP EMBEDDED_USER_DIR))
{ {