This commit is contained in:
Frank Skare
2019-06-25 05:14:58 +02:00
parent ba0141cfb1
commit 3d325d4fe5
8 changed files with 58 additions and 79 deletions

View File

@@ -7,6 +7,8 @@
- when single process queue is used the folder is no longer loaded - when single process queue is used the folder is no longer loaded
- the playlist is never cleared whenever the control key is down but - the playlist is never cleared whenever the control key is down but
files and URLs are appended instead 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 ### 4.3.1

View File

@@ -46,9 +46,11 @@ namespace RatingAddon
void mpv_ClientMessage(string[] args) 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; return;
Dic[mp.get_property_string("path")] = rating; Dic[mp.get_property_string("path")] = rating;

View File

@@ -32,10 +32,15 @@ namespace mpvnet
{ {
ParameterInfo[] parameters = i.GetParameters(); 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; continue;
Command cmd = new Command() { Name = i.Name.Replace("_","-"), Action = (Action<string[]>)i.CreateDelegate(typeof(Action<string[]>)) }; Command cmd = new Command() {
Name = i.Name.Replace("_", "-"),
Action = (Action<string[]>)i.CreateDelegate(typeof(Action<string[]>)) };
commands.Add(cmd); commands.Add(cmd);
} }
} }
@@ -45,10 +50,30 @@ namespace mpvnet
public static void open_files(string[] args) 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(() => { MainForm.Instance.Invoke(new Action(() => {
using (var d = new OpenFileDialog() { Multiselect = true }) using (var d = new OpenFileDialog() { Multiselect = true })
if (d.ShowDialog() == DialogResult.OK) 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) public static void cycle_audio(string[] args)
{ {
string filePath = mp.get_property_string("path", false); string filePath = mp.get_property_string("path", false);

View File

@@ -27,9 +27,6 @@ namespace mpvnet
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); 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")] [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex); private static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

View File

@@ -30,7 +30,7 @@
Alt+a script-message mpv.net load-audio #menu: Open > Load external audio files... 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... Alt+s script-message mpv.net load-sub #menu: Open > Load external subtitle files...
_ ignore #menu: Open > - _ 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... Ctrl+S script-message mpv.net show-media-search #menu: Open > Show media search...
_ ignore #menu: Open > - _ ignore #menu: Open > -
_ ignore #menu: Open > Recent _ ignore #menu: Open > Recent

View File

@@ -14,26 +14,27 @@ namespace mpvnet
using (Runspace runspace = RunspaceFactory.CreateRunspace()) using (Runspace runspace = RunspaceFactory.CreateRunspace())
{ {
runspace.ApartmentState = ApartmentState.STA; runspace.ApartmentState = ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open(); runspace.Open();
using (Pipeline pipeline = runspace.CreatePipeline()) using (Pipeline pipeline = runspace.CreatePipeline())
{ {
pipeline.Commands.AddScript( pipeline.Commands.AddScript(
@"Using namespace mpvnet; "Using namespace mpvnet\n" +
Using namespace System; "Using namespace System\n" +
[System.Reflection.Assembly]::LoadWithPartialName(""mpvnet"")"); "[System.Reflection.Assembly]::LoadWithPartialName(\"mpvnet\")\n");
pipeline.Commands.AddScript(code); pipeline.Commands.AddScript(code);
if (parameters != null)
foreach (var i in parameters)
pipeline.Commands[1].Parameters.Add(null, i);
try try
{ {
var ret = pipeline.Invoke(parameters); var ret = pipeline.Invoke();
if (ret.Count > 0) return ret[0];
if (ret.Count > 0)
return ret[0];
} }
catch (Exception ex) catch (Exception e)
{ {
try try
{ {
@@ -46,12 +47,12 @@ Using namespace System;
throw new Exception(); 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; return null;
} }
Msg.ShowException(ex); Msg.ShowException(e);
} }
} }
} }
@@ -70,17 +71,11 @@ Using namespace System;
eventObject.FilePath = filePath; eventObject.FilePath = filePath;
if (eventInfo.EventHandlerType == typeof(Action)) if (eventInfo.EventHandlerType == typeof(Action))
{
mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.Invoke)); mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.Invoke));
}
else if (eventInfo.EventHandlerType == typeof(Action<EndFileEventMode>)) else if (eventInfo.EventHandlerType == typeof(Action<EndFileEventMode>))
{
mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeEndFileEventMode)); mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeEndFileEventMode));
}
else if (eventInfo.EventHandlerType == typeof(Action<string[]>)) else if (eventInfo.EventHandlerType == typeof(Action<string[]>))
{
mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeStrings)); mi = eventObject.GetType().GetMethod(nameof(PowerShellEventObject.InvokeStrings));
}
else else
throw new Exception(); throw new Exception();
@@ -91,10 +86,7 @@ Using namespace System;
return; return;
} }
} }
Task.Run(() => Task.Run(() => PowerShellScript.Execute(File.ReadAllText(filePath), null));
{
PowerShellScript.Execute(File.ReadAllText(filePath), new string[] {});
});
} }
} }
@@ -104,24 +96,16 @@ Using namespace System;
public Delegate Delegate { get; set; } public Delegate Delegate { get; set; }
public string FilePath { get; set; } public string FilePath { get; set; }
public void Invoke() public void Invoke() => Task.Run(() => { PowerShellScript.Execute(File.ReadAllText(FilePath), null); });
{
Task.Run(() => { PowerShellScript.Execute(File.ReadAllText(FilePath), new string[] { }); });
}
public void InvokeEndFileEventMode(EndFileEventMode arg) public void InvokeEndFileEventMode(EndFileEventMode arg)
{ {
Task.Run(() => Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), new [] { arg.ToString() }));
{
PowerShellScript.Execute(File.ReadAllText(FilePath), new string[] { arg.ToString() });
});
} }
public void InvokeStrings(string[] args) public void InvokeStrings(string[] args)
{ {
Task.Run(() => { Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), args));
PowerShellScript.Execute(File.ReadAllText(FilePath), args);
});
} }
} }
} }

