Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address WIL todos in UICommon.cpp #11555

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 18 additions & 24 deletions Source/Core/UICommon/UICommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <sstream>
#ifdef _WIN32
#include <shlobj.h> // for SHGetFolderPath

#include <wil/resource.h>
#endif

#include "Common/Common.h"
Expand Down Expand Up @@ -305,51 +307,46 @@ void SetUserDirectory(std::string custom_path)
// -> Use GetExeDirectory()\User

// Get AppData path in case we need it.
// TODO: Maybe use WIL when it's available?
PWSTR appdata = nullptr;
bool appdata_found =
SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &appdata));
wil::unique_cotaskmem_string appdata;
bool appdata_found = SUCCEEDED(
SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, appdata.put()));

#ifndef STEAM
// Check our registry keys
// TODO: Maybe use WIL when it's available?
HKEY hkey;
wil::unique_hkey hkey;
DWORD local = 0;
std::unique_ptr<TCHAR[]> configPath;
if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE,
&hkey) == ERROR_SUCCESS)
hkey.put()) == ERROR_SUCCESS)
{
DWORD size = 4;
if (RegQueryValueEx(hkey, TEXT("LocalUserConfig"), nullptr, nullptr,
DWORD size = sizeof(local);
if (RegQueryValueEx(hkey.get(), TEXT("LocalUserConfig"), nullptr, nullptr,
reinterpret_cast<LPBYTE>(&local), &size) != ERROR_SUCCESS)
{
local = 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));
if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr,
if (RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr,
reinterpret_cast<LPBYTE>(configPath.get()), &size) != ERROR_SUCCESS)
{
configPath.reset();
}

RegCloseKey(hkey);
}

local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt");

// Attempt to check if the old User directory exists in My Documents.
// TODO: Maybe use WIL when it's available?
PWSTR documents = nullptr;
bool documents_found =
SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &documents));
// Attempt to check if the old User directory exists in Documents.
wil::unique_cotaskmem_string documents;
bool documents_found = SUCCEEDED(
SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, documents.put()));

std::optional<std::string> old_user_folder;
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
Expand All @@ -366,7 +363,7 @@ void SetUserDirectory(std::string custom_path)
}
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
// builds, which will look for the default User directory in Documents. If we set this key,
Expand All @@ -381,24 +378,21 @@ void SetUserDirectory(std::string custom_path)
{
user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP;
}

CoTaskMemFree(documents);
#else // ifndef STEAM
if (File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt")) // Case 1
{
user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP;
}
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
{
user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP;
}
#endif

CoTaskMemFree(appdata);
#else
if (File::IsDirectory(ROOT_DIR DIR_SEP EMBEDDED_USER_DIR))
{
Expand Down