Make more file saving methods threadsafe.

This commit is contained in:
UnknownShadow200 2016-06-26 12:52:04 +10:00
parent cb9df3e60d
commit 0d3a6d31c1
3 changed files with 32 additions and 19 deletions

View File

@ -329,28 +329,35 @@ namespace MCGalaxy
} }
} }
static readonly object saveLock = new object();
public static void SaveBlocks(IEnumerable<Blocks> givenList) { public static void SaveBlocks(IEnumerable<Blocks> givenList) {
try { try {
using (StreamWriter w = new StreamWriter("properties/block.properties")) { lock (saveLock)
w.WriteLine("#Version 2"); SaveBlocksCore(givenList);
w.WriteLine("# This file dictates what levels may use what blocks"); } catch (Exception e) {
w.WriteLine("# If someone has royally screwed up the ranks, just delete this file and let the server restart"); Server.ErrorLog(e);
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"); static void SaveBlocksCore(IEnumerable<Blocks> givenList) {
w.WriteLine("# lava : 60 : 80,67 : 40,41,55"); using (StreamWriter w = new StreamWriter("properties/block.properties")) {
w.WriteLine(""); 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) { foreach (Blocks bs in givenList) {
if (bs.IncludeInBlockProperties()) { if (bs.IncludeInBlockProperties()) {
string line = Block.Name(bs.type) + " : " + (int)bs.lowestRank + " : " + GrpCommands.getInts(bs.disallow) + " : " + GrpCommands.getInts(bs.allow); string line = Block.Name(bs.type) + " : " + (int)bs.lowestRank + " : " + GrpCommands.getInts(bs.disallow) + " : " + GrpCommands.getInts(bs.allow);
w.WriteLine(line); w.WriteLine(line);
}
} }
} }
} }
catch (Exception e) { Server.ErrorLog(e); }
} }
public static bool canPlace(Player p, byte type) { public static bool canPlace(Player p, byte type) {

View File

@ -70,7 +70,13 @@ namespace MCGalaxy {
} }
} }
static readonly object saveLock = new object();
public static void Save() { public static void Save() {
lock (saveLock)
SaveCore();
}
static void SaveCore() {
using (StreamWriter w = new StreamWriter("properties/ExtraCommandPermissions.properties")) { 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("# This file is used for setting up additional permissions that are needed in commands!!");
w.WriteLine("#"); w.WriteLine("#");

View File

@ -24,7 +24,7 @@ namespace MCGalaxy.Games {
public sealed partial class Team { public sealed partial class Team {
public static Dictionary<string, Team> TeamsList = new Dictionary<string, Team>(); public static Dictionary<string, Team> TeamsList = new Dictionary<string, Team>();
static readonly object readLock = new object(); static readonly object ioLock = new object();
public static Team FindTeam(Player p) { public static Team FindTeam(Player p) {
foreach (var team in TeamsList) { foreach (var team in TeamsList) {
@ -46,7 +46,7 @@ namespace MCGalaxy.Games {
} }
public static void SaveList() { public static void SaveList() {
lock (readLock) lock (ioLock)
using (CP437Writer w = new CP437Writer("extra/teams.txt")) using (CP437Writer w = new CP437Writer("extra/teams.txt"))
foreach (var pair in TeamsList) foreach (var pair in TeamsList)
{ {
@ -62,7 +62,7 @@ namespace MCGalaxy.Games {
public static void LoadList() { public static void LoadList() {
if (!File.Exists("extra/teams.txt")) return; if (!File.Exists("extra/teams.txt")) return;
Team team = new Team(); Team team = new Team();
lock (readLock) lock (ioLock)
PropertiesFile.Read("extra/teams.txt", ref team, LineProcessor, '='); PropertiesFile.Read("extra/teams.txt", ref team, LineProcessor, '=');
if (team.Name != null) TeamsList[team.Name] = team; if (team.Name != null) TeamsList[team.Name] = team;
} }