View File

@@ -515,6 +515,8 @@ namespace mpvnet
{ {
base.OnActivated(e); base.OnActivated(e);
CheckUrlInClipboard(); CheckUrlInClipboard();
Message m = new Message() { Msg = 0x0202 }; // WM_LBUTTONUP
Native.SendMessage(Handle, m.Msg, m.WParam, m.LParam);
} }
void CheckUrlInClipboard() void CheckUrlInClipboard()

View File

@@ -193,10 +193,7 @@ namespace mpvnet
mpv_event evt = (mpv_event)Marshal.PtrToStructure(ptr, typeof(mpv_event)); mpv_event evt = (mpv_event)Marshal.PtrToStructure(ptr, typeof(mpv_event));
if (WindowHandle == IntPtr.Zero) if (WindowHandle == IntPtr.Zero)
{
WindowHandle = FindWindowEx(MainForm.Hwnd, IntPtr.Zero, "mpv", null); WindowHandle = FindWindowEx(MainForm.Hwnd, IntPtr.Zero, "mpv", null);
//Native.EnableWindow(WindowHandle, true);
}
//Debug.WriteLine(evt.event_id.ToString()); //Debug.WriteLine(evt.event_id.ToString());
@@ -270,10 +267,6 @@ namespace mpvnet
{ {
found = true; found = true;
i.Action.Invoke(args.Skip(2).ToArray()); 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) if (!found)
@@ -368,17 +361,11 @@ namespace mpvnet
MethodInfo mi; MethodInfo mi;
if (eventInfo.EventHandlerType == typeof(Action)) if (eventInfo.EventHandlerType == typeof(Action))
{
mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.Invoke)); mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.Invoke));
}
else if (eventInfo.EventHandlerType == typeof(Action<EndFileEventMode>)) else if (eventInfo.EventHandlerType == typeof(Action<EndFileEventMode>))
{
mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeEndFileEventMode)); mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeEndFileEventMode));
}
else if (eventInfo.EventHandlerType == typeof(Action<string[]>)) else if (eventInfo.EventHandlerType == typeof(Action<string[]>))
{
mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeStrings)); mi = eventObject.GetType().GetMethod(nameof(PythonEventObject.InvokeStrings));
}
else else
throw new Exception(); throw new Exception();
@@ -400,28 +387,19 @@ namespace mpvnet
public static void commandv(params string[] args) public static void commandv(params string[] args)
{ {
if (Handle == IntPtr.Zero) if (Handle == IntPtr.Zero) return;
return;
IntPtr mainPtr = AllocateUtf8IntPtrArrayWithSentinel(args, out IntPtr[] byteArrayPointers); IntPtr mainPtr = AllocateUtf8IntPtrArrayWithSentinel(args, out IntPtr[] byteArrayPointers);
int err = mpv_command(Handle, mainPtr); 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) foreach (var ptr in byteArrayPointers)
Marshal.FreeHGlobal(ptr); Marshal.FreeHGlobal(ptr);
Marshal.FreeHGlobal(mainPtr); Marshal.FreeHGlobal(mainPtr);
} }
public static void command_string(string command, bool throwException = false) public static void command_string(string command, bool throwException = false)
{ {
if (Handle == IntPtr.Zero) if (Handle == IntPtr.Zero) return;
return;
int err = mpv_command_string(Handle, command); int err = mpv_command_string(Handle, command);
if (err < 0 && throwException) if (err < 0 && throwException)
throw new Exception($"{(mpv_error)err}\r\n\r\n" + command); throw new Exception($"{(mpv_error)err}\r\n\r\n" + command);
} }
@@ -430,7 +408,6 @@ namespace mpvnet
{ {
byte[] bytes = GetUtf8Bytes(value); byte[] bytes = GetUtf8Bytes(value);
int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes); int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes);
if (err < 0 && throwOnException) if (err < 0 && throwOnException)
throw new Exception($"{name}: {(mpv_error)err}"); throw new Exception($"{name}: {(mpv_error)err}");
} }
@@ -574,7 +551,7 @@ namespace mpvnet
if (loadFolder && !append) Task.Run(() => LoadFolder()); // user reported race condition if (loadFolder && !append) Task.Run(() => LoadFolder()); // user reported race condition
} }
static void LoadFolder() public static void LoadFolder()
{ {
Thread.Sleep(50); // user reported race condition Thread.Sleep(50); // user reported race condition
string path = get_property_string("path"); string path = get_property_string("path");