GMS 2 How to protect myself from file corruption?

Discussion in 'Programming' started by Dupletor, Aug 12, 2018.

  1. Dupletor

    Dupletor Member

    Joined:
    Apr 22, 2017
    Posts:
    328
    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.
     
  2. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    266
    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.
     
  3. Dupletor

    Dupletor Member

    Joined:
    Apr 22, 2017
    Posts:
    328
    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.
     
  4. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    266
    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.
     
  5. MarisFrance

    MarisFrance Member

    Joined:
    Jul 26, 2018
    Posts:
    22
    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.
     
  6. Dupletor

    Dupletor Member

    Joined:
    Apr 22, 2017
    Posts:
    328
    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: Aug 12, 2018
  7. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,227
    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. :)
     
    Ido-f likes this.
  8. Dupletor

    Dupletor Member

    Joined:
    Apr 22, 2017
    Posts:
    328
    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...
     
  9. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,227
    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 :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice