-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit2.cpp
More file actions
229 lines (208 loc) · 9.36 KB
/
Unit2.cpp
File metadata and controls
229 lines (208 loc) · 9.36 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include <System.JSON.hpp>
#include <System.StrUtils.hpp>
#include <System.RegularExpressions.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TServerAddForm *ServerAddForm;
//---------------------------------------------------------------------------
__fastcall TServerAddForm::TServerAddForm(TComponent* Owner)
: TForm(Owner)
{
// отключаем влияние стиля на цвет шрифта
ArgsMemo->StyleElements = ArgsMemo->StyleElements >> seFont;
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::FormShow(TObject *Sender)
{
if (EditServer && EditServerID >= 0) {
ServerAddForm->Caption = "Изменить сервер";
String serversFile = "servers.json";
TStringList *fileContent = new TStringList;
try {
fileContent->LoadFromFile(serversFile);
String jsonData = fileContent->Text;
TJSONArray *jsonArray = static_cast<TJSONArray*>(TJSONObject::ParseJSONValue(jsonData));
if (jsonArray != nullptr) {
TJSONObject *server = static_cast<TJSONObject*>(jsonArray->Get(EditServerID));
IPEdit->Text = server->GetValue("ip")->Value();
PortEdit->Text = server->GetValue("port")->Value();
PassEdit->Text = server->GetValue("password")->Value();
FileEdit->Text = server->GetValue("exe")->Value();
ArgsMemo->Text = server->GetValue("args")->Value();
ArgsMemo->Font->Color = clBlack;
fileContent->Text = jsonArray->ToString();
fileContent->SaveToFile(serversFile);
}
delete jsonArray;
}
__finally {
delete fileContent;
}
} else ServerAddForm->Caption = "Добавить сервер";
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::FileButtonClick(TObject *Sender)
{
if (OpenDialog->Execute()) FileEdit->Text = OpenDialog->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::ArgsMemoClick(TObject *Sender)
{
// очистка поля командной строки при настройке
if (ArgsMemo->Text.Pos("...")) {
ArgsMemo->Text = "";
ArgsMemo->Font->Color = clBlack;
}
}
//---------------------------------------------------------------------------
bool IsValidIPAddress(const String &ipAddress)
{
return TRegEx::Match(ipAddress, L"^(\\d{1,3}(\\.\\d{1,3}){3})$").Success;
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::SaveButtonClick(TObject *Sender)
{
if (IPEdit->Text == "" || PortEdit->Text == "" || FileEdit->Text == "" || ArgsMemo->Text == "" || ArgsMemo->Text.Pos("...")) {
Application->MessageBox(L"Не заполнены настройки!", Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
if (!IsValidIPAddress(IPEdit->Text)) {
Application->MessageBox(L"Неверно указан IP адрес!", Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
ArgsMemo->Text = StringReplace(ArgsMemo->Text, " /wait", "", TReplaceFlags() << rfReplaceAll);
String serversFile = "servers.json";
if (FileExists(serversFile)) { // добавляем / изменяем сервер в json
TStringList *fileContent = new TStringList;
try {
fileContent->LoadFromFile(serversFile);
String jsonData = fileContent->Text;
TJSONArray *jsonArray = static_cast<TJSONArray*>(TJSONObject::ParseJSONValue(jsonData));
if (jsonArray != nullptr) {
TJSONObject *serverData = new TJSONObject();
serverData->AddPair("ip", IPEdit->Text);
serverData->AddPair("port", PortEdit->Text);
serverData->AddPair("password", PassEdit->Text);
serverData->AddPair("exe", FileEdit->Text);
serverData->AddPair("args", ArgsMemo->Text);
if (EditServer && EditServerID >= 0 && EditServerID < jsonArray->Count) {
TJSONArray *newArray = new TJSONArray();
for (int i = 0; i < jsonArray->Count; i++) {
if (i == EditServerID) newArray->Add(serverData);
else {
TJSONObject *originalServer = static_cast<TJSONObject*>(jsonArray->Get(i));
newArray->Add(originalServer);
}
}
fileContent->Text = newArray->ToString();
fileContent->SaveToFile(serversFile);
} else {
// предотвращение дублирования
String newAddr = IPEdit->Text + ":" + PortEdit->Text;
String newExe = FileEdit->Text;
String newExeName = ExtractFileName(newExe);
for (int i = 0; i < jsonArray->Count; i++) {
TJSONObject *originalServer = static_cast<TJSONObject*>(jsonArray->Get(i));
String ip = originalServer->GetValue("ip")->Value();
String port = originalServer->GetValue("port")->Value();
String pass = originalServer->GetValue("password")->Value();
String exe = originalServer->GetValue("exe")->Value();
String args = originalServer->GetValue("args")->Value();
String serverAddr = ip + ":" + port;
String exeName = ExtractFileName(exe);
if (newAddr == serverAddr) {
Application->MessageBox(("Сервер " + newAddr + " уже есть в списке!").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
if (newExe == exe) {
Application->MessageBox(("Исполняемый файл " + newExe + " уже есть в списке!").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
if (newExeName == exeName) {
Application->MessageBox(("Исполняемый файл " + newExeName + " уже есть в списке!\nВ каждом сервере, который вы добавляете в мониторинг должны быть разные имена исполняемых файлов, например srcds1.exe, srcds2.exe и т.п.\n\nПожалуйста переименуйте их, после чего добавляйте в мониторинг.").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
}
jsonArray->Add(serverData);
fileContent->Text = jsonArray->ToString();
fileContent->SaveToFile(serversFile);
}
delete jsonArray;
}
}
__finally {
delete fileContent;
}
} else { // создаем и сохраняем json с первым сервером
TJSONArray *jsonArray = new TJSONArray();
TJSONObject *serverData = new TJSONObject();
serverData->AddPair("ip", IPEdit->Text);
serverData->AddPair("port", PortEdit->Text);
serverData->AddPair("password", PassEdit->Text);
serverData->AddPair("exe", FileEdit->Text);
serverData->AddPair("args", ArgsMemo->Text);
// предотвращение дублирования
String newAddr = IPEdit->Text + ":" + PortEdit->Text;
String newExe = FileEdit->Text;
String newExeName = ExtractFileName(newExe);
for (int i = 0; i < jsonArray->Count; i++) {
TJSONObject *originalServer = static_cast<TJSONObject*>(jsonArray->Get(i));
String ip = originalServer->GetValue("ip")->Value();
String port = originalServer->GetValue("port")->Value();
String pass = originalServer->GetValue("password")->Value();
String exe = originalServer->GetValue("exe")->Value();
String args = originalServer->GetValue("args")->Value();
String serverAddr = ip + ":" + port;
String exeName = ExtractFileName(exe);
if (newAddr == serverAddr) {
Application->MessageBox(("Сервер " + newAddr + " уже есть в списке!").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
if (newExe == exe) {
Application->MessageBox(("Исполняемый файл " + newExe + " уже есть в списке!").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
if (newExeName == exeName) {
Application->MessageBox(("Исполняемый файл " + newExeName + " уже есть в списке!\nВ каждом сервере, который вы добавляете в мониторинг должны быть разные имена исполняемых файлов, например srcds1.exe, srcds2.exe и т.п.\n\nПожалуйста переименуйте их, после чего добавляйте в мониторинг.").w_str(), Application->Title.w_str(), MB_OK | MB_ICONERROR);
return;
}
}
jsonArray->Add(serverData);
TStringList *fileContent = new TStringList;
try {
fileContent->Text = jsonArray->ToString();
fileContent->SaveToFile(serversFile);
}
__finally {
delete fileContent;
}
delete jsonArray;
}
MainForm->LoadServers();
ServerAddForm->Close();
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::CancelButtonClick(TObject *Sender)
{
ServerAddForm->Close();
}
//---------------------------------------------------------------------------
void __fastcall TServerAddForm::FormClose(TObject *Sender, TCloseAction &Action)
{
IPEdit->Text = "";
PortEdit->Text = "";
FileEdit->Text = "";
ArgsMemo->Text = "-console -port 27015 -tickrate 33 -game garrysmod +gamemode sandbox ...";
ArgsMemo->Font->Color = clSilver;
PassEdit->Text = "";
EditServer = false;
EditServerID = -1;
MainForm->LoadServers();
}
//---------------------------------------------------------------------------