new folder structure and new C# script host

This commit is contained in:
Frank Skare
2021-05-06 15:23:28 +02:00
parent 642c36bacf
commit 3583aa11ed
101 changed files with 96 additions and 244 deletions

216
src/Misc/App.cs Normal file
View File

@@ -0,0 +1,216 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Threading.Tasks;
using static mpvnet.Core;
namespace mpvnet
{
public static class App
{
public static string RegPath { get; } = @"HKCU\Software\" + Application.ProductName;
public static string ConfPath { get => core.ConfigFolder + "mpvnet.conf"; }
public static string ProcessInstance { get; set; } = "single";
public static string DarkMode { get; set; } = "always";
public static string DarkTheme { get; set; } = "dark";
public static string LightTheme { get; set; } = "light";
public static string StartSize { get; set; } = "previous";
public static bool RememberPosition { get; set; }
public static bool DebugMode { get; set; }
public static bool IsStartedFromTerminal { get; } = Environment.GetEnvironmentVariable("_started_from_console") == "yes";
public static bool RememberVolume { get; set; } = true;
public static bool AutoLoadFolder { get; set; } = true;
public static bool Queue { get; set; }
public static bool UpdateCheck { get; set; }
public static bool GlobalMediaKeys { get; set; }
public static int StartThreshold { get; set; } = 1500;
public static int RecentCount { get; set; } = 15;
public static float MinimumAspectRatio { get; set; } = 1.2f;
public static Extension Extension { get; set; }
public static bool IsDarkMode {
get => (DarkMode == "system" && Sys.IsDarkTheme) || DarkMode == "always";
}
public static void Init()
{
string dummy = core.ConfigFolder;
var dummy2 = core.Conf;
foreach (var i in Conf)
ProcessProperty(i.Key, i.Value, true);
if (DebugMode)
{
try
{
string filePath = core.ConfigFolder + "mpvnet-debug.log";
if (File.Exists(filePath))
File.Delete(filePath);
Trace.Listeners.Add(new TextWriterTraceListener(filePath));
Trace.AutoFlush = true;
//if (App.DebugMode)
// Trace.WriteLine("");
}
catch (Exception e)
{
Msg.ShowException(e);
}
}
string themeContent = null;
if (File.Exists(core.ConfigFolder + "theme.conf"))
themeContent = File.ReadAllText(core.ConfigFolder + "theme.conf");
Theme.Init(
themeContent,
Properties.Resources.theme,
IsDarkMode ? DarkTheme : LightTheme);
core.Shutdown += Shutdown;
core.Initialized += Initialized;
}
public static void RunTask(Action action)
{
Task.Run(() => {
try {
action.Invoke();
} catch (Exception e) {
ShowException(e);
}
});
}
public static string Version {
get {
return "Copyright (C) 2017-2021 mpv.net/mpv/mplayer\n" +
$"mpv.net {Application.ProductVersion} ({File.GetLastWriteTime(Application.ExecutablePath).ToShortDateString()})\n" +
$"{core.get_property_string("mpv-version")} ({File.GetLastWriteTime(Folder.Startup + "mpv-1.dll").ToShortDateString()})\nffmpeg {core.get_property_string("ffmpeg-version")}\nMIT License";
}
}
public static void ShowException(object obj)
{
if (obj is Exception e)
{
if (IsStartedFromTerminal)
ConsoleHelp.WriteError(e.ToString());
else
Msg.ShowException(e);
}
else
{
if (IsStartedFromTerminal)
ConsoleHelp.WriteError(obj.ToString());
else
Msg.ShowError(obj.ToString());
}
}
public static void ShowError(string title, string msg)
{
if (IsStartedFromTerminal)
{
ConsoleHelp.WriteError(title);
ConsoleHelp.WriteError(msg);
}
else
Msg.ShowError(title, msg);
}
static void Initialized()
{
if (RememberVolume)
{
core.set_property_int("volume", RegistryHelp.GetInt(RegPath, "Volume", 70));
core.set_property_string("mute", RegistryHelp.GetString(RegPath, "Mute", "no"));
}
}
static void Shutdown()
{
if (RememberVolume)
{
RegistryHelp.SetValue(RegPath, "Volume", core.get_property_int("volume"));
RegistryHelp.SetValue(RegPath, "Mute", core.get_property_string("mute"));
}
}
static Dictionary<string, string> _Conf;
public static Dictionary<string, string> Conf {
get {
if (_Conf == null)
{
_Conf = new Dictionary<string, string>();
if (File.Exists(ConfPath))
foreach (string i in File.ReadAllLines(ConfPath))
if (i.Contains("=") && !i.StartsWith("#"))
_Conf[i.Substring(0, i.IndexOf("=")).Trim()] = i.Substring(i.IndexOf("=") + 1).Trim();
}
return _Conf;
}
}
public static bool ProcessProperty(string name, string value, bool writeError = false)
{
switch (name)
{
case "global-media-keys": GlobalMediaKeys = value == "yes"; return true;
case "remember-position": RememberPosition = value == "yes"; return true;
case "debug-mode": DebugMode = value == "yes"; return true;
case "remember-volume": RememberVolume = value == "yes"; return true;
case "queue": Queue = value == "yes"; return true;
case "auto-load-folder": AutoLoadFolder = value == "yes"; return true;
case "update-check": UpdateCheck = value == "yes"; return true;
case "start-size": StartSize = value; return true;
case "process-instance": ProcessInstance = value; return true;
case "dark-mode": DarkMode = value; return true;
case "start-threshold": StartThreshold = value.ToInt(); return true;
case "recent-count": RecentCount = value.ToInt(); return true;
case "minimum-aspect-ratio": MinimumAspectRatio = value.ToFloat(); return true;
case "dark-theme": DarkTheme = value.Trim('\'', '"'); return true;
case "light-theme": LightTheme = value.Trim('\'', '"'); return true;
case "video-file-extensions": VideoTypes = value.Split(" ,;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); return true;
case "audio-file-extensions": AudioTypes = value.Split(" ,;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); return true;
case "image-file-extensions": ImageTypes = value.Split(" ,;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); return true;
default:
if (writeError)
ConsoleHelp.WriteError($"unknown mpvnet.conf property: {name}");
return false;
}
}
public static void ShowSetup()
{
int value = RegistryHelp.GetInt(RegistryHelp.ApplicationKey, Folder.Startup);
if (value != 1)
{
if (Msg.ShowQuestion("Would you like to setup mpv.net?",
"The setup allows to create a start menu shortcut, file associations and " +
"adding mpv.net to the Path environment variable.") == MsgResult.OK)
Commands.Execute("show-setup-dialog");
else
Msg.Show("The setup dialog can be found in the context menu at:\n\nTools > Setup");
RegistryHelp.SetValue(RegistryHelp.ApplicationKey, Folder.Startup, 1);
}
}
}
}

View File

@@ -0,0 +1,74 @@

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CSharp;
namespace mpvnet
{
class CSharpScriptHost
{
static List<object> References = new List<object>();
public static void ExecuteScriptsInFolder(string folder)
{
if (Directory.Exists(folder))
foreach (string file in Directory.GetFiles(folder, "*.cs"))
App.RunTask(() => Execute(file));
}
static void Execute(string file)
{
string code = File.ReadAllText(file);
string filename = Path.GetFileNameWithoutExtension(file) + " " + GetMD5(code) + ".dll";
string outputFile = Path.Combine(Path.GetTempPath(), filename);
if (!File.Exists(outputFile))
Compile(outputFile, file);
if (File.Exists(outputFile))
References.Add(Assembly.LoadFile(outputFile).CreateInstance("Script"));
}
public static void Compile(string outputFile, string file)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
string[] dependencies = {
"Microsoft.VisualBasic.dll",
"System.Core.dll", "System.Data.dll", "System.dll", "System.Drawing.dll", "System.Web.dll",
"System.Windows.Forms.dll", "System.Xaml.dll", "System.Xml.dll", "System.Xml.Linq.dll",
"WPF\\PresentationCore.dll", "WPF\\PresentationFramework.dll", "WPF\\WindowsBase.dll"
};
foreach (string i in dependencies)
parameters.ReferencedAssemblies.Add(i);
parameters.OutputAssembly = outputFile;
CompilerResults results = provider.CompileAssemblyFromFile(parameters, file);
var errors = results.Errors.Cast<CompilerError>().Select((i) => "Line Number " +
i.Line + "\r\n" + "Error Number: " + i.ErrorNumber + "\r\n" + i.ErrorText);
if (errors.Count() > 0)
ConsoleHelp.WriteError(string.Join("\r\n\r\n", errors), Path.GetFileName(file));
}
static string GetMD5(string code)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBuffer = Encoding.UTF8.GetBytes(code);
byte[] hashBuffer = md5.ComputeHash(inputBuffer);
return BitConverter.ToString(md5.ComputeHash(inputBuffer)).Replace("-", "");
}
}
}
}

