2015-08-17 22:05:00 +00:00
|
|
|
#include "CMemoryCardSys.hpp"
|
2020-04-17 00:19:55 +00:00
|
|
|
#include "Runtime/GameGlobalObjects.hpp"
|
|
|
|
#include "Runtime/IMain.hpp"
|
2022-02-25 00:16:49 +00:00
|
|
|
#include <Runtime/CBasics.hpp>
|
2022-02-21 09:04:16 +00:00
|
|
|
#include <SDL_filesystem.h>
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2022-02-21 09:04:16 +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;
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:20:45 +00:00
|
|
|
std::string CMemoryCardSys::ResolveDolphinCardPath(kabufuda::ECardSlot slot) {
|
2020-04-17 00:19:55 +00:00
|
|
|
if (g_Main->IsUSA() && !g_Main->IsTrilogy()) {
|
2022-02-21 09:04:16 +00:00
|
|
|
const auto dolphinPath = GetPrefPath("dolphin-emu");
|
|
|
|
if (!dolphinPath) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return {};
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
|
|
|
auto path = *dolphinPath;
|
|
|
|
path += fmt::format(FMT_STRING("GC/MemoryCard{:c}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
2020-04-17 00:19:55 +00:00
|
|
|
|
2022-02-25 00:16:49 +00:00
|
|
|
CBasics::Sstat theStat{};
|
|
|
|
if (CBasics::Stat(path.c_str(), &theStat) != 0 || !S_ISREG(theStat.st_mode)) {
|
2020-04-17 00:19:55 +00:00
|
|
|
/* legacy case for older dolphin versions */
|
2022-02-21 09:04:16 +00:00
|
|
|
const char* home = getenv("HOME");
|
|
|
|
if (home == nullptr || home[0] != '/') {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-17 00:19:55 +00:00
|
|
|
path = home;
|
2022-02-25 00:16:49 +00:00
|
|
|
#ifndef __APPLE__
|
2020-04-17 00:19:55 +00:00
|
|
|
path += fmt::format(FMT_STRING("/.dolphin-emu/GC/MemoryCard{:c}.USA.raw"),
|
|
|
|
slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
2022-02-25 00:16:49 +00:00
|
|
|
#else
|
|
|
|
path += fmt::format(FMT_STRING("/Library/Application Support/Dolphin/GC/MemoryCard{:c}.USA.raw"),
|
|
|
|
slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
|
|
|
#endif
|
|
|
|
if (CBasics::Stat(path.c_str(), &theStat) != 0 || !S_ISREG(theStat.st_mode)) {
|
2020-04-17 00:19:55 +00:00
|
|
|
return {};
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
return {};
|
2016-12-28 21:39:38 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-02-21 09:04:16 +00:00
|
|
|
const auto dolphinPath = GetPrefPath("dolphin-emu");
|
|
|
|
if (!dolphinPath) {
|
2021-06-02 15:06:22 +00:00
|
|
|
return {};
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
|
|
|
auto path = *dolphinPath + "GC";
|
|
|
|
int ret = mkdir(path.c_str(), 0755);
|
|
|
|
if (ret != 0 && ret != EEXIST) {
|
2021-06-02 15:06:22 +00:00
|
|
|
return {};
|
2022-02-21 09:04:16 +00:00
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
|
2021-06-02 15:06:22 +00:00
|
|
|
path += fmt::format(FMT_STRING("/MemoryCard{:c}.USA.raw"), slot == kabufuda::ECardSlot::SlotA ? 'A' : 'B');
|
2022-02-21 09:04:16 +00:00
|
|
|
auto* file = fopen(path.c_str(), "wbe");
|
|
|
|
if (file != nullptr) {
|
|
|
|
fclose(file);
|
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-21 09:04:16 +00:00
|
|
|
auto basePath = GetPrefPath("metaforce");
|
|
|
|
if (!basePath) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
path = *basePath + _GetDolphinCardPath(slot);
|
|
|
|
}
|
|
|
|
std::string tmpPath = path.substr(0, path.find_last_of('/'));
|
|
|
|
int ret = mkdir(tmpPath.c_str(), 0755);
|
|
|
|
if (ret != 0 && ret != EEXIST) {
|
|
|
|
return {};
|
2021-06-02 15:06:22 +00:00
|
|
|
}
|
2022-02-21 09:04:16 +00:00
|
|
|
auto* file = fopen(path.c_str(), "wbe");
|
|
|
|
if (file != nullptr) {
|
|
|
|
fclose(file);
|
2021-06-02 15:06:22 +00:00
|
|
|
return path;
|
|
|
|
}
|
2020-04-17 00:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2017-02-05 22:27:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|