From 3c3a49195f248192fae90b13a939c38cd7b5d1c1 Mon Sep 17 00:00:00 2001 From: Will Stafford Date: Tue, 16 Apr 2024 16:32:19 -0400 Subject: [PATCH 1/2] Add mgba save compatibility option. --- README.md | 3 +++ include/arm11/config.h | 1 + source/arm11/config.c | 5 +++- source/arm11/open_agb_firm.c | 46 ++++++++++++++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4f7b4ca..3224123 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,9 @@ Options for advanced users. No pun intended. * `11`, `13`: Flash 1m * `14`: SRAM 256k * `15`: None + +`bool mgbaSaveCompat` - Look for and place romName.sav and romName.ini in the ROM directory for compatibility with mbga save behavior. +* Default: `false` ## Patches open_agb_firm supports automatically applying IPS and UPS patches. To use a patch, rename the patch file to match the ROM file name (without the extension). diff --git a/include/arm11/config.h b/include/arm11/config.h index e1d5c6a..ffa07dc 100644 --- a/include/arm11/config.h +++ b/include/arm11/config.h @@ -52,6 +52,7 @@ typedef struct // [advanced] bool saveOverride; u16 defaultSave; + bool mgbaSaveCompat; } OafConfig; //static_assert(sizeof(OafConfig) == 76, "nope"); diff --git a/source/arm11/config.c b/source/arm11/config.c index c54e431..ea0135d 100644 --- a/source/arm11/config.c +++ b/source/arm11/config.c @@ -43,7 +43,8 @@ "volume=127\n\n" \ "[advanced]\n" \ "saveOverride=false\n" \ - "defaultSave=14" + "defaultSave=14\n" \ + "mgbaSaveCompat=false" @@ -142,6 +143,8 @@ static int cfgIniCallback(void* user, const char* section, const char* name, con config->saveOverride = (strcmp(value, "false") == 0 ? false : true); if(strcmp(name, "defaultSave") == 0) config->defaultSave = (u16)strtoul(value, NULL, 10); + if(strcmp(name, "mgbaSaveCompat") == 0) + config->mgbaSaveCompat = (strcmp(value, "false") == 0 ? false : true); } else return 0; // Error. diff --git a/source/arm11/open_agb_firm.c b/source/arm11/open_agb_firm.c index 02b3e45..bc872dc 100644 --- a/source/arm11/open_agb_firm.c +++ b/source/arm11/open_agb_firm.c @@ -89,7 +89,8 @@ static OafConfig g_oafConfig = // [advanced] false, // saveOverride - 14 // defaultSave + 14, // defaultSave + false, // mgbaSaveCompat }; static KHandle g_frameReadyEvent = 0; @@ -362,6 +363,27 @@ static Result showFileBrowser(char romAndSavePath[512]) return res; } +static int getBasePathEnd(char path[512]) +{ + // Returns the index of the final "/" + // Returns -1 on error or not found. + int retval = -1; + for (int i = 0; i < 512; i++) + { + char curChar = path[i]; + if (curChar == '\0') + { + break; + } + + if (curChar == '/') + { + retval = i; + } + } + return retval; +} + static void rom2GameCfgPath(char romPath[512]) { // Extract the file name and change the extension. @@ -370,11 +392,31 @@ static void rom2GameCfgPath(char romPath[512]) safeStrcpy(tmpIniFileName, strrchr(romPath, '/') + 1, 256 - 2); strcpy(tmpIniFileName + strlen(tmpIniFileName) - 4, ".ini"); + // Support swapping ini/save to be in rom directory + char newSaveDir[512]; + strcpy(newSaveDir, OAF_SAVE_DIR "/"); + + if (g_oafConfig.mgbaSaveCompat) + { + // Get rom basepath end + int baseEndIdx = getBasePathEnd(romPath); + + // Safety check, baseEndIdx < 511 and > 0 + // On error or OoB, it will revert to original behavior + if (baseEndIdx < 511 && baseEndIdx > 0) + { + // Replace the character after the trailing slash with null term + safeStrcpy(newSaveDir, romPath, 512); + newSaveDir[baseEndIdx + 1] = '\0'; + } + } + // Construct the new path. - strcpy(romPath, OAF_SAVE_DIR "/"); + strcpy(romPath, newSaveDir); strcat(romPath, tmpIniFileName); } + static void gameCfg2SavePath(char cfgPath[512], const u8 saveSlot) { if(saveSlot > 9) From 89ea45016c114706aab322c660056c1be68aad2d Mon Sep 17 00:00:00 2001 From: Will Stafford Date: Tue, 16 Apr 2024 16:45:04 -0400 Subject: [PATCH 2/2] Fix typo in readme and spaces/tabs --- README.md | 2 +- source/arm11/config.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3224123..a76e750 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ Options for advanced users. No pun intended. * `14`: SRAM 256k * `15`: None -`bool mgbaSaveCompat` - Look for and place romName.sav and romName.ini in the ROM directory for compatibility with mbga save behavior. +`bool mgbaSaveCompat` - Look for and place romName.sav and romName.ini in the ROM directory for compatibility with mGBA save behavior. * Default: `false` ## Patches diff --git a/source/arm11/config.c b/source/arm11/config.c index ea0135d..b888739 100644 --- a/source/arm11/config.c +++ b/source/arm11/config.c @@ -44,7 +44,7 @@ "[advanced]\n" \ "saveOverride=false\n" \ "defaultSave=14\n" \ - "mgbaSaveCompat=false" + "mgbaSaveCompat=false"