390
src/Misc/Commands.cs Normal file
View File

@@ -0,0 +1,390 @@

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Windows;
using VB = Microsoft.VisualBasic;
using static mpvnet.NewLine;
using static mpvnet.Core;
using System.Threading.Tasks;
namespace mpvnet
{
public class Commands
{
public static void Execute(string id, string[] args = null)
{
switch (id)
{
case "add-files-to-playlist": OpenFiles("append"); break; // deprecated 2019
case "cycle-audio": CycleAudio(); break;
case "execute-mpv-command": ExecuteMpvCommand(); break;
case "load-audio": LoadAudio(); break;
case "load-sub": LoadSubtitle(); break;
case "manage-file-associations": // deprecated 2019
case "open-conf-folder": ProcessHelp.ShellExecute(core.ConfigFolder); break;
case "open-files": OpenFiles(args); break;
case "open-optical-media": Open_DVD_Or_BD_Folder(); break;
case "open-url": OpenURL(); break;
case "playlist-first": PlaylistFirst(); break;
case "playlist-last": PlaylistLast(); break;
case "scale-window": ScaleWindow(float.Parse(args[0], CultureInfo.InvariantCulture)); break;
case "shell-execute": ProcessHelp.ShellExecute(args[0]); break;
case "show-about": ShowDialog(typeof(AboutWindow)); break;
case "show-audio-devices": ShowTextWithEditor("audio-device-list", core.get_property_osd_string("audio-device-list")); break;
case "show-command-palette": ShowDialog(typeof(CommandPaletteWindow)); break;
case "show-commands": ShowCommands(); break;
case "show-conf-editor": ShowDialog(typeof(ConfWindow)); break;
case "show-decoders": ShowTextWithEditor("decoder-list", mpvHelp.GetDecoders()); break;
case "show-demuxers": ShowTextWithEditor("demuxer-lavf-list", mpvHelp.GetDemuxers()); break;
case "show-history": ShowHistory(); break;
case "show-info": ShowInfo(); break;
case "show-input-editor": ShowDialog(typeof(InputWindow)); break;
case "show-keys": ShowTextWithEditor("input-key-list", core.get_property_string("input-key-list").Replace(",", BR)); break;
case "show-media-search": ShowDialog(typeof(EverythingWindow)); break;
case "show-profiles": ShowTextWithEditor("profile-list", mpvHelp.GetProfiles()); break;
case "show-playlist": ShowPlaylist(); break;
case "show-properties": ShowProperties(); break;
case "show-protocols": ShowTextWithEditor("protocol-list", mpvHelp.GetProtocols()); break;
case "show-setup-dialog": ShowDialog(typeof(SetupWindow)); break;
case "show-text": ShowText(args[0], Convert.ToInt32(args[1]), Convert.ToInt32(args[2])); break;
case "update-check": UpdateCheck.CheckOnline(true); break;
default: Msg.ShowError($"No command '{id}' found."); break;
}
}
public static void InvokeOnMainThread(Action action) => MainForm.Instance.BeginInvoke(action);
public static void ShowDialog(Type winType)
{
InvokeOnMainThread(new Action(() => {
Window win = Activator.CreateInstance(winType) as Window;
new WindowInteropHelper(win).Owner = MainForm.Instance.Handle;
win.ShowDialog();
}));
}
public static void OpenFiles(params 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;
}
InvokeOnMainThread(new Action(() => {
using (var d = new OpenFileDialog() { Multiselect = true })
if (d.ShowDialog() == DialogResult.OK)
core.LoadFiles(d.FileNames, loadFolder, append);
}));
}
public static void Open_DVD_Or_BD_Folder()
{
InvokeOnMainThread(new Action(() => {
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = "Select a DVD or Blu-ray folder.";
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == DialogResult.OK)
{
core.command("stop");
Thread.Sleep(500);
if (Directory.Exists(dialog.SelectedPath + "\\BDMV"))
{
core.set_property_string("bluray-device", dialog.SelectedPath);
core.LoadFiles(new[] { @"bd://" }, false, false);
}
else
{
core.set_property_string("dvd-device", dialog.SelectedPath);
core.LoadFiles(new[] { @"dvd://" }, false, false);
}
}
}
}));
}
public static void PlaylistFirst()
{
int pos = core.get_property_int("playlist-pos");
if (pos != 0)
core.set_property_int("playlist-pos", 0);
}
public static void PlaylistLast()
{
int pos = core.get_property_int("playlist-pos");
int count = core.get_property_int("playlist-count");
if (pos < count - 1)
core.set_property_int("playlist-pos", count - 1);
}
public static void ShowHistory()
{
if (File.Exists(core.ConfigFolder + "history.txt"))
ProcessHelp.ShellExecute(core.ConfigFolder + "history.txt");
else
{
if (Msg.ShowQuestion("Create history.txt file in config folder?",
"mpv.net will write the date, time and filename of opened files to it.") == MsgResult.OK)
File.WriteAllText(core.ConfigFolder + "history.txt", "");
}
}
public static void ShowInfo()
{
try
{
string performer, title, album, genre, date, duration, text = "";
long fileSize = 0;
string path = core.get_property_string("path");
if (path.Contains("://"))
path = core.get_property_string("media-title");
int width = core.get_property_int("video-params/w");
int height = core.get_property_int("video-params/h");
if (File.Exists(path))
{
fileSize = new FileInfo(path).Length;
if (AudioTypes.Contains(path.Ext()))
{
using (MediaInfo mediaInfo = new MediaInfo(path))
{
performer = mediaInfo.GetInfo(MediaInfoStreamKind.General, "Performer");
title = mediaInfo.GetInfo(MediaInfoStreamKind.General, "Title");
album = mediaInfo.GetInfo(MediaInfoStreamKind.General, "Album");
genre = mediaInfo.GetInfo(MediaInfoStreamKind.General, "Genre");
date = mediaInfo.GetInfo(MediaInfoStreamKind.General, "Recorded_Date");
duration = mediaInfo.GetInfo(MediaInfoStreamKind.Audio, "Duration/String");
if (performer != "") text += "Artist: " + performer + "\n";
if (title != "") text += "Title: " + title + "\n";
if (album != "") text += "Album: " + album + "\n";
if (genre != "") text += "Genre: " + genre + "\n";
if (date != "") text += "Year: " + date + "\n";
if (duration != "") text += "Length: " + duration + "\n";
text += "Size: " + mediaInfo.GetInfo(MediaInfoStreamKind.General, "FileSize/String") + "\n";
text += "Type: " + path.Ext().ToUpper();
core.commandv("show-text", text, "5000");
return;
}
}
else if (ImageTypes.Contains(path.Ext()))
{
using (MediaInfo mediaInfo = new MediaInfo(path))
{
text =
"Width: " + mediaInfo.GetInfo(MediaInfoStreamKind.Image, "Width") + "\n" +
"Height: " + mediaInfo.GetInfo(MediaInfoStreamKind.Image, "Height") + "\n" +
"Size: " + mediaInfo.GetInfo(MediaInfoStreamKind.General, "FileSize/String") + "\n" +
"Type: " + path.Ext().ToUpper();
core.commandv("show-text", text, "5000");
return;
}
}
}
TimeSpan position = TimeSpan.FromSeconds(core.get_property_number("time-pos"));
TimeSpan duration2 = TimeSpan.FromSeconds(core.get_property_number("duration"));
string videoFormat = core.get_property_string("video-format").ToUpper();
string audioCodec = core.get_property_string("audio-codec-name").ToUpper();
text = path.FileName() + "\n" +
FormatTime(position.TotalMinutes) + ":" +
FormatTime(position.Seconds) + " / " +
FormatTime(duration2.TotalMinutes) + ":" +
FormatTime(duration2.Seconds) + "\n" +
$"{width} x {height}\n";
if (fileSize > 0)
text += Convert.ToInt32(fileSize / 1024.0 / 1024.0) + " MB\n";
text += $"{videoFormat}\n{audioCodec}";
core.commandv("show-text", text, "5000");
string FormatTime(double value) => ((int)value).ToString("00");
}
catch (Exception e)
{
App.ShowException(e);
}
}
public static void ExecuteMpvCommand() // deprecated 2019
{
InvokeOnMainThread(new Action(() => {
string command = VB.Interaction.InputBox("Enter a mpv command to be executed.", "Execute Command", RegistryHelp.GetString(App.RegPath, "RecentExecutedCommand"));
if (string.IsNullOrEmpty(command))
return;
RegistryHelp.SetValue(App.RegPath, "RecentExecutedCommand", command);
core.command(command, false);
}));
}
public static void OpenURL()
{
InvokeOnMainThread(new Action(() => {
string clipboard = System.Windows.Forms.Clipboard.GetText();
if (string.IsNullOrEmpty(clipboard) || (!clipboard.Contains("://") && !File.Exists(clipboard)) ||
clipboard.Contains("\n"))
{
App.ShowError("No URL found", "The clipboard does not contain a valid URL or file.");
return;
}
core.LoadFiles(new [] { clipboard }, false, Control.ModifierKeys.HasFlag(Keys.Control));
}));
}
public static void LoadSubtitle()
{
InvokeOnMainThread(new Action(() => {
using (var d = new OpenFileDialog())
{
string path = core.get_property_string("path");
if (File.Exists(path))
d.InitialDirectory = Path.GetDirectoryName(path);
d.Multiselect = true;
if (d.ShowDialog() == DialogResult.OK)
foreach (string filename in d.FileNames)
core.commandv("sub-add", filename);
}
}));
}
public static void LoadAudio()
{
InvokeOnMainThread(new Action(() => {
using (var d = new OpenFileDialog())
{
string path = core.get_property_string("path");
if (File.Exists(path))
d.InitialDirectory = Path.GetDirectoryName(path);
d.Multiselect = true;
if (d.ShowDialog() == DialogResult.OK)
foreach (string i in d.FileNames)
core.commandv("audio-add", i);
}
}));
}
public static void CycleAudio()
{
MediaTrack[] tracks = core.MediaTracks.Where(track => track.Type == "a").ToArray();
if (tracks.Length < 2)
return;
int aid = core.get_property_int("aid");
if (++aid > tracks.Length)
aid = 1;
core.commandv("set", "aid", aid.ToString());
core.commandv("show-text", aid + ": " + tracks[aid - 1].Text.Substring(3), "5000");
}
public static void ShowCommands()
{
string code = @"
foreach ($item in ($json | ConvertFrom-Json | foreach { $_ } | sort name))
{
''
$item.name
foreach ($arg in $item.args)
{
$value = $arg.name + ' <' + $arg.type.ToLower() + '>'
if ($arg.optional -eq $true)
{
$value = '[' + $value + ']'
}
' ' + $value
}
}";
string json = core.get_property_string("command-list");
ShowTextWithEditor("command-list", PowerShell.InvokeAndReturnString(code, "json", json));
}
public static void ShowProperties()
{
var props = core.get_property_string("property-list").Split(',').OrderBy(prop => prop);
ShowTextWithEditor("property-list", string.Join(BR, props));
}
public static void ShowTextWithEditor(string name, string text)
{
string file = Path.GetTempPath() + $"\\{name}.txt";
File.WriteAllText(file, BR + text.Trim() + BR);
ProcessHelp.ShellExecute(file);
}
public static void ScaleWindow(float factor)
{
core.RaiseScaleWindow(factor);
}
public static void ShowText(string text, int duration = 0, int fontSize = 0)
{
if (string.IsNullOrEmpty(text))
return;
if (duration == 0)
duration = core.get_property_int("osd-duration");
if (fontSize == 0)
fontSize = core.get_property_int("osd-font-size");
core.command("show-text \"${osd-ass-cc/0}{\\\\fs" + fontSize +
"}${osd-ass-cc/1}" + text + "\" " + duration);
}
public static void ShowPlaylist(string[] args = null)
{
int duration = 5000;
if (args?.Length == 1)
duration = Convert.ToInt32(args[0]);
var size = core.get_property_number("osd-font-size");
core.set_property_number("osd-font-size", 40);
core.command("show-text ${playlist} " + duration);
App.RunTask(() => {
Thread.Sleep(6000);
core.set_property_number("osd-font-size", size);
});
}
}
}

