diff --git a/Changelog.md b/Changelog.md index cccf3fd..d52942c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,8 @@ - when single process queue is used the folder is no longer loaded - the playlist is never cleared whenever the control key is down but files and URLs are appended instead +- 2 powershell script hosting bugs were fixed and a new powershell example script + was added to the [scripting wiki page](https://github.com/stax76/mpv.net/wiki/Scripting#powershell) ### 4.3.1 diff --git a/addons/RatingAddon/RatingAddon.cs b/addons/RatingAddon/RatingAddon.cs index 0d2aca8..bb415fd 100644 --- a/addons/RatingAddon/RatingAddon.cs +++ b/addons/RatingAddon/RatingAddon.cs @@ -46,9 +46,11 @@ namespace RatingAddon void mpv_ClientMessage(string[] args) { - int rating; + if (args == null || + args.Length != 2 || + args[0] != "rate-file" || + !int.TryParse(args[1], out int rating)) - if (args?.Length != 2 || args[0] != "rate-file" || ! int.TryParse(args[1], out rating)) return; Dic[mp.get_property_string("path")] = rating; diff --git a/mpv.net/Misc/Command.cs b/mpv.net/Misc/Command.cs index 2b6dfb3..fa798e3 100644 --- a/mpv.net/Misc/Command.cs +++ b/mpv.net/Misc/Command.cs @@ -32,10 +32,15 @@ namespace mpvnet { ParameterInfo[] parameters = i.GetParameters(); - if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(string[])) + if (parameters == null || + parameters.Length != 1 || + parameters[0].ParameterType != typeof(string[])) continue; - Command cmd = new Command() { Name = i.Name.Replace("_","-"), Action = (Action)i.CreateDelegate(typeof(Action)) }; + Command cmd = new Command() { + Name = i.Name.Replace("_", "-"), + Action = (Action)i.CreateDelegate(typeof(Action)) }; + commands.Add(cmd); } } @@ -45,10 +50,30 @@ namespace mpvnet public static void open_files(string[] args) { + bool append = Control.ModifierKeys.HasFlag(Keys.Control); + bool loadFolder = true; + + foreach (string arg in args) + { + if (arg == "append") append = true; + if (arg == "no-folder") loadFolder = false; + } + MainForm.Instance.Invoke(new Action(() => { using (var d = new OpenFileDialog() { Multiselect = true }) if (d.ShowDialog() == DialogResult.OK) - mp.Load(d.FileNames, true, Control.ModifierKeys.HasFlag(Keys.Control)); + mp.Load(d.FileNames, loadFolder, append); + })); + } + + // deprecated in 2019 + public static void add_files_to_playlist(string[] args) + { + MainForm.Instance.Invoke(new Action(() => { + using (var d = new OpenFileDialog() { Multiselect = true }) + if (d.ShowDialog() == DialogResult.OK) + foreach (string file in d.FileNames) + mp.commandv("loadfile", file, "append"); })); } @@ -228,16 +253,6 @@ namespace mpvnet })); } - public static void add_files_to_playlist(string[] args) - { - MainForm.Instance.Invoke(new Action(() => { - using (var d = new OpenFileDialog() { Multiselect = true }) - if (d.ShowDialog() == DialogResult.OK) - foreach(string file in d.FileNames) - mp.commandv("loadfile", file, "append"); - })); - } - public static void cycle_audio(string[] args) { string filePath = mp.get_property_string("path", false); diff --git a/mpv.net/Native/Native.cs b/mpv.net/Native/Native.cs index a6e04af..4939619 100644 --- a/mpv.net/Native/Native.cs +++ b/mpv.net/Native/Native.cs @@ -27,9 +27,6 @@ namespace mpvnet [DllImport("user32.dll", SetLastError = true)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); - [DllImport("user32.dll")] - public static extern bool EnableWindow(IntPtr hWnd, bool bEnable); - [DllImport("user32.dll", EntryPoint = "GetWindowLong")] private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex); diff --git a/mpv.net/Resources/inputConf.txt b/mpv.net/Resources/inputConf.txt index 8bcee34..74148fa 100644 --- a/mpv.net/Resources/inputConf.txt +++ b/mpv.net/Resources/inputConf.txt @@ -30,7 +30,7 @@ Alt+a script-message mpv.net load-audio #menu: Open > Load external audio files... Alt+s script-message mpv.net load-sub #menu: Open > Load external subtitle files... _ ignore #menu: Open > - - _ script-message mpv.net add-files-to-playlist #menu: Open > Add files to playlist... + _ script-message mpv.net open-files append #menu: Open > Add files to playlist... Ctrl+S script-message mpv.net show-media-search #menu: Open > Show media search... _ ignore #menu: Open > - _ ignore #menu: Open > Recent diff --git a/mpv.net/Scripting/PowerShellScript.cs b/mpv.net/Scripting/PowerShellScript.cs index da6a382..7e6cee0 100644 --- a/mpv.net/Scripting/PowerShellScript.cs +++ b/mpv.net/Scripting/PowerShellScript.cs @@ -14,26 +14,27 @@ namespace mpvnet using (Runspace runspace = RunspaceFactory.CreateRunspace()) { runspace.ApartmentState = ApartmentState.STA; - runspace.ThreadOptions = PSThreadOptions.UseCurrentThread; runspace.Open(); using (Pipeline pipeline = runspace.CreatePipeline()) { pipeline.Commands.AddScript( -@"Using namespace mpvnet; -Using namespace System; -[System.Reflection.Assembly]::LoadWithPartialName(""mpvnet"")"); + "Using namespace mpvnet\n" + + "Using namespace System\n" + + "[System.Reflection.Assembly]::LoadWithPartialName(\"mpvnet\")\n"); pipeline.Commands.AddScript(code); + if (parameters != null) + foreach (var i in parameters) + pipeline.Commands[1].Parameters.Add(null, i); + try { - var ret = pipeline.Invoke(parameters); - - if (ret.Count > 0) - return ret[0]; + var ret = pipeline.Invoke(); + if (ret.Count > 0) return ret[0]; } - catch (Exception ex) + catch (Exception e) { try { @@ -46,12 +47,12 @@ Using namespace System; throw new Exception(); } } - catch (Exception ex2) + catch (Exception e2) { - Msg.ShowError("PowerShell Setup Problem\n\nEnsure you have at least PowerShell 5.1 installed.", ex2.ToString()); + Msg.ShowError("PowerShell Setup Problem\n\nEnsure you have at least PowerShell 5.1 installed.", e2.ToString()); return null; } - Msg.ShowException(ex); + Msg.ShowException(e); } } } @@ -70,17 +71,11 @@ Using namespace System; eventObject.FilePath = filePath; if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.Invoke)); - } else if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeEndFileEventMode)); - } else if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeStrings)); - } else throw new Exception(); @@ -91,10 +86,7 @@ Using namespace System; return; } } - Task.Run(() => - { - PowerShellScript.Execute(File.ReadAllText(filePath), new string[] {}); - }); + Task.Run(() => PowerShellScript.Execute(File.ReadAllText(filePath), null)); } } @@ -104,24 +96,16 @@ Using namespace System; public Delegate Delegate { get; set; } public string FilePath { get; set; } - public void Invoke() - { - Task.Run(() => { PowerShellScript.Execute(File.ReadAllText(FilePath), new string[] { }); }); - } + public void Invoke() => Task.Run(() => { PowerShellScript.Execute(File.ReadAllText(FilePath), null); }); public void InvokeEndFileEventMode(EndFileEventMode arg) { - Task.Run(() => - { - PowerShellScript.Execute(File.ReadAllText(FilePath), new string[] { arg.ToString() }); - }); + Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), new [] { arg.ToString() })); } public void InvokeStrings(string[] args) { - Task.Run(() => { - PowerShellScript.Execute(File.ReadAllText(FilePath), args); - }); + Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), args)); } } } \ No newline at end of file diff --git a/mpv.net/WinForms/MainForm.cs b/mpv.net/WinForms/MainForm.cs index eea3802..cd73c0b 100644 --- a/mpv.net/WinForms/MainForm.cs +++ b/mpv.net/WinForms/MainForm.cs @@ -515,6 +515,8 @@ namespace mpvnet { base.OnActivated(e); CheckUrlInClipboard(); + Message m = new Message() { Msg = 0x0202 }; // WM_LBUTTONUP + Native.SendMessage(Handle, m.Msg, m.WParam, m.LParam); } void CheckUrlInClipboard() diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index f197a9d..285ca5c 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -193,10 +193,7 @@ namespace mpvnet mpv_event evt = (mpv_event)Marshal.PtrToStructure(ptr, typeof(mpv_event)); if (WindowHandle == IntPtr.Zero) - { WindowHandle = FindWindowEx(MainForm.Hwnd, IntPtr.Zero, "mpv", null); - //Native.EnableWindow(WindowHandle, true); - } //Debug.WriteLine(evt.event_id.ToString()); @@ -270,10 +267,6 @@ namespace mpvnet { found = true; i.Action.Invoke(args.Skip(2).ToArray()); - MainForm.Instance.BeginInvoke(new Action(() => { - Message m = new Message() { Msg = 0x0202 }; // WM_LBUTTONUP - Native.SendMessage(MainForm.Instance.Handle, m.Msg, m.WParam, m.LParam); - })); } } if (!found) @@ -368,17 +361,11 @@ namespace mpvnet MethodInfo mi; if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.Invoke)); - } else if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeEndFileEventMode)); - } else if (eventInfo.EventHandlerType == typeof(Action)) - { mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeStrings)); - } else throw new Exception(); @@ -400,28 +387,19 @@ namespace mpvnet public static void commandv(params string[] args) { - if (Handle == IntPtr.Zero) - return; - + if (Handle == IntPtr.Zero) return; IntPtr mainPtr = AllocateUtf8IntPtrArrayWithSentinel(args, out IntPtr[] byteArrayPointers); int err = mpv_command(Handle, mainPtr); - - if (err < 0) - throw new Exception($"{(mpv_error)err}"); - + if (err < 0) throw new Exception($"{(mpv_error)err}"); foreach (var ptr in byteArrayPointers) Marshal.FreeHGlobal(ptr); - Marshal.FreeHGlobal(mainPtr); } public static void command_string(string command, bool throwException = false) { - if (Handle == IntPtr.Zero) - return; - + if (Handle == IntPtr.Zero) return; int err = mpv_command_string(Handle, command); - if (err < 0 && throwException) throw new Exception($"{(mpv_error)err}\r\n\r\n" + command); } @@ -430,7 +408,6 @@ namespace mpvnet { byte[] bytes = GetUtf8Bytes(value); int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes); - if (err < 0 && throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); } @@ -574,7 +551,7 @@ namespace mpvnet if (loadFolder && !append) Task.Run(() => LoadFolder()); // user reported race condition } - static void LoadFolder() + public static void LoadFolder() { Thread.Sleep(50); // user reported race condition string path = get_property_string("path");