From 57b1b6a905a673e96ef2c7fb31e43a26c3584442 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Tue, 6 Apr 2021 11:30:01 +0200 Subject: [PATCH] Model selection tree improvements --- Code/Forms/ModelSelectForm.cs | 27 +++++++++++++++++++++++---- Code/IO/IOUtils.cs | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Code/Forms/ModelSelectForm.cs b/Code/Forms/ModelSelectForm.cs index 5fc69e9..8b002ca 100644 --- a/Code/Forms/ModelSelectForm.cs +++ b/Code/Forms/ModelSelectForm.cs @@ -21,9 +21,6 @@ public partial class ModelSelectForm : Form public ModelSelectForm(Button modelButton, int modelNumber) { InitializeComponent(); - //Show(this); - //TopMost = true; - //CenterToScreen(); modelBtn = modelButton; modelNo = modelNumber; SelectLastUsed(); @@ -32,13 +29,25 @@ public ModelSelectForm(Button modelButton, int modelNumber) private void ModelSelectForm_Load(object sender, EventArgs e) { string modelDir = Config.Get("modelPath"); + if (!Directory.Exists(modelDir)) { Program.ShowMessage("The saved model directory does not exist - Make sure you've set a models folder!"); + Close(); + return; + } + + if (IOUtils.GetAmountOfFiles(modelDir, true, "*.pth") < 1) + { + Program.ShowMessage($"The saved model directory does not contain any model (.pth) files!\n\nPlease put some models into '{modelDir}'."); + Close(); return; } + + ForceLowercaseExtensions(modelDir); DirectoryInfo modelsDir = new DirectoryInfo(modelDir); BuildTree(modelsDir, modelTree.Nodes); + if (Config.GetBool("modelSelectAutoExpand")) modelTree.ExpandAll(); else @@ -51,7 +60,7 @@ private async void SelectLastUsed() return; while (modelTree.Nodes.Count < 1) - await Task.Delay(1); + await Task.Delay(100); if (string.IsNullOrWhiteSpace(Program.currentModel1)) modelTree.SelectedNode = modelTree.Nodes[0]; @@ -70,6 +79,15 @@ private void CheckNodesRecursive(TreeNode parentNode) } } + private void ForceLowercaseExtensions(string path) + { + foreach (FileInfo file in IOUtils.GetFileInfosSorted(path, true, "*.*")) + { + if(file.Extension == ".PTH") + file.MoveTo(Path.ChangeExtension(file.FullName, "pth")); + } + } + private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe) { TreeNode curNode = addInMe.Add(directoryInfo.Name); @@ -79,6 +97,7 @@ private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe) if (file.Extension == ".pth") // Hide any other file extension curNode.Nodes.Add(file.FullName, Path.ChangeExtension(file.Name, null)); } + foreach (DirectoryInfo subdir in directoryInfo.GetDirectories()) { if (subdir.GetFiles("*.pth", SearchOption.AllDirectories).Length > 0) // Don't list folders that have no PTH files diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 9e7bf24..4240f94 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -493,5 +493,28 @@ public static bool HasEnoughDiskSpace(int mBytes, string drivePath, float multip return true; return false; } + + public static string[] GetFilesSorted(string path, bool recursive = false, string pattern = "*") + { + SearchOption opt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + return Directory.GetFiles(path, pattern, opt).OrderBy(x => Path.GetFileName(x)).ToArray(); + } + + public static string[] GetFilesSorted(string path, string pattern = "*") + { + return GetFilesSorted(path, false, pattern); + } + + public static string[] GetFilesSorted(string path) + { + return GetFilesSorted(path, false, "*"); + } + + public static FileInfo[] GetFileInfosSorted(string path, bool recursive = false, string pattern = "*") + { + SearchOption opt = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; + DirectoryInfo dir = new DirectoryInfo(path); + return dir.GetFiles(pattern, opt).OrderBy(x => x.Name).ToArray(); + } } }