65
src/Misc/Extension.cs Normal file
View File

@@ -0,0 +1,65 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using static mpvnet.Core;
namespace mpvnet
{
public class Extension
{
[ImportMany]
public IEnumerable<IExtension> Extensions = null;
readonly CompositionContainer CompositionContainer;
public Extension()
{
try
{
AggregateCatalog catalog = new AggregateCatalog();
string dir = Folder.Startup + "Extensions";
if (Directory.Exists(dir))
{
string[] knownExtensions = { "RatingExtension", "ScriptingExtension" };
foreach (string extDir in Directory.GetDirectories(dir))
{
if (knownExtensions.Contains(Path.GetFileName(extDir)))
catalog.Catalogs.Add(new DirectoryCatalog(extDir, Path.GetFileName(extDir) + ".dll"));
else
ConsoleHelp.WriteError("Failed to load extension:\n\n" + extDir +
"\n\nOnly extensions that ship with mpv.net are allowed in <startup>\\extensions" +
"\n\nUser extensions have to use <config folder>\\extensions" +
"\n\nNever copy or install a new mpv.net version over a old mpv.net version.");
}
}
dir = core.ConfigFolder + "extensions";
if (Directory.Exists(dir))
foreach (string extDir in Directory.GetDirectories(dir))
catalog.Catalogs.Add(new DirectoryCatalog(extDir, Path.GetFileName(extDir) + ".dll"));
if (catalog.Catalogs.Count > 0)
{
CompositionContainer = new CompositionContainer(catalog);
CompositionContainer.ComposeParts(this);
}
}
catch (Exception ex)
{
App.ShowException(ex);
}
}
}
public interface IExtension
{
}
}

