From f4b3f4bbc6f1a53a8fe39e23f4c84ab8d32e8969 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Fri, 5 Feb 2021 23:19:33 +0100 Subject: [PATCH] Fixed double ReverseRenaming calls, related improvements --- Code/IO/IOUtils.cs | 50 ++++++++++++++++------------------- Code/Main/AutoEncode.cs | 4 --- Code/Main/Interpolate.cs | 3 ++- Code/Main/InterpolateSteps.cs | 3 ++- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 9ffef90..db3bdfe 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -168,20 +168,6 @@ namespace Flowframes.IO File.Move(path, targetPath); } - public static bool TryCopy(string source, string dest, bool overwrite = true) // Copy with error handling. Returns false if failed - { - try - { - File.Copy(source, dest, overwrite); - } - catch (Exception e) - { - MessageBox.Show("Copy from \"" + source + "\" to \"" + dest + " (Overwrite: " + overwrite + ") failed: \n\n" + e.Message); - return false; - } - return true; - } - public static int GetFilenameCounterLength(string file, string prefixToRemove = "") { string filenameNoExt = Path.GetFileNameWithoutExtension(file); @@ -209,31 +195,36 @@ namespace Flowframes.IO } } - static bool TryCopy(string source, string target) + static bool TryCopy(string source, string target, bool overwrite = true) { try { - File.Copy(source, target); + File.Copy(source, target, overwrite); } - catch + catch (Exception e) { + Logger.Log($"Failed to move '{source}' to '{target}' (Overwrite: {overwrite}): {e.Message}"); return false; } + return true; } - public static bool TryMove(string source, string target, bool deleteIfExists = true) + public static bool TryMove(string source, string target, bool overwrite = true) { try { - if (deleteIfExists && File.Exists(target)) + if (overwrite && File.Exists(target)) File.Delete(target); + File.Move(source, target); } - catch + catch (Exception e) { + Logger.Log($"Failed to move '{source}' to '{target}' (Overwrite: {overwrite}): {e.Message}"); return false; } + return true; } @@ -280,28 +271,33 @@ namespace Flowframes.IO return oldNewNamesMap; } - public static async Task ReverseRenaming(string basePath, Dictionary oldNewMap, bool clearDict) // Relative -> absolute paths + public static async Task ReverseRenaming(string basePath, Dictionary oldNewMap) // Relative -> absolute paths { Dictionary absPaths = oldNewMap.ToDictionary(x => Path.Combine(basePath, x.Key), x => Path.Combine(basePath, x.Value)); - await ReverseRenaming(absPaths, clearDict); + await ReverseRenaming(absPaths); } - public static async Task ReverseRenaming(Dictionary oldNewMap, bool clearDict) // Takes absolute paths only + public static async Task ReverseRenaming(Dictionary oldNewMap) // Takes absolute paths only { if (oldNewMap == null || oldNewMap.Count < 1) return; int counter = 0; + int failCount = 0; foreach (KeyValuePair pair in oldNewMap) { - TryMove(pair.Value, pair.Key); + bool success = TryMove(pair.Value, pair.Key); + + if (!success) + failCount++; + + if (failCount >= 100) + break; + counter++; if (counter % 1000 == 0) await Task.Delay(1); } - - if (clearDict) - oldNewMap.Clear(); } public static float GetVideoFramerate (string path) diff --git a/Code/Main/AutoEncode.cs b/Code/Main/AutoEncode.cs index 18eb792..efa97e0 100644 --- a/Code/Main/AutoEncode.cs +++ b/Code/Main/AutoEncode.cs @@ -114,10 +114,6 @@ namespace Flowframes.Main } if (Interpolate.canceled) return; - - if(!Interpolate.current.stepByStep) - await IOUtils.ReverseRenaming(Interpolate.current.framesFolder, AiProcess.filenameMap, true); // Get timestamps back - await CreateVideo.ChunksToVideos(Interpolate.current.tempFolder, videoChunksFolder, Interpolate.current.outFilename); } catch (Exception e) diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index ac03c69..914a951 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -60,7 +60,8 @@ namespace Flowframes Program.mainForm.SetProgress(100); if(!currentlyUsingAutoEnc) await CreateVideo.Export(current.interpFolder, current.outFilename, current.outMode, false); - await IOUtils.ReverseRenaming(Interpolate.current.framesFolder, AiProcess.filenameMap, true); // Get timestamps back + await IOUtils.ReverseRenaming(current.framesFolder, AiProcess.filenameMap); // Get timestamps back + AiProcess.filenameMap.Clear(); await Cleanup(); Program.mainForm.SetWorking(false); Logger.Log("Total processing time: " + FormatUtils.Time(sw.Elapsed)); diff --git a/Code/Main/InterpolateSteps.cs b/Code/Main/InterpolateSteps.cs index f9c970b..8d28cfb 100644 --- a/Code/Main/InterpolateSteps.cs +++ b/Code/Main/InterpolateSteps.cs @@ -108,7 +108,8 @@ namespace Flowframes.Main if (canceled) return; Program.mainForm.SetStatus("Running AI..."); await RunAi(current.interpFolder, current.ai, true); - await IOUtils.ReverseRenaming(current.framesFolder, AiProcess.filenameMap, true); // Get timestamps back + await IOUtils.ReverseRenaming(current.framesFolder, AiProcess.filenameMap); // Get timestamps back + AiProcess.filenameMap.Clear(); Program.mainForm.SetProgress(0); }