From ec10970dbd9828529c26f30ad31ff96d911a7c22 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 7 Mar 2025 21:42:11 +1100 Subject: [PATCH] Ensure more files are written in a safer way to reduce chance of data corruption in case of disk full --- MCGalaxy/Blocks/BlockDefinitions.cs | 2 +- MCGalaxy/Blocks/BlockProperties.cs | 3 ++- MCGalaxy/Chat/Colors.cs | 3 ++- MCGalaxy/Commands/Alias.cs | 3 ++- MCGalaxy/Config/ConfigElement.cs | 3 ++- MCGalaxy/Config/Permissions/BlockPerms.cs | 3 ++- MCGalaxy/Config/Permissions/CommandExtraPerms.cs | 2 +- MCGalaxy/Config/Permissions/CommandPerms.cs | 2 +- MCGalaxy/Drawing/Image/ImagePalette.cs | 7 ++++--- MCGalaxy/Economy/Economy.cs | 3 ++- MCGalaxy/Games/RoundsGame/RoundsGameConfig.cs | 3 ++- MCGalaxy/Games/Team.cs | 2 +- MCGalaxy/Levels/LevelConfig.cs | 3 ++- MCGalaxy/Modules/Awards/AwardsList.cs | 5 +++-- MCGalaxy/Modules/Awards/PlayerAwards.cs | 2 +- MCGalaxy/Modules/Games/ZombieSurvival/ZSConfig.cs | 3 ++- MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs | 3 ++- MCGalaxy/Modules/Warps/Warp.cs | 3 ++- MCGalaxy/Network/CPESupport.cs | 3 ++- MCGalaxy/Player/Group.cs | 3 ++- MCGalaxy/Server/Authentication/AuthService.cs | 3 ++- 21 files changed, 40 insertions(+), 24 deletions(-) diff --git a/MCGalaxy/Blocks/BlockDefinitions.cs b/MCGalaxy/Blocks/BlockDefinitions.cs index f99aebb28..0adafc25a 100644 --- a/MCGalaxy/Blocks/BlockDefinitions.cs +++ b/MCGalaxy/Blocks/BlockDefinitions.cs @@ -164,7 +164,7 @@ namespace MCGalaxy { static void SaveCore(bool global, BlockDefinition[] defs, string path) { string separator = null; - using (StreamWriter w = new StreamWriter(path)) { + using (StreamWriter w = FileIO.CreateGuarded(path)) { w.WriteLine("["); var ser = new JsonConfigWriter(w, elems); diff --git a/MCGalaxy/Blocks/BlockProperties.cs b/MCGalaxy/Blocks/BlockProperties.cs index 55bf500a6..0148a853e 100644 --- a/MCGalaxy/Blocks/BlockProperties.cs +++ b/MCGalaxy/Blocks/BlockProperties.cs @@ -101,7 +101,8 @@ namespace MCGalaxy.Blocks { } static void SaveCore(string group, BlockProps[] list, byte scope) { - using (StreamWriter w = new StreamWriter("blockprops/" + group + ".txt")) { + using (StreamWriter w = FileIO.CreateGuarded("blockprops/" + group + ".txt")) + { w.WriteLine("# This represents the physics properties for blocks, in the format of:"); w.WriteLine("# id : Is rails : Is tdoor : Is door : Is message block : Is portal : " + "Killed by water : Killed by lava : Kills players : death message : " + diff --git a/MCGalaxy/Chat/Colors.cs b/MCGalaxy/Chat/Colors.cs index dcd952051..2b4aa0032 100644 --- a/MCGalaxy/Chat/Colors.cs +++ b/MCGalaxy/Chat/Colors.cs @@ -255,7 +255,8 @@ namespace MCGalaxy { } static void SaveCore() { - using (StreamWriter w = new StreamWriter(Paths.CustomColorsFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.CustomColorsFile)) + { foreach (ColorDesc col in List) { if (!col.IsModified()) continue; diff --git a/MCGalaxy/Commands/Alias.cs b/MCGalaxy/Commands/Alias.cs index f21f278c0..80cdb97f2 100644 --- a/MCGalaxy/Commands/Alias.cs +++ b/MCGalaxy/Commands/Alias.cs @@ -56,7 +56,8 @@ namespace MCGalaxy.Commands } public static void SaveCustom() { - using (StreamWriter sw = new StreamWriter(Paths.AliasesFile)) { + using (StreamWriter sw = FileIO.CreateGuarded(Paths.AliasesFile)) + { sw.WriteLine("# Aliases can be in one of three formats:"); sw.WriteLine("# trigger : command"); sw.WriteLine("# e.g. \"xyz : help\" means /xyz is treated as /help "); diff --git a/MCGalaxy/Config/ConfigElement.cs b/MCGalaxy/Config/ConfigElement.cs index d5ed76ec5..d664a5330 100644 --- a/MCGalaxy/Config/ConfigElement.cs +++ b/MCGalaxy/Config/ConfigElement.cs @@ -94,7 +94,8 @@ namespace MCGalaxy } public static void SerialiseSimple(ConfigElement[] elements, string path, object instance) { - using (StreamWriter w = new StreamWriter(path)) { + using (StreamWriter w = FileIO.CreateGuarded(path)) + { w.WriteLine("#Settings file"); SerialiseElements(elements, w, instance); } diff --git a/MCGalaxy/Config/Permissions/BlockPerms.cs b/MCGalaxy/Config/Permissions/BlockPerms.cs index d98a13fc2..7b9f0f0f9 100644 --- a/MCGalaxy/Config/Permissions/BlockPerms.cs +++ b/MCGalaxy/Config/Permissions/BlockPerms.cs @@ -73,7 +73,8 @@ namespace MCGalaxy.Blocks } static void SaveList(string path, BlockPerms[] list, string action) { - using (StreamWriter w = new StreamWriter(path)) { + using (StreamWriter w = FileIO.CreateGuarded(path)) + { WriteHeader(w, "block", "each block", "Block ID", "lava", action); foreach (BlockPerms perms in list) diff --git a/MCGalaxy/Config/Permissions/CommandExtraPerms.cs b/MCGalaxy/Config/Permissions/CommandExtraPerms.cs index 99f8ad064..661fe6af6 100644 --- a/MCGalaxy/Config/Permissions/CommandExtraPerms.cs +++ b/MCGalaxy/Config/Permissions/CommandExtraPerms.cs @@ -86,7 +86,7 @@ namespace MCGalaxy.Commands } static void SaveCore() { - using (StreamWriter w = new StreamWriter(Paths.CmdExtraPermsFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.CmdExtraPermsFile)) { WriteHeader(w, "extra command permissions", "extra permissions in some commands", "CommandName:ExtraPermissionNumber", "countdown:1", "use"); diff --git a/MCGalaxy/Config/Permissions/CommandPerms.cs b/MCGalaxy/Config/Permissions/CommandPerms.cs index f45aa98e9..26e156212 100644 --- a/MCGalaxy/Config/Permissions/CommandPerms.cs +++ b/MCGalaxy/Config/Permissions/CommandPerms.cs @@ -76,7 +76,7 @@ namespace MCGalaxy.Commands } static void SaveCore() { - using (StreamWriter w = new StreamWriter(Paths.CmdPermsFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.CmdPermsFile)) { WriteHeader(w, "command", "each command", "CommandName", "gun", "use"); foreach (CommandPerms perms in List) diff --git a/MCGalaxy/Drawing/Image/ImagePalette.cs b/MCGalaxy/Drawing/Image/ImagePalette.cs index e2f78b5d7..27a83fb8c 100644 --- a/MCGalaxy/Drawing/Image/ImagePalette.cs +++ b/MCGalaxy/Drawing/Image/ImagePalette.cs @@ -90,10 +90,11 @@ namespace MCGalaxy.Drawing public void Save() { - using (StreamWriter w = new StreamWriter(FileName)) { - w.WriteLine("#Line layout - block:red:green:blue"); - + using (StreamWriter w = FileIO.CreateGuarded(FileName)) + { + w.WriteLine("#Line layout - block:red:green:blue"); if (Entries == null) return; + foreach (PaletteEntry e in Entries) w.WriteLine(e.Block + ":" + e.R + ":" + e.G + ":" + e.B); } diff --git a/MCGalaxy/Economy/Economy.cs b/MCGalaxy/Economy/Economy.cs index 4519daf1b..412adb784 100644 --- a/MCGalaxy/Economy/Economy.cs +++ b/MCGalaxy/Economy/Economy.cs @@ -90,7 +90,8 @@ namespace MCGalaxy.Eco } static void SaveCore() { - using (StreamWriter w = new StreamWriter(Paths.EconomyPropsFile, false)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.EconomyPropsFile)) + { w.WriteLine("enabled:" + Enabled); foreach (Item item in Items) diff --git a/MCGalaxy/Games/RoundsGame/RoundsGameConfig.cs b/MCGalaxy/Games/RoundsGame/RoundsGameConfig.cs index 885fa1b83..24000beec 100644 --- a/MCGalaxy/Games/RoundsGame/RoundsGameConfig.cs +++ b/MCGalaxy/Games/RoundsGame/RoundsGameConfig.cs @@ -72,7 +72,8 @@ namespace MCGalaxy.Games public virtual void Save() { if (cfg == null) cfg = ConfigElement.GetAll(GetType()); - using (StreamWriter w = new StreamWriter(Path)) { + using (StreamWriter w = FileIO.CreateGuarded(Path)) + { w.WriteLine("#" + GameName + " configuration"); ConfigElement.Serialise(cfg, w, this); } diff --git a/MCGalaxy/Games/Team.cs b/MCGalaxy/Games/Team.cs index 44e1f7b11..6090772a1 100644 --- a/MCGalaxy/Games/Team.cs +++ b/MCGalaxy/Games/Team.cs @@ -93,7 +93,7 @@ namespace MCGalaxy.Games { public static void SaveList() { lock (ioLock) - using (StreamWriter w = new StreamWriter("extra/teams.txt")) + using (StreamWriter w = FileIO.CreateGuarded("extra/teams.txt")) foreach (Team team in Teams) { w.WriteLine("Name=" + team.Name); diff --git a/MCGalaxy/Levels/LevelConfig.cs b/MCGalaxy/Levels/LevelConfig.cs index 92c9be0d9..6bae14ce2 100644 --- a/MCGalaxy/Levels/LevelConfig.cs +++ b/MCGalaxy/Levels/LevelConfig.cs @@ -359,7 +359,8 @@ namespace MCGalaxy public void Save(string path, string map) { try { lock (saveLock) { - using (StreamWriter w = new StreamWriter(path)) { + using (StreamWriter w = FileIO.CreateGuarded(path)) + { w.WriteLine("#Level properties for " + map); w.WriteLine("#Drown-time is in tenths of a second"); ConfigElement.Serialise(Server.levelConfig, w, this); diff --git a/MCGalaxy/Modules/Awards/AwardsList.cs b/MCGalaxy/Modules/Awards/AwardsList.cs index 378b622b4..ea0dfb75d 100644 --- a/MCGalaxy/Modules/Awards/AwardsList.cs +++ b/MCGalaxy/Modules/Awards/AwardsList.cs @@ -70,7 +70,7 @@ namespace MCGalaxy.Modules.Awards static readonly object saveLock = new object(); public static void Save() { lock (saveLock) - using (StreamWriter w = new StreamWriter("text/awardsList.txt")) + using (StreamWriter w = FileIO.CreateGuarded("text/awardsList.txt")) { WriteHeader(w); foreach (Award a in Awards) { @@ -81,7 +81,8 @@ namespace MCGalaxy.Modules.Awards public static void Load() { if (!File.Exists("text/awardsList.txt")) { - using (StreamWriter w = new StreamWriter("text/awardsList.txt")) { + using (StreamWriter w = FileIO.CreateGuarded("text/awardsList.txt")) + { WriteHeader(w); w.WriteLine("Gotta start somewhere : Built your first house"); w.WriteLine("Climbing the ladder : Earned a rank advancement"); diff --git a/MCGalaxy/Modules/Awards/PlayerAwards.cs b/MCGalaxy/Modules/Awards/PlayerAwards.cs index 6ae425a2c..d3ca2ee1f 100644 --- a/MCGalaxy/Modules/Awards/PlayerAwards.cs +++ b/MCGalaxy/Modules/Awards/PlayerAwards.cs @@ -79,7 +79,7 @@ namespace MCGalaxy.Modules.Awards static readonly object saveLock = new object(); public static void Save() { lock (saveLock) - using (StreamWriter w = new StreamWriter("text/playerAwards.txt")) + using (StreamWriter w = FileIO.CreateGuarded("text/playerAwards.txt")) { foreach (PlayerAward a in Awards) w.WriteLine(a.Player + " : " + a.Awards.Join(",")); diff --git a/MCGalaxy/Modules/Games/ZombieSurvival/ZSConfig.cs b/MCGalaxy/Modules/Games/ZombieSurvival/ZSConfig.cs index e2b5faf29..f07e96d2a 100644 --- a/MCGalaxy/Modules/Games/ZombieSurvival/ZSConfig.cs +++ b/MCGalaxy/Modules/Games/ZombieSurvival/ZSConfig.cs @@ -100,7 +100,8 @@ namespace MCGalaxy.Modules.Games.ZS public override void Save() { if (cfg == null) cfg = ConfigElement.GetAll(typeof(ZSConfig)); - using (StreamWriter w = new StreamWriter(Path)) { + using (StreamWriter w = FileIO.CreateGuarded(Path)) + { w.WriteLine("# no-pillaring-during-zombie = Disables pillaring while Zombie Survival is activated."); w.WriteLine("# zombie-name-while-infected = Sets the zombies name while actived if there is a value."); w.WriteLine(); diff --git a/MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs b/MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs index 83c1a7421..c2c318d0c 100644 --- a/MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs +++ b/MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs @@ -76,7 +76,8 @@ namespace MCGalaxy.Modules.Relay.Discord public void Save() { if (cfg == null) cfg = ConfigElement.GetAll(typeof(DiscordConfig)); - using (StreamWriter w = new StreamWriter(PROPS_PATH)) { + using (StreamWriter w = FileIO.CreateGuarded(PROPS_PATH)) + { w.WriteLine("# Discord relay bot configuration"); w.WriteLine("# See " + Updater.SourceURL + "/wiki/Discord-relay-bot/"); w.WriteLine(); diff --git a/MCGalaxy/Modules/Warps/Warp.cs b/MCGalaxy/Modules/Warps/Warp.cs index 55e8f77c8..6c30d6c01 100644 --- a/MCGalaxy/Modules/Warps/Warp.cs +++ b/MCGalaxy/Modules/Warps/Warp.cs @@ -125,7 +125,8 @@ namespace MCGalaxy.Modules.Warps } public void Save() { - using (StreamWriter w = new StreamWriter(Filename)) { + using (StreamWriter w = FileIO.CreateGuarded(Filename)) + { foreach (Warp warp in Items) { w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" + diff --git a/MCGalaxy/Network/CPESupport.cs b/MCGalaxy/Network/CPESupport.cs index 062fc56ce..6ee1f6c91 100644 --- a/MCGalaxy/Network/CPESupport.cs +++ b/MCGalaxy/Network/CPESupport.cs @@ -208,7 +208,8 @@ namespace MCGalaxy } static void SaveDisabledList() { - using (StreamWriter w = new StreamWriter(Paths.CPEDisabledFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.CPEDisabledFile)) + { w.WriteLine("# CPE configuration"); w.WriteLine("# This file allows disabling non-classic (CPE) extensions "); w.WriteLine("# To disable an extension, just change '= True' to '= False'"); diff --git a/MCGalaxy/Player/Group.cs b/MCGalaxy/Player/Group.cs index 1d29f8c85..58b2cba61 100644 --- a/MCGalaxy/Player/Group.cs +++ b/MCGalaxy/Player/Group.cs @@ -326,7 +326,8 @@ namespace MCGalaxy static void SaveGroups(List givenList) { if (cfg == null) cfg = ConfigElement.GetAll(typeof(Group)); - using (StreamWriter w = new StreamWriter(Paths.RankPropsFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.RankPropsFile)) + { w.WriteLine("#Version 3"); w.WriteLine("#RankName = string"); w.WriteLine("#\tThe name of the rank (e.g. Guest)"); diff --git a/MCGalaxy/Server/Authentication/AuthService.cs b/MCGalaxy/Server/Authentication/AuthService.cs index b5db62807..eeb608942 100644 --- a/MCGalaxy/Server/Authentication/AuthService.cs +++ b/MCGalaxy/Server/Authentication/AuthService.cs @@ -93,7 +93,8 @@ namespace MCGalaxy.Authentication } static void SaveServices() { - using (StreamWriter w = new StreamWriter(Paths.AuthServicesFile)) { + using (StreamWriter w = FileIO.CreateGuarded(Paths.AuthServicesFile)) + { w.WriteLine("# Authentication services configuration"); w.WriteLine("# There is no reason to modify these configuration settings, unless the server has been configured"); w.WriteLine("# to send heartbeats to multiple authentication services (e.g. both ClassiCube.net and BetaCraft.uk)");