View File

@@ -0,0 +1,78 @@

using System.Globalization;
using System.IO;
public static class Extensions
{
public static bool ContainsEx(this string instance, string value)
{
if (instance != null && value != null)
return instance.Contains(value);
return false;
}
public static bool StartsWithEx(this string instance, string value)
{
if (instance != null && value != null)
return instance.StartsWith(value);
return false;
}
public static string ToUpperEx(this string instance)
{
if (instance != null)
return instance.ToUpper();
return "";
}
public static string ToLowerEx(this string instance)
{
if (instance != null)
return instance.ToLower();
return "";
}
public static string FileName(this string instance)
{
if (string.IsNullOrEmpty(instance))
return "";
int index = instance.LastIndexOf('\\');
if (index > -1)
return instance.Substring(index + 1);
index = instance.LastIndexOf('/');
if (index > -1)
return instance.Substring(index + 1);
return instance;
}
public static string Ext(this string instance)
{
if (instance == null)
return "";
return Path.GetExtension(instance).TrimStart('.').ToLower();
}
public static int ToInt(this string instance)
{
int.TryParse(instance, out int result);
return result;
}
public static float ToFloat(this string instance)
{
float.TryParse(instance.Replace(",", "."), NumberStyles.Float,
CultureInfo.InvariantCulture, out float result);
return result;
}
}

250
src/Misc/Help.cs Normal file
View File

@@ -0,0 +1,250 @@

