-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigData.cs
More file actions
79 lines (68 loc) · 2.47 KB
/
ConfigData.cs
File metadata and controls
79 lines (68 loc) · 2.47 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using YamlDotNet.Serialization;
public class ConfigData
{
public int timeout { get; set; }
public bool startup { get; set; }
public bool debug { get; set; }
public int wait { get; set; }
public List<Dictionary<string, string>> settings { get; set; } // 改成字符串key和value
public List<Dictionary<string, string>> exit { get; set; } // 改成字符串key和value
}
public static class YamlConfigLoader
{
public static ConfigData LoadConfig()
{
string exePath = AppDomain.CurrentDomain.BaseDirectory;
string yamlPath = Path.Combine(exePath, "config.yaml");
if (!File.Exists(yamlPath))
{
throw new FileNotFoundException("未找到配置文件: config.yaml");
}
string yaml = File.ReadAllText(yamlPath);
var deserializer = new DeserializerBuilder().Build();
var result = deserializer.Deserialize<Dictionary<string, object>>(yaml);
var config = new ConfigData
{
timeout = Convert.ToInt32(result["timeout"]),
startup = Convert.ToBoolean(result["startup"]),
debug = Convert.ToBoolean(result["debug"]),
wait = Convert.ToInt32(result["wait"]),
settings = new List<Dictionary<string, string>>(),
exit = new List<Dictionary<string, string>>()
};
// 解析 settings
if (result.ContainsKey("settings"))
{
var settingsList = result["settings"] as List<object>;
foreach (var item in settingsList)
{
var entry = item as Dictionary<object, object>;
var dict = new Dictionary<string, string>();
foreach (var kv in entry)
{
dict[kv.Key.ToString()] = kv.Value.ToString();
}
config.settings.Add(dict);
}
}
// 解析 exit
if (result.ContainsKey("exit"))
{
var exitList = result["exit"] as List<object>;
foreach (var item in exitList)
{
var entry = item as Dictionary<object, object>;
var dict = new Dictionary<string, string>();
foreach (var kv in entry)
{
dict[kv.Key.ToString()] = kv.Value.ToString();
}
config.exit.Add(dict);
}
}
return config;
}
}