Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Commit b59bade

Browse files
committed
change: 配置文件格式改为 YAML
feat: 动态生成配置文件 opti: 优化退出程序时的体验 opti: 对部分代码进行微调
1 parent 96f0242 commit b59bade

6 files changed

Lines changed: 69 additions & 95 deletions

File tree

CSharp-OpenBMCLAPI/CSharp-OpenBMCLAPI.csproj

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
<Nullable>enable</Nullable>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<None Remove="DefaultConfig.json5" />
13-
</ItemGroup>
14-
1511
<ItemGroup>
1612
<PackageReference Include="log4net" Version="2.0.17" />
1713
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
@@ -20,13 +16,10 @@
2016
<PackageReference Include="SocketIOClient" Version="3.1.1" />
2117
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
2218
<PackageReference Include="WindowsFirewallHelper" Version="2.2.0.86" />
19+
<PackageReference Include="YamlDotNet" Version="15.1.2" />
2320
<PackageReference Include="ZstdSharp.Port" Version="0.7.6" />
2421
</ItemGroup>
2522

26-
<ItemGroup>
27-
<EmbeddedResource Include="DefaultConfig.json5" />
28-
</ItemGroup>
29-
3023
<ItemGroup>
3124
<ProjectReference Include="..\BmclApiDownloadService\BmclApiDownloadService.csproj" />
3225
</ItemGroup>

CSharp-OpenBMCLAPI/DefaultConfig.json5

Lines changed: 0 additions & 28 deletions
This file was deleted.

