Rename AtomicIO to a FileIO

This commit is contained in:
UnknownShadow200 2025-03-07 21:19:31 +11:00
parent a889b3e85d
commit b6cc486990
7 changed files with 33 additions and 15 deletions

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<AssemblyName>MCGalaxyCLI</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MCGalaxy\MCGalaxy_dotnet.csproj" />
</ItemGroup>
</Project>

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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));

View File

@ -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,

View File

@ -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); }
}
}
}

View File

@ -20,8 +20,8 @@ using System.IO;
namespace MCGalaxy
{
/// <summary> Provides utility methods for atomic File I/O operations. </summary>
public static class AtomicIO
/// <summary> Provides utility methods for File I/O operations. </summary>
public static class FileIO
{
/// <summary> Attempts to delete a file from disc, if it exists </summary>
/// <returns> true if file was successfully deleted, false if file did not exist to begin with </returns>
@ -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);
}
}
}