Skip to content

Commit ffae0f4

Browse files
authored
Changed File.Move usages to FileSystem.ReplaceFile because it can move files/directories between drives (#1535)
* Changed File.Move usages to FileSystem.ReplaceFile because it can move files between drives * Ignored filereplace file cleanup errors because copy succeeded anyway
1 parent ad872ff commit ffae0f4

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

NitroxLauncher/Patching/NitroxEntryPatch.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using dnlib.DotNet;
77
using dnlib.DotNet.Emit;
8+
using NitroxModel.OS;
89
using FileAttributes = System.IO.FileAttributes;
910

1011
namespace NitroxLauncher.Patching
@@ -66,7 +67,7 @@ public void Apply()
6667
{
6768
throw error;
6869
}
69-
File.Move(modifiedAssemblyCSharp, assemblyCSharp);
70+
FileSystem.Instance.ReplaceFile(modifiedAssemblyCSharp, assemblyCSharp);
7071
}
7172

7273
private Exception RetryWait(Action action, int interval, int retries = 0)
@@ -112,9 +113,8 @@ public void Remove()
112113

113114
File.SetAttributes(assemblyCSharp, FileAttributes.Normal);
114115
}
115-
116-
File.Delete(assemblyCSharp);
117-
File.Move(modifiedAssemblyCSharp, assemblyCSharp);
116+
117+
FileSystem.Instance.ReplaceFile(modifiedAssemblyCSharp, assemblyCSharp);
118118
}
119119

120120
private static int FindNitroxExecuteInstructionIndex(IList<Instruction> methodInstructions)

NitroxLauncher/Patching/QModHelper.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using NitroxModel.Logger;
4+
using NitroxModel.OS;
45

56
namespace NitroxLauncher.Patching
67
{
@@ -42,7 +43,7 @@ private static void RenameFile(string subnauticaBasePath, string fileToRename, s
4243
try
4344
{
4445
string newFilePath = Path.Combine(subnauticaBasePath, newFileName);
45-
File.Move(fileToRenamePath, newFilePath);
46+
FileSystem.Instance.ReplaceFile(fileToRenamePath, newFilePath);
4647
qModPatched = !qModPatched;
4748
Log.Info("Removing/Restoring QMod initialisation has been successful");
4849
}

NitroxModel/OS/FileSystem.cs

+19-10
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ namespace NitroxModel.OS
1414
{
1515
public class FileSystem
1616
{
17-
private static readonly Lazy<FileSystem> instance = new(() =>
17+
private static readonly Lazy<FileSystem> instance = new(() => Environment.OSVersion.Platform switch
1818
{
19-
return Environment.OSVersion.Platform switch
20-
{
21-
PlatformID.Unix => new UnixFileSystem(),
22-
PlatformID.MacOSX => new MacFileSystem(),
23-
_ => new WinFileSystem()
24-
};
19+
PlatformID.Unix => new UnixFileSystem(),
20+
PlatformID.MacOSX => new MacFileSystem(),
21+
_ => new WinFileSystem()
2522
},
2623
LazyThreadSafetyMode.ExecutionAndPublication);
2724

@@ -187,10 +184,12 @@ public string ZipFilesInDirectory(string dir, string outputPath = null, string f
187184

188185
/// <summary>
189186
/// Replaces target file with source file. If target file does not exist then it moves the file.
187+
/// This falls back to a copy if the target is on a different drive.
188+
/// The source file will always be deleted.
190189
/// </summary>
191190
/// <param name="source">Source file to replace with.</param>
192191
/// <param name="target">Target file to replace.</param>
193-
/// <returns>True if file was moved or replaced.</returns>
192+
/// <returns>True if file was moved or replaced successfully.</returns>
194193
public bool ReplaceFile(string source, string target)
195194
{
196195
if (!File.Exists(source))
@@ -210,6 +209,7 @@ public bool ReplaceFile(string source, string target)
210209
}
211210
catch (IOException ex)
212211
{
212+
// TODO: Need to test on Linux because the ex.HResult will likely not work or be different number cross-platform.
213213
switch ((uint)ex.HResult)
214214
{
215215
case 0x80070498:
@@ -222,8 +222,17 @@ public bool ReplaceFile(string source, string target)
222222
Log.Debug($"Renaming file '{target}' to '{backupFileName}' as backup plan if file replace fails");
223223
File.Move(target, backupFileName);
224224
File.Copy(source, target);
225-
File.Delete(source);
226-
File.Delete(backupFileName);
225+
226+
// Cleanup redundant files, ignoring errors.
227+
try
228+
{
229+
File.Delete(source);
230+
File.Delete(backupFileName);
231+
}
232+
catch (Exception)
233+
{
234+
// ignored
235+
}
227236
}
228237
catch (Exception ex2)
229238
{

0 commit comments

Comments
 (0)