I had a user run into a computer crash, coincidentally at the same moment that my game was writing some data to disk. After restarting their system, the file was heavily corrupted, being filled with nothing but empty characters. Making matters worse, Steam sync then uploaded this junk file, overwriting their old cloud save, totally wiping out their progress, with no way to recover it. Obviously a terribly thing.
I thought my approach to file saving was pretty solid. Here’s what I’m doing currently:
// Write the data to a temp file, not the existing file.
File.WriteAllText(tempFileName, dataAsJson);
// Now copy that completed temp file onto the existing file.
File.Copy(tempFileName, fileInfo.FullName, true);
// Now delete the temp file.
File.Delete(tempFileName);
The idea here was that if anything went wrong with File.WriteAllText, the content was being written to a temp file, not to their existing save file. Only after a successful WriteAllText was I copying it to the existing file path. But that is apparently insufficient, and it seems that the crash occurring during File.Copy resulted in a corrupted file.
Anyone have any approaches to a more robust file save?
I’m planning on trying something fairly complex, which I’d happily avoid doing if someone knows of a simpler way to make my existing approach bullet proof.
Anyway, my proposed new approach will be to make a backup of the existing file before I try to save new data to it. After I perform the File.Copy, I’ll then read the file again, and make sure it has the correct content. As long as it does, I’ll delete the backup file. But if something goes wrong, the backup file will remain. Upon starting up the game, I’ll check whether any of those backup files exist, and prompt the user to recover them.
Or maybe that’ll be vulnerable to the same kind of problems?