using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;
using static mpvnet.Core;
using static mpvnet.NewLine;
namespace mpvnet
{
public static class ProcessHelp
{
public static void Execute(string file, string arguments = null)
{
using (Process proc = new Process())
{
proc.StartInfo.FileName = file;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.UseShellExecute = false;
proc.Start();
}
}
public static void ShellExecute(string file, string arguments = null)
{
using (Process proc = new Process())
{
proc.StartInfo.FileName = file;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.UseShellExecute = true;
proc.Start();
}
}
}
public static class ConsoleHelp
{
public static int Padding { get; set; }
public static void WriteError(object obj, string module = "mpv.net")
{
Write(obj, module, ConsoleColor.DarkRed, false);
}
public static void Write(object obj, string module = "mpv.net")
{
Write(obj, module, ConsoleColor.Black, true);
}
public static void Write(object obj, string module, ConsoleColor color)
{
Write(obj, module, color, false);
}
public static void Write(object obj, string module, ConsoleColor color, bool useDefaultColor)
{
if (obj == null)
return;
string value = obj.ToString();
if (!string.IsNullOrEmpty(module))
module = "[" + module + "] ";
if (useDefaultColor)
Console.ResetColor();
else
Console.ForegroundColor = color;
value = module + value;
if (Padding > 0 && value.Length < Padding)
value = value.PadRight(Padding);
if (color == ConsoleColor.Red || color == ConsoleColor.DarkRed)
Console.Error.WriteLine(value);
else
Console.WriteLine(value);
Console.ResetColor();
Trace.WriteLine(obj);
}
}
public class CursorHelp
{
static bool IsVisible = true;
public static void Show()
{
if (!IsVisible)
{
Cursor.Show();
IsVisible = true;
}
}
public static void Hide()
{
if (IsVisible)
{
Cursor.Hide();
IsVisible = false;
}
}
public static bool IsPosDifferent(Point screenPos)
{
return
Math.Abs(screenPos.X - Control.MousePosition.X) > 10 ||
Math.Abs(screenPos.Y - Control.MousePosition.Y) > 10;
}
}
public class mpvHelp
{
public static string WM_APPCOMMAND_to_mpv_key(int value)
{
switch (value)
{
case 5: return "SEARCH"; // BROWSER_SEARCH
case 6: return "FAVORITES"; // BROWSER_FAVORITES
case 7: return "HOMEPAGE"; // BROWSER_HOME
case 15: return "MAIL"; // LAUNCH_MAIL
case 33: return "PRINT"; // PRINT
case 11: return "NEXT"; // MEDIA_NEXTTRACK
case 12: return "PREV"; // MEDIA_PREVIOUSTRACK
case 13: return "STOP"; // MEDIA_STOP
case 14: return "PLAYPAUSE"; // MEDIA_PLAY_PAUSE
case 46: return "PLAY"; // MEDIA_PLAY
case 47: return "PAUSE"; // MEDIA_PAUSE
case 48: return "RECORD"; // MEDIA_RECORD
case 49: return "FORWARD"; // MEDIA_FAST_FORWARD
case 50: return "REWIND"; // MEDIA_REWIND
case 51: return "CHANNEL_UP"; // MEDIA_CHANNEL_UP
case 52: return "CHANNEL_DOWN"; // MEDIA_CHANNEL_DOWN
}
return null;
}
public static string GetProfiles()
{
string code = @"
foreach ($item in ($json | ConvertFrom-Json | foreach { $_ } | sort name))
{
$item.name
''
foreach ($option in $item.options)
{
' ' + $option.key + ' = ' + $option.value
}
''
}";
string json = core.get_property_string("profile-list");
return PowerShell.InvokeAndReturnString(code, "json", json).Trim();
}
public static string GetDecoders()
{
string code = @"
foreach ($item in ($json | ConvertFrom-Json | foreach { $_ } | sort codec))
{
$item.codec + ' - ' + $item.description
}";
string json = core.get_property_string("decoder-list");
return PowerShell.InvokeAndReturnString(code, "json", json).Trim();
}
public static string GetProtocols()
{
string list = core.get_property_string("protocol-list");
return string.Join(BR, list.Split(',').OrderBy(a => a));
}
public static string GetDemuxers()
{
string list = core.get_property_string("demuxer-lavf-list");
return string.Join(BR, list.Split(',').OrderBy(a => a));
}
}
public class RegistryHelp
{
public static string ApplicationKey { get; } = @"HKCU\Software\" + Application.ProductName;
public static void SetValue(string path, string name, object value)
{
using (RegistryKey regKey = GetRootKey(path).CreateSubKey(path.Substring(5), RegistryKeyPermissionCheck.ReadWriteSubTree))
regKey.SetValue(name, value);
}
public static string GetString(string path, string name, string defaultValue = "")
{
object value = GetValue(path, name, defaultValue);
return !(value is string) ? defaultValue : value.ToString();
}
public static int GetInt(string path, string name, int defaultValue = 0)
{
object value = GetValue(path, name, defaultValue);
return !(value is int) ? defaultValue : (int)value;
}
public static object GetValue(string path, string name, object defaultValue = null)
{
using (RegistryKey regKey = GetRootKey(path).OpenSubKey(path.Substring(5)))
return regKey == null ? null : regKey.GetValue(name, defaultValue);
}
public static void RemoveKey(string path)
{
try
{
GetRootKey(path).DeleteSubKeyTree(path.Substring(5), false);
}
catch { }
}
public static void RemoveValue(string path, string name)
{
try
{
using (RegistryKey regKey = GetRootKey(path).OpenSubKey(path.Substring(5), true))
if (regKey != null)
regKey.DeleteValue(name, false);
}
catch { }
}
static RegistryKey GetRootKey(string path)
{
switch (path.Substring(0, 4))
{
case "HKLM": return Registry.LocalMachine;
case "HKCU": return Registry.CurrentUser;
case "HKCR": return Registry.ClassesRoot;
default: throw new Exception();
}
}
}
}

189
src/Misc/Misc.cs Normal file
View File

