Ensure more files are written in a safer way to reduce chance of data corruption in case of disk full

This commit is contained in:
UnknownShadow200 2025-03-07 21:42:11 +11:00
parent 0adf900232
commit ec10970dbd
21 changed files with 40 additions and 24 deletions

View File

@ -164,7 +164,7 @@ namespace MCGalaxy {
static void SaveCore(bool global, BlockDefinition[] defs, string path) { static void SaveCore(bool global, BlockDefinition[] defs, string path) {
string separator = null; string separator = null;
using (StreamWriter w = new StreamWriter(path)) { using (StreamWriter w = FileIO.CreateGuarded(path)) {
w.WriteLine("["); w.WriteLine("[");
var ser = new JsonConfigWriter(w, elems); var ser = new JsonConfigWriter(w, elems);

View File

@ -101,7 +101,8 @@ namespace MCGalaxy.Blocks {
} }
static void SaveCore(string group, BlockProps[] list, byte scope) { 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("# 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 : " + w.WriteLine("# id : Is rails : Is tdoor : Is door : Is message block : Is portal : " +
"Killed by water : Killed by lava : Kills players : death message : " + "Killed by water : Killed by lava : Kills players : death message : " +

View File

@ -255,7 +255,8 @@ namespace MCGalaxy {
} }
static void SaveCore() { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.CustomColorsFile)) { using (StreamWriter w = FileIO.CreateGuarded(Paths.CustomColorsFile))
{
foreach (ColorDesc col in List) { foreach (ColorDesc col in List) {
if (!col.IsModified()) continue; if (!col.IsModified()) continue;

View File

@ -56,7 +56,8 @@ namespace MCGalaxy.Commands
} }
public static void SaveCustom() { 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("# Aliases can be in one of three formats:");
sw.WriteLine("# trigger : command"); sw.WriteLine("# trigger : command");
sw.WriteLine("# e.g. \"xyz : help\" means /xyz is treated as /help <args given by user>"); sw.WriteLine("# e.g. \"xyz : help\" means /xyz is treated as /help <args given by user>");

View File

@ -94,7 +94,8 @@ namespace MCGalaxy
} }
public static void SerialiseSimple(ConfigElement[] elements, string path, object instance) { 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"); w.WriteLine("#Settings file");
SerialiseElements(elements, w, instance); SerialiseElements(elements, w, instance);
} }

View File

@ -73,7 +73,8 @@ namespace MCGalaxy.Blocks
} }
static void SaveList(string path, BlockPerms[] list, string action) { 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); WriteHeader(w, "block", "each block", "Block ID", "lava", action);
foreach (BlockPerms perms in list) foreach (BlockPerms perms in list)

View File

@ -86,7 +86,7 @@ namespace MCGalaxy.Commands
} }
static void SaveCore() { 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", WriteHeader(w, "extra command permissions", "extra permissions in some commands",
"CommandName:ExtraPermissionNumber", "countdown:1", "use"); "CommandName:ExtraPermissionNumber", "countdown:1", "use");

View File

