How to Verify Save File Integrity

I believe that clarifying this topic is of primary importance to ensure a high standard of game development.

How do I…

I’m worried that players’ save files might get corrupted, especially if in-game purchases have been made. I’ve searched extensively on the site without finding the optimal solution: I’d like to understand the best way to create backups and verify their integrity.

What is the expected result

I would like to be able to verify the integrity of each save.

What is the actual result

Currently, I alternate saves between two slots (A and B). I also save a variable, SLOTPOINT, which controls which save to execute.

But I asked ChatGPT 4-Preview for the best solution, and it explained that this is it:

Use Atomic Save Operations

Save to a Temporary File First:

When saving the game, write your data to a temporary file (e.g., SAVE_TEMP).
This ensures that you aren’t overwriting your existing save file during the write operation, which could be interrupted (e.g., app crash, device shutdown).

Replace the Old Save File:

Once the temporary save is successful, replace the old save file (SAVE) with the new one.
This can be done by deleting the old file and renaming SAVE_TEMP to SAVE.
This operation is fast and minimizes the risk of file corruption.

Error Handling:

If the save operation fails at any point, the original SAVE file remains intact.
Always check for exceptions or errors during file operations and handle them appropriately.

Here’s the system I developed to improve saved data security. Let me know any critiques, ideas, or comments to help improve it.

The game alternates saves: the first time on slot 1, then on slot 2, and vice versa.

For each save:

  1. The global variable SLOTPOINT is updated and saved.
  2. The global variable CHECKER_BACKUP (whose default value is “FIRST GAME”) is updated and saved.
  3. A backup is saved.

The loading process works as follows:

  1. Data is loaded from SLOTPOINT. If the value is 1, it loads SAVE1; if it’s 2, it loads SAVE2.
  2. If the loaded SLOTPOINT value is not 1 or 2, it loads CHECKER_BACKUP (default value is “FIRST GAME”).
  3. If CHECKER_BACKUP is “FIRST GAME,” there have been no previous saves, and the first game begins.
  4. If CHECKER_BACKUP is “USED,” there was an issue with SLOTPOINT and the last two saves. In this case, the BACKUP is loaded, and data is imported.

LOAD

SAVE