-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlobalProperties.cs
155 lines (151 loc) · 4.15 KB
/
GlobalProperties.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
#nullable enable
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
namespace Team123it.Arcaea.MarveCube.Standalone
{
/// <summary>
/// 提供适用于 <see cref="Standalone"/> 的全局属性的类。无法继承此类。
/// </summary>
public static class GlobalProperties
{
/// <summary>
/// 获取当前服务器是否在维护中的标志。
/// <para>注:为防止出现数据丢失或损坏或发生意外情况,获取过程中发生任何异常都将视为正在维护中。</para>
/// </summary>
public static bool Maintaining
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
if (settings.Value<JObject>("settings").Value<bool>("isMaintaining"))
{
return true;
}
else
{
return false;
}
}
catch
{
return true;
}
}
else
{
return true;
}
}
}
/// <summary>
/// 获取HTTPS证书密码。
/// <para>若证书密码设置项不存在则返回 <see langword="null"/> 。</para>
/// </summary>
/// <exception cref="FileNotFoundException" />
public static string? HttpsCertificatePassword
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
string pass = Encoding.UTF8.GetString(Convert.FromBase64String(config.Value<string>("httpsCerPass")));
return pass;
}
catch
{
return null;
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
public static string MainServerURLPrefix
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
string prefix = config.Value<string>("mainServerURLPrefix");
return prefix;
}
catch
{
return null;
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
public static string StandaloneKey
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
string key = config.Value<string>("standaloneKey");
return key;
}
catch
{
return null;
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
/// <summary>
/// 获取API的监听端口。
/// </summary>
/// <exception cref="FileNotFoundException" />
/// <exception cref="JsonException" />
public static int ListenPort
{
get
{
if (File.Exists(Path.Combine(AppContext.BaseDirectory, "data", "config.json")))
{
try
{
var settings = JObject.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "data", "config.json"), Encoding.UTF8));
var config = settings.Value<JObject>("config");
return config.Value<int>("listenPort");
}
catch (Exception ex)
{
throw new JsonException($"配置文件 {Path.Combine(AppContext.BaseDirectory, "data", "config.json")} 读取失败: {ex.Message}");
}
}
else
{
throw new FileNotFoundException($"找不到配置文件(config.json): {Path.Combine(AppContext.BaseDirectory, "data", "config.json")}");
}
}
}
}
}