Config: Use Microsoft's INI implementation instead of a 3rd party's

This commit is contained in:
2023-12-02 13:04:50 +01:00
parent 7528764ff3
commit 03a9a3793a
3 changed files with 23 additions and 51 deletions

View File

@ -22,11 +22,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.1" /> <PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.1" />
<PackageReference Include="ini-parser" Version="2.5.2" />
<PackageReference Include="LinqKit" Version="1.2.5" /> <PackageReference Include="LinqKit" Version="1.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.25" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="6.0.25" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2151.40" /> <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2151.40" />
<PackageReference Include="RazorLight" Version="2.3.1" /> <PackageReference Include="RazorLight" Version="2.3.1" />
<PackageReference Include="ScottPlot.WPF" Version="4.1.68" /> <PackageReference Include="ScottPlot.WPF" Version="4.1.68" />

View File

@ -1,8 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using IniParser; using Microsoft.Extensions.Configuration;
using IniParser.Model;
namespace Elwig.Helpers { namespace Elwig.Helpers {
public class Config { public class Config {
@ -13,7 +12,8 @@ namespace Elwig.Helpers {
public string? DatabaseLog = null; public string? DatabaseLog = null;
public string? Branch = null; public string? Branch = null;
public IList<string?[]> Scales; public IList<string?[]> Scales;
private readonly List<string?[]> ScaleList = new(); private readonly List<string?[]> ScaleList = [];
private static readonly string[] trueValues = ["1", "true", "yes", "on"];
public Config(string filename) { public Config(string filename) {
FileName = filename; FileName = filename;
@ -22,57 +22,24 @@ namespace Elwig.Helpers {
} }
public void Read() { public void Read() {
var parser = new FileIniDataParser(); var config = new ConfigurationBuilder().AddIniFile(FileName).Build();
IniData? ini = null;
try {
ini = parser.ReadFile(FileName, Utils.UTF8);
} catch {}
if (ini == null || !ini.TryGetKey("database.file", out string db)) { DatabaseFile = Utils.GetAbsolutePath(config["database:file"] ?? "database.sqlite3", App.DataPath);
DatabaseFile = App.DataPath + "database.sqlite3"; var log = config["database:log"];
} else if (db.Length > 1 && (db[1] == ':' || db[0] == '/' || db[0] == '\\')) { DatabaseLog = log != null ? Utils.GetAbsolutePath(log, App.DataPath) : null;
DatabaseFile = db; Branch = config["general:branch"];
} else { Debug = trueValues.Contains(config["general:debug"]?.ToLower());
DatabaseFile = App.DataPath + db;
}
if (ini == null || !ini.TryGetKey("database.log", out string log)) {
DatabaseLog = null;
} else if (log.Length > 1 && (log[1] == ':' || log[0] == '/' || log[0] == '\\')) {
DatabaseLog = log;
} else {
DatabaseLog = App.DataPath + log;
}
if (ini == null || !ini.TryGetKey("general.branch", out string branch)) {
Branch = null;
} else {
Branch = branch;
}
if (ini == null || !ini.TryGetKey("general.debug", out string debug)) {
Debug = false;
} else {
debug = debug.ToLower();
Debug = debug == "1" || debug == "true" || debug == "yes" || debug == "on";
}
var scales = config.AsEnumerable().Where(i => i.Key.StartsWith("scale.")).GroupBy(i => i.Key.Split(':')[0][6..]).Select(i => i.Key);
ScaleList.Clear(); ScaleList.Clear();
Scales = ScaleList; Scales = ScaleList;
if (ini != null) { foreach (var s in scales) {
foreach (var s in ini.Sections.Where(s => s.SectionName.StartsWith("scale."))) { string? scaleLog = config[$"scale.{s}:log"];
string? scaleLog = null; if (scaleLog != null) scaleLog = Utils.GetAbsolutePath(scaleLog, App.DataPath);
if (s.Keys["log"] != null) { ScaleList.Add([
scaleLog = s.Keys["log"]; s, config[$"scale.{s}:type"], config[$"scale.{s}:model"], config[$"scale.{s}:connection"],
if (scaleLog.Length <= 1 || (scaleLog[1] != ':' && scaleLog[0] != '/' && scaleLog[0] != '\\')) { config[$"scale.{s}:empty"], config[$"scale.{s}:filling"], config[$"scale.{s}:limit"], scaleLog
scaleLog = App.DataPath + scaleLog; ]);
}
}
ScaleList.Add(new string?[] {
s.SectionName[6..], s.Keys["type"], s.Keys["model"], s.Keys["connection"],
s.Keys["empty"], s.Keys["filling"], s.Keys["limit"], scaleLog
});
}
} }
} }

View File

@ -11,6 +11,7 @@ using Elwig.Dialogs;
using System.Text; using System.Text;
using System.Numerics; using System.Numerics;
using Elwig.Models.Entities; using Elwig.Models.Entities;
using System.IO;
namespace Elwig.Helpers { namespace Elwig.Helpers {
public static partial class Utils { public static partial class Utils {
@ -345,5 +346,9 @@ namespace Elwig.Helpers {
} }
return output.OrderByDescending(l => l.Count()); return output.OrderByDescending(l => l.Count());
} }
public static string GetAbsolutePath(string path, string basePath) {
return (path.Length > 1 && (path[1] == ':' || path[0] == '/' || path[0] == '\\')) ? Path.Combine(basePath, path) : path;
}
} }
} }