2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CMemoryCardSys.hpp"
|
|
|
|
|
2020-04-17 19:10:52 +00:00
|
|
|
#include "Runtime/GameGlobalObjects.hpp"
|
|
|
|
#include "Runtime/IMain.hpp"
|
|
|
|
|
2016-12-28 21:39:38 +00:00
|
|
|
#include <shlobj.h>
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2017-12-06 03:26:15 +00:00
|
|
|
#if WINDOWS_STORE
|
|
|
|
using namespace Windows::Storage;
|
|
|
|
#endif
|
|
|
|
|
2016-12-28 21:39:38 +00:00
|
|
|
/* 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. */
|
2018-12-08 05:30:43 +00:00
|
|
|
kabufuda::SystemString CMemoryCardSys::ResolveDolphinCardPath(kabufuda::ECardSlot slot) {
|
2020-04-17 00:19:55 +00:00
|
|
|
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
|
2017-12-06 03:26:15 +00:00
|
|
|
#if !WINDOWS_STORE
|
2020-04-17 00:19:55 +00:00
|
|
|
/* Detect where the User directory is. There are two different cases
|
2018-12-08 05:30:43 +00:00
|
|
|
* 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;
|
|
|
|
kabufuda::SystemChar configPath[MAX_PATH] = {0};
|
|
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER, _SYS_STR("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, &hkey) ==
|
|
|
|
ERROR_SUCCESS) {
|
|
|
|
DWORD size = MAX_PATH;
|
|
|
|
if (RegQueryValueEx(hkey, _SYS_STR("UserConfigPath"), nullptr, nullptr, (LPBYTE)configPath, &size) !=
|
|
|
|
ERROR_SUCCESS)
|
|
|
|
configPath[0] = 0;
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get My Documents path in case we need it. */
|
|
|
|
kabufuda::SystemChar my_documents[MAX_PATH];
|
|
|
|
bool my_documents_found =
|
|
|
|
SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
|
|
|
|
|
|
|
|
kabufuda::SystemString path;
|
|
|
|
if (configPath[0]) /* Case 1 */
|
|
|
|
path = configPath;
|
|
|
|
else if (my_documents_found) /* Case 2 */
|
|
|
|
path = kabufuda::SystemString(my_documents) + _SYS_STR("/Dolphin Emulator");
|
|
|
|
else /* Unable to find */
|
|
|
|
return {};
|
2017-12-06 03:26:15 +00:00
|
|
|
#else
|
2020-04-17 00:19:55 +00:00
|
|
|
StorageFolder ^ localFolder = ApplicationData::Current->LocalFolder;
|
|
|
|
kabufuda::SystemString path(localFolder->Path->Data());
|
2017-12-06 03:26:15 +00:00
|
|
|
#endif
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
path += fmt::format(FMT_STRING(_SYS_STR("/GC/MemoryCard{}.USA.raw")),
|
|
|
|
slot == kabufuda::ECardSlot::SlotA ? _SYS_STR('A') : _SYS_STR('B'));
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
hecl::Sstat theStat;
|
|
|
|
if (hecl::Stat(path.c_str(), &theStat) || !S_ISREG(theStat.st_mode))
|
|
|
|
return {};
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
return {};
|
2016-12-28 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
kabufuda::SystemString CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot slot) {
|
2020-04-17 00:19:55 +00:00
|
|
|
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
|
2017-12-06 03:26:15 +00:00
|
|
|
#if !WINDOWS_STORE
|
2020-04-17 00:19:55 +00:00
|
|
|
/* Detect where the User directory is. There are two different cases
|
2018-12-08 05:30:43 +00:00
|
|
|
* 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;
|
|
|
|
kabufuda::SystemChar configPath[MAX_PATH] = {0};
|
|
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER, _SYS_STR("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, &hkey) ==
|
|
|
|
ERROR_SUCCESS) {
|
|
|
|
DWORD size = MAX_PATH;
|
|
|
|
if (RegQueryValueEx(hkey, _SYS_STR("UserConfigPath"), nullptr, nullptr, (LPBYTE)configPath, &size) !=
|
|
|
|
ERROR_SUCCESS)
|
|
|
|
configPath[0] = 0;
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get My Documents path in case we need it. */
|
|
|
|
kabufuda::SystemChar my_documents[MAX_PATH];
|
|
|
|
bool my_documents_found =
|
|
|
|
SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
|
|
|
|
|
|
|
|
kabufuda::SystemString path;
|
|
|
|
if (configPath[0]) /* Case 1 */
|
|
|
|
path = configPath;
|
|
|
|
else if (my_documents_found) /* Case 2 */
|
|
|
|
path = kabufuda::SystemString(my_documents) + _SYS_STR("/Dolphin Emulator");
|
|
|
|
else /* Unable to find */
|
|
|
|
return {};
|
2017-12-06 03:26:15 +00:00
|
|
|
#else
|
2020-04-17 00:19:55 +00:00
|
|
|
StorageFolder ^ localFolder = ApplicationData::Current->LocalFolder;
|
|
|
|
kabufuda::SystemString path(localFolder->Path->Data());
|
2017-12-06 03:26:15 +00:00
|
|
|
#endif
|
2017-02-04 08:20:09 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
path += _SYS_STR("/GC");
|
|
|
|
if (hecl::RecursiveMakeDir(path.c_str()) < 0)
|
|
|
|
return {};
|
2017-02-04 08:20:09 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
path += fmt::format(FMT_STRING(_SYS_STR("/MemoryCard{}.USA.raw")),
|
|
|
|
slot == kabufuda::ECardSlot::SlotA ? _SYS_STR('A') : _SYS_STR('B'));
|
|
|
|
const auto fp = hecl::FopenUnique(path.c_str(), _SYS_STR("wb"));
|
|
|
|
if (fp == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-02-04 08:20:09 +00:00
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
return {};
|
2017-02-04 08:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|