diff --git a/CodeLegacy/Extensions/ExtensionMethods.cs b/CodeLegacy/Extensions/ExtensionMethods.cs index 5ec71e5..5aa7658 100644 --- a/CodeLegacy/Extensions/ExtensionMethods.cs +++ b/CodeLegacy/Extensions/ExtensionMethods.cs @@ -285,29 +285,48 @@ namespace Flowframes return filePath.IsConcatFile() ? $"{rateStr}-safe 0 -f concat " : ""; } - public static string Get(this Dictionary dict, string key, bool returnKeyInsteadOfEmptyString = false, bool ignoreCase = false) + public static string Get(this Dictionary dict, string key, bool returnKeyInsteadOfEmptyString = false, bool ignoreCase = false) { if (key == null) key = ""; + // Safely handle a null dictionary for (int i = 0; i < (dict == null ? 0 : dict.Count); i++) { + var elementKey = dict.ElementAt(i).Key; + var elementValue = dict.ElementAt(i).Value; + + // Convert the dictionary key to string (handling null) + string dictKeyString = (elementKey == null ? "" : elementKey.ToString()); + if (ignoreCase) { - if (key.Lower() == dict.ElementAt(i).Key.Lower()) - return dict.ElementAt(i).Value; + if (key.Lower() == dictKeyString.Lower()) + return elementValue; } else { - if (key == dict.ElementAt(i).Key) - return dict.ElementAt(i).Value; + if (key == dictKeyString) + return elementValue; } } - if (returnKeyInsteadOfEmptyString) - return key; - else - return ""; + return returnKeyInsteadOfEmptyString ? key : ""; + } + + /// Get a value from a dictionary, returns if not found. For strings, the default fallback is an empty string instead of null. + public static TValue Get(this IDictionary dictionary, TKey key, TValue fallback = default) + { + // For string values, use empty string instead of null as default value + if (typeof(TValue) == typeof(string) && fallback != null) + { + fallback = (TValue)(object)string.Empty; + } + + if (dictionary == null) + return fallback; + + return dictionary.TryGetValue(key, out var value) ? value : fallback; } public static void FillFromEnum(this ComboBox comboBox, Dictionary stringMap = null, int defaultIndex = -1, List exclusionList = null, bool useKeyNames = false) where TEnum : Enum diff --git a/CodeLegacy/Forms/BatchForm.cs b/CodeLegacy/Forms/BatchForm.cs index e8d71ab..c80fe25 100644 --- a/CodeLegacy/Forms/BatchForm.cs +++ b/CodeLegacy/Forms/BatchForm.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using Flowframes.Data; +using static Flowframes.Ui.ControlExtensions; namespace Flowframes.Forms { @@ -25,7 +26,7 @@ namespace Flowframes.Forms RefreshGui(); } - public void RefreshGui () + public void RefreshGui() { taskList.Items.Clear(); @@ -34,29 +35,29 @@ namespace Flowframes.Forms InterpSettings entry = Program.batchQueue.ElementAt(i); string outFormat = Strings.OutputFormat.Get(entry.outSettings.Format.ToString()); string inPath = string.IsNullOrWhiteSpace(entry.inPath) ? "No Path" : Path.GetFileName(entry.inPath).Trunc(40); - string str = $"#{i+1}: {inPath} - {entry.inFps.Float} FPS => {entry.interpFactor}x {entry.ai.NameShort} ({entry.model.Name}) => {outFormat}"; + string str = $"#{i + 1}: {inPath} - {entry.inFps.Float} FPS => {entry.interpFactor}x {entry.ai.NameShort} ({entry.model.Name}) => {outFormat}"; taskList.Items.Add(str); } } - private void RefreshIndex () + private void RefreshIndex() { - for(int i = 0; i < taskList.Items.Count; i++) + for (int i = 0; i < taskList.Items.Count; i++) { string[] split = taskList.Items[i].ToString().Split(':'); - split[0] = $"#{i+1}"; + split[0] = $"#{i + 1}"; taskList.Items[i] = string.Join(":", split); } } - public void SetWorking (bool working) + public void SetWorking(bool working) { runBtn.Enabled = !working; addToQueue.Enabled = !working; stopBtn.Visible = working; forceStopBtn.Visible = working; stopBtn.Enabled = working; - } + } private void BatchForm_Load(object sender, EventArgs e) { @@ -119,7 +120,7 @@ namespace Flowframes.Forms Queue temp = new Queue(); - for(int i = 0; i < Program.batchQueue.Count; i++) + for (int i = 0; i < Program.batchQueue.Count; i++) { if (i != taskList.SelectedIndex) temp.Enqueue(Program.batchQueue.ElementAt(i)); @@ -146,24 +147,40 @@ namespace Flowframes.Forms await LoadDroppedPaths(droppedPaths); } - public async Task LoadDroppedPaths (string[] droppedPaths, bool start = false) - { - foreach (string path in droppedPaths) + public async Task LoadDroppedPaths(string[] droppedPaths, bool start = false) + { + if (droppedPaths == null || droppedPaths.Length < 1) + return; + + var prevOpacity = Program.mainForm.Opacity; + Program.mainForm.Invoke(() => Program.mainForm.Opacity = 0f); + + try { - Logger.Log($"BatchForm: Dropped path: '{path}'", true); + foreach (string path in droppedPaths) + { + Logger.Log($"BatchForm: Dropped path: '{path}'", true); - InterpSettings current = Program.mainForm.GetCurrentSettings(path); - current.inFpsDetected = await IoUtils.GetFpsFolderOrVideo(path); - current.inFps = current.inFpsDetected; + InterpSettings current = Program.mainForm.GetCurrentSettings(path); + current.inFpsDetected = await IoUtils.GetFpsFolderOrVideo(path); + current.inFps = current.inFpsDetected; - if(current.inFps.Float <= 0) - current.inFps = InterpolateUtils.AskForFramerate(new DirectoryInfo(path).Name, false); + if (current.inFps.Float <= 0) + current.inFps = InterpolateUtils.AskForFramerate(new DirectoryInfo(path).Name, false); - current.outFps = current.inFps * current.interpFactor; + current.outFps = current.inFps * current.interpFactor; - Program.batchQueue.Enqueue(current); - RefreshGui(); + Program.batchQueue.Enqueue(current); + RefreshGui(); + } } + catch (Exception ex) + { + Logger.Log($"BatchForm: Error while loading dropped paths: {ex.Message}", true); + } + + Program.mainForm.Invoke(() => Program.mainForm.Opacity = prevOpacity); + BringToFront(); if (start) runBtn_Click(null, null); diff --git a/CodeLegacy/Main/Interpolate.cs b/CodeLegacy/Main/Interpolate.cs index d180e18..1bc616a 100644 --- a/CodeLegacy/Main/Interpolate.cs +++ b/CodeLegacy/Main/Interpolate.cs @@ -1,5 +1,4 @@ -using Flowframes; -using Flowframes.Media; +using Flowframes.Media; using Flowframes.Data; using Flowframes.IO; using Flowframes.Magick; @@ -17,7 +16,6 @@ using System.Threading.Tasks; using System.Windows.Forms; using Padding = Flowframes.Data.Padding; using Utils = Flowframes.Main.InterpolateUtils; -using System.Drawing.Imaging; namespace Flowframes { diff --git a/CodeLegacy/MiscUtils/ParseUtils.cs b/CodeLegacy/MiscUtils/ParseUtils.cs index b3c8c7e..c8f8bb1 100644 --- a/CodeLegacy/MiscUtils/ParseUtils.cs +++ b/CodeLegacy/MiscUtils/ParseUtils.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Flowframes.MiscUtils {