Implement numbered saves to avoid catastrophic save game loss

This commit is contained in:
Travis Burtrum 2021-08-27 20:25:26 -04:00
parent 86785bcd72
commit 3733e676f9
3 changed files with 53 additions and 13 deletions

View File

@ -6,6 +6,10 @@ Alternative Everdrive64 menu
originally written by saturnu, and released on the originally written by saturnu, and released on the
[Everdrive64 forum](http://krikzz.com/forum/index.php?topic=816.0). [Everdrive64 forum](http://krikzz.com/forum/index.php?topic=816.0).
## Reason for this fork
The original version overwrote 1 save file per game on system reset, but if you (or your kids...) accidentally turn it off, or hit reset twice, you just lost your entire game progress forever. I have modified the saving/loading to *never* overwrite a save file, and instead save `gamename.0000.SRM`, then `gamename.0001.SRM` next and so on, up to `gamename.9999.SRM`, so that absolute worst case if you mess up you only lose 1 gaming session's save. If need be you can put the SD card into a computer and delete the latest faulty save. Upon starting a game it'll always load the highest numbered save.
## Building ## Building
If you want to build the menu, you need an n64 toolchain. This is terrible to build, I ended up creating a Dockerfile in the docker folder, instructions included in it. If you want to build the menu, you need an n64 toolchain. This is terrible to build, I ended up creating a Dockerfile in the docker folder, instructions included in it.

View File

@ -50,7 +50,6 @@
#include "cic.h" #include "cic.h"
#define ED64PLUS #define ED64PLUS
#define USE_TRUETYPE
#ifdef ED64PLUS #ifdef ED64PLUS
#define ED64_FIRMWARE_PATH "ED64P" #define ED64_FIRMWARE_PATH "ED64P"
@ -1625,20 +1624,39 @@ int backupSaveData(display_context_t disp)
int saveTypeFromSd(display_context_t disp, char *rom_name, int stype) int saveTypeFromSd(display_context_t disp, char *rom_name, int stype)
{ {
TRACE(disp, rom_filename); TRACE(disp, rom_filename);
TCHAR fname[256] = {0};
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%s", save_path, rom_name, saveTypeToExtension(stype, ext_type));
TCHAR fname1[50] = {0}; const char* save_type_extension = saveTypeToExtension(stype, ext_type);
sprintf(fname1, "/"ED64_FIRMWARE_PATH"/%s/", save_path); TCHAR fname[256] = {0};
printText(fname1, 3, -1, disp); int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
TCHAR fname2[50] = {0}; FRESULT result;
sprintf(fname2, "%s.%s", rom_name, saveTypeToExtension(stype, ext_type)); FILINFO fnoba;
printText(fname2, 3, -1, disp); printText("Finding latest save slot...", 3, -1, disp);
display_show(disp);
while (true) {
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04x.%s", save_path, rom_name, save_count, save_type_extension);
result = f_stat (fname, &fnoba);
if (result != FR_OK) {
// we found our first missing save slot, break
break;
}
++save_count;
}
if (save_count > 0) {
// we've went 1 past the end, so back up
sprintf(fname, "Found latest save slot: %04x", --save_count);
printText(fname, 3, -1, disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04x.%s", save_path, rom_name, save_count, save_type_extension);
} else {
// not even a 0000 was found, so look at the original name before numbering was implemented
printText("No save slot found!", 3, -1, disp);
printText("Looking for non-numbered file...", 3, -1, disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%s", save_path, rom_name, save_type_extension);
}
display_show(disp);
int size = saveTypeToSize(stype); // int byte int size = saveTypeToSize(stype); // int byte
uint8_t cartsave_data[size]; uint8_t cartsave_data[size];
FRESULT result;
FIL file; FIL file;
UINT bytesread; UINT bytesread;
result = f_open(&file, fname, FA_READ); result = f_open(&file, fname, FA_READ);
@ -1709,14 +1727,30 @@ int saveTypeFromSd(display_context_t disp, char *rom_name, int stype)
int saveTypeToSd(display_context_t disp, char *rom_name, int stype) int saveTypeToSd(display_context_t disp, char *rom_name, int stype)
{ {
//after reset create new savefile //after reset create new savefile
const char* save_type_extension = saveTypeToExtension(stype, ext_type);
TCHAR fname[256]; //TODO: change filename buffers to 256!!! TCHAR fname[256]; //TODO: change filename buffers to 256!!!
int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%s", save_path, rom_name, saveTypeToExtension(stype, ext_type)); FRESULT result;
FILINFO fnoba;
printText("Finding unused save slot...", 3, -1, disp);
display_show(disp);
while (true) {
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04x.%s", save_path, rom_name, save_count, save_type_extension);
result = f_stat (fname, &fnoba);
if (result != FR_OK) {
// we found our first missing save slot, break
break;
}
++save_count;
}
sprintf(fname, "Found unused save slot: %04x", save_count);
printText(fname, 3, -1, disp);
display_show(disp);
sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%04x.%s", save_path, rom_name, save_count, save_type_extension);
int size = saveTypeToSize(stype); // int byte int size = saveTypeToSize(stype); // int byte
TRACEF(disp, "size for save=%i", size); TRACEF(disp, "size for save=%i", size);
FRESULT result;
FIL file; FIL file;
UINT bytesread; UINT bytesread;
result = f_open(&file, fname, FA_WRITE | FA_OPEN_ALWAYS); //Could use FA_CREATE_ALWAYS but this could lead to the posibility of the file being emptied result = f_open(&file, fname, FA_WRITE | FA_OPEN_ALWAYS); //Could use FA_CREATE_ALWAYS but this could lead to the posibility of the file being emptied
@ -1755,6 +1789,7 @@ int saveTypeToSd(display_context_t disp, char *rom_name, int stype)
else else
{ {
TRACE(disp, "COULDNT CREATE FILE :-("); TRACE(disp, "COULDNT CREATE FILE :-(");
printText("Error saving game to SD, couldn't create file!", 3, -1, disp);
} }
} }

View File

@ -29,4 +29,5 @@ void menu_about(display_context_t disp)
printText("ChillyWilly", 9, -1, disp); printText("ChillyWilly", 9, -1, disp);
printText("ShaunTaylor", 9, -1, disp); printText("ShaunTaylor", 9, -1, disp);
printText("Conle", 9, -1, disp); printText("Conle", 9, -1, disp);
printText("moparisthebest", 9, -1, disp);
} //TODO: make scrolling text, should include libraries used. } //TODO: make scrolling text, should include libraries used.