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