97 lines
3.7 KiB
C#
97 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Elwig.Helpers {
|
|
|
|
public record struct ScaleConfig {
|
|
public string Id;
|
|
public string? Type;
|
|
public string? Model;
|
|
public string? Connection;
|
|
public string? Empty;
|
|
public string? Filling;
|
|
public string? Limit;
|
|
public string? Log;
|
|
public string? _Log;
|
|
|
|
public ScaleConfig(string id, string? type, string? model, string? cnx, string? empty, string? filling, string? limit, string? log) {
|
|
Id = id;
|
|
Type = type;
|
|
Model = model;
|
|
Connection = cnx;
|
|
Empty = empty;
|
|
Filling = filling;
|
|
Limit = limit;
|
|
_Log = log;
|
|
Log = log != null ? Path.Combine(App.DataPath, log) : null;
|
|
}
|
|
}
|
|
|
|
public class Config {
|
|
|
|
private static readonly string[] TrueValues = ["1", "true", "yes", "on"];
|
|
|
|
private readonly string FileName;
|
|
|
|
public bool Debug;
|
|
public string DatabaseFile = App.DataPath + "database.sqlite3";
|
|
public string? DatabaseLog = null;
|
|
public string? Branch = null;
|
|
public string? UpdateUrl = null;
|
|
public bool UpdateAuto = false;
|
|
|
|
public IList<ScaleConfig> Scales;
|
|
private readonly List<ScaleConfig> ScaleList = [];
|
|
|
|
public Config(string filename) {
|
|
FileName = filename;
|
|
Scales = ScaleList;
|
|
Read();
|
|
}
|
|
|
|
public void Read() {
|
|
var config = new ConfigurationBuilder().AddIniFile(FileName).Build();
|
|
|
|
DatabaseFile = Path.Combine(App.DataPath, config["database:file"] ?? "database.sqlite3");
|
|
var log = config["database:log"];
|
|
DatabaseLog = log != null ? Path.Combine(App.DataPath, log) : null;
|
|
Branch = config["general:branch"];
|
|
Debug = TrueValues.Contains(config["general:debug"]?.ToLower());
|
|
UpdateUrl = config["update:url"];
|
|
UpdateAuto = TrueValues.Contains(config["update:auto"]?.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;
|
|
foreach (var s in scales) {
|
|
ScaleList.Add(new(
|
|
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"], config[$"scale.{s}:log"]
|
|
));
|
|
}
|
|
}
|
|
|
|
public void Write() {
|
|
using var file = new StreamWriter(FileName, false, Utils.UTF8);
|
|
file.Write($"\r\n[general]\r\n");
|
|
if (Branch != null) file.Write($"branch = {Branch}\r\n");
|
|
if (Debug) file.Write("debug = true\r\n");
|
|
file.Write($"\r\n[database]\r\nfile = {DatabaseFile}\r\n");
|
|
if (DatabaseLog != null) file.Write($"log = {DatabaseLog}\r\n");
|
|
file.Write($"\r\n[update]\r\n");
|
|
if (UpdateUrl != null) file.Write($"url = {UpdateUrl}\r\n");
|
|
if (UpdateAuto) file.Write($"auto = true\r\n");
|
|
|
|
foreach (var s in ScaleList) {
|
|
file.Write($"\r\n[scale.{s.Id}]\r\ntype = {s.Type}\r\nmodel = {s.Model}\r\nconnection = {s.Connection}\r\n");
|
|
if (s.Empty != null) file.Write($"empty = {s.Empty}\r\n");
|
|
if (s.Filling != null) file.Write($"filling = {s.Filling}\r\n");
|
|
if (s.Limit != null) file.Write($"limit = {s.Limit}\r\n");
|
|
if (s._Log != null) file.Write($"log = {s._Log}\r\n");
|
|
}
|
|
}
|
|
}
|
|
}
|