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))
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}");
}
}

View File

@@ -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;

View File

@@ -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<string, string> _Conf;
public static Dictionary<string, string> Conf {

View File

@@ -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);
}

View File

@@ -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,9 +447,12 @@ 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
if (err < 0)
{
if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}");
return 0;
}
return lpBuffer.ToInt32();
}
@@ -454,9 +461,12 @@ 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
if (err < 0)
{
if (throwOnException) throw new Exception($"{name}: {(mpv_error)err}");
return 0;
}
return val;
}
@@ -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<int> 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,7 +699,11 @@ namespace mpvnet
{
MediaTracks.Clear();
using (MediaInfo mi = new MediaInfo(get_property_string("path")))
string path = get_property_string("path");
if (File.Exists(path))
{
using (MediaInfo mi = new MediaInfo(path))
{
int count = mi.GetCount(MediaInfoStreamKind.Video);
@@ -767,6 +780,8 @@ namespace mpvnet
}
}
}
lock (Chapters)
{
Chapters.Clear();