neos-modding-group / NeosModLoader

A mod loader for Neos VR
GNU Lesser General Public License v3.0
61 stars 22 forks source link

Improve safety of mod configuration save #97

Open zkxs opened 1 year ago

zkxs commented 1 year ago

Currently, NML nukes the existing mod configuration file as it writes a new one. If something goes wrong, this can result in an empty or partially written file. Relevant code here.

If this fails it's a pretty bad experience for the user, as NML will have all sorts of errors trying to load the broken configs, so not only has the user lost their configuration, they've also got to go manually delete the broken files.

This configuration save process could be made much more robust by writing to an intermediate file and then only on success copying that over the real config file. For example, using a temporary MyMod.tmp.json file:

  1. Delete MyMod.tmp.json if it exists
  2. Write new config to MyMod.tmp.json
  3. Optionally, verify that MyMod.tmp.json is contains valid JSON
  4. Delete MyMod.json
  5. Move MyMod.tmp.json to MyMod.json

There may be other ways of doing this—I've just suggested one possibility. For example, if C# has an API somewhere that can atomically replace an existing file (like a unix-style mv) that'd be even better.