diff --git a/ImperatorToCK3/CommonUtils/FileHelper.cs b/ImperatorToCK3/CommonUtils/FileHelper.cs index f0c8e7d12..6a7c9e35d 100644 --- a/ImperatorToCK3/CommonUtils/FileHelper.cs +++ b/ImperatorToCK3/CommonUtils/FileHelper.cs @@ -68,4 +68,27 @@ public static void DeleteWithRetries(string filePath) { throw new UserErrorException($"Failed to delete \"{filePath}\". {CloseProgramsHint}"); } } + + public static void MoveWithRetries(string sourceFilePath, string destFilePath) { + const int maxAttempts = 10; + + int currentAttempt = 0; + + var policy = Policy + .Handle(IsFilesSharingViolation) + .WaitAndRetry(maxAttempts, + sleepDurationProvider: _ => TimeSpan.FromSeconds(30), + onRetry: (_, _, _) => { + currentAttempt++; + Logger.Warn($"Attempt {currentAttempt} to move \"{sourceFilePath}\" to \"{destFilePath}\" failed."); + Logger.Warn(CloseProgramsHint); + }); + + try { + policy.Execute(() => File.Move(sourceFilePath, destFilePath)); + } catch (IOException ex) when (IsFilesSharingViolation(ex)) { + Logger.Debug(ex.ToString()); + throw new UserErrorException($"Failed to move \"{sourceFilePath}\" to \"{destFilePath}\". {CloseProgramsHint}"); + } + } } \ No newline at end of file diff --git a/ImperatorToCK3/Helpers/RakalyCaller.cs b/ImperatorToCK3/Helpers/RakalyCaller.cs index ac27c3b05..b513b2bbc 100644 --- a/ImperatorToCK3/Helpers/RakalyCaller.cs +++ b/ImperatorToCK3/Helpers/RakalyCaller.cs @@ -143,7 +143,7 @@ public static void MeltSave(string savePath) { if (File.Exists(destFileName)) { FileHelper.DeleteWithRetries(destFileName); } - File.Move(meltedSaveName, destFileName); + FileHelper.MoveWithRetries(meltedSaveName, destFileName); } // https://stackoverflow.com/a/47918132/10249243