Trouble syncing save files with Steam Auto-Cloud, and a manual Greenworks solution

Prelude​

Hi, everyone. I want to be super clear up front: I do not currently have an issue, and I am not seeking an answer to any question. What I would like to do is document a large problem I discovered and how I solved it. I did extensive searching while trying to find a resolution, and either no one has run into the problem before (doubtful), or a certain past bug in the engine itself is “clogging” up the google search results, making it difficult to discover the discussion history of my actual problem.

Mods, if I’ve misunderstood where a thread such as this should go, apologies, but this seemed like the most appropriate place.

So, without further ado…

The Problem​

A few weekends ago, I attempted to turn Steam Auto-Cloud on for my in-development game. From reading the Steam documentation, it seemed pretty straightforward. RPG Maker MZ stores its save files in the save directory under the root project, and all of those files have the .rmmzsave suffix. There’s global.rmmzsave, config.rmmzsave, and then a bunch of files corresponding to numbered save slots, file#.rmmzsave. I added a Steam Auto-Cloud rule to sync all files in the save directory that end in .rmmzsave, and I figured that was a wrap.

It was not.

If you watch the Steam console when exiting the game, you’ll get a message like this:
Upload I/O Operation Failed for file save/file1.rmmzsave src\clientdll\remotestoragefilesynccontext.cpp (288) : Assertion Failed: Failed to read file 'save/file1.rmmzsave' for upload (2)

Further, the Steam client will indicate a “cloud desync” error, and hitting the retry button won’t resolve the issue.

Interestingly, I started playing around with specifying rules on file-by-file basis, and config.rmmzsave would sync with Auto-Cloud, along with a custom file I’m generating as part of a custom feature I wrote, but global and the save files won’t… which kind of defeats the whole point.

The Search​

My attempts to find out who had done what with RPG Maker MZ and Steam Auto-Cloud were thwarted when using both this forum’s search feature and Google. The problem is that, at some point in the past, the RPG Maker engine itself utilized Steam Cloud for backing up game projects, and this led to a bunch of frustration on these forums and, it seems in some cases, weeks or months of lost work. As a result, searches for anything with “steam cloud” in the key words turns up all these grievances, which are unrelated to my issue of setting up Steam Cloud to work with save files for my game.

If anyone has posted on this forum before about how to solve Steam Auto-Cloud backup of save files for RPG Maker games, I couldn’t find it. The closest I got was this reference on Reddit to someone saying they “read somewhere” that RPG Maker games can’t use Steam Cloud.

Well, if that’s true, then I’ve figured out a thing that Omori devs didn't, so there’s a feather in my cap for sure.

The Solution​

After hours of fighting with Auto-Cloud, I decided to see if I could make a manual implementation work and was eventually successful. I discovered that you can manually sync files to the Steam Cloud using Greenworks. I had already spent a weekend some months ago pulling my hair out in order to get Greenworks properly integrated into my game project so that I could support Steam achievements. My description of the solution will assume that Greenworks is integrated already.
  1. Upload save files to the cloud whenever StorageManager.fsWriteFile is called. This is the function that is invoked whenever any file in the save directory is written to. You want to wire up greenworks.saveFilesToCloud to this function so that whenever your game changes a save file, that file gets synced to the cloud.
  2. Update Scene_Boot to include an additional pre-load step. This is a manual implementation, and so we will need to check the Steam Cloud at application start up and see if we need to download any files. I hooked into Scene_Boot.prototype.isReady and added a check to ensure that all my synced files had completed their download callbacks before proceeding.
  3. Update Scene_Boot to walk the list of cloud files and update local files with anything missing from the list. For this you need two Greenworks functions: greenworks.getFileCount and greenworks.getFileNameAndSize. Use the number you get from the file count to iterate over getFileNameAndSize. Wire this into the previous step, so that Scene_Boot doesn’t proceed until everything is in sync.
  4. Turn off save file compression. Unfortunately, Greenworks doesn’t support reading and writing binary data, only text. RPG Maker MZ compresses save files with gzip by default, which is a binary format. Saving and writing the files as text will make them incompatible with the editor. And so, in order to make this work, that feature had to go. The simplest way to achieve it is to overwrite StorageManager.jsonToZip and StorageManager.zipToJson so that they are merely pass-throughs (e.g. they just receive the text data and return it without compressing it). This increased save file size about 9x in my project, but save files are relatively small. Even for a mid-game save, I’m still only in the 500k range. This does also make the save files trivial to hack, but we’re largely not making multiplayer or tournament-type games here, so this seems like an acceptable solution. It certainly is for my project.
  5. Finally, use greenworks.readTextFromFile to get the contents of any files that are missing and write the file (or files) using RPG Maker's StorageManager.saveToLocalFile. There's a bit of callback hell that has to be endured here, because they are asynchronous calls, but the end result works.
After implementing my solution, I was able to create saves on computer #1 (Windows), then open up my game on computer #2 (Linux) and see the save files appear when starting the game. Making changes to an existing save on one computer also correctly propogates to the other, even though there’s no code explicitly handling this. It seems that once a file is marked by the code, the Steam client will take care of updates to it, presumably so long as every write action by the game application is being written to the Steam Cloud.

Closing Thoughts​

I thought that turning on Steam Auto-Cloud would be a really simple process, but it wasn’t. Building a manual Steam Cloud solution ended up taking a whole weekend. I hope this post helps someone else save some time.

I’d be curious to know: a) if anyone knows why Steam Auto-Cloud fails to sync RPG Maker save files; and b) if anyone has come up with any solution before, either similar to mine or different from it.

If anyone wants more detail on the code that does this, I’d be happy to oblige. I think some might also be interested in a write up of all the work it took to get Greenworks integrated. That’s a separate topic, but I’m happy to start another thread if there’s interest.
I don't think I've seen anyone else post how they sync with Steam Cloud, but the way I did it for my games was to choose a root path that Steam listed as a commonly-used location for save games in the Steamworks documentation, and then modify a plugin by taarna23 to save in a folder in that root path. I don't need Greenworks for this to work.

For my Steam games, I don't save to the game's folder because I packaged the game, and also because I've heard that when you update your game, sometimes the save files in the game's folder became unusable (or disappear) - I don't know if this happens for sure, because I never tried it.
When you hit "deployment" in RPG Maker, the final build does not include a save folder. The engine creates it if it doesn't exist when the game starts up. So... I have no idea how updating your game could cause that folder to get erased, unless Steam is doing something unexpected on their side. I've deployed many times to my demo and playtest app, and I have never seen anything like that, but I'll keep an eye out.

Good to know that there's another solution. I, personally, don't like when applications spew their data around my filesystem, so I think I'll keep this one. I prefer for application data to remain compartmentalized. But if save files were to ever disappear, that would be a good reason to change.

Original thread: https://forums.rpgmakerweb.com/threads/trouble-syncing-save-files-with-steam-auto-cloud-and-a-manual-greenworks-solution.159883/