From b6cc48699077601a3a34dae9296f1e1aa55f6c5c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 7 Mar 2025 21:19:31 +1100 Subject: [PATCH] Rename AtomicIO to a FileIO --- CLI/MCGalaxyCLI_dotnet9.csproj | 12 ++++++++++++ MCGalaxy/Bots/BotsFile.cs | 3 +-- MCGalaxy/Commands/Moderation/CmdReport.cs | 2 +- MCGalaxy/Levels/EnvPreset.cs | 2 +- MCGalaxy/Scripting/Scripting.cs | 4 ++-- MCGalaxy/Server/Maintenance/Updater.cs | 14 +++++++------- MCGalaxy/util/IO/FileIO.cs | 11 +++++++++-- 7 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 CLI/MCGalaxyCLI_dotnet9.csproj diff --git a/CLI/MCGalaxyCLI_dotnet9.csproj b/CLI/MCGalaxyCLI_dotnet9.csproj new file mode 100644 index 000000000..ab3290f06 --- /dev/null +++ b/CLI/MCGalaxyCLI_dotnet9.csproj @@ -0,0 +1,12 @@ + + + Exe + net9.0 + true + MCGalaxyCLI + + + + + + diff --git a/MCGalaxy/Bots/BotsFile.cs b/MCGalaxy/Bots/BotsFile.cs index 77fa6413d..b64ef1be3 100644 --- a/MCGalaxy/Bots/BotsFile.cs +++ b/MCGalaxy/Bots/BotsFile.cs @@ -91,8 +91,7 @@ namespace MCGalaxy.Bots { try { using (StreamWriter w = new StreamWriter(tmpPath)) { WriteAll(w, props); } - AtomicIO.TryDelete(path); - File.Move(tmpPath, path); + FileIO.Replace(tmpPath, path); } catch (Exception ex) { Logger.LogError("Error saving bots to " + path, ex); } diff --git a/MCGalaxy/Commands/Moderation/CmdReport.cs b/MCGalaxy/Commands/Moderation/CmdReport.cs index 28ce227d0..e4f083625 100644 --- a/MCGalaxy/Commands/Moderation/CmdReport.cs +++ b/MCGalaxy/Commands/Moderation/CmdReport.cs @@ -181,7 +181,7 @@ namespace MCGalaxy.Commands.Moderation { static void DeleteReport(string user) { string backup = "extra/reportedbackups/" + user + ".txt"; - AtomicIO.TryDelete(backup); + FileIO.TryDelete(backup); File.Move(ReportPath(user), backup); } diff --git a/MCGalaxy/Levels/EnvPreset.cs b/MCGalaxy/Levels/EnvPreset.cs index c3a3204cf..868cbb082 100644 --- a/MCGalaxy/Levels/EnvPreset.cs +++ b/MCGalaxy/Levels/EnvPreset.cs @@ -70,7 +70,7 @@ namespace MCGalaxy { public static void ListFor(Player p) { p.Message("&HPresets: &f{0}", Presets.Join(pr => pr.Key)); - string[] files = AtomicIO.TryGetFiles(FOLDER, "*" + FILE_EXTENSION); + string[] files = FileIO.TryGetFiles(FOLDER, "*" + FILE_EXTENSION); if (files == null) return; string all = files.Join(f => Path.GetFileNameWithoutExtension(f)); diff --git a/MCGalaxy/Scripting/Scripting.cs b/MCGalaxy/Scripting/Scripting.cs index f5f3be350..beba77d11 100644 --- a/MCGalaxy/Scripting/Scripting.cs +++ b/MCGalaxy/Scripting/Scripting.cs @@ -125,7 +125,7 @@ namespace MCGalaxy.Scripting public static void AutoloadCommands() { - string[] files = AtomicIO.TryGetFiles(COMMANDS_DLL_DIR, "*.dll"); + string[] files = FileIO.TryGetFiles(COMMANDS_DLL_DIR, "*.dll"); if (files == null) return; foreach (string path in files) { AutoloadCommands(path); } @@ -178,7 +178,7 @@ namespace MCGalaxy.Scripting public static void AutoloadPlugins() { - string[] files = AtomicIO.TryGetFiles(PLUGINS_DLL_DIR, "*.dll"); + string[] files = FileIO.TryGetFiles(PLUGINS_DLL_DIR, "*.dll"); if (files == null) return; // Ensure that plugin files are loaded in a consistent order, diff --git a/MCGalaxy/Server/Maintenance/Updater.cs b/MCGalaxy/Server/Maintenance/Updater.cs index 02b3acd2a..3211a7e99 100644 --- a/MCGalaxy/Server/Maintenance/Updater.cs +++ b/MCGalaxy/Server/Maintenance/Updater.cs @@ -123,14 +123,14 @@ namespace MCGalaxy // Move current files to previous files (by moving instead of copying, // can overwrite original the files without breaking the server) - AtomicIO.TryMove(serverDLL, "prev_MCGalaxy_.dll"); - AtomicIO.TryMove(serverGUI, "prev_MCGalaxy.exe"); - AtomicIO.TryMove(serverCLI, "prev_MCGalaxyCLI.exe"); + FileIO.TryMove(serverDLL, "prev_MCGalaxy_.dll"); + FileIO.TryMove(serverGUI, "prev_MCGalaxy.exe"); + FileIO.TryMove(serverCLI, "prev_MCGalaxyCLI.exe"); // Move update files to current files - AtomicIO.TryMove("MCGalaxy_.update", serverDLL); - AtomicIO.TryMove("MCGalaxy.update", serverGUI); - AtomicIO.TryMove("MCGalaxyCLI.update", serverCLI); + FileIO.TryMove("MCGalaxy_.update", serverDLL); + FileIO.TryMove("MCGalaxy.update", serverGUI); + FileIO.TryMove("MCGalaxyCLI.update", serverCLI); Server.Stop(true, "Updating server."); } catch (Exception ex) { @@ -145,7 +145,7 @@ namespace MCGalaxy } static void DeleteFiles(params string[] paths) { - foreach (string path in paths) { AtomicIO.TryDelete(path); } + foreach (string path in paths) { FileIO.TryDelete(path); } } } } diff --git a/MCGalaxy/util/IO/FileIO.cs b/MCGalaxy/util/IO/FileIO.cs index fd4a41fa0..3922dfde9 100644 --- a/MCGalaxy/util/IO/FileIO.cs +++ b/MCGalaxy/util/IO/FileIO.cs @@ -20,8 +20,8 @@ using System.IO; namespace MCGalaxy { - /// Provides utility methods for atomic File I/O operations. - public static class AtomicIO + /// Provides utility methods for File I/O operations. + public static class FileIO { /// Attempts to delete a file from disc, if it exists /// true if file was successfully deleted, false if file did not exist to begin with @@ -56,5 +56,12 @@ namespace MCGalaxy return null; } } + + public static void Replace(string src, string dst) { + TryDelete(dst + ".old"); + + File.Move(dst, dst + ".old"); + File.Move(src, dst); + } } }