My project’s save file is being corrupted if/when unity crashes during play mode. The only way that could happen is if the engine is somehow calling higher-level game logic during the crash (my SaveManager), and then interrupting it during an asynchronous serialization of the save file.
Does anyone have any information as to what exactly happens during a crash? Does OnDisable/OnDestroy/Application.Quit get called as the player dumps itself?
Everything in Unity scripting-wise happens in a single thread, one thing after another. It’s only once stuff leaves scripting control does anything remotely asynchronous happen.
Are you doing a final save at the point the application terminates, like via OnQuit or OnFocus or OnPause?? If so, once your app quits, your app’s logic could be in an unpredictable state, since obviously no code after the exception that crashed it gets called.
One way to guard against save file corruption is to write a checksum to the file, and round-robin write the file between two files, file A and file B, back and forth. Never switch to the other file until you verify the one you just wrote can be re-read into memory and the checksum / hash matches.
I was indeed launching an async task to serialize the file at quit-time. Excellent explanation on the checksum strategy, I’m definitely going to start working in that direction from now on.