Refactoring

This commit is contained in:
stax76
2026-01-09 09:14:02 +01:00
parent 9686c28f6a
commit 724a9adab8
14 changed files with 68 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
 
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet.Windows; namespace MpvNet.Windows;

View File

@@ -7,7 +7,7 @@ using System.Windows.Forms;
using System.Windows.Interop; using System.Windows.Interop;
using System.Windows; using System.Windows;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
using MpvNet.Windows.WinForms; using MpvNet.Windows.WinForms;
using MpvNet.Windows.WPF.Views; using MpvNet.Windows.WPF.Views;
using MpvNet.Windows.WPF; using MpvNet.Windows.WPF;
@@ -332,7 +332,7 @@ public class GuiCommand
int width = Player.GetPropertyInt("video-params/w"); int width = Player.GetPropertyInt("video-params/w");
int height = Player.GetPropertyInt("video-params/h"); int height = Player.GetPropertyInt("video-params/h");
TimeSpan len = TimeSpan.FromSeconds(Player.GetPropertyDouble("duration")); TimeSpan len = TimeSpan.FromSeconds(Player.GetPropertyDouble("duration"));
text = path.FileName() + "\n"; text = path.FileName + "\n";
text += FormatTime(len.TotalMinutes) + ":" + FormatTime(len.Seconds) + "\n"; text += FormatTime(len.TotalMinutes) + ":" + FormatTime(len.Seconds) + "\n";
if (fileSize > 0) if (fileSize > 0)
text += Convert.ToInt32(fileSize / 1024.0 / 1024.0) + " MB\n"; text += Convert.ToInt32(fileSize / 1024.0 / 1024.0) + " MB\n";

View File

@@ -2,7 +2,7 @@
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet.Windows.UI; namespace MpvNet.Windows.UI;

View File

@@ -10,7 +10,7 @@ using System.Text.RegularExpressions;
using MpvNet.Windows.WPF; using MpvNet.Windows.WPF;
using MpvNet.Windows.UI; using MpvNet.Windows.UI;
using MpvNet.Help; using MpvNet.Help;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
using MpvNet.MVVM; using MpvNet.MVVM;
using MpvNet.Windows.WPF.MsgBox; using MpvNet.Windows.WPF.MsgBox;

View File

@@ -1,7 +1,7 @@
 
using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.Mvvm.Messaging;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
using MpvNet.Help; using MpvNet.Help;
using MpvNet.MVVM; using MpvNet.MVVM;

View File

@@ -1,5 +1,5 @@
 
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet; namespace MpvNet;

View File

@@ -1,7 +1,7 @@
 
using System.Reflection; using System.Reflection;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet; namespace MpvNet;
@@ -34,7 +34,7 @@ public class ExtensionLoader
{ {
foreach (string dir in Directory.GetDirectories(path)) foreach (string dir in Directory.GetDirectories(path))
{ {
LoadDll(dir.AddSep() + Path.GetFileName(dir) + ".dll"); LoadDll(dir.Separator + Path.GetFileName(dir) + ".dll");
} }
} }
} }

View File

@@ -1,7 +0,0 @@

namespace MpvNet.ExtensionMethod;
public static class ObjectExtension
{
public static string ToStringEx(this object instance) => instance?.ToString() ?? "";
}

View File

@@ -0,0 +1,10 @@

namespace MpvNet.Extensions;
public static class ObjectExtensions
{
extension(object instance)
{
public string ToStringEx() => instance?.ToString() ?? "";
}
}

View File

@@ -1,5 +1,5 @@
 