CSharp-OpenBMCLAPI/Modules/Cluster.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private void InitializeService()
164164
});
165165
application = builder.Build();
166166
application.UseHttpsRedirection();
167-
var path = $"{SharedData.Config.clusterFileDirectory}cache";
167+
var path = Path.Combine(SharedData.Config.clusterFileDirectory, $"cache");
168168
// application.UseStaticFiles();
169169
application.MapGet("/download/{hash}", (context) => HttpServiceProvider.LogAndRun(context, () =>
170170
{
@@ -205,11 +205,11 @@ private void InitializeService()
205205
/// </returns>
206206
protected X509Certificate2 LoadAndConvertCert()
207207
{
208-
X509Certificate2 cert = X509Certificate2.CreateFromPemFile($"{SharedData.Config.clusterFileDirectory}certifications/cert.pem",
209-
$"{SharedData.Config.clusterFileDirectory}certifications/key.pem");
208+
X509Certificate2 cert = X509Certificate2.CreateFromPemFile(Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications/cert.pem"),
209+
Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications/key.pem"));
210210
byte[] pfxCert = cert.Export(X509ContentType.Pfx);
211211
SharedData.Logger.LogDebug($"将 PEM 格式的证书转换为 PFX 格式");
212-
using (var file = File.Create($"{SharedData.Config.clusterFileDirectory}certifications/cert.pfx"))
212+
using (var file = File.Create(Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications/cert.pfx")))
213213
{
214214
file.Write(pfxCert);
215215
}
@@ -257,7 +257,7 @@ await socket.EmitAsync("enable", (SocketIOResponse resp) =>
257257
host = SharedData.Config.HOST,
258258
port = SharedData.Config.PORT,
259259
version = SharedData.Config.clusterVersion,
260-
byoc = SharedData.Config.byoc,
260+
byoc = SharedData.Config.bringYourOwnCertficate,
261261
noFastEnable = SharedData.Config.noFastEnable,
262262
flavor = new
263263
{
@@ -445,6 +445,11 @@ private async Task DownloadFile(string hash, string path, bool force = false)
445445
/// <returns></returns>
446446
public async Task RequestCertification()
447447
{
448+
if (SharedData.Config.bringYourOwnCertficate)
449+
{
450+
SharedData.Logger.LogDebug($"{nameof(SharedData.Config.bringYourOwnCertficate)} 为 true,跳过请求证书……");
451+
return;
452+
}
448453
await socket.EmitAsync("request-cert", (SocketIOResponse resp) =>
449454
{
450455
var data = resp;
@@ -456,10 +461,10 @@ await socket.EmitAsync("request-cert", (SocketIOResponse resp) =>
456461
string? certString = cert.GetString();
457462
string? keyString = key.GetString();
458463

459-
string certPath = $"{SharedData.Config.clusterFileDirectory}certifications/cert.pem";
460-
string keyPath = $"{SharedData.Config.clusterFileDirectory}certifications/key.pem";
464+
string certPath = Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications/cert.pem");
465+
string keyPath = Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications/key.pem");
461466

462-
Directory.CreateDirectory($"{SharedData.Config.clusterFileDirectory}certifications");
467+
Directory.CreateDirectory(Path.Combine(SharedData.Config.clusterFileDirectory, $"certifications"));
463468

464469
using (var file = File.Create(certPath))
465470
{

CSharp-OpenBMCLAPI/Modules/Config.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json;
22
using System.ComponentModel;
3+
using YamlDotNet.Serialization;
34

45
namespace CSharpOpenBMCLAPI.Modules
56
{
@@ -8,38 +9,48 @@ namespace CSharpOpenBMCLAPI.Modules
89
/// </summary>
910
public class Config
1011
{
11-
// 集群启动时的文件检查模式
12-
// 0: None(不检查,不推荐)
13-
// 1: Exists(检查文件是否存在)
14-
// 2: SizeOnly(检查文件大小,推荐,默认)
15-
// 3: Hash(完整计算哈希,时间长,推荐不常重启或是分片节点使用)
12+
[YamlMember(Description = """
13+
0: None(不检查,不推荐)")]
14+
1: Exists(检查文件是否存在)")]
15+
2: SizeOnly(检查文件大小,推荐,默认)")]
16+
3: Hash(完整计算哈希,时间长,推荐不常重启或是分片节点使用)
17+
""", Order = 2)]
1618
public FileVerificationMode startupCheckMode;
1719

18-
// 跳过启动前检查
19-
// 这会导致无法发现文件错误,但是能够将内存占用压缩到约正常情况下的 30%!
20-
// 当此项启用时,"startupCheckMode"无效
20+
[YamlMember(Description = """
21+
跳过启动前检查跳过启动前检查
22+
这会导致无法发现文件错误,但是能够将内存占用压缩到约正常情况下的 30%!
23+
[注] 当此项启用时,"startupCheckMode"无效
24+
""", Order = 2)]
2125
public bool skipStartupCheck;
2226

23-
// 指示 token 应当在距离其失效前的多少毫秒进行刷新
27+
[YamlMember(Description = "指示 token 应当在距离其失效前的多少毫秒进行刷新", Order = 1)]
2428
public int refreshTokenTime;
25-
// 指示应该将要服务的文件放在哪里(服务路径)
29+
30+
[YamlMember(Description = "指示应该将要服务的文件放在哪里(服务路径)", Order = 1)]
2631
public string clusterFileDirectory;
27-
// 指示节点端的版本,不应由用户更改
28-
[Browsable(false)]
32+
33+
[YamlIgnore]
34+
[YamlMember(Description = "指示节点端的版本,不应由用户更改")]
2935
public string clusterVersion;
30-
// 用户访问时使用的 IP 或域名
31-
[JsonProperty("host")]
36+
37+
[YamlMember(Alias = "host", Description = "用户访问时使用的 IP 或域名", Order = 0)]
3238
public string HOST { get; set; }
33-
// 对外服务端口
34-
[JsonProperty("port")]
39+
40+
[YamlMember(Alias = "port", Description = "对外服务端口", Order = 0)]
3541
public ushort PORT { get; set; }
36-
// 是否使用自定义域名
37-
public bool byoc;
38-
// 指示是否执行快速上线,若为 true 则每次都不执行
42+
43+
[YamlMember(Alias = "bringYourOwnCertificate", Description = "是否不使用主控分发的证书", Order = 1)]
44+
public bool bringYourOwnCertficate;
45+
46+
47+
[YamlMember(Description = "指示是否执行快速上线,若为 true 则每次都不执行", Order = 1)]
3948
public bool noFastEnable;
4049

50+
[YamlMember(Description = "指示是否禁用访问日志输出", Order = 1)]
4151
public bool disableAccessLog;
4252

53+
[YamlIgnore]
4354
public string cacheDirectory { get => Path.Combine(this.clusterFileDirectory, "cache"); }
4455

4556
public Config()
@@ -53,7 +64,7 @@ public Config()
5364

5465
this.HOST = "";
5566
this.PORT = 4000;
56-
this.byoc = false;
67+
this.bringYourOwnCertficate = false;
5768
this.noFastEnable = false;
5869

5970
this.disableAccessLog = false;

CSharp-OpenBMCLAPI/Program.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010
using TeraIO.Runnable;
11+
using YamlDotNet.Serialization;
1112

1213
namespace CSharpOpenBMCLAPI
1314
{
@@ -64,45 +65,31 @@ protected void LoadPlugins()
6465

6566
protected Config GetConfig()
6667
{
67-
if (!File.Exists("config.json5"))
68+
const string configPath = "config.yml";
69+
if (!File.Exists(configPath))
6870
{
69-
// 获取正在运行方法所在的命名空间空间
70-
Type? type = MethodBase.GetCurrentMethod()?.DeclaringType;
71-
72-
string? _namespace = type?.Namespace;
73-
74-
// 获取当前运行的 Assembly
75-
Assembly _assembly = Assembly.GetExecutingAssembly();
76-
77-
// 获取资源名称
78-
string resourceName = $"{_namespace}.DefaultConfig.json5";
79-
80-
// 从 Assembly 中提取资源
81-
Stream? stream = _assembly.GetManifestResourceStream(resourceName);
82-
83-
if (stream != null)
84-
{
85-
using (var file = File.Create("config.json5"))
86-
{
87-
file.Seek(0, SeekOrigin.Begin);
88-
stream.CopyTo(file);
89-
}
90-
}
91-
92-
return new Config();
71+
Config config = new Config();
72+
Serializer serializer = new Serializer();
73+
File.WriteAllText(configPath, serializer.Serialize(config));
74+
return config;
9375
}
9476
else
9577
{
96-
string file = File.ReadAllText("config.json5");
97-
Config? config = JsonConvert.DeserializeObject<Config>(file);
78+
string file = File.ReadAllText(configPath);
79+
Deserializer deserializer = new Deserializer();
80+
Config? config = deserializer.Deserialize<Config>(file);
81+
Config result;
9882
if (config != null)
9983
{
100-
return config;
84+
result = config;
10185
}
10286
else
10387
{
104-
return new Config();
88+
result = new Config();
10589
}
90+
Serializer serializer = new Serializer();
91+
File.WriteAllText(configPath, serializer.Serialize(config));
92+
return result;
10693
}
10794
}
10895

@@ -121,7 +108,8 @@ protected override int Run(string[] args)
121108
}
122109
else
123110
{
124-
using (var file = File.Create("totals.bson"))
111+
const string bsonFilePath = "totals.bson";
112+
using (var file = File.Create(bsonFilePath))
125113
{
126114
file.Write(Utils.BsonSerializeObject(SharedData.DataStatistician));
127115
}

README.MD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282

8383
**Newtonsoft**
8484
- [Newtonsoft.Json](https://www.newtonsoft.com/json) - 使用 Newtonsoft.Json 解析 Json
85+
- [Newtonsoft.Json.Bson](https://www.newtonsoft.com/json) - 使用 Newtonsoft.Json.Bson 解析 Bson,用于统计数据的存储
8586

8687
**Behzad Khosravifar**
8788
- [Downloader](https://github.com/bezzad/Downloader) - 使用 Downloader 下载文件
@@ -90,7 +91,6 @@
9091
- [SocketIOCLient](https://github.com/doghappy/socket.io-client-csharp) - 使用 SocketIOClient 与主控通信
9192

9293
**Apache**
93-
- [Avro](https://avro.apache.org/) - 使用 Avro 包解析主控返回的解压缩过的文件列表
9494
- [log4net](https://logging.apache.org/log4net/) - 使用 log4net 记录日志(虽然目前没用到)
9595

9696
**@oleg-st (Oleg Stepanischev)**
@@ -99,6 +99,11 @@
9999
**SALTWOOD**
100100
- [TeraIO](https://github.com/SALTWOOD/TeraIO) - 其实也没用到多少(目前是废物一个),以后会抽空把一些 utils 丢进去的
101101

102+
**@aaubry (Antoine Aubry)**
103+
- [YamlDotNet](https://github.com/aaubry/YamlDotNet) - 用于配置文件的读写
104+
105+
# 友情链接
106+
102107
[OpenBMCLAPI](https://github.com/bangbang93/openbmclapi)
103108

104109
[go-openbmclapi](https://github.com/LiterMC/go-openbmclapi)

0 commit comments

Comments
 (0)