-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.cs
More file actions
189 lines (175 loc) · 5.61 KB
/
Copy pathConfigManager.cs
File metadata and controls
189 lines (175 loc) · 5.61 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
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
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace RYCBEditorX.Utils
{
public static class ConfigManager
{
private static readonly string ConfigDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Profiles", "Config");
private static readonly string ConfigPath = Path.Combine(ConfigDirectory, "Settings.json");
private static AppConfig _currentConfig;
private static readonly object _lock = new();
private static readonly JsonSerializerOptions _jsonOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true
};
/// <summary>
/// 初始化配置管理器
/// </summary>
public static void Initialize()
{
if (!Directory.Exists(ConfigDirectory))
{
Directory.CreateDirectory(ConfigDirectory);
}
if (!File.Exists(ConfigPath))
{
_currentConfig = CreateDefaultConfig();
SaveConfig();
}
else
{
LoadConfig();
}
}
/// <summary>
/// 获取当前配置(只读)
/// </summary>
public static AppConfig CurrentConfig
{
get
{
lock (_lock)
{
return _currentConfig;
}
}
}
/// <summary>
/// 加载配置文件
/// </summary>
public static void LoadConfig()
{
try
{
lock (_lock)
{
string json = File.ReadAllText(ConfigPath);
_currentConfig = JsonSerializer.Deserialize<AppConfig>(json, _jsonOptions);
}
}
catch (Exception ex)
{
// 如果加载失败,使用默认配置
_currentConfig = CreateDefaultConfig();
GlobalConfig.CurrentLogger?.Log($"加载配置文件失败,使用默认配置: {ex.Message}",
module: EnumLogModule.CUSTOM, customModuleName: "配置管理");
}
}
/// <summary>
/// 保存当前配置到文件
/// </summary>
public static void SaveConfig()
{
try
{
lock (_lock)
{
string json = JsonSerializer.Serialize(_currentConfig, _jsonOptions);
File.WriteAllText(ConfigPath, json);
}
}
catch (Exception ex)
{
GlobalConfig.CurrentLogger?.Log($"保存配置文件失败: {ex.Message}",
module: EnumLogModule.CUSTOM, customModuleName: "配置管理");
}
}
/// <summary>
/// 异步保存当前配置到文件
/// </summary>
public static async Task SaveConfigAsync()
{
try
{
string json;
lock (_lock)
{
json = JsonSerializer.Serialize(_currentConfig, _jsonOptions);
}
await File.WriteAllTextAsync(ConfigPath, json);
}
catch (Exception ex)
{
GlobalConfig.CurrentLogger?.Log($"异步保存配置文件失败: {ex.Message}",
module: EnumLogModule.CUSTOM, customModuleName: "配置管理");
}
}
/// <summary>
/// 更新配置并保存
/// </summary>
/// <param name="updateAction">更新配置的回调函数</param>
public static void UpdateConfig(Action<AppConfig> updateAction)
{
lock (_lock)
{
updateAction?.Invoke(_currentConfig);
SaveConfig();
}
}
/// <summary>
/// 异步更新配置并保存
/// </summary>
/// <param name="updateAction">更新配置的回调函数</param>
public static async Task UpdateConfigAsync(Action<AppConfig> updateAction)
{
lock (_lock)
{
updateAction?.Invoke(_currentConfig);
}
await SaveConfigAsync();
}
/// <summary>
/// 重置为默认配置
/// </summary>
public static void ResetToDefault()
{
lock (_lock)
{
_currentConfig = CreateDefaultConfig();
SaveConfig();
}
}
/// <summary>
/// 创建默认配置
/// </summary>
private static AppConfig CreateDefaultConfig()
{
return new AppConfig
{
Skin = "dark",
MaximumFileSize = 307200,
Language = "zh-CN",
PythonPath = "python",
AutoSave = new AutoSaveConfig { Enabled = true, Interval = 5 },
AutoBackup = new AutoBackupConfig { Enabled = true, Interval = 1, Path = "Backup\\" },
Font = "Microsoft YaHei UI",
XshdFilePath = "Highlightings\\",
Editor = new EditorConfig
{
Theme = "IDEA",
ShowLineNumber = true,
FontName = "Jetbrains Mono",
FontSize = 12
},
Downloading = new DownloadingConfig
{
ParallelDownload = true,
ParallelCount = 8
}
};
}
}
}