-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.Config.cs
77 lines (70 loc) · 2.05 KB
/
Helper.Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using Microsoft.Extensions.Configuration;
namespace Helper;
public partial class Config
{
private readonly string file;
private readonly IConfigurationRoot config;
private readonly IConfigurationSection section;
public Config(string path, string key = "General")
{
ArgumentNullException.ThrowIfNull(StringH.WhiteSpaceCheck(path), nameof(path));
file = path + "\\config.ini";
if (!File.Exists(file)) File.Create(file).Close();
config = new ConfigurationBuilder()
.AddIniFile(file)
.Build();
section = config.GetSection(key);
}
public void Reload() => config.Reload();
public async void Save()
{
await File.WriteAllTextAsync(file, ToString());
Reload();
}
public override string ToString()
{
string result = "";
foreach (var item in config.AsEnumerable())
{
if (item.Value is null && !item.Key.Contains(':'))
{
result += $"[{item.Key}]\r\n";
}
else if (item.Value is not null && item.Key.Contains(':'))
{
result += $"{item.Key[(item.Key.IndexOf(':') + 1)..]}={item.Value}\r\n";
}
}
return result;
}
public string? Version
{
get { return section["game_version"]; }
set { section["game_version"] = value; }
}
public string? Channel
{
get
{
string? channel = section["channel"], sub_channel = section["sub_channel"];
if (channel != null && sub_channel != null)
{
return $"hk4e_{channel}_{sub_channel}";
}
else
{
return null;
}
}
set
{
string? channel = value?[5..^2];
string? sub_channel = value?[^1..];
if (channel != null && sub_channel != null)
{
section["channel"] = channel;
section["sub_channel"] = sub_channel;
}
}
}
}