namespace MpvNet.ExtensionMethod; namespace MpvNet.Extensions;
public static class PathStringExtensions public static class PathStringExtensions
{ {
@@ -18,56 +18,60 @@ public static class PathStringExtensions
{ {
if (chars[x] == '/') if (chars[x] == '/')
return ""; return "";
if (chars[x] == '\\') if (chars[x] == '\\')
return ""; return "";
if (chars[x] == '.') if (chars[x] == '.')
return path[(x + (includeDot ? 0 : 1))..].ToLowerInvariant(); return path[(x + (includeDot ? 0 : 1))..].ToLowerInvariant();
} }
return ""; return "";
} }
}
public static string FileName(this string instance) public string FileName
{ {
if (string.IsNullOrEmpty(instance)) get
return ""; {
if (string.IsNullOrEmpty(filepath))
return "";
int index = instance.LastIndexOf('\\'); int index = filepath.LastIndexOf('\\');
if (index > -1) if (index > -1)
return instance[(index + 1)..]; return filepath[(index + 1)..];
index = instance.LastIndexOf('/'); index = filepath.LastIndexOf('/');
if (index > -1) if (index > -1)
return instance[(index + 1)..]; return filepath[(index + 1)..];
return instance; return filepath;
} }
}
public static string ShortPath(this string instance, int maxLength) public string ShortPath(int maxLength)
{ {
if (string.IsNullOrEmpty(instance)) if (string.IsNullOrEmpty(filepath))
return ""; return "";
if (instance.Length > maxLength && instance.Substring(1, 2) == ":\\") if (filepath.Length > maxLength && filepath.Substring(1, 2) == ":\\")
instance = instance[..3] + "...\\" + instance.FileName(); filepath = $"{filepath[..3]}...\\{filepath.FileName}";
return instance; return filepath;
} }
// Ensure trailing directory separator char // Ensure trailing directory separator char
public static string AddSep(this string instance) public string Separator
{ {
if (string.IsNullOrEmpty(instance)) get
return ""; {
if (string.IsNullOrEmpty(filepath))
return "";
if (!instance.EndsWith(Path.DirectorySeparatorChar.ToString())) if (!filepath.EndsWith(Path.DirectorySeparatorChar.ToString()))
instance = instance + Path.DirectorySeparatorChar; filepath = filepath + Path.DirectorySeparatorChar;
return instance; return filepath;
}
}
} }
} }

View File

@@ -1,15 +1,15 @@
 
using System.Globalization; using System.Globalization;
namespace MpvNet.ExtensionMethod; namespace MpvNet.Extensions;
public static class StringExtension public static class StringExtensions
{ {
public static string ToUpperEx(this string instance) => (instance != null) ? instance.ToUpperInvariant() : ""; public static string ToUpperEx(this string instance) => instance?.ToUpperInvariant() ?? "";
public static string ToLowerEx(this string instance) => (instance != null) ? instance.ToLowerInvariant() : ""; public static string ToLowerEx(this string instance) => instance?.ToLowerInvariant() ?? "";
public static string TrimEx(this string? instance) => (instance == null) ? "" : instance.Trim(); public static string TrimEx(this string? instance) => instance?.Trim() ?? "";
public static int ToInt(this string instance, int defaultValue = 0) public static int ToInt(this string instance, int defaultValue = 0)
{ {

View File

@@ -1,5 +1,5 @@
 
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet; namespace MpvNet;

View File

@@ -1,10 +1,10 @@
 
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
namespace MpvNet; namespace MpvNet;
public class Folder public class Folder
{ {
public static string Startup { get; } = Path.GetDirectoryName(Environment.ProcessPath)!.AddSep(); public static string Startup { get; } = Path.GetDirectoryName(Environment.ProcessPath)!.Separator;
public static string AppData { get; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).AddSep(); public static string AppData { get; } = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).Separator;
} }

View File

@@ -8,7 +8,7 @@ using System.Text.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using MpvNet.ExtensionMethod; using MpvNet.Extensions;
using MpvNet.Help; using MpvNet.Help;
using MpvNet.Native; using MpvNet.Native;
@@ -127,7 +127,7 @@ public class MainPlayer : MpvClient
{ {
string configDir = CommandLine.GetValue("config-dir"); string configDir = CommandLine.GetValue("config-dir");
string fullPath = System.IO.Path.GetFullPath(configDir); string fullPath = System.IO.Path.GetFullPath(configDir);
App.InputConf.Path = fullPath.AddSep() + "input.conf"; App.InputConf.Path = fullPath.Separator + "input.conf";
string content = App.InputConf.GetContent(); string content = App.InputConf.GetContent();
if (!string.IsNullOrEmpty(content)) if (!string.IsNullOrEmpty(content))
@@ -250,7 +250,7 @@ public class MainPlayer : MpvClient
string? mpvnet_home = Environment.GetEnvironmentVariable("MPVNET_HOME"); string? mpvnet_home = Environment.GetEnvironmentVariable("MPVNET_HOME");
if (Directory.Exists(mpvnet_home)) if (Directory.Exists(mpvnet_home))
return _configFolder = mpvnet_home.AddSep(); return _configFolder = mpvnet_home.Separator;
_configFolder = Folder.Startup + "portable_config"; _configFolder = Folder.Startup + "portable_config";
@@ -260,7 +260,7 @@ public class MainPlayer : MpvClient
if (!Directory.Exists(_configFolder)) if (!Directory.Exists(_configFolder))
Directory.CreateDirectory(_configFolder); Directory.CreateDirectory(_configFolder);
_configFolder = _configFolder.AddSep(); _configFolder = _configFolder.Separator;
} }
return _configFolder; return _configFolder;