This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerMain.cpp
188 lines (184 loc) · 6.66 KB
/
ServerMain.cpp
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
#include "ServerMain.h"
using namespace std;
int RestartCount;
tm* NowTime;
tm* LastRestartTime;
LocalConfig MainConfig;
void ReadConfigV1() {
// 读取配置文件 V1
MainConfig.Command = "";
MainConfig.RestartTimeout = 5;
color(COLOR_PROCESS);
puts("[PROC] 读取配置文件 config.txt ...");
/*if(!fileExits("config.txt")) {
color(COLOR_ERROR);
puts("[ERR!] 配置文件不存在!将在10秒后重试 ……");
timeout(10);
ReadConfig();
return;
}
if(!fileRead("config.txt")) {
color(COLOR_ERROR);
puts("[ERR!] 配置文件无法读取!将在10秒后重试 ……");
timeout(10);
ReadConfig();
return;
}*/
ifstream in("config.txt");
string reader;
color(COLOR_SUCCESS);
puts("[SUCC] 配置文件V1打开成功!");
while(reader != "#Config_Start.") {
in >> reader;
if(in.eof()) {
color(COLOR_ERROR);
puts("[ERR!] 配置文件没有任何配置组!将在10秒后重试 ……");
timeout(10);
ReadConfigV1();
return;
}
}
while(reader != "#Config_End.") {
in >> reader;
if(in.eof()) {
color(COLOR_ERROR);
puts("[ERR!] 配置文件没有有效结尾!将在10秒后重试 ……");
timeout(10);
ReadConfigV1();
return;
}
if(reader == "command=") {
stringstream command;
in >> reader;
while(reader != "#Config_End." && reader != "timeout=") {
command << reader << " ";
in >> reader;
}
MainConfig.Command = command.str();
}
if(reader == "timeout=") {
int tmout;
in >> tmout;
MainConfig.RestartTimeout = tmout;
}
}
in.close();
color(COLOR_SUCCESS);
puts("[SUCC] 配置文件V1读取成功:");
cout << "\tCommand: " << MainConfig.Command << endl;
cout << "\tRestartTimeout: " << MainConfig.RestartTimeout << endl;
puts("");
}
void ReadConfig() {
// 读取配置文件 V2
MainConfig.Command = "";
MainConfig.RestartTimeout = 5;
color(COLOR_PROCESS);
puts("[PROC] 读取配置文件 config.json ...");
if(!fileExits("config.json")) {
if(fileExits("config.txt") && fileRead("config.txt")) {
puts("[PROC] 检测到配置文件V1 (config.txt),转换为V2。");
ReadConfigV1();
puts("[PROC] 开始转换为V2。");
ofstream out("config.json");
out << "{" << endl;
out << " \"readme\": [" << endl;
out << " \"在 readme 内的内容将被忽略。若要了解更多信息,请访问 https://github.com/HJFunnyMinecraft/ServerMain/README.md\"," << endl;
out << " \"Contents inside readme will be ignore. To learn more, please visit https://github.com/HJFunnyMinecraft/ServerMain/README.md\"" << endl;
out << " ]," << endl;
out << " \"command\": \"" << MainConfig.Command << "\"," << endl;
out << " \"timeout\": " << MainConfig.RestartTimeout << endl;
out << "}" << endl;
out.close();
color(7);
puts("配置文件V1转V2已结束:config.json");
puts("请访问 https://github.com/HJFunnyMinecraft/ServerMain/README.md,检查转换结果。");
puts("当配置完成时,请关闭编辑器……");
system("notepad config.json");
} else {
puts("[PROC] config.json 不存在,加载默认配置模板。");
ofstream out("config.json");
out << "{" << endl;
out << " \"readme\": [" << endl;
out << " \"在 readme 内的内容将被忽略。若要了解更多信息,请访问 https://github.com/HJFunnyMinecraft/ServerMain/README.md\"," << endl;
out << " \"Contents inside readme will be ignore. To learn more, please visit https://github.com/HJFunnyMinecraft/ServerMain/README.md\"" << endl;
out << " ]," << endl;
out << " \"command\": \"\"," << endl;
out << " \"timeout\": 5" << endl;
out << "}" << endl;
out.close();
color(7);
puts("默认配置模板已创建:config.json");
puts("请访问 https://github.com/HJFunnyMinecraft/ServerMain/README.md,配置此程序。");
puts("当配置完成时,请关闭编辑器……");
system("notepad config.json");
}
ReadConfig();
return;
}
if(!fileRead("config.json")) {
color(COLOR_ERROR);
puts("[ERR!] 配置文件无法读取!将在10秒后重试 ……");
timeout(10);
ReadConfig();
return;
}
ifstream in("config.json");
Json::Reader reader;
Json::Value root;
color(COLOR_SUCCESS);
puts("[SUCC] 配置文件打开成功!");
if(reader.parse(in, root)) {
MainConfig.Command = root["command"].asString();
MainConfig.RestartTimeout = root["timeout"].asInt();
}
in.close();
color(COLOR_SUCCESS);
puts("[SUCC] 配置文件读取成功:");
cout << "\tCommand: " << MainConfig.Command << endl;
cout << "\tRestartTimeout: " << MainConfig.RestartTimeout << endl;
puts("");
}
int main() {
system("chcp 65001"); // 使用 UTF-8 格式化控制台
system("echo off");
system("cls");
setTitle("SM服务器启动器 V1.4");
color(COLOR_SUCCESS);
puts("[SUCC] 软件启动成功!");
puts("");
color(COLOR_TIPS);
puts("========== SM服务器启动器 ==========");
puts("版本:V1.4");
puts("构建日期:2022-12-14");
puts("作者:CodeZhangBorui");
puts("");
puts("");
while(1) {
ReadConfig();
NowTime = GetNowTime();
LastRestartTime = NowTime;
stringstream title;
title
<< "SM服务器启动器 V1.4 [ 进程退出次数:"
<< RestartCount
<< " 上次重启:"
<< GetDateString(LastRestartTime)
<< " "
<< GetTimeString(LastRestartTime)
<< " ]";
setTitle(title.str());
color(COLOR_TIPS);
puts("> 启动进程……");
puts("");
color(COLOR_DEFAULT);
system(MainConfig.Command.c_str());
puts("");
color(COLOR_PROCESS);
puts("检测到进程终止,即将重启 ……");
puts("若希望停止进程,请直接关闭此窗口。");
timeout(MainConfig.RestartTimeout);
RestartCount++;
puts("");
}
}