Skip to content

Commit

Permalink
Implement numbered saves to avoid catastrophic save game loss
Browse files Browse the repository at this point in the history
  • Loading branch information
moparisthebest committed Aug 28, 2021
1 parent 86785bc commit 3733e67
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -6,6 +6,10 @@ Alternative Everdrive64 menu
originally written by saturnu, and released on the
[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

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.
Expand Down
61 changes: 48 additions & 13 deletions src/main.c
Expand Up @@ -50,7 +50,6 @@
#include "cic.h"

#define ED64PLUS
#define USE_TRUETYPE

#ifdef ED64PLUS
#define ED64_FIRMWARE_PATH "ED64P"
Expand Down Expand Up @@ -1625,20 +1624,39 @@ int backupSaveData(display_context_t disp)
int saveTypeFromSd(display_context_t disp, char *rom_name, int stype)
{
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};
sprintf(fname1, "/"ED64_FIRMWARE_PATH"/%s/", save_path);
printText(fname1, 3, -1, disp);
TCHAR fname2[50] = {0};
sprintf(fname2, "%s.%s", rom_name, saveTypeToExtension(stype, ext_type));
printText(fname2, 3, -1, disp);
const char* save_type_extension = saveTypeToExtension(stype, ext_type);
TCHAR fname[256] = {0};
int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
FRESULT result;
FILINFO fnoba;
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
uint8_t cartsave_data[size];

FRESULT result;
FIL file;
UINT bytesread;
result = f_open(&file, fname, FA_READ);
Expand Down Expand Up @@ -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)
{
//after reset create new savefile
const char* save_type_extension = saveTypeToExtension(stype, ext_type);
TCHAR fname[256]; //TODO: change filename buffers to 256!!!

sprintf(fname, "/"ED64_FIRMWARE_PATH"/%s/%s.%s", save_path, rom_name, saveTypeToExtension(stype, ext_type));
int save_count = 0; //TODO: once this crosses 9999 bad infinite-loop type things happen, look into that one day
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
TRACEF(disp, "size for save=%i", size);

FRESULT result;
FIL file;
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
Expand Down Expand Up @@ -1755,6 +1789,7 @@ int saveTypeToSd(display_context_t disp, char *rom_name, int stype)
else
{
TRACE(disp, "COULDNT CREATE FILE :-(");
printText("Error saving game to SD, couldn't create file!", 3, -1, disp);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/menu_about.c
Expand Up @@ -29,4 +29,5 @@ void menu_about(display_context_t disp)
printText("ChillyWilly", 9, -1, disp);
printText("ShaunTaylor", 9, -1, disp);
printText("Conle", 9, -1, disp);
printText("moparisthebest", 9, -1, disp);
} //TODO: make scrolling text, should include libraries used.

0 comments on commit 3733e67

Please sign in to comment.