@ -76,7 +76,7 @@ namespace MCGalaxy.Commands
} }
static void SaveCore() { 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"); WriteHeader(w, "command", "each command", "CommandName", "gun", "use");
foreach (CommandPerms perms in List) foreach (CommandPerms perms in List)

View File

@ -90,10 +90,11 @@ namespace MCGalaxy.Drawing
public void Save() { public void Save() {
using (StreamWriter w = new StreamWriter(FileName)) { using (StreamWriter w = FileIO.CreateGuarded(FileName))
{
w.WriteLine("#Line layout - block:red:green:blue"); w.WriteLine("#Line layout - block:red:green:blue");
if (Entries == null) return; if (Entries == null) return;
foreach (PaletteEntry e in Entries) foreach (PaletteEntry e in Entries)
w.WriteLine(e.Block + ":" + e.R + ":" + e.G + ":" + e.B); w.WriteLine(e.Block + ":" + e.R + ":" + e.G + ":" + e.B);
} }

View File

@ -90,7 +90,8 @@ namespace MCGalaxy.Eco
} }
static void SaveCore() { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.EconomyPropsFile, false)) { using (StreamWriter w = FileIO.CreateGuarded(Paths.EconomyPropsFile))
{
w.WriteLine("enabled:" + Enabled); w.WriteLine("enabled:" + Enabled);
foreach (Item item in Items) foreach (Item item in Items)

View File

@ -72,7 +72,8 @@ namespace MCGalaxy.Games
public virtual void Save() { public virtual void Save() {
if (cfg == null) cfg = ConfigElement.GetAll(GetType()); if (cfg == null) cfg = ConfigElement.GetAll(GetType());
using (StreamWriter w = new StreamWriter(Path)) { using (StreamWriter w = FileIO.CreateGuarded(Path))
{
w.WriteLine("#" + GameName + " configuration"); w.WriteLine("#" + GameName + " configuration");
ConfigElement.Serialise(cfg, w, this); ConfigElement.Serialise(cfg, w, this);
} }

View File

@ -93,7 +93,7 @@ namespace MCGalaxy.Games {
public static void SaveList() { public static void SaveList() {
lock (ioLock) lock (ioLock)
using (StreamWriter w = new StreamWriter("extra/teams.txt")) using (StreamWriter w = FileIO.CreateGuarded("extra/teams.txt"))
foreach (Team team in Teams) foreach (Team team in Teams)
{ {
w.WriteLine("Name=" + team.Name); w.WriteLine("Name=" + team.Name);

View File

@ -359,7 +359,8 @@ namespace MCGalaxy
public void Save(string path, string map) { public void Save(string path, string map) {
try { try {
lock (saveLock) { lock (saveLock) {
using (StreamWriter w = new StreamWriter(path)) { using (StreamWriter w = FileIO.CreateGuarded(path))
{
w.WriteLine("#Level properties for " + map); w.WriteLine("#Level properties for " + map);
w.WriteLine("#Drown-time is in tenths of a second"); w.WriteLine("#Drown-time is in tenths of a second");
ConfigElement.Serialise(Server.levelConfig, w, this); ConfigElement.Serialise(Server.levelConfig, w, this);

View File

@ -70,7 +70,7 @@ namespace MCGalaxy.Modules.Awards
static readonly object saveLock = new object(); static readonly object saveLock = new object();
public static void Save() { public static void Save() {
lock (saveLock) lock (saveLock)
using (StreamWriter w = new StreamWriter("text/awardsList.txt")) using (StreamWriter w = FileIO.CreateGuarded("text/awardsList.txt"))
{ {
WriteHeader(w); WriteHeader(w);
foreach (Award a in Awards) { foreach (Award a in Awards) {
@ -81,7 +81,8 @@ namespace MCGalaxy.Modules.Awards
public static void Load() { public static void Load() {
if (!File.Exists("text/awardsList.txt")) { if (!File.Exists("text/awardsList.txt")) {
using (StreamWriter w = new StreamWriter("text/awardsList.txt")) { using (StreamWriter w = FileIO.CreateGuarded("text/awardsList.txt"))
{
WriteHeader(w); WriteHeader(w);
w.WriteLine("Gotta start somewhere : Built your first house"); w.WriteLine("Gotta start somewhere : Built your first house");
w.WriteLine("Climbing the ladder : Earned a rank advancement"); w.WriteLine("Climbing the ladder : Earned a rank advancement");

View File

@ -79,7 +79,7 @@ namespace MCGalaxy.Modules.Awards
static readonly object saveLock = new object(); static readonly object saveLock = new object();
public static void Save() { public static void Save() {
lock (saveLock) lock (saveLock)
using (StreamWriter w = new StreamWriter("text/playerAwards.txt")) using (StreamWriter w = FileIO.CreateGuarded("text/playerAwards.txt"))
{ {
foreach (PlayerAward a in Awards) foreach (PlayerAward a in Awards)
w.WriteLine(a.Player + " : " + a.Awards.Join(",")); w.WriteLine(a.Player + " : " + a.Awards.Join(","));

View File

@ -100,7 +100,8 @@ namespace MCGalaxy.Modules.Games.ZS
public override void Save() { public override void Save() {
if (cfg == null) cfg = ConfigElement.GetAll(typeof(ZSConfig)); 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("# 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("# zombie-name-while-infected = Sets the zombies name while actived if there is a value.");
w.WriteLine(); w.WriteLine();

View File

@ -76,7 +76,8 @@ namespace MCGalaxy.Modules.Relay.Discord
public void Save() { public void Save() {
if (cfg == null) cfg = ConfigElement.GetAll(typeof(DiscordConfig)); 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("# Discord relay bot configuration");
w.WriteLine("# See " + Updater.SourceURL + "/wiki/Discord-relay-bot/"); w.WriteLine("# See " + Updater.SourceURL + "/wiki/Discord-relay-bot/");
w.WriteLine(); w.WriteLine();

View File

@ -125,7 +125,8 @@ namespace MCGalaxy.Modules.Warps
} }
public void Save() { public void Save() {
using (StreamWriter w = new StreamWriter(Filename)) { using (StreamWriter w = FileIO.CreateGuarded(Filename))
{
foreach (Warp warp in Items) foreach (Warp warp in Items)
{ {
w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" + w.WriteLine(warp.Name + ":" + warp.Level + ":" + warp.Pos.X + ":" +

View File

@ -208,7 +208,8 @@ namespace MCGalaxy
} }
static void SaveDisabledList() { static void SaveDisabledList() {
using (StreamWriter w = new StreamWriter(Paths.CPEDisabledFile)) { using (StreamWriter w = FileIO.CreateGuarded(Paths.CPEDisabledFile))
{
w.WriteLine("# CPE configuration"); w.WriteLine("# CPE configuration");
w.WriteLine("# This file allows disabling non-classic (CPE) extensions "); w.WriteLine("# This file allows disabling non-classic (CPE) extensions ");
w.WriteLine("# To disable an extension, just change '= True' to '= False'"); w.WriteLine("# To disable an extension, just change '= True' to '= False'");

View File

@ -326,7 +326,8 @@ namespace MCGalaxy
static void SaveGroups(List<Group> givenList) { static void SaveGroups(List<Group> givenList) {
if (cfg == null) cfg = ConfigElement.GetAll(typeof(Group)); 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("#Version 3");
w.WriteLine("#RankName = string"); w.WriteLine("#RankName = string");
w.WriteLine("#\tThe name of the rank (e.g. Guest)"); w.WriteLine("#\tThe name of the rank (e.g. Guest)");

View File

@ -93,7 +93,8 @@ namespace MCGalaxy.Authentication
} }
static void SaveServices() { static void SaveServices() {
using (StreamWriter w = new StreamWriter(Paths.AuthServicesFile)) { using (StreamWriter w = FileIO.CreateGuarded(Paths.AuthServicesFile))
{
w.WriteLine("# Authentication services configuration"); w.WriteLine("# Authentication services configuration");
w.WriteLine("# There is no reason to modify these configuration settings, unless the server has been configured"); 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)"); w.WriteLine("# to send heartbeats to multiple authentication services (e.g. both ClassiCube.net and BetaCraft.uk)");