Skip to content

Commit 3ac52e6

Browse files
authored
Libretro: Add option to define FDS savefile format (#105)
1 parent c0d6d9e commit 3ac52e6

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

libretro/libretro.cpp

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ static bool is_pal;
103103
static byte custpal[64*3];
104104
static char slash;
105105

106+
static enum {
107+
FDS_SAVEFILE_SAV_UPS = 0,
108+
FDS_SAVEFILE_UPS,
109+
FDS_SAVEFILE_IPS,
110+
} fds_savefile_format;
111+
static bool fds_sav_extension;
112+
static bool fds_ups_extension;
113+
static bool fds_ips_extension;
114+
static bool fds_patch_format_ups;
115+
static bool fds_patch_format_ips;
116+
106117
static const byte royaltea_palette[64][3] =
107118
{
108119
{0x5A,0x61,0x65}, {0x00,0x23,0xA8}, {0x0F,0x17,0xB0}, {0x28,0x12,0x9F},
@@ -420,11 +431,18 @@ static void NST_CALLBACK file_io_callback(void*, Api::User::File &file)
420431
break;
421432
case Api::User::File::LOAD_FDS:
422433
{
423-
char base[256];
424-
sprintf(base, "%s%c%s.sav", g_save_dir, slash, g_basename);
434+
std::string base;
435+
std::string ext;
436+
if (fds_sav_extension)
437+
ext = ".sav";
438+
else if (fds_ups_extension)
439+
ext = ".ups";
440+
else if (fds_ips_extension)
441+
ext = ".ips";
442+
base = std::string(g_save_dir) + slash + g_basename + ext;
425443
if (log_cb)
426-
log_cb(RETRO_LOG_INFO, "Want to load FDS sav from: %s\n", base);
427-
std::ifstream in_tmp(base,std::ifstream::in|std::ifstream::binary);
444+
log_cb(RETRO_LOG_INFO, "Want to load FDS savefile using %s extension from: %s\n", ext.c_str(), base.c_str());
445+
std::ifstream in_tmp(base.c_str(),std::ifstream::in|std::ifstream::binary);
428446

429447
if (!in_tmp.is_open())
430448
return;
@@ -434,14 +452,23 @@ static void NST_CALLBACK file_io_callback(void*, Api::User::File &file)
434452
break;
435453
case Api::User::File::SAVE_FDS:
436454
{
437-
char base[256];
438-
sprintf(base, "%s%c%s.sav", g_save_dir, slash, g_basename);
455+
std::string base;
456+
std::string ext;
457+
if (fds_sav_extension)
458+
ext = ".sav";
459+
else if (fds_ups_extension)
460+
ext = ".ups";
461+
else if (fds_ips_extension)
462+
ext = ".ips";
463+
base = std::string(g_save_dir) + slash + g_basename + ext;
439464
if (log_cb)
440-
log_cb(RETRO_LOG_INFO, "Want to save FDS sav to: %s\n", base);
441-
std::ofstream out_tmp(base,std::ifstream::out|std::ifstream::binary);
465+
log_cb(RETRO_LOG_INFO, "Want to save FDS savefile using %s extension to: %s\n", ext.c_str(), base.c_str());
466+
std::ofstream out_tmp(base.c_str(),std::ifstream::out|std::ifstream::binary);
442467

443-
if (out_tmp.is_open())
468+
if ((out_tmp.is_open()) && (fds_patch_format_ups))
444469
file.GetPatchContent(Api::User::File::PATCH_UPS, out_tmp);
470+
else if ((out_tmp.is_open()) && (fds_patch_format_ips))
471+
file.GetPatchContent(Api::User::File::PATCH_IPS, out_tmp);
445472
}
446473
break;
447474
default:
@@ -1117,7 +1144,28 @@ static void check_variables(void)
11171144

11181145
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
11191146
fds_auto_insert = (strcmp(var.value, "enabled") == 0);
1120-
1147+
1148+
var.key = "nestopia_fds_savefile_format";
1149+
1150+
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
1151+
{
1152+
if (strcmp(var.value, "sav_ups") == 0)
1153+
fds_savefile_format = FDS_SAVEFILE_SAV_UPS;
1154+
else if (strcmp(var.value, "ups") == 0)
1155+
fds_savefile_format = FDS_SAVEFILE_UPS;
1156+
else if (strcmp(var.value, "ips") == 0)
1157+
fds_savefile_format = FDS_SAVEFILE_IPS;
1158+
1159+
/* FDS savefile format checks are positioned here to allow changes at runtime.
1160+
* This makes it possible to convert any savefile currently in use to the preferred format.*/
1161+
fds_sav_extension = (fds_savefile_format == FDS_SAVEFILE_SAV_UPS);
1162+
fds_ups_extension = (fds_savefile_format == FDS_SAVEFILE_UPS);
1163+
fds_ips_extension = (fds_savefile_format == FDS_SAVEFILE_IPS);
1164+
fds_patch_format_ups = ((fds_savefile_format == FDS_SAVEFILE_SAV_UPS) ||
1165+
(fds_savefile_format == FDS_SAVEFILE_UPS));
1166+
fds_patch_format_ips = (fds_savefile_format == FDS_SAVEFILE_IPS);
1167+
}
1168+
11211169
var.key = "nestopia_blargg_ntsc_filter";
11221170

11231171
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))

libretro/libretro_core_options.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ struct retro_core_option_v2_definition option_defs_us[] = {
114114
},
115115
"enabled"
116116
},
117+
{
118+
"nestopia_fds_savefile_format",
119+
"FDS Savefile Format",
120+
NULL,
121+
"Define which format will be used by savefiles generated for FDS games. Based on the format chosen, FDS savefiles might be used interchangeably with other cores and standalone emulators. It is recommended to use with caution, as improper handling may cause undesired overwrites, or deletion of the existing saves.",
122+
NULL,
123+
"system",
124+
{
125+
{ "sav_ups", "SAV + UPS (Default)" },
126+
{ "ups", "UPS (Standalone Nestopia)" },
127+
{ "ips", "IPS (Mesen)" },
128+
{ NULL, NULL },
129+
},
130+
"sav_ups"
131+
},
117132

118133
/* Video */
119134

0 commit comments

Comments
 (0)