From 0d3a6d31c1a63f0603aebf5bc5d16fa5b9481f5b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 26 Jun 2016 12:52:04 +1000 Subject: [PATCH] Make more file saving methods threadsafe. --- Blocks/Block.Permissions.cs | 39 +++++++++++++++++++++-------------- Commands/CommandOtherPerms.cs | 6 ++++++ Games/Team.List.cs | 6 +++--- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/Blocks/Block.Permissions.cs b/Blocks/Block.Permissions.cs index 7440599b3..aa6bb2aa5 100644 --- a/Blocks/Block.Permissions.cs +++ b/Blocks/Block.Permissions.cs @@ -329,28 +329,35 @@ namespace MCGalaxy } } + static readonly object saveLock = new object(); public static void SaveBlocks(IEnumerable givenList) { try { - using (StreamWriter w = new StreamWriter("properties/block.properties")) { - w.WriteLine("#Version 2"); - w.WriteLine("# This file dictates what levels may use what blocks"); - w.WriteLine("# If someone has royally screwed up the ranks, just delete this file and let the server restart"); - w.WriteLine("# Allowed ranks: " + Group.concatList(false, false, true)); - w.WriteLine("# Disallow and allow can be left empty, just make sure there's 2 spaces between the colons"); - w.WriteLine("# This works entirely on permission values, not names. Do not enter a rank name. Use its permission value"); - w.WriteLine("# BlockName : LowestRank : Disallow : Allow"); - w.WriteLine("# lava : 60 : 80,67 : 40,41,55"); - w.WriteLine(""); + lock (saveLock) + SaveBlocksCore(givenList); + } catch (Exception e) { + Server.ErrorLog(e); + } + } + + static void SaveBlocksCore(IEnumerable givenList) { + using (StreamWriter w = new StreamWriter("properties/block.properties")) { + w.WriteLine("#Version 2"); + w.WriteLine("# This file dictates what levels may use what blocks"); + w.WriteLine("# If someone has royally screwed up the ranks, just delete this file and let the server restart"); + w.WriteLine("# Allowed ranks: " + Group.concatList(false, false, true)); + w.WriteLine("# Disallow and allow can be left empty, just make sure there's 2 spaces between the colons"); + w.WriteLine("# This works entirely on permission values, not names. Do not enter a rank name. Use its permission value"); + w.WriteLine("# BlockName : LowestRank : Disallow : Allow"); + w.WriteLine("# lava : 60 : 80,67 : 40,41,55"); + w.WriteLine(""); - foreach (Blocks bs in givenList) { - if (bs.IncludeInBlockProperties()) { - string line = Block.Name(bs.type) + " : " + (int)bs.lowestRank + " : " + GrpCommands.getInts(bs.disallow) + " : " + GrpCommands.getInts(bs.allow); - w.WriteLine(line); - } + foreach (Blocks bs in givenList) { + if (bs.IncludeInBlockProperties()) { + string line = Block.Name(bs.type) + " : " + (int)bs.lowestRank + " : " + GrpCommands.getInts(bs.disallow) + " : " + GrpCommands.getInts(bs.allow); + w.WriteLine(line); } } } - catch (Exception e) { Server.ErrorLog(e); } } public static bool canPlace(Player p, byte type) { diff --git a/Commands/CommandOtherPerms.cs b/Commands/CommandOtherPerms.cs index e9224562c..e62110cab 100644 --- a/Commands/CommandOtherPerms.cs +++ b/Commands/CommandOtherPerms.cs @@ -70,7 +70,13 @@ namespace MCGalaxy { } } + static readonly object saveLock = new object(); public static void Save() { + lock (saveLock) + SaveCore(); + } + + static void SaveCore() { using (StreamWriter w = new StreamWriter("properties/ExtraCommandPermissions.properties")) { w.WriteLine("# This file is used for setting up additional permissions that are needed in commands!!"); w.WriteLine("#"); diff --git a/Games/Team.List.cs b/Games/Team.List.cs index 450b842d2..89526ad7f 100644 --- a/Games/Team.List.cs +++ b/Games/Team.List.cs @@ -24,7 +24,7 @@ namespace MCGalaxy.Games { public sealed partial class Team { public static Dictionary TeamsList = new Dictionary(); - static readonly object readLock = new object(); + static readonly object ioLock = new object(); public static Team FindTeam(Player p) { foreach (var team in TeamsList) { @@ -46,7 +46,7 @@ namespace MCGalaxy.Games { } public static void SaveList() { - lock (readLock) + lock (ioLock) using (CP437Writer w = new CP437Writer("extra/teams.txt")) foreach (var pair in TeamsList) { @@ -62,7 +62,7 @@ namespace MCGalaxy.Games { public static void LoadList() { if (!File.Exists("extra/teams.txt")) return; Team team = new Team(); - lock (readLock) + lock (ioLock) PropertiesFile.Read("extra/teams.txt", ref team, LineProcessor, '='); if (team.Name != null) TeamsList[team.Name] = team; }