Skip to content

Commit

Permalink
When saving bots, write to temp file first instead of writing directl…
Browse files Browse the repository at this point in the history
…y to real file
  • Loading branch information
UnknownShadow200 committed Nov 9, 2024
1 parent fb46d6a commit dcde2cc
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions MCGalaxy/Bots/BotsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,19 @@ static void SaveCore(Level lvl) {
if (!File.Exists(path) && bots.Length == 0) return;

List<BotProperties> props = new List<BotProperties>(bots.Length);
for (int i = 0; i < bots.Length; i++) {
for (int i = 0; i < bots.Length; i++)
{
BotProperties data = new BotProperties();
data.FromBot(bots[i]);
props.Add(data);
}

string tmpPath = path + ".tmp";
try {
using (StreamWriter w = new StreamWriter(path)) { WriteAll(w, props); }
using (StreamWriter w = new StreamWriter(tmpPath)) { WriteAll(w, props); }

AtomicIO.TryDelete(path);
File.Move(tmpPath, path);
} catch (Exception ex) {
Logger.LogError("Error saving bots to " + path, ex);
}
Expand Down

0 comments on commit dcde2cc

Please sign in to comment.