NeosModLoader
NeosModLoader copied to clipboard
Improve safety of mod configuration save
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:
- Delete
MyMod.tmp.jsonif it exists - Write new config to
MyMod.tmp.json - Optionally, verify that
MyMod.tmp.jsonis contains valid JSON - Delete
MyMod.json - Move
MyMod.tmp.jsontoMyMod.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.