tweaks and refactoring

This commit is contained in:
Frank Skare
2019-07-18 05:47:16 +02:00
parent 01da76bdc4
commit 85a23fbae1
5 changed files with 110 additions and 96 deletions

View File

@@ -59,7 +59,9 @@ namespace RatingAddon
{ {
if (args[0] != "rate-file" || ! int.TryParse(args[1], out int rating)) if (args[0] != "rate-file" || ! int.TryParse(args[1], out int rating))
return; 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}"); mp.commandv("show-text", $"Rating: {rating}");
} }
} }

View File

@@ -186,7 +186,8 @@ namespace mpvnet
InvokeOnMainThread(new Action(() => { InvokeOnMainThread(new Action(() => {
using (var d = new OpenFileDialog()) 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; d.Multiselect = true;
if (d.ShowDialog() == DialogResult.OK) if (d.ShowDialog() == DialogResult.OK)
foreach (string i in d.FileNames) foreach (string i in d.FileNames)
@@ -200,7 +201,9 @@ namespace mpvnet
InvokeOnMainThread(new Action(() => { InvokeOnMainThread(new Action(() => {
using (var d = new OpenFileDialog()) 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; d.Multiselect = true;
if (d.ShowDialog() == DialogResult.OK) if (d.ShowDialog() == DialogResult.OK)
@@ -212,10 +215,10 @@ namespace mpvnet
public static void CycleAudio() public static void CycleAudio()
{ {
string filePath = mp.get_property_string("path", false); string path = mp.get_property_string("path");
if (!File.Exists(filePath)) return; 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(); MediaTrack[] audTracks = mp.MediaTracks.Where(track => track.Type == "a").ToArray();
if (audTracks.Length < 2) return; if (audTracks.Length < 2) return;

View File

@@ -53,7 +53,6 @@ namespace mpvnet
{ {
string filePath = mp.ConfFolder + "\\mpvnet-debug.log"; string filePath = mp.ConfFolder + "\\mpvnet-debug.log";
if (File.Exists(filePath)) File.Delete(filePath); if (File.Exists(filePath)) File.Delete(filePath);
Trace.Listeners.Clear();
Trace.Listeners.Add(new TextWriterTraceListener(filePath)); Trace.Listeners.Add(new TextWriterTraceListener(filePath));
Trace.AutoFlush = true; Trace.AutoFlush = true;
} }
@@ -64,11 +63,6 @@ namespace mpvnet
} }
} }
public static void Exit()
{
if (Trace.Listeners.Count > 0) Trace.Listeners[0].Close();
}
static Dictionary<string, string> _Conf; static Dictionary<string, string> _Conf;
public static Dictionary<string, string> Conf { public static Dictionary<string, string> Conf {

View File

@@ -8,6 +8,7 @@ using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Diagnostics;
namespace mpvnet namespace mpvnet
{ {
@@ -378,7 +379,7 @@ namespace mpvnet
protected override void WndProc(ref Message m) protected override void WndProc(ref Message m)
{ {
//System.Diagnostics.Debug.WriteLine(m); //Debug.WriteLine(m);
switch (m.Msg) switch (m.Msg)
{ {
@@ -549,7 +550,6 @@ namespace mpvnet
} }
RegHelp.SetObject(App.RegPath, "Recent", RecentFiles.ToArray()); RegHelp.SetObject(App.RegPath, "Recent", RecentFiles.ToArray());
App.Exit();
mp.commandv("quit"); mp.commandv("quit");
mp.ShutdownAutoResetEvent.WaitOne(3000); mp.ShutdownAutoResetEvent.WaitOne(3000);
} }

View File

@@ -410,16 +410,14 @@ namespace mpvnet
{ {
if (Handle == IntPtr.Zero) return; if (Handle == IntPtr.Zero) return;
int err = mpv_command_string(Handle, command); int err = mpv_command_string(Handle, command);
if (err < 0 && throwException) if (err < 0 && throwException) throw new Exception($"{(mpv_error)err}\n\n" + command);
throw new Exception($"{(mpv_error)err}\r\n\r\n" + 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 throwOnException = false)
{ {
byte[] bytes = GetUtf8Bytes(value); byte[] bytes = GetUtf8Bytes(value);
int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes); int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, ref bytes);
if (err < 0 && throwOnException) if (err < 0 && throwOnException) throw new Exception($"{name}: {(mpv_error)err}");
throw new Exception($"{name}: {(mpv_error)err}");
} }
public static string get_property_string(string name, bool throwOnException = false) public static string get_property_string(string name, bool throwOnException = false)
@@ -427,7 +425,13 @@ namespace mpvnet
try try
{ {
int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_STRING, out IntPtr lpBuffer); 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); string ret = StringFromNativeUtf8(lpBuffer);
mpv_free(lpBuffer); mpv_free(lpBuffer);
return ret; return ret;
@@ -443,10 +447,13 @@ namespace mpvnet
{ {
int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, out IntPtr lpBuffer); int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, out IntPtr lpBuffer);
if (err < 0 && throwOnException) if (err < 0)
throw new Exception($"{name}: {(mpv_error)err}"); {
else if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}");
return lpBuffer.ToInt32(); return 0;
}
return lpBuffer.ToInt32();
} }
public static double get_property_number(string name, bool throwOnException = false) public static double get_property_number(string name, bool throwOnException = false)
@@ -454,10 +461,13 @@ namespace mpvnet
double val = 0; double val = 0;
int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_DOUBLE, ref val); int err = mpv_get_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_DOUBLE, ref val);
if (err < 0 && throwOnException) if (err < 0)
throw new Exception($"{name}: {(mpv_error)err}"); {
else if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}");
return val; return 0;
}
return val;
} }
public static bool get_property_bool(string name, bool throwOnException = false) public static bool get_property_bool(string name, bool throwOnException = false)
@@ -474,9 +484,7 @@ namespace mpvnet
{ {
Int64 val = value; Int64 val = value;
int err = mpv_set_property(Handle, GetUtf8Bytes(name), mpv_format.MPV_FORMAT_INT64, ref val); 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<int> action) public static void observe_property_int(string name, Action<int> action)
@@ -642,8 +650,9 @@ namespace mpvnet
static string LastHistoryPath; static string LastHistoryPath;
static DateTime LastHistoryStartDateTime; 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); int totalMinutes = Convert.ToInt32((DateTime.Now - LastHistoryStartDateTime).TotalMinutes);
if (File.Exists(LastHistoryPath) && totalMinutes > 1) if (File.Exists(LastHistoryPath) && totalMinutes > 1)
@@ -655,7 +664,7 @@ namespace mpvnet
Path.GetFileNameWithoutExtension(LastHistoryPath) + "\r\n"); Path.GetFileNameWithoutExtension(LastHistoryPath) + "\r\n");
} }
LastHistoryPath = filePath; LastHistoryPath = path;
LastHistoryStartDateTime = DateTime.Now; LastHistoryStartDateTime = DateTime.Now;
} }
@@ -690,81 +699,87 @@ namespace mpvnet
{ {
MediaTracks.Clear(); 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); using (MediaInfo mi = new MediaInfo(path))
for (int i = 0; i < count; i++)
{ {
MediaTrack track = new MediaTrack(); int count = mi.GetCount(MediaInfoStreamKind.Video);
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);
}
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++) count = mi.GetCount(MediaInfoStreamKind.Audio);
{
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.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++) count = mi.GetCount(MediaInfoStreamKind.Text);
{
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 = 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++) count = get_property_int("edition-list/count");
{
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) for (int i = 0; i < count; i++)
{ {
if (!string.IsNullOrEmpty(val) && !(track.Text != null && track.Text.Contains(val))) MediaTrack track = new MediaTrack();
track.Text += " " + val + ","; 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) lock (Chapters)