Skip to content

Commit

Permalink
Improved conf file reader/writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
stax76 committed Dec 14, 2023
1 parent 35b17bc commit d4d147e
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 265 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

# v7.0.0.3 Beta (2023-??-??)

- mpv.net can no longer be downloaded from the Microsoft store due
to a general very poor experience with the package creation and submission.
This means winget download support is unavailable until a new winget solution is implemented.
- New menu item added at Settings/Setup to add mpv.net to the path environment variable.
- Improved conf file reader/writer.

# v7.0.0.2 Beta (2023-12-13)

Expand Down
3 changes: 3 additions & 0 deletions src/MpvNet.Windows/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ dotnet_diagnostic.IDE0044.severity = silent

# Member does not access instance data and can be marked as static
dotnet_diagnostic.CA1822.severity = none

# IDE0057: Use range operator
csharp_style_prefer_range_operator = false
71 changes: 36 additions & 35 deletions src/MpvNet.Windows/WPF/ConfWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -131,7 +132,7 @@ void LoadSettings()
{
if (setting.Name == confItem.Name && confItem.Section == "" && !confItem.IsSectionItem)
{
setting.Value = confItem.Value.Trim('\'', '"');
setting.Value = confItem.Value;
setting.StartValue = setting.Value;
setting.ConfItem = confItem;
confItem.SettingBase = setting;
Expand Down Expand Up @@ -221,14 +222,10 @@ void LoadConf(string file)
string line = currentLine.Trim();

if (line == "")
{
comment += "\r\n";
}
else if (line.StartsWith("#"))
{
comment += line.Trim() + "\r\n";
}
else if (line.StartsWith("[") && line.Contains("]"))
else if (line.StartsWith("[") && line.Contains(']'))
{
if (!isSectionItem && comment != "" && comment != "\r\n")
ConfItems.Add(new ConfItem() {
Expand All @@ -238,8 +235,11 @@ void LoadConf(string file)
comment = "";
isSectionItem = true;
}
else if (line.Contains("="))
else if (line.Contains('=') || Regex.Match(line, "^[\\w-]+$").Success)
{
if (!line.Contains('='))
line += "=yes";

ConfItem item = new ConfItem();
item.File = Path.GetFileNameWithoutExtension(file);
item.IsSectionItem = isSectionItem;
Expand All @@ -248,15 +248,21 @@ void LoadConf(string file)
item.Section = section;
section = "";

if (line.Contains("#") && !line.Contains("'") && !line.Contains("\""))
if (line.Contains('#') && !line.Contains("'") && !line.Contains("\""))
{
item.LineComment = line.Substring(line.IndexOf("#")).Trim();
line = line.Substring(0, line.IndexOf("#")).Trim();
}

int pos = line.IndexOf("=");
string left = line.Substring(0, pos).Trim().ToLower();
string left = line.Substring(0, pos).Trim().ToLower().TrimStart('-');
string right = line.Substring(pos + 1).Trim();

if (right.StartsWith('\'') && right.EndsWith('\''))
right = right.Trim('\'');

if (right.StartsWith('"') && right.EndsWith('"'))
right = right.Trim('"');

if (left == "fs")
left = "fullscreen";
Expand All @@ -271,6 +277,23 @@ void LoadConf(string file)
}
}

string EscapeValue(string value)
{
if (value.Contains('\''))
return '"' + value + '"';

if (value.Contains('"'))
return '\'' + value + '\'';

if (value.Contains('"') || value.Contains('#') || value.StartsWith("%") ||
value.StartsWith(" ") || value.EndsWith(" "))
{
return '\'' + value + '\'';
}

return value;
}

string GetContent(string filename)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -288,7 +311,7 @@ string GetContent(string filename)
{
if (item.Name != "")
{
sb.Append(item.Name + " = " + item.Value);
sb.Append(item.Name + " = " + EscapeValue(item.Value));

if (item.LineComment != "")
sb.Append(" " + item.LineComment);
Expand All @@ -299,17 +322,7 @@ string GetContent(string filename)
}
else if ((item.SettingBase.Value ?? "") != item.SettingBase.Default)
{
string? value;

if (item.SettingBase.Type == "string" ||
item.SettingBase.Type == "folder" ||
item.SettingBase.Type == "color")

value = "'" + item.SettingBase.Value + "'";
else
value = item.SettingBase.Value;

sb.Append(item.Name + " = " + value);
sb.Append(item.Name + " = " + EscapeValue(item.SettingBase.Value!));

if (item.LineComment != "")
sb.Append(" " + item.LineComment);
Expand All @@ -325,19 +338,7 @@ string GetContent(string filename)
continue;

if ((setting.Value ?? "") != setting.Default)
{
string? value;

if (setting.Type == "string" ||
setting.Type == "folder" ||
setting.Type == "color")

value = "'" + setting.Value + "'";
else
value = setting.Value;

sb.AppendLine(setting.Name + " = " + value);
}
sb.AppendLine(setting.Name + " = " + EscapeValue(setting.Value!));
}

foreach (ConfItem item in ConfItems)
Expand All @@ -356,7 +357,7 @@ string GetContent(string filename)
if (item.Comment != "")
sb.Append(item.Comment);

sb.Append(item.Name + " = " + item.Value);
sb.Append(item.Name + " = " + EscapeValue(item.Value));

if (item.LineComment != "")
sb.Append(" " + item.LineComment);
Expand Down
49 changes: 29 additions & 20 deletions src/MpvNet/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,38 +268,47 @@ public string ConfigFolder {
Dictionary<string, string>? _Conf;

public Dictionary<string, string> Conf {
get {
if (_Conf == null)
{
App.ApplyInputDefaultBindingsFix();
get
{
if (_Conf != null)
return _Conf;

_Conf = new Dictionary<string, string>();
App.ApplyInputDefaultBindingsFix();

if (File.Exists(ConfPath))
_Conf = new Dictionary<string, string>();

if (File.Exists(ConfPath))
{
foreach (string? it in File.ReadAllLines(ConfPath))
{
foreach (string? it in File.ReadAllLines(ConfPath))
{
string line = it.TrimStart(' ', '-').TrimEnd();
string line = it.TrimStart(' ', '-').TrimEnd();

if (!line.Contains('=') || line.StartsWith("#"))
if (line.StartsWith("#"))
continue;

if (!line.Contains('='))
{
if (Regex.Match(line, "^[\\w-]+$").Success)
line += "=yes";
else
continue;
}

string key = line[..line.IndexOf("=")].Trim();
string value = line[(line.IndexOf("=") + 1)..].Trim();
string key = line[..line.IndexOf("=")].Trim();
string value = line[(line.IndexOf("=") + 1)..].Trim();

if (value.Contains('#') && !value.StartsWith("#") &&
!value.StartsWith("'#") && !value.StartsWith("\"#"))
if (value.Contains('#') && !value.StartsWith("#") &&
!value.StartsWith("'#") && !value.StartsWith("\"#"))

value = value[..value.IndexOf("#")].Trim();
value = value[..value.IndexOf("#")].Trim();

_Conf[key] = value;
}
_Conf[key] = value;
}

foreach (var i in _Conf)
ProcessProperty(i.Key, i.Value);
}

foreach (var i in _Conf)
ProcessProperty(i.Key, i.Value);

return _Conf;
}
}
Expand Down
Binary file removed src/Setup/MSIX/Images/LockScreenLogo.scale-200.png
Binary file not shown.
Binary file removed src/Setup/MSIX/Images/SplashScreen.scale-200.png
Binary file not shown.
Binary file not shown.
Binary file removed src/Setup/MSIX/Images/Square44x44Logo.scale-200.png
Binary file not shown.
Binary file not shown.
Binary file removed src/Setup/MSIX/Images/StoreLogo.png
Binary file not shown.
Binary file removed src/Setup/MSIX/Images/Wide310x150Logo.scale-200.png
Binary file not shown.
73 changes: 0 additions & 73 deletions src/Setup/MSIX/MSIX.wapproj

This file was deleted.

Loading

0 comments on commit d4d147e

Please sign in to comment.