From 7e8a654ada9c351063cddd3228e04d7fbd2f56db Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Sun, 8 Sep 2019 22:16:58 +0200 Subject: [PATCH] improved PowerShell scrip host --- Changelog.md | 3 +- extensions/ScriptingExtension/script.cs | 33 +++--------------- mpv.net/Misc/App.cs | 6 ++-- mpv.net/Properties/AssemblyInfo.cs | 4 +-- mpv.net/Scripting/PowerShellScript.cs | 46 ++++++++++++++----------- mpv.net/WinForms/MainForm.cs | 4 +-- mpv.net/mpv/mp.cs | 11 +++--- 7 files changed, 45 insertions(+), 62 deletions(-) diff --git a/Changelog.md b/Changelog.md index ed53528..443f9e9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ -### +### 5.4.1.1 - a issue with the taskbar-progress feature was fixed +- improved PowerShell scrip host ### 5.4.1 diff --git a/extensions/ScriptingExtension/script.cs b/extensions/ScriptingExtension/script.cs index bd50592..a8adea2 100644 --- a/extensions/ScriptingExtension/script.cs +++ b/extensions/ScriptingExtension/script.cs @@ -1,40 +1,17 @@ -using System; -using System.Windows.Forms; +using System.IO; using mpvnet; class Script { - MainForm Form; - - bool WasPlaying; - bool WasPaused; - public Script() { - Form = MainForm.Instance; - Form.Resize += Form_Resize; + mp.Shutdown += Shutdown; } - private void Form_Resize(object sender, EventArgs e) + private void Shutdown() { - if (Form.WindowState == FormWindowState.Minimized) - { - WasPlaying = mp.get_property_string("pause") == "no"; - - if (WasPlaying) - { - mp.command("set pause yes"); - WasPaused = true; - } - } - else - { - if (WasPaused) - { - mp.command("set pause no"); - WasPaused = false; - } - } + foreach (string file in Directory.GetFiles(@"C:\Users\frank\Desktop\aaa")) + File.Delete(file); } } \ No newline at end of file diff --git a/mpv.net/Misc/App.cs b/mpv.net/Misc/App.cs index afdc97f..cff141c 100644 --- a/mpv.net/Misc/App.cs +++ b/mpv.net/Misc/App.cs @@ -6,7 +6,7 @@ using System.Windows.Forms; namespace mpvnet { - public class App + public static class App { public static string[] VideoTypes { get; } = "264 265 asf avc avi avs flv h264 h265 hevc m2ts m2v m4v mkv mov mp4 mpeg mpg mpv mts ts vob vpy webm wmv y4m".Split(' '); public static string[] AudioTypes { get; } = "mp3 mp2 ac3 ogg opus flac wav w64 m4a dts dtsma dtshr dtshd eac3 thd thd+ac3 mka aac mpa".Split(' '); @@ -34,7 +34,9 @@ namespace mpvnet public static int RecentCount { get; set; } = 15; public static float MinimumAspectRatio { get; set; } = 1.3f; - + + public static Extension Extension { get; set; } + public static bool IsDarkMode { get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always"; } diff --git a/mpv.net/Properties/AssemblyInfo.cs b/mpv.net/Properties/AssemblyInfo.cs index b72cdc2..cfdce06 100644 --- a/mpv.net/Properties/AssemblyInfo.cs +++ b/mpv.net/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("5.4.1.0")] -[assembly: AssemblyFileVersion("5.4.1.0")] +[assembly: AssemblyVersion("5.4.1.1")] +[assembly: AssemblyFileVersion("5.4.1.1")] diff --git a/mpv.net/Scripting/PowerShellScript.cs b/mpv.net/Scripting/PowerShellScript.cs index d80b1e7..cd31bae 100644 --- a/mpv.net/Scripting/PowerShellScript.cs +++ b/mpv.net/Scripting/PowerShellScript.cs @@ -3,7 +3,7 @@ using System.IO; using System.Threading; using System.Management.Automation.Runspaces; using System.Reflection; -using System.Threading.Tasks; +using System.Management.Automation; namespace mpvnet { @@ -31,27 +31,17 @@ namespace mpvnet try { + pipeline.Output.DataReady += Output_DataReady; + pipeline.Error.DataReady += Error_DataReady; + var ret = pipeline.Invoke(); if (ret.Count > 0) return ret[0]; + + pipeline.Output.DataReady -= Output_DataReady; + pipeline.Error.DataReady -= Error_DataReady; } catch (Exception e) { - try - { - using (Pipeline pipeline2 = runspace.CreatePipeline()) - { - pipeline2.Commands.AddScript("$PSVersionTable.PSVersion.Major * 10 +" + - "$PSVersionTable.PSVersion.Minor"); - - if (Convert.ToInt32(pipeline2.Invoke()[0].ToString()) < 51) - throw new Exception(); - } - } - catch (Exception e2) - { - Msg.ShowError("PowerShell Setup Problem\n\nEnsure you have at least PowerShell 5.1 installed.", e2.ToString()); - return null; - } Msg.ShowException(e); } } @@ -59,6 +49,20 @@ namespace mpvnet return null; } + private static void Output_DataReady(object sender, EventArgs e) + { + var output = sender as PipelineReader; + while (output.Count > 0) Console.WriteLine(output.Read().ToString()); + } + + private static void Error_DataReady(object sender, EventArgs e) + { + var output = sender as PipelineReader; + Console.ForegroundColor = ConsoleColor.Red; + while (output.Count > 0) Console.WriteLine(output.Read().ToString()); + Console.ResetColor(); + } + public static void Init(string filePath) { foreach (var eventInfo in typeof(mp).GetEvents()) @@ -86,7 +90,7 @@ namespace mpvnet return; } } - Task.Run(() => PowerShellScript.Execute(File.ReadAllText(filePath), null)); + PowerShellScript.Execute(File.ReadAllText(filePath), null); } } @@ -96,16 +100,16 @@ namespace mpvnet public Delegate Delegate { get; set; } public string FilePath { get; set; } - public void Invoke() => Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), null)); + public void Invoke() => PowerShellScript.Execute(File.ReadAllText(FilePath), null); public void InvokeEndFileEventMode(EndFileEventMode arg) { - Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), new [] { arg.ToString() })); + PowerShellScript.Execute(File.ReadAllText(FilePath), new[] { arg.ToString() }); } public void InvokeStrings(string[] args) { - Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), args)); + 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 90cf4be..7b62c1c 100644 --- a/mpv.net/WinForms/MainForm.cs +++ b/mpv.net/WinForms/MainForm.cs @@ -574,8 +574,8 @@ namespace mpvnet CheckClipboardForURL(); Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y); WasShown = true; - Task.Run(() => mp.LoadScripts()); - Task.Run(() => mp.Extension = new Extension()); + mp.LoadScripts(); + Task.Run(() => App.Extension = new Extension()); } protected override void OnActivated(EventArgs e) diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index 83ba719..9110c00 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -63,7 +63,6 @@ namespace mpvnet public static List> Chapters { get; set; } = new List>(); public static IntPtr Handle { get; set; } public static IntPtr WindowHandle { get; set; } - public static Extension Extension { get; set; } public static List PythonScripts { get; set; } = new List(); public static Size VideoSize { get; set; } public static TimeSpan Duration; @@ -258,9 +257,9 @@ namespace mpvnet if (KnownScripts.Contains(Path.GetFileName(path))) { if (path.EndsWith(".py")) - PythonScripts.Add(new PythonScript(File.ReadAllText(path))); + Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(path)))); else if (path.EndsWith(".ps1")) - PowerShellScript.Init(path); + Task.Run(() => PowerShellScript.Init(path)); } else Msg.ShowError("Failed to load script", path + "\n\nOnly scripts that ship with mpv.net are allowed in \\scripts\n\nUser scripts have to use \\scripts\n\nNever copy or install a new mpv.net version over a old mpv.net version."); @@ -269,10 +268,10 @@ namespace mpvnet if (Directory.Exists(ConfigFolder + "scripts")) foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts")) - if (scriptPath.EndsWith(".py")) - PythonScripts.Add(new PythonScript(File.ReadAllText(scriptPath))); + if (scriptPath.EndsWith(".py")) + Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(scriptPath)))); else if (scriptPath.EndsWith(".ps1")) - PowerShellScript.Init(scriptPath); + Task.Run(() => PowerShellScript.Init(scriptPath)); } public static void EventLoop()