diff --git a/README.md b/README.md index 11bf8ba..9ef9128 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,9 @@ mpv.net bugs and requests: - when the main windows gets activated and the clipboard content starts with http mpv.net will ask to play the URL, previously this was restricted to YouTube URLs - Python script errors show line and column whenever it is supported by IronPython +- if conf files exist in the startup directory mpv.net will use the startup + directory as config directory instead of creating default conf files in appdata +- renamed commands are handled now by migration code instead of being broken ### 3.4 (2019-05-03) diff --git a/mpv.net/MainForm.cs b/mpv.net/MainForm.cs index 75ee3eb..8ad39c2 100644 --- a/mpv.net/MainForm.cs +++ b/mpv.net/MainForm.cs @@ -307,55 +307,32 @@ namespace mpvnet public void BuildMenu() { string content = File.ReadAllText(mp.InputConfPath); - List lines = null; - Dictionary commandInputDic = new Dictionary(); + var items = CommandItem.GetItems(content); - if (content.Contains("#menu:")) - lines = content.Split('\r', '\n').ToList(); - else + if (!content.Contains("#menu:")) { - lines = Properties.Resources.inputConf.Split('\r', '\n').ToList(); - - foreach (string i in content.Split('\r', '\n')) - { - string line = i.Trim(); - if (line.StartsWith("#") || !line.Contains(" ")) continue; - string input = line.Substring(0, line.IndexOf(" ")).Trim(); - string command = line.Substring(line.IndexOf(" ") + 1).Trim(); - commandInputDic[command] = input; - } + var defaultItems = CommandItem.GetItems(Properties.Resources.inputConf); + foreach (CommandItem item in items) + foreach (CommandItem defaultItem in defaultItems) + if (item.Command == defaultItem.Command) + defaultItem.Input = item.Input; + items = defaultItems; } - foreach (string line in lines) + foreach (CommandItem item in items) { - if (!line.Contains("#menu:")) continue; - string left = line.Substring(0, line.IndexOf("#menu:")).Trim(); - if (left.StartsWith("#")) continue; - string command = left.Substring(left.IndexOf(" ") + 1).Trim(); - string menu = line.Substring(line.IndexOf("#menu:") + "#menu:".Length).Trim(); - string input = left.Substring(0, left.IndexOf(" ")); - if (input == "_") input = ""; - if (menu.Contains(";")) input = menu.Substring(0, menu.IndexOf(";")).Trim(); - string path = menu.Substring(menu.IndexOf(";") + 1).Trim().Replace("&", "&&"); - if (path == "" || command == "") continue; - - if (commandInputDic.Count > 0) - if (commandInputDic.ContainsKey(command)) - input = commandInputDic[command]; - else - input = ""; - + if (string.IsNullOrEmpty(item.Path)) + continue; + string path = item.Path.Replace("&", "&&"); MenuItem menuItem = ContextMenu.Add(path, () => { try { - mp.command_string(command); - } - catch (Exception ex) { + mp.command_string(item.Command); + } catch (Exception ex) { Msg.ShowException(ex); } }); - if (menuItem != null) - menuItem.ShortcutKeyDisplayString = input + " "; + menuItem.ShortcutKeyDisplayString = item.Input + " "; } } diff --git a/mpv.net/Misc.cs b/mpv.net/Misc.cs index 5d11914..548181c 100644 --- a/mpv.net/Misc.cs +++ b/mpv.net/Misc.cs @@ -198,46 +198,67 @@ namespace mpvnet } } + public static ObservableCollection GetItems(string content) + { + var items = new ObservableCollection(); + + if (!string.IsNullOrEmpty(content)) + { + foreach (string line in content.Split('\r', '\n')) + { + string val = line.Trim(); + if (val.StartsWith("#")) continue; + if (!val.Contains(" ")) continue; + CommandItem item = new CommandItem(); + item.Input = val.Substring(0, val.IndexOf(" ")).Replace("_", ""); + val = val.Substring(val.IndexOf(" ") + 1); + + if (val.Contains("#menu:")) + { + item.Path = val.Substring(val.IndexOf("#menu:") + 6).Trim(); + val = val.Substring(0, val.IndexOf("#menu:")); + + if (item.Path.Contains(";")) + item.Path = item.Path.Substring(item.Path.IndexOf(";") + 1).Trim(); + } + + item.Command = val.Trim(); + if (item.Command == "") + continue; + if (item.Command.ToLower() == "ignore") + item.Command = ""; + MigrateCommands(item); + items.Add(item); + } + } + return items; + } + private static ObservableCollection _Items; public static ObservableCollection Items { get { if (_Items is null) - { - _Items = new ObservableCollection(); - - if (File.Exists(mp.InputConfPath)) - { - foreach (string line in File.ReadAllLines(mp.InputConfPath)) - { - string val = line.Trim(); - if (val.StartsWith("#")) continue; - if (!val.Contains(" ")) continue; - CommandItem item = new CommandItem(); - item.Input = val.Substring(0, val.IndexOf(" ")).Replace("_", ""); - val = val.Substring(val.IndexOf(" ") + 1); - - if (val.Contains("#menu:")) - { - item.Path = val.Substring(val.IndexOf("#menu:") + 6).Trim(); - val = val.Substring(0, val.IndexOf("#menu:")); - - if (item.Path.Contains(";")) - item.Path = item.Path.Substring(item.Path.IndexOf(";") + 1).Trim(); - } - - item.Command = val.Trim(); - if (item.Command == "") - continue; - if (item.Command.ToLower() == "ignore") - item.Command = ""; - _Items.Add(item); - } - } - } + _Items = GetItems(File.ReadAllText(mp.InputConfPath)); return _Items; } } + + public static void MigrateCommands(CommandItem item) + { + switch (item.Command) + { + case "script-message mpv.net show-prefs": + item.Command = "script-message mpv.net show-conf-editor"; + break; + case "script-message mpv.net show-keys": + item.Command = "script-message mpv.net show-input-editor"; + break; + case "script-message mpv.net history": + item.Command = "script-message mpv.net show-history"; + break; + } + } } public class CursorHelp diff --git a/mpv.net/mp.cs b/mpv.net/mp.cs index 4b5be15..bb8f21e 100644 --- a/mpv.net/mp.cs +++ b/mpv.net/mp.cs @@ -74,9 +74,11 @@ namespace mpvnet { string portableFolder = Application.StartupPath + "\\portable_config\\"; string appdataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\mpv\\"; + string startupFolder = Application.StartupPath + "\\"; if (!Directory.Exists(appdataFolder) && !Directory.Exists(portableFolder) && - Sys.IsDirectoryWritable(Application.StartupPath)) + Sys.IsDirectoryWritable(Application.StartupPath) && + !File.Exists(startupFolder + "mpv.conf")) { using (TaskDialog td = new TaskDialog()) { @@ -84,15 +86,17 @@ namespace mpvnet td.Content = "[MPV documentation about files on Windows.](https://mpv.io/manual/master/#files-on-windows)"; td.AddCommandLink("appdata", appdataFolder, appdataFolder); td.AddCommandLink("portable", portableFolder, portableFolder); + td.AddCommandLink("startup", startupFolder, startupFolder); td.AllowCancel = false; _MpvConfFolder = td.Show(); } } - else - if (Directory.Exists(portableFolder)) - _MpvConfFolder = portableFolder; - else - _MpvConfFolder = appdataFolder; + else if (Directory.Exists(portableFolder)) + _MpvConfFolder = portableFolder; + else if (Directory.Exists(appdataFolder)) + _MpvConfFolder = appdataFolder; + else if (File.Exists(Application.StartupPath + "\\mpv.conf")) + _MpvConfFolder = Application.StartupPath + "\\"; if (string.IsNullOrEmpty(_MpvConfFolder)) _MpvConfFolder = appdataFolder; if (!Directory.Exists(_MpvConfFolder)) Directory.CreateDirectory(_MpvConfFolder);