From 85a23fbae1960b7d6d0c554c8854b9486644c1d5 Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Thu, 18 Jul 2019 05:47:16 +0200 Subject: [PATCH] tweaks and refactoring --- addons/RatingAddon/RatingAddon.cs | 4 +- mpv.net/Misc/Command.cs | 13 ++- mpv.net/Misc/Misc.cs | 6 - mpv.net/WinForms/MainForm.cs | 4 +- mpv.net/mpv/mp.cs | 179 ++++++++++++++++-------------- 5 files changed, 110 insertions(+), 96 deletions(-) diff --git a/addons/RatingAddon/RatingAddon.cs b/addons/RatingAddon/RatingAddon.cs index 87b6a55..f36647b 100644 --- a/addons/RatingAddon/RatingAddon.cs +++ b/addons/RatingAddon/RatingAddon.cs @@ -59,7 +59,9 @@ namespace RatingAddon { if (args[0] != "rate-file" || ! int.TryParse(args[1], out int rating)) return; - Dic[mp.get_property_string("path")] = rating; + string path = mp.get_property_string("path"); + if (!File.Exists(path)) return; + Dic[path] = rating; mp.commandv("show-text", $"Rating: {rating}"); } } diff --git a/mpv.net/Misc/Command.cs b/mpv.net/Misc/Command.cs index 403e2bd..25de5bd 100644 --- a/mpv.net/Misc/Command.cs +++ b/mpv.net/Misc/Command.cs @@ -186,7 +186,8 @@ namespace mpvnet InvokeOnMainThread(new Action(() => { using (var d = new OpenFileDialog()) { - d.InitialDirectory = Path.GetDirectoryName(mp.get_property_string("path", false)); + string path = mp.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) @@ -200,7 +201,9 @@ namespace mpvnet InvokeOnMainThread(new Action(() => { using (var d = new OpenFileDialog()) { - d.InitialDirectory = Path.GetDirectoryName(mp.get_property_string("path", false)); + string path = mp.get_property_string("path"); + if (File.Exists(path)) + d.InitialDirectory = Path.GetDirectoryName(path); d.Multiselect = true; if (d.ShowDialog() == DialogResult.OK) @@ -212,10 +215,10 @@ namespace mpvnet public static void CycleAudio() { - string filePath = mp.get_property_string("path", false); - if (!File.Exists(filePath)) return; + string path = mp.get_property_string("path"); + if (!File.Exists(path)) return; - using (MediaInfo mi = new MediaInfo(filePath)) + using (MediaInfo mi = new MediaInfo(path)) { MediaTrack[] audTracks = mp.MediaTracks.Where(track => track.Type == "a").ToArray(); if (audTracks.Length < 2) return; diff --git a/mpv.net/Misc/Misc.cs b/mpv.net/Misc/Misc.cs index 2d624a7..1b01adc 100644 --- a/mpv.net/Misc/Misc.cs +++ b/mpv.net/Misc/Misc.cs @@ -53,7 +53,6 @@ namespace mpvnet { string filePath = mp.ConfFolder + "\\mpvnet-debug.log"; if (File.Exists(filePath)) File.Delete(filePath); - Trace.Listeners.Clear(); Trace.Listeners.Add(new TextWriterTraceListener(filePath)); Trace.AutoFlush = true; } @@ -64,11 +63,6 @@ namespace mpvnet } } - public static void Exit() - { - if (Trace.Listeners.Count > 0) Trace.Listeners[0].Close(); - } - static Dictionary _Conf; public static Dictionary Conf { diff --git a/mpv.net/WinForms/MainForm.cs b/mpv.net/WinForms/MainForm.cs index cfd3ff0..a56a628 100644 --- a/mpv.net/WinForms/MainForm.cs +++ b/mpv.net/WinForms/MainForm.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; +using System.Diagnostics; namespace mpvnet { @@ -378,7 +379,7 @@ namespace mpvnet protected override void WndProc(ref Message m) { - //System.Diagnostics.Debug.WriteLine(m); + //Debug.WriteLine(m); switch (m.Msg) { @@ -549,7 +550,6 @@ namespace mpvnet } RegHelp.SetObject(App.RegPath, "Recent", RecentFiles.ToArray()); - App.Exit(); mp.commandv("quit"); mp.ShutdownAutoResetEvent.WaitOne(3000); } diff --git a/mpv.net/mpv/mp.cs b/mpv.net/mpv/mp.cs index 6cbd2c5..1df08b9 100644 --- a/mpv.net/mpv/mp.cs +++ b/mpv.net/mpv/mp.cs @@ -410,16 +410,14 @@ namespace mpvnet { if (Handle == IntPtr.Zero) return; int err = mpv_command_string(Handle, command); - if (err < 0 && throwException) - throw new Exception($"{(mpv_error)err}\r\n\r\n" + command); + if (err < 0 && throwException) throw new Exception($"{(mpv_error)err}\n\n" + command); } public static void set_property_string(string name, string value, bool throwOnException = false) { byte[] bytes = GetUtf8Bytes(value); int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes); - if (err < 0 && throwOnException) - throw new Exception($"{name}: {(mpv_error)err}"); + if (err < 0 && throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); } public static string get_property_string(string name, bool throwOnException = false) @@ -427,7 +425,13 @@ namespace mpvnet try { int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, out IntPtr lpBuffer); - if (err < 0 && throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); + + if (err < 0) + { + if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); + return ""; + } + string ret = StringFromNativeUtf8(lpBuffer); mpv_free(lpBuffer); return ret; @@ -443,10 +447,13 @@ namespace mpvnet { int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, out IntPtr lpBuffer); - if (err < 0 && throwOnException) - throw new Exception($"{name}: {(mpv_error)err}"); - else - return lpBuffer.ToInt32(); + if (err < 0) + { + if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); + return 0; + } + + return lpBuffer.ToInt32(); } public static double get_property_number(string name, bool throwOnException = false) @@ -454,10 +461,13 @@ namespace mpvnet double val = 0; int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_DOUBLE, ref val); - if (err < 0 && throwOnException) - throw new Exception($"{name}: {(mpv_error)err}"); - else - return val; + if (err < 0) + { + if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); + return 0; + } + + return val; } public static bool get_property_bool(string name, bool throwOnException = false) @@ -474,9 +484,7 @@ namespace mpvnet { Int64 val = value; int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, ref val); - - if (err < 0 && throwOnException) - throw new Exception($"{name}: {(mpv_error)err}"); + if (err < 0 && throwOnException) throw new Exception($"{name}: {(mpv_error)err}"); } public static void observe_property_int(string name, Action action) @@ -642,8 +650,9 @@ namespace mpvnet static string LastHistoryPath; static DateTime LastHistoryStartDateTime; - static void WriteHistory(string filePath) + static void WriteHistory(string path) { + if (!File.Exists(path)) return; int totalMinutes = Convert.ToInt32((DateTime.Now - LastHistoryStartDateTime).TotalMinutes); if (File.Exists(LastHistoryPath) && totalMinutes > 1) @@ -655,7 +664,7 @@ namespace mpvnet Path.GetFileNameWithoutExtension(LastHistoryPath) + "\r\n"); } - LastHistoryPath = filePath; + LastHistoryPath = path; LastHistoryStartDateTime = DateTime.Now; } @@ -690,81 +699,87 @@ namespace mpvnet { MediaTracks.Clear(); - using (MediaInfo mi = new MediaInfo(get_property_string("path"))) + string path = get_property_string("path"); + + if (File.Exists(path)) { - int count = mi.GetCount(MediaInfoStreamKind.Video); - - for (int i = 0; i < count; i++) + using (MediaInfo mi = new MediaInfo(path)) { - MediaTrack track = new MediaTrack(); - Add(track, mi.GetVideo(i, "Format")); - Add(track, mi.GetVideo(i, "Format_Profile")); - Add(track, mi.GetVideo(i, "Width") + "x" + mi.GetVideo(i, "Height")); - Add(track, mi.GetVideo(i, "FrameRate") + " FPS"); - Add(track, mi.GetVideo(i, "Language/String")); - Add(track, mi.GetVideo(i, "Forced") == "Yes" ? "Forced" : ""); - Add(track, mi.GetVideo(i, "Default") == "Yes" ? "Default" : ""); - Add(track, mi.GetVideo(i, "Title")); - track.Text = "V: " + track.Text.Trim(' ', ','); - track.Type = "v"; - track.ID = i + 1; - MediaTracks.Add(track); - } + int count = mi.GetCount(MediaInfoStreamKind.Video); - count = mi.GetCount(MediaInfoStreamKind.Audio); + for (int i = 0; i < count; i++) + { + MediaTrack track = new MediaTrack(); + Add(track, mi.GetVideo(i, "Format")); + Add(track, mi.GetVideo(i, "Format_Profile")); + Add(track, mi.GetVideo(i, "Width") + "x" + mi.GetVideo(i, "Height")); + Add(track, mi.GetVideo(i, "FrameRate") + " FPS"); + Add(track, mi.GetVideo(i, "Language/String")); + Add(track, mi.GetVideo(i, "Forced") == "Yes" ? "Forced" : ""); + Add(track, mi.GetVideo(i, "Default") == "Yes" ? "Default" : ""); + Add(track, mi.GetVideo(i, "Title")); + track.Text = "V: " + track.Text.Trim(' ', ','); + track.Type = "v"; + track.ID = i + 1; + MediaTracks.Add(track); + } - for (int i = 0; i < count; i++) - { - MediaTrack track = new MediaTrack(); - Add(track, mi.GetAudio(i, "Language/String")); - Add(track, mi.GetAudio(i, "Format")); - Add(track, mi.GetAudio(i, "Format_Profile")); - Add(track, mi.GetAudio(i, "BitRate/String")); - Add(track, mi.GetAudio(i, "Channel(s)/String")); - Add(track, mi.GetAudio(i, "SamplingRate/String")); - Add(track, mi.GetAudio(i, "Forced") == "Yes" ? "Forced" : ""); - Add(track, mi.GetAudio(i, "Default") == "Yes" ? "Default" : ""); - Add(track, mi.GetAudio(i, "Title")); - track.Text = "A: " + track.Text.Trim(' ', ','); - track.Type = "a"; - track.ID = i + 1; - MediaTracks.Add(track); - } + count = mi.GetCount(MediaInfoStreamKind.Audio); - count = mi.GetCount(MediaInfoStreamKind.Text); + for (int i = 0; i < count; i++) + { + MediaTrack track = new MediaTrack(); + Add(track, mi.GetAudio(i, "Language/String")); + Add(track, mi.GetAudio(i, "Format")); + Add(track, mi.GetAudio(i, "Format_Profile")); + Add(track, mi.GetAudio(i, "BitRate/String")); + Add(track, mi.GetAudio(i, "Channel(s)/String")); + Add(track, mi.GetAudio(i, "SamplingRate/String")); + Add(track, mi.GetAudio(i, "Forced") == "Yes" ? "Forced" : ""); + Add(track, mi.GetAudio(i, "Default") == "Yes" ? "Default" : ""); + Add(track, mi.GetAudio(i, "Title")); + track.Text = "A: " + track.Text.Trim(' ', ','); + track.Type = "a"; + track.ID = i + 1; + MediaTracks.Add(track); + } - for (int i = 0; i < count; i++) - { - MediaTrack track = new MediaTrack(); - Add(track, mi.GetText(i, "Language/String")); - Add(track, mi.GetText(i, "Format")); - Add(track, mi.GetText(i, "Format_Profile")); - Add(track, mi.GetText(i, "Forced") == "Yes" ? "Forced" : ""); - Add(track, mi.GetText(i, "Default") == "Yes" ? "Default" : ""); - Add(track, mi.GetText(i, "Title")); - track.Text = "S: " + track.Text.Trim(' ', ','); - track.Type = "s"; - track.ID = i + 1; - MediaTracks.Add(track); - } + count = mi.GetCount(MediaInfoStreamKind.Text); - count = get_property_int("edition-list/count"); + for (int i = 0; i < count; i++) + { + MediaTrack track = new MediaTrack(); + Add(track, mi.GetText(i, "Language/String")); + Add(track, mi.GetText(i, "Format")); + Add(track, mi.GetText(i, "Format_Profile")); + Add(track, mi.GetText(i, "Forced") == "Yes" ? "Forced" : ""); + Add(track, mi.GetText(i, "Default") == "Yes" ? "Default" : ""); + Add(track, mi.GetText(i, "Title")); + track.Text = "S: " + track.Text.Trim(' ', ','); + track.Type = "s"; + track.ID = i + 1; + MediaTracks.Add(track); + } - for (int i = 0; i < count; i++) - { - MediaTrack track = new MediaTrack(); - track.Text = "E: " + get_property_string($"edition-list/{i}/title"); - track.Type = "e"; - track.ID = i; - MediaTracks.Add(track); - } + count = get_property_int("edition-list/count"); - void Add(MediaTrack track, string val) - { - if (!string.IsNullOrEmpty(val) && !(track.Text != null && track.Text.Contains(val))) - track.Text += " " + val + ","; + for (int i = 0; i < count; i++) + { + MediaTrack track = new MediaTrack(); + track.Text = "E: " + get_property_string($"edition-list/{i}/title"); + track.Type = "e"; + track.ID = i; + MediaTracks.Add(track); + } + + void Add(MediaTrack track, string val) + { + if (!string.IsNullOrEmpty(val) && !(track.Text != null && track.Text.Contains(val))) + track.Text += " " + val + ","; + } } } + } lock (Chapters)