Put saves in ./mkxp-z/Saves/ in libretro save directory

Since the libretro save directory isn't guaranteed to be private for
each core, or even private for just saves, we'd better create a
directory structure in the libretro save directory for our saves.
This commit is contained in:
刘皓 2025-05-07 18:35:25 -04:00
parent c85e9554ec
commit 66256e9156
No known key found for this signature in database
GPG key ID: 7901753DB465B711

View file

@ -161,7 +161,9 @@ template <> struct atomic<uint64_t> {
int mkxp_physfs_allow_duplicates = false; int mkxp_physfs_allow_duplicates = false;
struct physfs_allow_duplicates_guard { struct physfs_allow_duplicates_guard {
physfs_allow_duplicates_guard() { bool old_value;
physfs_allow_duplicates_guard() : old_value(mkxp_physfs_allow_duplicates) {
mkxp_physfs_allow_duplicates = true; mkxp_physfs_allow_duplicates = true;
} }
@ -174,7 +176,7 @@ struct physfs_allow_duplicates_guard {
struct physfs_allow_duplicates_guard &operator=(struct physfs_allow_duplicates_guard &&guard) noexcept = delete; struct physfs_allow_duplicates_guard &operator=(struct physfs_allow_duplicates_guard &&guard) noexcept = delete;
~physfs_allow_duplicates_guard() { ~physfs_allow_duplicates_guard() {
mkxp_physfs_allow_duplicates = false; mkxp_physfs_allow_duplicates = old_value;
} }
}; };
@ -403,13 +405,13 @@ static bool init_sandbox() {
{ {
const char *save_path; const char *save_path;
if (environment(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_path)) { if (environment(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_path) && save_path != nullptr) {
// Save to the subdirectory of the save directory corresponding to the game's name set in Game.ini // Save to the subdirectory of the save directory corresponding to the game's name set in Game.ini
std::string save_path_subdir(save_path); std::string save_path_subdir(save_path);
#ifdef _WIN32 #ifdef _WIN32
save_path_subdir.push_back('\\'); save_path_subdir.append("\\mkxp-z\\Saves\\");
#else #else
save_path_subdir.push_back('/'); save_path_subdir.append("/mkxp-z/Saves/");
#endif // _WIN32 #endif // _WIN32
if (!conf->windowTitle.empty()) { if (!conf->windowTitle.empty()) {
save_path_subdir.append(conf->windowTitle); save_path_subdir.append(conf->windowTitle);
@ -420,7 +422,7 @@ static bool init_sandbox() {
} }
// Sanitize forbidden characters in the game name // Sanitize forbidden characters in the game name
for (size_t i = std::strlen(save_path) + 1; i < save_path_subdir.length(); ++i) { for (size_t i = std::strlen(save_path) + (sizeof "/mkxp-z/Saves/" - 1); i < save_path_subdir.length(); ++i) {
if (save_path_subdir[i] < 32 || save_path_subdir[i] == '/' || save_path_subdir[i] == '\\' || save_path_subdir[i] == '*' || save_path_subdir[i] == '?' || save_path_subdir[i] == '|' || ((save_path_subdir[i] == ' ' || save_path_subdir[i] == '.') && i + 1 == save_path_subdir.length())) { if (save_path_subdir[i] < 32 || save_path_subdir[i] == '/' || save_path_subdir[i] == '\\' || save_path_subdir[i] == '*' || save_path_subdir[i] == '?' || save_path_subdir[i] == '|' || ((save_path_subdir[i] == ' ' || save_path_subdir[i] == '.') && i + 1 == save_path_subdir.length())) {
save_path_subdir[i] = '_'; save_path_subdir[i] = '_';
} else if (save_path_subdir[i] == '"') { } else if (save_path_subdir[i] == '"') {