• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How to protect myself from file corruption?

Dupletor

Member
Hello.

In the game I am developing, I use save files very intensively in order to optimally create a realtime experience.
That includes access to files with different properties at different frequencies and even information buses for parallel execution (In Linux I could create globals, but I don't trust Window's Kernel, lol).

However, as I access files too frequently, I don't want a player to randomly lose the entire save in case the game closes while the file is saving. So far I was unable to find any sort of problems with the standard saving system in the game, but I barely executed the game 1000 times, so I have no idea what would be the error rate for save corruption, all I know is it is at least very rare.
Is there a way to prevent corruption completely, or is the risk mandatory? I would like to know security strategies I could use.
 
A

arirish

Guest
Could you, say, if a file successfully saves, duplicate it with file_copy, so you always have a recent backup to hand? I don't know very much about the file handling functions, I'm just spit-balling.
 

Dupletor

Member
Well, having a copy would be useful if I could identify the file is corrupted.
But a corrupted file would mean something like "the player teleported to position (143, 0) and is stuck in a wall forever now". The game can't know (143,0) is an impossible position, for example.
I can't make the game know the file was corrupted in a random case to simply swap to the copy, and if I have to force the player to go to the files and swap the original with the copy, he could just change the values in the original anyway.

I just want to make the system as safe as it can be so 5 years old kids don't have to learn what AppData is so they can save themselves from a corrupted file.
 
A

arirish

Guest
Well, checks COULD be implemented to work out if the player is in an impossible position, but then the corruption could be in another form. Impossible to know. You could also have a manual 'save corrupted, try last backup' option somewhere in your game's options, but I recognize that's a pretty non-standard way of doing things and not what you're looking for. I have exhausted my very limited thoughts on this subject, but it was fun to think about. Hopefully someone with more in-depth knowledge will have some ideas for you.
 
M

MarisFrance

Guest
Save your data to a string, not to a file. If game crashes, it won't affect old save file.
After string complete, write it to file. Writing string to a file is a very simple code, free of bugs and crashes.
 

Dupletor

Member
If the game crashes, it won't save*.
This is incompatible with realtime experiences, and unrelated to the topic.
What I seek is possibly some sort of unconventional low level solution, like an atomic save function.
At least that would be the ideal, I need to be as close to it as possible, as there just isn't an atomic saving system and every game can have its file corrupted unless they simulate that.

Is it even possible to simulate it?

PS:
Just found out that vim has atomic save options. Is there a way to do it in GMS, or to atomically send data to a C# program?
By the way, is the save system atomic in GMS at all? lol
 
Last edited:
Auto save at regular intervals in the background, keeping all backup files.

Save a checksum for each save in a separate file.

Verify the saved file with the checksum.

When loading, or at regular intervals, verify the files using the checksum data.
If that check file does not exist, or the checksum fails, you know your save file is corrupt.

In addition, save a daily copy of all valid save files to the cloud in multiple locations, virtually and physically. If the users computer catches fire, gets bathed in magnetic radiation or stolen you can still recover their save files. :)
 

Dupletor

Member
I do autosave at regular intervals. Every step (some files, a file is only saved when its info is altered). However, increasing the intervals doesn't fix anything, only reduces the luck factor.

a- Well, I guess only one file can get corrupted at a time from crashing, right?

As soon as loading an incomplete file will load '0' or "" to anything after EOF in GMS, I could add a 1 check at the end of files...
And if I save a backup, either the backup or the actual file gets corrupted, but never both. (Assuming a)
In case that works, thanks for the idea! :D

(Screw cloud backups, Undertale style lol)

I could give it a try, but I can't test because the current state is actually pretty safe. Maybe I could forkbomb the game and crash it in random intervals until the save gets corrupted to make some statistics about how secure the system is...
 
You might want to look at Journalling : https://en.wikipedia.org/wiki/Journaling_file_system

Basically, you write the changes you are going to make to a journal file first.

Then, you save the changes to the actual file. Then you verify the file is not corrupted.

Then you update the journal file to say that the write was successful.

If there was a powercut, or some problem writing the actual file (which you would verify using a checksum or similar method), you simply check the journal and redo the last incomplete operation.

If you want to have even more security, you can make a journal for the journal :)
 
Top