-
Notifications
You must be signed in to change notification settings - Fork 3
/
Config.cs
101 lines (93 loc) · 4.01 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Linq;
namespace Cliver.Bot
{
public class Config
{
public static bool ReadOnly { get; private set; } = true;
static public void Reload(string configfile = null)
{
Cliver.Config.Reload();
foreach (SettingsFieldInfo sfi in Cliver.Config.GetSettingsFieldInfos())
sections2SettingsFieldInfo[sfi.Name] = sfi;
if (configfile == null)
{
ReadOnly = false;
}
else
{
ReadOnly = true;
Dictionary<string, Dictionary<string, object>> sections2parameters2value = Serialization.Json.Load<Dictionary<string, Dictionary<string, object>>>(configfile);
foreach (string section in sections2parameters2value.Keys)
{
Dictionary<string, object> parameters2value = sections2parameters2value[section];
SettingsFieldInfo sfi = sections2SettingsFieldInfo[section];
Cliver.Settings o = sfi.GetObject();
foreach (string p in parameters2value.Keys)
{
FieldInfo fi = sfi.Type.GetField(p, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
object v = parameters2value[p];
setValue(fi, o, v);
}
}
}
}
static Dictionary<string, SettingsFieldInfo> sections2SettingsFieldInfo = new Dictionary<string, SettingsFieldInfo>();
static void setValue(FieldInfo fi, object o, object v)
{
if (v is string)
{
if (fi.FieldType == typeof(int))
v = int.Parse((string)v);
else if (fi.FieldType == typeof(double))
v = double.Parse((string)v);
else if (fi.FieldType == typeof(long))
v = long.Parse((string)v);
}
fi.SetValue(o, v);
}
static public void Save(string configfile = null)
{
if (configfile == null)
{
Cliver.Config.Save();
}
else
{
Dictionary<string, Dictionary<string, object>> sections2parameters2value = new Dictionary<string, Dictionary<string, object>>();
foreach (string s in sections2SettingsFieldInfo.Keys)
{
SettingsFieldInfo sfi = sections2SettingsFieldInfo[s];
Dictionary<string, object> parameters2value = new Dictionary<string, object>();
sections2parameters2value[s] = parameters2value;
Cliver.Settings o = sfi.GetObject();
foreach (FieldInfo fi in sfi.Type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
parameters2value[fi.Name] = fi.GetValue(o);
}
Serialization.Json.Save(configfile, sections2parameters2value);
}
}
static public void Set(string section, string parameter, object value)
{
SettingsFieldInfo sfi = sections2SettingsFieldInfo[section];
FieldInfo fi = sfi.Type.GetField(parameter, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Cliver.Settings o = sfi.GetObject();
setValue(fi, o, value);
}
static public object Get(string section, string parameter)
{
SettingsFieldInfo sfi = sections2SettingsFieldInfo[section];
FieldInfo fi = sfi.Type.GetField(parameter, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
Cliver.Settings o = sfi.GetObject();
return fi.GetValue(o);
}
}
}