From 3f36248120459f35e686e5f3e88b7c4ef4f300ce Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 27 Aug 2017 18:38:45 +1000 Subject: [PATCH] Bots file load/save is thread safe. --- MCGalaxy/Bots/BotsFile.cs | 8 +++++--- MCGalaxy/Levels/Level.Fields.cs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/MCGalaxy/Bots/BotsFile.cs b/MCGalaxy/Bots/BotsFile.cs index d8e1e6203..a5f4438e7 100644 --- a/MCGalaxy/Bots/BotsFile.cs +++ b/MCGalaxy/Bots/BotsFile.cs @@ -29,7 +29,8 @@ namespace MCGalaxy.Bots { return "extra/bots/" + mapName + ".json"; } - public static void Load(Level lvl) { + public static void Load(Level lvl) { lock (lvl.botsIOLock) { LoadCore(lvl); } } + static void LoadCore(Level lvl) { string path = BotsPath(lvl.MapName); if (!File.Exists(path)) return; string json = File.ReadAllText(path); @@ -49,7 +50,8 @@ namespace MCGalaxy.Bots { } } - public static void Save(Level lvl) { + public static void Save(Level lvl) { lock (lvl.botsIOLock) { SaveCore(lvl); } } + static void SaveCore(Level lvl) { PlayerBot[] bots = lvl.Bots.Items; string path = BotsPath(lvl.MapName); if (!File.Exists(path) && bots.Length == 0) return; @@ -67,7 +69,7 @@ namespace MCGalaxy.Bots { } catch (Exception ex) { Logger.Log(LogType.Warning, "Failed to save bots file"); Logger.LogError(ex); - } + } } static void LoadAi(BotProperties props, PlayerBot bot) { diff --git a/MCGalaxy/Levels/Level.Fields.cs b/MCGalaxy/Levels/Level.Fields.cs index d472b48b7..829e4aab8 100644 --- a/MCGalaxy/Levels/Level.Fields.cs +++ b/MCGalaxy/Levels/Level.Fields.cs @@ -74,7 +74,7 @@ namespace MCGalaxy { /// true if both worldChat and Server.worldChat are true. public bool SeesServerWideChat { get { return Config.ServerWideChat && ServerConfig.ServerWideChat; } } - internal readonly object queueLock = new object(), saveLock = new object(), savePropsLock = new object(); + internal readonly object queueLock = new object(), saveLock = new object(), savePropsLock = new object(), botsIOLock = new object(); public List blockqueue = new List(); BufferedBlockSender bulkSender;