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"
|
2022-02-22 05:53:57 +00:00
|
|
|
#include "Runtime/CBasics.hpp"
|
2020-04-17 19:10:52 +00:00
|
|
|
|
2022-02-22 05:53:57 +00:00
|
|
|
#include <ShlObj.h>
|
|
|
|
#include <SDL_filesystem.h>
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2022-02-22 05:53:57 +00:00
|
|
|
static std::optional<std::string> GetPrefPath(const char* app) {
|
|
|
|
char* path = SDL_GetPrefPath(nullptr, app);
|
|
|
|
if (path == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::string str{path};
|
|
|
|
SDL_free(path);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
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. */
|
2021-06-30 18:20:45 +00:00
|
|
|
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;
|
2021-06-30 18:20:45 +00:00
|
|
|
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;
|
2021-06-30 18:20:45 +00:00
|
|
|
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. */
|
2021-06-30 18:20:45 +00:00
|
|
|
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));
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string path;
|
2021-06-02 18:08:20 +00:00
|
|
|
if (configPath[0]) /* Case 1 */
|
2021-06-30 18:20:45 +00:00
|
|
|
path = nowide::narrow(configPath);
|
2021-06-02 18:08:20 +00:00
|
|
|
else if (my_documents_found) /* Case 2 */
|
2021-06-30 18:20:45 +00:00
|
|
|
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;
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string path(localFolder->Path->Data());
|
2021-06-02 18:08:20 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
path += fmt::format(FMT_STRING("/GC/MemoryCard{}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
2021-06-02 18:08:20 +00:00
|
|
|
|
2022-02-22 05:53:57 +00:00
|
|
|
struct _stat64 theStat{};
|
|
|
|
if (_stat64(path.c_str(), &theStat) || !S_ISREG(theStat.st_mode))
|
2021-06-02 18:08:20 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string CMemoryCardSys::_CreateDolphinCard(kabufuda::ECardSlot slot, bool dolphin) {
|
2020-04-17 00:19:55 +00:00
|
|
|
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
|
2021-06-02 15:06:22 +00:00
|
|
|
if (dolphin) {
|
2017-12-06 03:26:15 +00:00
|
|
|
#if !WINDOWS_STORE
|
2021-06-02 15:06:22 +00:00
|
|
|
/* 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
|
|
|
|
2021-06-02 15:06:22 +00:00
|
|
|
/* Check our registry keys */
|
|
|
|
HKEY hkey;
|
2021-06-30 18:20:45 +00:00
|
|
|
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 15:06:22 +00:00
|
|
|
DWORD size = MAX_PATH;
|
2021-06-30 18:20:45 +00:00
|
|
|
if (RegQueryValueEx(hkey, L"UserConfigPath", nullptr, nullptr, (LPBYTE)configPath, &size) != ERROR_SUCCESS)
|
2021-06-02 15:06:22 +00:00
|
|
|
configPath[0] = 0;
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
|
2021-06-02 15:06:22 +00:00
|
|
|
/* Get My Documents path in case we need it. */
|
2021-06-30 18:20:45 +00:00
|
|
|
wchar_t my_documents[MAX_PATH];
|
2021-06-02 15:06:22 +00:00
|
|
|
bool my_documents_found =
|
|
|
|
SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
|
2020-04-17 00:19:55 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string path;
|
2021-06-02 15:06:22 +00:00
|
|
|
if (configPath[0]) /* Case 1 */
|
2021-06-30 18:20:45 +00:00
|
|
|
path = nowide::narrow(configPath);
|
2021-06-02 15:06:22 +00:00
|
|
|
else if (my_documents_found) /* Case 2 */
|
2021-06-30 18:20:45 +00:00
|
|
|
path = nowide::narrow(my_documents) + "/Dolphin Emulator";
|
2021-06-02 15:06:22 +00:00
|
|
|
else /* Unable to find */
|
|
|
|
return {};
|
2017-12-06 03:26:15 +00:00
|
|
|
#else
|
2021-06-02 15:06:22 +00:00
|
|
|
StorageFolder ^ localFolder = ApplicationData::Current->LocalFolder;
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string path(localFolder->Path->Data());
|
2017-12-06 03:26:15 +00:00
|
|
|
#endif
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
path += "/GC";
|
2022-02-22 05:53:57 +00:00
|
|
|
if (CBasics::RecursiveMakeDir(path.c_str()) < 0)
|
2021-06-02 18:08:20 +00:00
|
|
|
return {};
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
path += fmt::format(FMT_STRING("/MemoryCard{}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
2022-02-22 05:53:57 +00:00
|
|
|
const nowide::wstackstring wpath(path);
|
|
|
|
FILE* fp = _wfopen(wpath.get(), L"wb");
|
2021-06-02 18:08:20 +00:00
|
|
|
if (fp == nullptr) {
|
2021-06-02 15:06:22 +00:00
|
|
|
return {};
|
2021-06-02 18:08:20 +00:00
|
|
|
}
|
2022-02-22 05:53:57 +00:00
|
|
|
fclose(fp);
|
2016-12-28 21:39:38 +00:00
|
|
|
|
2021-06-02 15:06:22 +00:00
|
|
|
return path;
|
|
|
|
} else {
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string path = _GetDolphinCardPath(slot);
|
|
|
|
if (path.find('/') == std::string::npos) {
|
2022-02-22 05:53:57 +00:00
|
|
|
auto prefPath = GetPrefPath("Metaforce");
|
|
|
|
if (!prefPath) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
path = *prefPath + _GetDolphinCardPath(slot);
|
2021-06-02 15:06:22 +00:00
|
|
|
}
|
2022-02-22 05:53:57 +00:00
|
|
|
std::string tmpPath = path.substr(0, path.find_last_of('/'));
|
|
|
|
CBasics::RecursiveMakeDir(tmpPath.c_str());
|
|
|
|
const nowide::wstackstring wpath(path);
|
|
|
|
FILE* fp = _wfopen(wpath.get(), L"wb");
|
2021-06-02 15:06:22 +00:00
|
|
|
if (fp) {
|
2022-02-22 05:53:57 +00:00
|
|
|
fclose(fp);
|
2021-06-02 15:06:22 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
}
|
|
|
|
return {};
|
2016-12-28 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|