From 9c6c0f350698ab0394721e0634d45f2abdb2e474 Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Mon, 29 Jul 2019 22:05:06 +0200 Subject: [PATCH] fix issue with startup folder and config folder beeing identical --- Changelog.md | 5 +++++ .../ScriptingExtension/ScriptingExtension.cs | 5 ++--- mpv.net/Misc/Command.cs | 8 ++++---- mpv.net/Misc/Extension.cs | 7 +++---- mpv.net/Misc/Misc.cs | 7 +++++++ mpv.net/WPF/EverythingWindow.xaml.cs | 3 +-- mpv.net/mpv/mp.cs | 19 +++++++++---------- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Changelog.md b/Changelog.md index b0dedcd..9a6179c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,11 @@ environment variable and the OS default apps settings can be opened (Win 10 only) - Error messages are shown when unknown scripts and extensions are found in the startup folder because user scripts and extensions are supposed to be located in the config folder instead +- when the config folder and the startup folder were identical then extensions were loaded + twice and script were loaded four times because the script host is an extension, there is + now a check to ensure extensions and scripts are only loaded once. On first start there + is no longer an option to select the startup folder as config folder, it's still possible + using a custom folder but discouraged ### 5.0 diff --git a/extensions/ScriptingExtension/ScriptingExtension.cs b/extensions/ScriptingExtension/ScriptingExtension.cs index e472981..2982608 100644 --- a/extensions/ScriptingExtension/ScriptingExtension.cs +++ b/extensions/ScriptingExtension/ScriptingExtension.cs @@ -9,7 +9,6 @@ using System; using System.ComponentModel.Composition; using System.Collections.Generic; using System.IO; -using System.Windows.Forms; using mpvnet; using CSScriptLibrary; @@ -29,8 +28,8 @@ namespace ScriptingExtension // the file name of extensions must end with 'Exten if (Directory.Exists(mp.ConfigFolder + "scripts")) scriptFiles.AddRange(Directory.GetFiles(mp.ConfigFolder + "scripts", "*.cs")); - if (Directory.Exists(Application.StartupPath + "\\scripts")) - foreach (string file in Directory.GetFiles(Application.StartupPath + "\\scripts", "*.cs")) + if (Directory.Exists(PathHelp.StartupPath + "scripts") && mp.ConfigFolder != PathHelp.StartupPath) + foreach (string file in Directory.GetFiles(PathHelp.StartupPath + "scripts", "*.cs")) App.UnknownModule(file); if (scriptFiles.Count == 0) return; diff --git a/mpv.net/Misc/Command.cs b/mpv.net/Misc/Command.cs index 0e7a762..b1c46ef 100644 --- a/mpv.net/Misc/Command.cs +++ b/mpv.net/Misc/Command.cs @@ -93,7 +93,7 @@ namespace mpvnet { fileSize = new FileInfo(path).Length; - if (App.AudioTypes.Contains(Path.GetExtension(path).ToLower().TrimStart('.'))) + if (App.AudioTypes.Contains(PathHelp.GetShortExtension(path))) { using (MediaInfo mediaInfo = new MediaInfo(path)) { @@ -111,13 +111,13 @@ namespace mpvnet if (date != "") text += "Year: " + date + "\n"; if (duration != "") text += "Length: " + duration + "\n"; text += "Size: " + mediaInfo.GetInfo(MediaInfoStreamKind.General, "FileSize/String") + "\n"; - text += "Type: " + Path.GetExtension(path).ToUpper().TrimStart('.'); + text += "Type: " + PathHelp.GetShortExtension(path).ToUpper(); mp.commandv("show-text", text, "5000"); return; } } - else if (App.ImageTypes.Contains(Path.GetExtension(path).ToLower().TrimStart('.'))) + else if (App.ImageTypes.Contains(PathHelp.GetShortExtension(path))) { using (MediaInfo mediaInfo = new MediaInfo(path)) { @@ -125,7 +125,7 @@ namespace mpvnet "Width: " + mediaInfo.GetInfo(MediaInfoStreamKind.Image, "Width") + "\n" + "Height: " + mediaInfo.GetInfo(MediaInfoStreamKind.Image, "Height") + "\n" + "Size: " + mediaInfo.GetInfo(MediaInfoStreamKind.General, "FileSize/String") + "\n" + - "Type: " + Path.GetExtension(path).ToUpper().TrimStart('.'); + "Type: " + PathHelp.GetShortExtension(path).ToUpper(); mp.commandv("show-text", text, "5000"); return; diff --git a/mpv.net/Misc/Extension.cs b/mpv.net/Misc/Extension.cs index c82aac3..299af97 100644 --- a/mpv.net/Misc/Extension.cs +++ b/mpv.net/Misc/Extension.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.IO; -using System.Windows.Forms; using System.Linq; namespace mpvnet @@ -20,7 +19,7 @@ namespace mpvnet try { AggregateCatalog catalog = new AggregateCatalog(); - string dir = Application.StartupPath + "\\Extensions"; + string dir = PathHelp.StartupPath + "Extensions"; if (Directory.Exists(dir)) { @@ -35,9 +34,9 @@ namespace mpvnet } } - dir = mp.ConfigFolder + "\\Extensions"; + dir = mp.ConfigFolder + "Extensions"; - if (Directory.Exists(dir)) + if (Directory.Exists(dir) && mp.ConfigFolder != PathHelp.StartupPath) foreach (string i in Directory.GetDirectories(dir)) catalog.Catalogs.Add(new DirectoryCatalog(i, "*Extension.dll")); diff --git a/mpv.net/Misc/Misc.cs b/mpv.net/Misc/Misc.cs index a136d99..88e1c74 100644 --- a/mpv.net/Misc/Misc.cs +++ b/mpv.net/Misc/Misc.cs @@ -388,6 +388,8 @@ namespace mpvnet public class PathHelp { + public static string StartupPath { get; } = Application.StartupPath + "\\"; + public static string GetFileName(string path) { if (string.IsNullOrEmpty(path)) return ""; @@ -397,5 +399,10 @@ namespace mpvnet if (index > -1) return path.Substring(index + 1); return path; } + + public static string GetShortExtension(string path) + { + return Path.GetExtension(path).ToLower().TrimStart('.'); + } } } \ No newline at end of file diff --git a/mpv.net/WPF/EverythingWindow.xaml.cs b/mpv.net/WPF/EverythingWindow.xaml.cs index b578825..f26eea6 100644 --- a/mpv.net/WPF/EverythingWindow.xaml.cs +++ b/mpv.net/WPF/EverythingWindow.xaml.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -140,7 +139,7 @@ namespace mpvnet for (i = 0; i < Everything_GetNumResults(); i++) { Everything_GetResultFullPathName(i, buf, bufsize); - string ext = Path.GetExtension(buf.ToString()).TrimStart('.').ToLower(); + string ext = PathHelp.GetShortExtension(buf.ToString()); if (App.AudioTypes.Contains(ext) || App.VideoTypes.Contains(ext) || App.ImageTypes.Contains(ext)) diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index 47f2c1b..5affc17 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -138,17 +138,16 @@ namespace mpvnet get { if (_ConfigFolder == null) { - _ConfigFolder = Application.StartupPath + "\\portable_config\\"; + _ConfigFolder = PathHelp.StartupPath + "portable_config\\"; if (!Directory.Exists(_ConfigFolder)) _ConfigFolder = RegHelp.GetString(App.RegPath, "ConfigFolder"); if (!Directory.Exists(_ConfigFolder)) { - string portableFolder = Application.StartupPath + "\\portable_config\\"; + string portableFolder = PathHelp.StartupPath + "portable_config\\"; string appdataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mpv.net\\"; string appdataFolderMpv = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mpv\\"; - string startupFolder = Application.StartupPath + "\\"; using (TaskDialog td = new TaskDialog()) { @@ -215,7 +214,7 @@ namespace mpvnet public static void LoadMpvScripts() { - string[] startupScripts = Directory.GetFiles(Application.StartupPath + "\\Scripts"); + string[] startupScripts = Directory.GetFiles(PathHelp.StartupPath + "Scripts"); foreach (string path in startupScripts) if (path.EndsWith(".lua") || path.EndsWith(".js")) @@ -229,9 +228,9 @@ namespace mpvnet public static void LoadScripts() { - if (Directory.Exists(Application.StartupPath + "\\Scripts")) + if (Directory.Exists(PathHelp.StartupPath + "Scripts")) { - foreach (string path in Directory.GetFiles(Application.StartupPath + "\\Scripts")) + foreach (string path in Directory.GetFiles(PathHelp.StartupPath + "Scripts")) { if (KnownScripts.Contains(Path.GetFileName(path))) { @@ -615,7 +614,7 @@ namespace mpvnet LastLoad = DateTime.Now; for (int i = 0; i < files.Length; i++) - if (App.SubtitleTypes.Contains(Path.GetExtension(files[i]).TrimStart('.').ToLower())) + if (App.SubtitleTypes.Contains(PathHelp.GetShortExtension(files[i]))) commandv("sub-add", files[i]); else if (i == 0 && !append) @@ -636,9 +635,9 @@ namespace mpvnet if (!File.Exists(path) || get_property_int("playlist-count") != 1) return; List files = Directory.GetFiles(Path.GetDirectoryName(path)).ToList(); files = files.Where((file) => - App.VideoTypes.Contains(Path.GetExtension(file).TrimStart('.').ToLower()) || - App.AudioTypes.Contains(Path.GetExtension(file).TrimStart('.').ToLower()) || - App.ImageTypes.Contains(Path.GetExtension(file).TrimStart('.').ToLower())).ToList(); + App.VideoTypes.Contains(PathHelp.GetShortExtension(file)) || + App.AudioTypes.Contains(PathHelp.GetShortExtension(file)) || + App.ImageTypes.Contains(PathHelp.GetShortExtension(file))).ToList(); files.Sort(new StringLogicalComparer()); int index = files.IndexOf(path); files.Remove(path);