metaforce/Runtime/CMemoryCardSysWin.cpp

132 lines
4.4 KiB
C++
Raw Permalink Normal View History

#include "Runtime/CMemoryCardSys.hpp"
2020-04-17 19:10:52 +00:00
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/IMain.hpp"
#include <shlobj.h>
2021-04-10 08:42:06 +00:00
namespace metaforce {
2017-12-06 03:26:15 +00:00
#if WINDOWS_STORE
using namespace Windows::Storage;
#endif
/* Partial path-selection logic from
* https://github.com/dolphin-emu/dolphin/blob/master/Source/Core/UICommon/UICommon.cpp
* Modified to not use dolphin-binary-relative paths. */
std::string CMemoryCardSys::ResolveDolphinCardPath(kabufuda::ECardSlot slot) {
2021-06-02 18:08:20 +00:00
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
#if !WINDOWS_STORE
/* Detect where the User directory is. There are two different cases
* 1. HKCU\Software\Dolphin Emulator\UserConfigPath exists
* -> Use this as the user directory path
* 2. My Documents exists
* -> Use My Documents\Dolphin Emulator as the User directory path
*/
/* Check our registry keys */
HKEY hkey;
wchar_t configPath[MAX_PATH] = {0};
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Dolphin Emulator", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) {
2021-06-02 18:08:20 +00:00
DWORD size = MAX_PATH;
if (RegQueryValueEx(hkey, L"UserConfigPath", nullptr, nullptr, (LPBYTE)configPath, &size) != ERROR_SUCCESS)
2021-06-02 18:08:20 +00:00
configPath[0] = 0;
RegCloseKey(hkey);
}
/* Get My Documents path in case we need it. */
wchar_t my_documents[MAX_PATH];
2021-06-02 18:08:20 +00:00
bool my_documents_found =
SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
std::string path;
2021-06-02 18:08:20 +00:00
if (configPath[0]) /* Case 1 */
path = nowide::narrow(configPath);
2021-06-02 18:08:20 +00:00
else if (my_documents_found) /* Case 2 */
path = nowide::narrow(my_documents) + "/Dolphin Emulator";
2021-06-02 18:08:20 +00:00
else /* Unable to find */
return {};
#else
StorageFolder ^ localFolder = ApplicationData::Current->LocalFolder;
std::string path(localFolder->Path->Data());
2021-06-02 18:08:20 +00:00
#endif
path += fmt::format(FMT_STRING("/GC/MemoryCard{}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
2021-06-02 18:08:20 +00:00
hecl::Sstat theStat;
if (hecl::Stat(path.c_str(), &theStat) || !S_ISREG(theStat.st_mode))
return {};
return path;
}
return {};
}
std::string CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot slot, bool dolphin) {
2020-04-17 00:19:55 +00:00
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
if (dolphin) {
2017-12-06 03:26:15 +00:00
#if !WINDOWS_STORE
/* Detect where the User directory is. There are two different cases
* 1. HKCU\Software\Dolphin Emulator\UserConfigPath exists
* -> Use this as the user directory path
* 2. My Documents exists
* -> Use My Documents\Dolphin Emulator as the User directory path
*/
2020-04-17 00:19:55 +00:00
/* Check our registry keys */
HKEY hkey;
wchar_t configPath[MAX_PATH] = {0};
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Dolphin Emulator", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) {
DWORD size = MAX_PATH;
if (RegQueryValueEx(hkey, L"UserConfigPath", nullptr, nullptr, (LPBYTE)configPath, &size) != ERROR_SUCCESS)
configPath[0] = 0;
RegCloseKey(hkey);
}
2020-04-17 00:19:55 +00:00
/* Get My Documents path in case we need it. */
wchar_t my_documents[MAX_PATH];
bool my_documents_found =
SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
2020-04-17 00:19:55 +00:00
std::string path;
if (configPath[0]) /* Case 1 */
path = nowide::narrow(configPath);
else if (my_documents_found) /* Case 2 */
path = nowide::narrow(my_documents) + "/Dolphin Emulator";
else /* Unable to find */
return {};
2017-12-06 03:26:15 +00:00
#else
StorageFolder ^ localFolder = ApplicationData::Current->LocalFolder;
std::string path(localFolder->Path->Data());
2017-12-06 03:26:15 +00:00
#endif
path += "/GC";
2021-06-02 18:08:20 +00:00
if (hecl::RecursiveMakeDir(path.c_str()) < 0)
return {};
path += fmt::format(FMT_STRING("/MemoryCard{}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
const auto fp = hecl::FopenUnique(path.c_str(), "wb");
2021-06-02 18:08:20 +00:00
if (fp == nullptr) {
return {};
2021-06-02 18:08:20 +00:00
}
return path;
} else {
std::string path = _GetDolphinCardPath(slot);
hecl::SanitizePath(path);
if (path.find('/') == std::string::npos) {
path = hecl::GetcwdStr() + "/" + _GetDolphinCardPath(slot);
}
std::string tmpPath = path.substr(0, path.find_last_of("/"));
hecl::RecursiveMakeDir(tmpPath.c_str());
const auto fp = hecl::FopenUnique(path.c_str(), "wb");
if (fp) {
return path;
}
}
2020-04-17 00:19:55 +00:00
}
return {};
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce