diff --git a/Changelog.md b/Changelog.md index 614c0b9..3b93856 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,7 +4,9 @@ - new: menu item 'View > Show Progress' (p key) to show progress bar - new: `script-message mpv.net playlist-first`, unlike mpv does not restart if the first file is already active - +- new: if mpv.net is started from the terminal and an error happens + then the error is printed to the terminal instead of shown + with a message box - fix: update routine did only work when mpv.net was located in 'Program Files' - fix: errors were just ignored and only seen printed in the terminal in case mpv.net was started from the terminal, now for every error a message box is shown diff --git a/Release.ps1 b/Release.ps1 index 63d503f..2fefcf9 100644 --- a/Release.ps1 +++ b/Release.ps1 @@ -2,15 +2,15 @@ $ErrorActionPreference = 'Stop' $desktopDir = [Environment]::GetFolderPath('Desktop') -$exePath = (Get-Location).Path + '\mpv.net\bin\x64\mpvnet.exe' +$exePath = $PSScriptRoot + '\mpv.net\bin\x64\mpvnet.exe' $version = [Reflection.Assembly]::LoadFile($exePath).GetName().Version $msbuild = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe' -$iscc = 'C:\Program Files (x86)\Inno Setup 6\ISCC.exe' +$inno = 'C:\Program Files (x86)\Inno Setup 6\ISCC.exe' $7z = 'C:\Program Files\7-Zip\7z.exe' if ($version.Revision -ne 0) { - & $msbuild mpv.net.sln /p:Configuration=Debug /p:Platform=x64 + & $msbuild mpv.net.sln -t:Rebuild -p:Configuration=Debug -p:Platform=x64 if ($LastExitCode) { throw $LastExitCode } $targetDir = "$desktopDir\mpv.net-portable-x64-$version-beta" @@ -38,10 +38,10 @@ else & $msbuild mpv.net.sln /p:Configuration=Debug /p:Platform=x86 if ($LastExitCode) { throw $LastExitCode } - & $iscc /Darch=x64 setup.iss + & $inno /Darch=x64 setup.iss if ($LastExitCode) { throw $LastExitCode } - & $iscc /Darch=x86 setup.iss + & $inno /Darch=x86 setup.iss if ($LastExitCode) { throw $LastExitCode } $targetDir = $desktopDir + "\mpv.net-portable-x64-$version" diff --git a/mpv.net/Misc/App.cs b/mpv.net/Misc/App.cs index cf1c6c2..13079ee 100644 --- a/mpv.net/Misc/App.cs +++ b/mpv.net/Misc/App.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.IO; using System.Windows.Forms; using UI; +using static libmpv; namespace mpvnet { @@ -86,7 +87,11 @@ namespace mpvnet mp.LogMessage += LogMessage; } - private static void LogMessage(string msg) => Msg.ShowError(msg); + private static void LogMessage(mpv_log_level level, string msg) + { + if (!App.IsStartedFromTerminal && level == mpv_log_level.MPV_LOG_LEVEL_FATAL) + Msg.ShowError(msg); + } private static void Initialized() { diff --git a/mpv.net/Misc/Command.cs b/mpv.net/Misc/Command.cs index 70c0c4f..83a132f 100644 --- a/mpv.net/Misc/Command.cs +++ b/mpv.net/Misc/Command.cs @@ -278,7 +278,9 @@ namespace mpvnet public static void CycleAudio() { string path = mp.get_property_string("path"); - if (!File.Exists(path)) return; + + if (!File.Exists(path)) + return; using (MediaInfo mi = new MediaInfo(path)) { diff --git a/mpv.net/Misc/Help.cs b/mpv.net/Misc/Help.cs index ce603bb..caa9ffe 100644 --- a/mpv.net/Misc/Help.cs +++ b/mpv.net/Misc/Help.cs @@ -7,8 +7,8 @@ class ConsoleHelp public static void WriteError(object obj) { Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(obj); + Console.WriteLine("[mpvnet] " + obj); Console.ResetColor(); Trace.WriteLine(obj); } -} \ No newline at end of file +} diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index d0f4c48..0e30e00 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -28,8 +28,8 @@ namespace mpvnet // Lua/JS event libmpv event // MPV_EVENT_NONE + public static event Action LogMessage; // log-message MPV_EVENT_LOG_MESSAGE public static event Action Shutdown; // shutdown MPV_EVENT_SHUTDOWN - public static event Action LogMessage; // log-message MPV_EVENT_LOG_MESSAGE public static event Action GetPropertyReply; // get-property-reply MPV_EVENT_GET_PROPERTY_REPLY public static event Action SetPropertyReply; // set-property-reply MPV_EVENT_SET_PROPERTY_REPLY public static event Action CommandReply; // command-reply MPV_EVENT_COMMAND_REPLY @@ -97,7 +97,7 @@ namespace mpvnet if (Handle == IntPtr.Zero) throw new Exception("error mpv_create"); - mpv_request_log_messages(Handle, "fatal"); + mpv_request_log_messages(Handle, "info"); Task.Run(() => EventLoop()); if (App.IsStartedFromTerminal) @@ -306,7 +306,7 @@ namespace mpvnet case mpv_event_id.MPV_EVENT_LOG_MESSAGE: { var data = (mpv_event_log_message)Marshal.PtrToStructure(evt.data, typeof(mpv_event_log_message)); - LogMessage?.Invoke($"[{data.prefix}] {data.text}"); + LogMessage?.Invoke(data.log_level, $"[{data.prefix}] {data.text}"); } break; case mpv_event_id.MPV_EVENT_GET_PROPERTY_REPLY: @@ -343,8 +343,10 @@ namespace mpvnet VideoSizeAutoResetEvent.Set(); Task.Run(new Action(() => ReadMetaData())); string path = mp.get_property_string("path"); + if (path.Contains("://")) path = mp.get_property_string("media-title"); + WriteHistory(path); FileLoaded?.Invoke(); } @@ -504,90 +506,71 @@ namespace mpvnet Marshal.FreeHGlobal(mainPtr); if (err < 0) - throw new Exception("error executing command:\n\n" + - string.Join("\n", args) + "\r\n\r\n" + GetError(err)); + HandleError(err, true, "error executing command:", string.Join("\n", args)); } public static void command(string command, bool throwException = false) { mpv_error err = mpv_command_string(Handle, command); - if (err < 0 && throwException) - throw new Exception("error executing command:\n\n" + command + "\r\n\r\n" + GetError(err)); + if (err < 0) + HandleError(err, throwException, "error executing command:", command); } - public static void set_property_string(string name, string value, bool throwOnException = false) + public static void set_property_string(string name, string value, bool throwException = false) { byte[] bytes = GetUtf8Bytes(value); mpv_error err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes); - if (err < 0 && throwOnException) - throw new Exception($"error setting property: {name} = " + value + "\r\n\r\n" + GetError(err)); + if (err < 0) + HandleError(err, throwException, $"error setting property: {name} = " + value); } - public static string get_property_string(string name, bool throwOnException = false) + public static string get_property_string(string name, bool throwException = false) { - try + mpv_error err = mpv_get_property(Handle, GetUtf8Bytes(name), + mpv_format.MPV_FORMAT_STRING, out IntPtr lpBuffer); + + if (err == 0) { - mpv_error err = mpv_get_property(Handle, GetUtf8Bytes(name), - mpv_format.MPV_FORMAT_STRING, out IntPtr lpBuffer); - - if (err < 0) - { - if (throwOnException) - throw new Exception($"error getting property: {name}\n\n" + GetError(err)); - return ""; - } - string ret = ConvertFromUtf8(lpBuffer); mpv_free(lpBuffer); return ret; } - catch (Exception e) - { - if (throwOnException) - throw e; - return ""; - } + + HandleError(err, throwException, $"error getting property: {name}"); + return ""; } - public static int get_property_int(string name, bool throwOnException = false) + public static int get_property_int(string name, bool throwException = false) { mpv_error err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, out IntPtr lpBuffer); if (err < 0) - { - if (throwOnException) - throw new Exception($"error getting property: {name}\n\n" + GetError(err)); - return 0; - } + HandleError(err, throwException, $"error getting property: {name}"); return lpBuffer.ToInt32(); } - public static double get_property_number(string name, bool throwOnException = false) + public static double get_property_number(string name, bool throwException = false) { mpv_error err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_DOUBLE, out double value); if (err < 0) - { - if (throwOnException) - throw new Exception($"error getting property: {name}\n\n" + GetError(err)); - return 0; - } + HandleError(err, throwException, $"error getting property: {name}"); return value; } - public static void set_property_int(string name, int value, bool throwOnException = false) + public static void set_property_int(string name, int value, bool throwException = false) { Int64 val = value; mpv_error err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, ref val); - if (err < 0 && throwOnException) - throw new Exception($"error setting property: {name} = {value}\n\n" + GetError(err)); + if (err < 0) + HandleError(err, throwException, $"error setting property: {name} = {value}"); } public static void observe_property_int(string name, Action action) @@ -596,7 +579,7 @@ namespace mpvnet name, mpv_format.MPV_FORMAT_INT64); if (err < 0) - throw new Exception($"error observing property: {name}\n\n" + GetError(err)); + HandleError(err, true, $"error observing property: {name}"); else lock (IntPropChangeActions) IntPropChangeActions.Add(new KeyValuePair>(name, action)); @@ -608,7 +591,7 @@ namespace mpvnet name, mpv_format.MPV_FORMAT_DOUBLE); if (err < 0) - throw new Exception($"error observing property: {name}\n\n" + GetError(err)); + HandleError(err, true, $"error observing property: {name}"); else lock (DoublePropChangeActions) DoublePropChangeActions.Add(new KeyValuePair>(name, action)); @@ -620,7 +603,7 @@ namespace mpvnet name, mpv_format.MPV_FORMAT_FLAG); if (err < 0) - throw new Exception($"error observing property: {name}\n\n" + GetError(err)); + HandleError(err, true, $"error observing property: {name}"); else lock (BoolPropChangeActions) BoolPropChangeActions.Add(new KeyValuePair>(name, action)); @@ -632,12 +615,24 @@ namespace mpvnet name, mpv_format.MPV_FORMAT_STRING); if (err < 0) - throw new Exception($"error observing property: {name}\n\n" + GetError(err)); + HandleError(err, true, $"error observing property: {name}"); else lock (StringPropChangeActions) StringPropChangeActions.Add(new KeyValuePair>(name, action)); } + public static void HandleError(mpv_error err, bool throwException, params string[] messages) + { + if (throwException) + { + foreach (string msg in messages) + ConsoleHelp.WriteError(msg); + + ConsoleHelp.WriteError(GetError(err)); + throw new Exception(string.Join("\r\r", messages) + "\r\r"+ GetError(err)); + } + } + public static void ProcessCommandLine(bool preInit) { var args = Environment.GetCommandLineArgs().Skip(1); @@ -666,6 +661,7 @@ namespace mpvnet if (preInit && preInitProperties.Contains(left)) { mp.ProcessProperty(left, right); + if (!App.ProcessProperty(left, right)) set_property_string(left, right, true); } @@ -674,6 +670,7 @@ namespace mpvnet if (!PrintCommandLineArgument(arg)) { mp.ProcessProperty(left, right); + if (!App.ProcessProperty(left, right)) set_property_string(left, right, true); } @@ -681,7 +678,8 @@ namespace mpvnet } catch (Exception e) { - Msg.ShowException(e); + if (!App.IsStartedFromTerminal) + Msg.ShowException(e); } } }