alfishe / nintendon-t

Automatically exported from code.google.com/p/nintendon-t
0 stars 0 forks source link

Automatically keep a backup of the previous save #150

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
By automatically keeping a backup of the previous save, any corruption of the 
current save can easily be undone by reverting to the older file. 

Original issue reported on code.google.com by matt.sep...@gmail.com on 12 Jan 2015 at 9:28

GoogleCodeExporter commented 9 years ago
This would be pretty useful, and it would quite possibly save some people from 
losing all of their progress to corrupted memory card files.  Here are some 
proposed changes to the current revision (r276) that would automatically backup 
the current save file:

(Sorry for the poor formatting, I wrote this in Notepad and don't frequently 
[read: almost never] write code in C.  This is untested and may need changes.)

Replace lines 556 and 557 of /loader/source/main.c with the following:

else {
      char backupPath[30];
      backupPath = sprintf(MemCardBackup, "%s/%s%s.raw", BasePath, MemCardName, "backup"); //"backup" should probably be a timestamp
      FILE *backup = fopen(backupPath, "wb");

      if (backup == NULL) {
          ClearScreen();
          PrintFormat(DEFAULT_SIZE, MAROON, MENU_POS_X, 232, "Failed to create Memory Card Backup!");
          ExitToLoader(1);
       }
      else {
            size_t bytesRead, bytesWritten;
            unsigned char backupBuffer[8192];
            do {
                  bytesRead = fread(backupBuffer, 1, sizeof backupBuffer, f);
                  if (bytesRead) bytesWritten = fwrite(backupBuffer, 1, bytesRead, backup);
                  else  bytesWritten  = 0;
             } while ((bytesRead > 0) && (bytesRead == bytesWritten ));
             if (bytesWritten) {
             ClearScreen();
                 PrintFormat(DEFAULT_SIZE, MAROON, MENU_POS_X, 232, "Failed to write Memory Card Backup!");
                 ExitToLoader(1);
             }
        }
        fclose(backup);
        fclose(f);
}

Original comment by garfunkiel@gmail.com on 13 Jan 2015 at 2:41