-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
194 lines (173 loc) · 5.11 KB
/
Program.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Reflection;
using System.Text.Json;
using AutoUpdateConfig;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
try
{
// 示例配置文件
const string exampleConfig = "app.example.json";
// 本程序的配置文件
const string programConfig = "app.json";
if (!File.Exists(exampleConfig))
{
Log.Error($"Not fonud \"{exampleConfig}\"");
goto EXIT;
}
if (!File.Exists(programConfig))
{
File.Copy(exampleConfig, programConfig);
}
// 读取程序配置文件内容
var strConfig = File.ReadAllText(programConfig);
// 程序配置字典
var ditcConfig = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(strConfig);
if (ditcConfig == null)
{
Log.Error("Load config failed");
goto EXIT;
}
// 订阅地址 用于下载最新的yaml
var downloadLink = ditcConfig["downloadLink"].ToString();
if (downloadLink.IsNullOrWhiteSpace())
{
Log.Error("Parameter \"downloadLink\" is empty");
goto EXIT;
}
// 合并后最终的文件名
var saveName = ditcConfig["saveName"].ToString();
if (saveName == null)
{
Log.Error("Parameter \"saveName\" is empty");
goto EXIT;
}
// 自定义的配置项
var custom = ditcConfig["custom"].Deserialize<Dictionary<string, object>>();
if (custom == null)
{
Log.Error("Parameter \"custom\" is empty");
goto EXIT;
}
var strLastYaml = await Utils.GetContent(downloadLink);
if (strLastYaml.IsNullOrWhiteSpace())
{
Log.Error("Empty data");
goto EXIT;
}
// 创建一个 Deserializer 对象
var deserializer = new DeserializerBuilder()
.WithNamingConvention(HyphenatedNamingConvention.Instance)
.Build();
// 反序列化 YAML 为对象
var dictLast = deserializer.Deserialize<Dictionary<string, object>>(strLastYaml);
var keys = new List<string>();
foreach (var kvp in custom)
{
var key = kvp.Key;
var val = (JsonElement)kvp.Value;
Merge(key, val, ref dictLast, ref keys);
}
// 将修改或新增的项移动到文件头部
var newDict = new Dictionary<string, object>();
foreach (var key in keys)
{
if (dictLast != null)
{
newDict.Add(key, dictLast[key]);
// 移除新添加的项
dictLast.Remove(key);
}
}
InsertDictionary(newDict, dictLast);
// 启用混合端口时 无需再分开设置
if (custom.ContainsKey("mixed-port"))
{
newDict.Remove("port");
newDict.Remove("socks-port");
}
// 创建序列化器实例
var serializer = new SerializerBuilder().Build();
// 将对象序列化为 YAML 字符串
var strNewYaml = serializer.Serialize(newDict);
var ver = Assembly.GetExecutingAssembly().GetName().Version;
if (ver != null)
{
strNewYaml =
$"# Auto Update Config v{ver.Major}.{ver.Minor}.{ver.Build}{Environment.NewLine}"
+ $"# Project url https://github.com/pdone/AutoUpdateConfig{Environment.NewLine}"
+ $"# Update time {DateTime.Now:F}{Environment.NewLine}"
+ strNewYaml;
}
File.WriteAllText(saveName, strNewYaml);
Log.Info($"Results of the merger \"{saveName}\"");
}
catch (Exception ex)
{
Log.Error($"{ex}");
goto EXIT;
}
EXIT:
var sec = 3;
Log.Info($"Exit in {sec} seconds");
Task.Delay(sec * 1000).Wait();
static void Merge(string key, JsonElement jo, ref Dictionary<string, object> dictLast, ref List<string> kesy)
{
switch (jo.ValueKind)
{
case JsonValueKind.Array:
var list = new List<string>();
for (var i = 0; i < jo.GetArrayLength(); i++)
{
list.Add(jo[i].GetString());
}
dictLast[key] = list;
kesy.Add(key);
break;
case JsonValueKind.String:
if (dictLast.TryAdd(key, jo.GetString()))
{
kesy.Add(key);
}
else
{
dictLast[key] = jo.GetString();
}
break;
case JsonValueKind.Number:
if (dictLast.TryAdd(key, jo.GetInt32()))
{
kesy.Add(key);
}
else
{
dictLast[key] = jo.GetInt32();
}
break;
case JsonValueKind.True:
case JsonValueKind.False:
if (dictLast.TryAdd(key, jo.GetBoolean()))
{
kesy.Add(key);
}
else
{
dictLast[key] = jo.GetBoolean();
}
break;
case JsonValueKind.Object:
case JsonValueKind.Undefined:
case JsonValueKind.Null:
default:
break;
}
}
static void InsertDictionary(Dictionary<string, object> target, Dictionary<string, object> source, bool overwrite = true)
{
foreach (var kvp in source)
{
if (overwrite || !target.ContainsKey(kvp.Key))
{
target[kvp.Key] = kvp.Value;
}
}
}