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