improved PowerShell scrip host

This commit is contained in:
Frank Skare
2019-09-08 22:16:58 +02:00
parent c2c33228a0
commit 7e8a654ada
7 changed files with 45 additions and 62 deletions

View File

@@ -1,6 +1,7 @@
### ### 5.4.1.1
- a issue with the taskbar-progress feature was fixed - a issue with the taskbar-progress feature was fixed
- improved PowerShell scrip host
### 5.4.1 ### 5.4.1

View File

@@ -1,40 +1,17 @@
using System; using System.IO;
using System.Windows.Forms;
using mpvnet; using mpvnet;
class Script class Script
{ {
MainForm Form;
bool WasPlaying;
bool WasPaused;
public Script() public Script()
{ {
Form = MainForm.Instance; mp.Shutdown += Shutdown;
Form.Resize += Form_Resize;
} }
private void Form_Resize(object sender, EventArgs e) private void Shutdown()
{ {
if (Form.WindowState == FormWindowState.Minimized) foreach (string file in Directory.GetFiles(@"C:\Users\frank\Desktop\aaa"))
{ File.Delete(file);
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;
}
}
} }
} }

View File

@@ -6,7 +6,7 @@ using System.Windows.Forms;
namespace mpvnet 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[] 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(' '); 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 int RecentCount { get; set; } = 15;
public static float MinimumAspectRatio { get; set; } = 1.3f; public static float MinimumAspectRatio { get; set; } = 1.3f;
public static Extension Extension { get; set; }
public static bool IsDarkMode { public static bool IsDarkMode {
get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always"; get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always";
} }

View File

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("5.4.1.0")] [assembly: AssemblyVersion("5.4.1.1")]
[assembly: AssemblyFileVersion("5.4.1.0")] [assembly: AssemblyFileVersion("5.4.1.1")]

View File

@@ -3,7 +3,7 @@ using System.IO;
using System.Threading; using System.Threading;
using System.Management.Automation.Runspaces; using System.Management.Automation.Runspaces;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Management.Automation;
namespace mpvnet namespace mpvnet
{ {
@@ -31,27 +31,17 @@ namespace mpvnet
try try
{ {
pipeline.Output.DataReady += Output_DataReady;
pipeline.Error.DataReady += Error_DataReady;
var ret = pipeline.Invoke(); var ret = pipeline.Invoke();
if (ret.Count > 0) return ret[0]; if (ret.Count > 0) return ret[0];
pipeline.Output.DataReady -= Output_DataReady;
pipeline.Error.DataReady -= Error_DataReady;
} }
catch (Exception e) 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); Msg.ShowException(e);
} }
} }
@@ -59,6 +49,20 @@ namespace mpvnet
return null; return null;
} }
private static void Output_DataReady(object sender, EventArgs e)
{
var output = sender as PipelineReader<PSObject>;
while (output.Count > 0) Console.WriteLine(output.Read().ToString());
}
private static void Error_DataReady(object sender, EventArgs e)
{
var output = sender as PipelineReader<Object>;
Console.ForegroundColor = ConsoleColor.Red;
while (output.Count > 0) Console.WriteLine(output.Read().ToString());
Console.ResetColor();
}
public static void Init(string filePath) public static void Init(string filePath)
{ {
foreach (var eventInfo in typeof(mp).GetEvents()) foreach (var eventInfo in typeof(mp).GetEvents())
@@ -86,7 +90,7 @@ namespace mpvnet
return; 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 Delegate Delegate { get; set; }
public string FilePath { 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) 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) public void InvokeStrings(string[] args)
{ {
Task.Run(() => PowerShellScript.Execute(File.ReadAllText(FilePath), args)); PowerShellScript.Execute(File.ReadAllText(FilePath), args);
} }
} }
} }

View File

@@ -574,8 +574,8 @@ namespace mpvnet
CheckClipboardForURL(); CheckClipboardForURL();
Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y); Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
WasShown = true; WasShown = true;
Task.Run(() => mp.LoadScripts()); mp.LoadScripts();
Task.Run(() => mp.Extension = new Extension()); Task.Run(() => App.Extension = new Extension());
} }
protected override void OnActivated(EventArgs e) protected override void OnActivated(EventArgs e)

View File

@@ -63,7 +63,6 @@ namespace mpvnet
public static List<KeyValuePair<string, double>> Chapters { get; set; } = new List<KeyValuePair<string, double>>(); public static List<KeyValuePair<string, double>> Chapters { get; set; } = new List<KeyValuePair<string, double>>();
public static IntPtr Handle { get; set; } public static IntPtr Handle { get; set; }
public static IntPtr WindowHandle { get; set; } public static IntPtr WindowHandle { get; set; }
public static Extension Extension { get; set; }
public static List<PythonScript> PythonScripts { get; set; } = new List<PythonScript>(); public static List<PythonScript> PythonScripts { get; set; } = new List<PythonScript>();
public static Size VideoSize { get; set; } public static Size VideoSize { get; set; }
public static TimeSpan Duration; public static TimeSpan Duration;
@@ -258,9 +257,9 @@ namespace mpvnet
if (KnownScripts.Contains(Path.GetFileName(path))) if (KnownScripts.Contains(Path.GetFileName(path)))
{ {
if (path.EndsWith(".py")) 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")) else if (path.EndsWith(".ps1"))
PowerShellScript.Init(path); Task.Run(() => PowerShellScript.Init(path));
} }
else else
Msg.ShowError("Failed to load script", path + "\n\nOnly scripts that ship with mpv.net are allowed in <startup>\\scripts\n\nUser scripts have to use <config folder>\\scripts\n\nNever copy or install a new mpv.net version over a old mpv.net version."); Msg.ShowError("Failed to load script", path + "\n\nOnly scripts that ship with mpv.net are allowed in <startup>\\scripts\n\nUser scripts have to use <config folder>\\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")) if (Directory.Exists(ConfigFolder + "scripts"))
foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts")) foreach (string scriptPath in Directory.GetFiles(ConfigFolder + "scripts"))
if (scriptPath.EndsWith(".py")) if (scriptPath.EndsWith(".py"))
PythonScripts.Add(new PythonScript(File.ReadAllText(scriptPath))); Task.Run(() => PythonScripts.Add(new PythonScript(File.ReadAllText(scriptPath))));
else if (scriptPath.EndsWith(".ps1")) else if (scriptPath.EndsWith(".ps1"))
PowerShellScript.Init(scriptPath); Task.Run(() => PowerShellScript.Init(scriptPath));
} }
public static void EventLoop() public static void EventLoop()