@@ -0,0 +1,189 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Windows.Forms;
using Microsoft.Win32;
using static mpvnet.Core;
namespace mpvnet
{
public static class NewLine
{
public static string BR = Environment.NewLine;
public static string BR2 = Environment.NewLine + Environment.NewLine;
}
public class Sys
{
public static bool IsDarkTheme {
get {
object value = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
if (value is null)
value = 1;
return (int)value == 0;
}
}
}
public class StringLogicalComparer : IComparer, IComparer<string>
{
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern int StrCmpLogical(string x, string y);
int IComparer_Compare(object x, object y) => StrCmpLogical(x.ToString(), y.ToString());
int IComparer.Compare(object x, object y) => IComparer_Compare(x, y);
int IComparerOfString_Compare(string x, string y) => StrCmpLogical(x, y);
int IComparer<string>.Compare(string x, string y) => IComparerOfString_Compare(x, y);
}
public class FileAssociation
{
static string ExePath = Application.ExecutablePath;
static string ExeFilename = Path.GetFileName(Application.ExecutablePath);
static string ExeFilenameNoExt = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
static string[] Types;
public static void Register(string[] types)
{
Types = types;
RegistryHelp.SetValue(@"HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\" + ExeFilename, null, ExePath);
RegistryHelp.SetValue(@"HKCR\Applications\" + ExeFilename, "FriendlyAppName", "mpv.net media player");
RegistryHelp.SetValue($@"HKCR\Applications\{ExeFilename}\shell\open\command", null, $"\"{ExePath}\" \"%1\"");
RegistryHelp.SetValue(@"HKLM\SOFTWARE\Clients\Media\mpv.net\Capabilities", "ApplicationDescription", "mpv.net media player");
RegistryHelp.SetValue(@"HKLM\SOFTWARE\Clients\Media\mpv.net\Capabilities", "ApplicationName", "mpv.net");
RegistryHelp.SetValue(@"HKCR\SystemFileAssociations\video\OpenWithList\" + ExeFilename, null, "");
RegistryHelp.SetValue(@"HKCR\SystemFileAssociations\audio\OpenWithList\" + ExeFilename, null, "");
RegistryHelp.SetValue(@"HKLM\SOFTWARE\RegisteredApplications", "mpv.net", @"SOFTWARE\Clients\Media\mpv.net\Capabilities");
foreach (string ext in Types)
{
RegistryHelp.SetValue($@"HKCR\Applications\{ExeFilename}\SupportedTypes", "." + ext, "");
RegistryHelp.SetValue($@"HKCR\" + "." + ext, null, ExeFilenameNoExt + "." + ext);
RegistryHelp.SetValue($@"HKCR\" + "." + ext + @"\OpenWithProgIDs", ExeFilenameNoExt + "." + ext, "");
if (VideoTypes.Contains(ext))
RegistryHelp.SetValue(@"HKCR\" + "." + ext, "PerceivedType", "video");
if (AudioTypes.Contains(ext))
RegistryHelp.SetValue(@"HKCR\" + "." + ext, "PerceivedType", "audio");
if (ImageTypes.Contains(ext))
RegistryHelp.SetValue(@"HKCR\" + "." + ext, "PerceivedType", "image");
RegistryHelp.SetValue($@"HKCR\" + ExeFilenameNoExt + "." + ext + @"\shell\open\command", null, $"\"{ExePath}\" \"%1\"");
RegistryHelp.SetValue(@"HKLM\SOFTWARE\Clients\Media\mpv.net\Capabilities\FileAssociations", "." + ext, ExeFilenameNoExt + "." + ext);
}
}
}
public class MediaTrack
{
public string Text { get; set; }
public string Type { get; set; }
public int ID { get; set; }
}
public class CommandItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string Path { get; set; } = "";
public string Command { get; set; } = "";
public string Display { get { return string.IsNullOrEmpty(Path) ? Command : Path; } }
public CommandItem() { }
public CommandItem(SerializationInfo info, StreamingContext context) { }
void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
string _Input = "";
public string Input {
get => _Input;
set {
_Input = value;
NotifyPropertyChanged();
}
}
public static ObservableCollection<CommandItem> GetItems(string content)
{
var items = new ObservableCollection<CommandItem>();
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(" "));
if (item.Input == "_")
item.Input = "";
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);
}
}
return items;
}
static ObservableCollection<CommandItem> _Items;
public static ObservableCollection<CommandItem> Items {
get {
if (_Items is null)
_Items = GetItems(File.ReadAllText(core.InputConfPath));
return _Items;
}
}
}
public class Folder
{
public static string Startup { get; } = Application.StartupPath + @"\";
}
}

243
src/Misc/PowerShell.cs Normal file
View File

@@ -0,0 +1,243 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
using static mpvnet.Core;
using static mpvnet.NewLine;
namespace mpvnet
{
public class PowerShell
{
public Runspace Runspace { get; set; }
public Pipeline Pipeline { get; set; }
public string Module { get; set; }
public bool Print { get; set; }
public List<string> Scripts { get; } = new List<string>();
public List<KeyValuePair<string, object>> Variables = new List<KeyValuePair<string, object>>();
public string[] Arguments { get; }
public event Action<string, object[]> Event;
public event Action<string, object> PropertyChanged;
public List<KeyValuePair<string, ScriptBlock>> EventHandlers = new List<KeyValuePair<string, ScriptBlock>>();
public List<KeyValuePair<string, ScriptBlock>> PropChangedHandlers = new List<KeyValuePair<string, ScriptBlock>>();
public static List<PowerShell> References { get; } = new List<PowerShell>();
public object Invoke() => Invoke(null, null);
public object Invoke(string variable, object obj)
{
try
{
Runspace = RunspaceFactory.CreateRunspace();
Runspace.ApartmentState = ApartmentState.STA;
Runspace.Open();
Pipeline = Runspace.CreatePipeline();
foreach (string script in Scripts)
Pipeline.Commands.AddScript(script);
if (Arguments != null)
foreach (string param in Arguments)
foreach (Command command in Pipeline.Commands)
command.Parameters.Add(null, param);
Runspace.SessionStateProxy.SetVariable("mp", this);
foreach (var i in Variables)
Runspace.SessionStateProxy.SetVariable(i.Key, i.Value);
if (!string.IsNullOrEmpty(variable))
Runspace.SessionStateProxy.SetVariable(variable, obj);
if (Print)
{
Pipeline.Output.DataReady += Output_DataReady;
Pipeline.Error.DataReady += Error_DataReady;
}
return Pipeline.Invoke();
}
catch (RuntimeException e)
{
string message = e.Message + BR + BR + e.ErrorRecord.ScriptStackTrace.Replace(
" <ScriptBlock>, <No file>", "") + BR + BR + Module + BR;
throw new PowerShellException(message);
}
catch (Exception e)
{
throw e;
}
}
public static string InvokeAndReturnString(string code, string varName, object varValue)
{
PowerShell ps = new PowerShell() { Print = false };
ps.Scripts.Add(code);
string ret = string.Join(Environment.NewLine, (ps.Invoke(varName, varValue)
as IEnumerable<object>).Select(item => item.ToString())).ToString();
ps.Runspace.Dispose();
return ret;
}
public void Output_DataReady(object sender, EventArgs e)
{
var output = sender as PipelineReader<PSObject>;
while (output.Count > 0)
ConsoleHelp.Write(output.Read(), Module);
}
public void Error_DataReady(object sender, EventArgs e)
{
var output = sender as PipelineReader<Object>;
while (output.Count > 0)
ConsoleHelp.WriteError(output.Read(), Module);
}
public void RedirectStreams(PSEventJob job)
{
if (Print)
{
job.Output.DataAdded += Output_DataAdded;
job.Error.DataAdded += Error_DataAdded;
}
}
public void commandv(params string[] args) => core.commandv(args);
public void command(string command) => core.command(command);
public bool get_property_bool(string name) => core.get_property_bool(name);
public void set_property_bool(string name, bool value) => core.set_property_bool(name, value);
public int get_property_int(string name) => core.get_property_int(name);
public void set_property_int(string name, int value) => core.set_property_int(name, value);
public double get_property_number(string name) => core.get_property_number(name);
public void set_property_number(string name, double value) => core.set_property_number(name, value);
public string get_property_string(string name) => core.get_property_string(name);
public void set_property_string(string name, string value) => core.set_property_string(name, value);
public void observe_property(string name, string type, ScriptBlock sb)
{
PropChangedHandlers.Add(new KeyValuePair<string, ScriptBlock>(name, sb));
switch (type)
{
case "bool": case "boolean":
core.observe_property_bool(name, (value) => App.RunTask(() => PropertyChanged.Invoke(name, value)));
break;
case "string":
core.observe_property_string(name, (value) => App.RunTask(() => PropertyChanged.Invoke(name, value)));
break;
case "int": case "integer":
core.observe_property_int(name, (value) => App.RunTask(() => PropertyChanged.Invoke(name, value)));
break;
case "float": case "double":
core.observe_property_double(name, (value) => App.RunTask(() => PropertyChanged.Invoke(name, value)));
break;
case "nil": case "none": case "native":
core.observe_property(name, () => App.RunTask(() => PropertyChanged.Invoke(name, null)));
break;
default:
App.ShowError("Invalid Type", "Valid types are: bool or boolean, string, int or integer, float or double, nil or none or native");
break;
}
}
public void register_event(string name, ScriptBlock sb)
{
EventHandlers.Add(new KeyValuePair<string, ScriptBlock>(name, sb));
switch (name)
{
case "log-message":
core.LogMessageAsync += (level, msg) => Event.Invoke("log-message", new object[] { level, msg });
break;
case "end-file":
core.EndFileAsync += (reason) => Event.Invoke("end-file", new object[] { reason });
break;
case "client-message":
core.ClientMessageAsync += (args) => Event.Invoke("client-message", args);
break;
case "shutdown":
core.Shutdown += () => Event.Invoke("shutdown", null);
break;
case "get-property-reply":
core.GetPropertyReplyAsync += () => Event.Invoke("get-property-reply", null);
break;
case "set-property-reply":
core.SetPropertyReplyAsync += () => Event.Invoke("set-property-reply", null);
break;
case "command-reply":
core.CommandReplyAsync += () => Event.Invoke("command-reply", null);
break;
case "start-file":
core.StartFileAsync += () => Event.Invoke("start-file", null);
break;
case "file-loaded":
core.FileLoadedAsync += () => Event.Invoke("file-loaded", null);
break;
case "idle":
core.IdleAsync += () => Event.Invoke("idle", null);
break;
case "video-reconfig":
core.VideoReconfigAsync += () => Event.Invoke("video-reconfig", null);
break;
case "audio-reconfig":
core.AudioReconfigAsync += () => Event.Invoke("audio-reconfig", null);
break;
case "seek":
core.SeekAsync += () => Event.Invoke("seek", null);
break;
case "playback-restart":
core.PlaybackRestartAsync += () => Event.Invoke("playback-restart", null);
break;
}
}
void Output_DataAdded(object sender, DataAddedEventArgs e)
{
var output = sender as PSDataCollection<PSObject>;
ConsoleHelp.Write(output[e.Index], Module);
}
void Error_DataAdded(object sender, DataAddedEventArgs e)
{
var error = sender as PSDataCollection<ErrorRecord>;
ConsoleHelp.WriteError(error[e.Index], Module);
}
}
public class PowerShellException : Exception
{
public PowerShellException(string message) : base(message)
{
}
}
}

105
src/Misc/Program.cs Normal file
View File

@@ -0,0 +1,105 @@

using System;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using static mpvnet.Core;
namespace mpvnet
{
static class Program
{
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (App.IsStartedFromTerminal)
WinAPI.AttachConsole(-1 /*ATTACH_PARENT_PROCESS*/);
if (core.ConfigFolder == "")
return;
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
if (args.Length >= 2 && args[0] == "--reg-file-assoc")
{
if (args[1] == "audio")
FileAssociation.Register(Core.AudioTypes);
else if (args[1] == "video")
FileAssociation.Register(Core.VideoTypes);
else if (args[1] == "image")
FileAssociation.Register(Core.ImageTypes);
else
FileAssociation.Register(args.Skip(1).ToArray());
return;
}
App.Init();
Mutex mutex = new Mutex(true, "mpvnetProcessInstance", out bool isFirst);
if ((App.ProcessInstance == "single" || App.ProcessInstance == "queue") && !isFirst)
{
List<string> files = new List<string>();
files.Add(App.ProcessInstance);
foreach (string arg in args)
{
if (!arg.StartsWith("--") && (arg == "-" || arg.Contains("://") ||
arg.Contains(":\\") || arg.StartsWith("\\\\")))
files.Add(arg);
else if (arg == "--queue")
files[0] = "queue";
}
Process[] procs = Process.GetProcessesByName("mpvnet");
for (int i = 0; i < 20; i++)
{
foreach (Process proc in procs)
{
if (proc.MainWindowHandle != IntPtr.Zero)
{
WinAPI.AllowSetForegroundWindow(proc.Id);
var data = new WinAPI.COPYDATASTRUCT();
data.lpData = string.Join("\n", files.ToArray());
data.cbData = data.lpData.Length * 2 + 1;
WinAPI.SendMessage(proc.MainWindowHandle, 0x004A /*WM_COPYDATA*/, IntPtr.Zero, ref data);
mutex.Dispose();
if (App.IsStartedFromTerminal)
WinAPI.FreeConsole();
return;
}
}
Thread.Sleep(50);
}
mutex.Dispose();
return;
}
Application.Run(new MainForm());
if (App.IsStartedFromTerminal)
WinAPI.FreeConsole();
mutex.Dispose();
}
catch (Exception ex)
{
Msg.ShowException(ex);
}
}
}
}

96
src/Misc/Theme.cs Normal file
View File

@@ -0,0 +1,96 @@

using System.Collections.Generic;
using System.Windows.Media;
namespace mpvnet
{
public class Theme
{
public string Name { get; set; }
public Dictionary<string, string> Dictionary { get; } = new Dictionary<string, string>();
public static List<Theme> DefaultThemes { get; set; }
public static List<Theme> CustomThemes { get; set; }
public static Theme Current { get; set; }
public static Brush Foreground { get; set; }
public static Brush Foreground2 { get; set; }
public static Brush Background { get; set; }
public static Brush Heading { get; set; }
public System.Drawing.Color GetWinFormsColor(string key)
{
return System.Drawing.ColorTranslator.FromHtml(Dictionary[key]);
}
public Brush GetBrush(string key)
{
return new SolidColorBrush((Color)ColorConverter.ConvertFromString(Dictionary[key]));
}
public static void Init(string customContent, string defaultContent, string activeTheme)
{
DefaultThemes = Load(defaultContent);
CustomThemes = Load(customContent);
foreach (Theme theme in CustomThemes)
{
if (theme.Name == activeTheme)
{
bool isKeyMissing = false;
foreach (string key in DefaultThemes[0].Dictionary.Keys)
{
if (!theme.Dictionary.ContainsKey(key))
{
isKeyMissing = true;
ConsoleHelp.WriteError($"Theme '{activeTheme}' misses '{key}'");
break;
}
}
if (!isKeyMissing)
Current = theme;
break;
}
}
if (Current == null)
foreach (Theme theme in DefaultThemes)
if (theme.Name == activeTheme)
Current = theme;
if (Current == null)
Current = DefaultThemes[0];
Foreground = Current.GetBrush("foreground");
Foreground2 = Current.GetBrush("foreground2");
Background = Current.GetBrush("background");
Heading = Current.GetBrush("heading");
}
static List<Theme> Load(string content)
{
List<Theme> list = new List<Theme>();
Theme theme = null;
foreach (string currentLine in (content ?? "").Split(new [] { '\r', '\n' }))
{
string line = currentLine.Trim();
if (line.StartsWith("[") && line.EndsWith("]"))
list.Add(theme = new Theme() { Name = line.Substring(1, line.Length - 2).Trim() });
if (line.Contains("=") && theme != null)
{
string left = line.Substring(0, line.IndexOf("=")).Trim();
theme.Dictionary[left] = line.Substring(line.IndexOf("=") + 1).Trim();
}
}
return list;
}
}
}

79
src/Misc/UpdateCheck.cs Normal file
View File

@@ -0,0 +1,79 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using static mpvnet.Core;
namespace mpvnet
{
class UpdateCheck
{
public static void DailyCheck()
{
if (App.UpdateCheck && RegistryHelp.GetInt(RegistryHelp.ApplicationKey, "UpdateCheckLast")
!= DateTime.Now.DayOfYear)
CheckOnline();
}
public static async void CheckOnline(bool showUpToDateMessage = false)
{
try
{
using (HttpClient client = new HttpClient())
{
RegistryHelp.SetValue(RegistryHelp.ApplicationKey, "UpdateCheckLast", DateTime.Now.DayOfYear);
client.DefaultRequestHeaders.Add("User-Agent", "mpv.net");
var response = await client.GetAsync("https://api.github.com/repos/stax76/mpv.net/releases/latest");
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
Match match = Regex.Match(content, @"""mpv\.net-([\d\.]+)-portable\.zip""");
Version onlineVersion = Version.Parse(match.Groups[1].Value);
Version currentVersion = Assembly.GetEntryAssembly().GetName().Version;
if (onlineVersion <= currentVersion)
{
if (showUpToDateMessage)
Msg.Show($"{Application.ProductName} is up to date.");
return;
}
if ((RegistryHelp.GetString(RegistryHelp.ApplicationKey, "UpdateCheckVersion")
!= onlineVersion.ToString() || showUpToDateMessage) && Msg.ShowQuestion(
$"New version {onlineVersion} is available, update now?") == MsgResult.OK)
{
string url = $"https://github.com/stax76/mpv.net/releases/download/{onlineVersion}/mpv.net-{onlineVersion}-portable.zip";
using (Process proc = new Process())
{
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
proc.StartInfo.FileName = "powershell.exe";
proc.StartInfo.Arguments = $"-NoExit -ExecutionPolicy Bypass -File \"{Folder.Startup + "Setup\\update.ps1"}\" \"{url}\" \"{Folder.Startup.TrimEnd(Path.DirectorySeparatorChar)}\"";
if (Folder.Startup.Contains("Program Files"))
proc.StartInfo.Verb = "runas";
proc.Start();
}
core.command("quit");
}
RegistryHelp.SetValue(RegistryHelp.ApplicationKey, "UpdateCheckVersion", onlineVersion.ToString());
}
}
catch (Exception ex)
{
if (showUpToDateMessage)
Msg.ShowException(ex);
}
}
}
}