-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprotolayer_hat.cpp
399 lines (344 loc) · 11.1 KB
/
protolayer_hat.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <ctime>
#include "protolayer_hat.h"
#include "config_new.h"
#include "srvmgrdef.h"
#include "lib\utils.hpp"
#include "syslib.h"
#include "zxmgr.h"
#include "srvmgr.h"
#include "player_info.h"
#include "screenshots.h"
namespace NetHat
{
std::string HatAddr = "127.0.0.1";
uint16_t HatPort = 7999;
std::string ControlAddr = "";
uint16_t ControlPort = 0;
SOCKET Socket = NULL;
bool Connected = false;
uint32_t LastReconnect = 0;
PacketReceiver Receiver;
bool HaveInfo = false;
ServerInfo Info;
bool ShuttingDown = false;
uint32_t LastUpdate = 0;
}
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PACKETS SENDED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
bool NetCmd_HatAuth()
{
Packet pack;
pack.WriteUInt8(0x10);
pack.WriteUInt32(Config::ServerCaps);
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
bool NetCmd_Shutdown()
{
NetHat::ShuttingDown = true;
Packet pack;
pack.WriteUInt8(0x11);
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
bool NetCmd_UpdateInfo()
{
if(Config::ServerCaps & SVC_DETAILED_INFO)
{
Packet pack;
pack.WriteUInt8(0x12);
// базовая информация
if(NetHat::Info.PlayerCount == 0xFF && NetHat::Info.MapLevel == 0xFF && NetHat::Info.GameMode == 0xFF && NetHat::Info.MapSize == 0xFF) // shutdown
{
return NetCmd_Shutdown();
}
else
{
pack.WriteUInt32(NetHat::Info.GameMode);
pack.WriteString(NetHat::Info.MapName);
pack.WriteUInt8(NetHat::Info.MapLevel);
uint32_t p_mapwidth = *(uint32_t*)(*(uint32_t*)(0x006B16A8) + 0x50000);
uint32_t p_mapheight = *(uint32_t*)(*(uint32_t*)(0x006B16A8) + 0x50004);
uint32_t p_maptime = *(uint32_t*)(*(uint32_t*)(0x00642C2C) + 0x254) / 1000;
pack.WriteUInt32(p_mapwidth - 16); // Map Width
pack.WriteUInt32(p_mapheight - 16); // Map Height (может быть != Map Width)
pack.WriteUInt32(p_maptime); // Map Time
pack.WriteUInt32(Config::ServerFlags);
std::vector<byte*> tmp_players_Z = zxmgr::GetPlayers();
std::vector<byte*> tmp_players;
// удаляем AI-игроков
for(std::vector<byte*>::iterator it = tmp_players_Z.begin(); it != tmp_players_Z.end(); ++it)
{
byte* player = (*it);
if(!*(uint32_t*)(player + 0x2C)) // NOT AI
tmp_players.push_back(player);
}
pack.WriteUInt32(tmp_players.size());
for(std::vector<byte*>::iterator it = tmp_players.begin(); it != tmp_players.end(); ++it)
{
byte* player = (*it);
const char* player_nickname = *(const char**)(player + 0x18);
const char* player_login = "artificial";
if(!*(uint32_t*)(player + 0x2C))
player_login = *(const char**)(player + 0xA78);
uint32_t player_id1 = *(uint32_t*)(player + 0x10);
uint32_t player_id2 = *(uint32_t*)(player + 0x14);
pack.WriteString(player_nickname);
pack.WriteString(player_login);
pack.WriteUInt32(player_id1);
pack.WriteUInt32(player_id2);
byte* vd = zxmgr::GetNetworkStruct(player);
bool player_connected = (vd);
pack.WriteUInt8(player_connected);
if(*(uint32_t*)(player + 0x2C) || !player_connected) // AI or disconnected
{
pack.WriteString("");
}
else
{
if(vd)
{
const char* vd_ip = (const char*)(vd + 8);
//pack << std::string(vd_ip);
pack.WriteString(vd_ip);
}
else pack.WriteString("");
}
}
// send the logins to return
std::vector<std::string> logins;
std::string mask = Config::ChrBase;
mask += "*";
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFileA(mask.c_str(), &fd);
if(h != INVALID_HANDLE_VALUE)
{
bool found = true;
while(found)
{
std::string login = std::string(fd.cFileName);
found = (FindNextFileA(h, &fd) != 0);
if(login.find(".") != std::string::npos)
continue;
logins.push_back(login);
}
}
FindClose(h);
pack.WriteUInt32(logins.size());
for(std::vector<std::string>::iterator it = logins.begin(); it != logins.end(); ++it)
{
std::string& login = (*it);
pack.WriteString(login);
}
}
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
else
{
Packet pack;
pack.WriteUInt8(0xD2);
pack.WriteUInt8(NetHat::Info.PlayerCount);
pack.WriteUInt8(NetHat::Info.MapLevel);
pack.WriteUInt8(NetHat::Info.GameMode);
pack.WriteUInt8(NetHat::Info.MapSize);
pack.WriteString(NetHat::Info.MapName);
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
}
bool NetCmd_Broadcast(std::string what)
{
Packet pack;
pack.WriteUInt8(0x13);
pack.WriteString(what);
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
bool NetCmd_UnlockLogin(std::string login)
{
Packet pack;
pack.WriteUInt8(0x14);
pack.WriteString(login);
return (SOCK_SendPacket(NetHat::Socket, pack, Config::ProtocolVersion) == 0);
}
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [/PACKETS SENDED ] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CONNECTION CONTROL !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
void _stdcall Net_HatPrepare()
{
char* stadr = *(char**)(0x006D15B4);
std::string dadr(stadr);
std::vector<std::string> ipd = Explode(dadr, ":");
NetHat::ControlAddr = ipd[0];
if(ipd.size() == 2)
NetHat::ControlPort = StrToInt(ipd[1]) + 1000;
char* stadd = *(char**)(0x006D15B8);
std::string dadd(stadd);
ipd = Explode(dadd, ":");
NetHat::HatAddr = ipd[0];
if(ipd.size() == 2)
NetHat::HatPort = StrToInt(ipd[1]);
NetHat::ShuttingDown = false;
NetHat::LastReconnect = 0;
}
bool Net_HatInit()
{
if(NetHat::ControlPort == 0 && !NetHat::ControlAddr.length()) return false;
if(NetHat::ShuttingDown) return false;
NetHat::Socket = SOCK_Connect(NetHat::HatAddr, NetHat::HatPort, NetHat::ControlAddr, NetHat::ControlPort);
if(NetHat::Socket == SERR_NOTCREATED)
{
Net_HatShutdown();
//Printf("Warning: control connection to hat failed.");
return false;
}
NetHat::Connected = true;
NetHat::Receiver.Connect(NetHat::Socket);
NetHat::LastUpdate = GetTickCount();
if(!NetCmd_HatAuth())
{
Net_HatShutdown();
Printf("Warning: control connection to hat failed (disconnected).");
return false;
}
Printf("Control connection to hat established.");
return true;
}
bool Net_HatProcess()
{
if(NetHat::ShuttingDown) return false;
if(!NetHat::Connected) return false;
if(!NetHat::Receiver.Receive(Config::ProtocolVersion)) return false;
if(GetTickCount() - NetHat::LastUpdate > 15000 && NetHat::HaveInfo)
{
if(!NetCmd_UpdateInfo()) NetHat::Connected = false;
NetHat::LastUpdate = GetTickCount();
}
Packet pack;
while(NetHat::Receiver.GetPacket(pack))
{
uint8_t packet_id = pack.ReadUInt8();
switch(packet_id)
{
case 0x63: // broadcast from hat
{
std::string message = pack.ReadString();
zxmgr::SendMessage(NULL, message.c_str());
break;
}
case 0x64: // screenshot information
{
std::string login = pack.ReadString();
pack.ReadUInt8(); // ????
uint32_t uid = pack.ReadUInt32();
bool done = pack.ReadUInt8() != 0;
std::string url = pack.ReadString();
ClientScreenshot* cs = ClientScreenshot_FindByUID(uid);
// means the server crashed, and we are receiving screenshot info from earlier time
if (!cs)
{
if (done)
{
if (!!url.size())
Printf("screenshot: %s: Comeback transmission succeeded (saved to %s).", login.c_str(), url.c_str());
else Printf("screenshot: %s: Comeback transmission failed.", login.c_str());
}
else Printf("screenshot: %s: Comeback transmission initiated.", login.c_str());
}
else
{
if (cs->SourcePlayer)
{
if (done)
{
if (!!url.size())
zxmgr::SendMessage(cs->SourcePlayer, "screenshot: %s: Transmission succeeded (saved to %s).", login.c_str(), url.c_str());
else zxmgr::SendMessage(cs->SourcePlayer, "screenshot: %s: Transmission succeeded (saved to %s).", login.c_str(), url.c_str());
}
else zxmgr::SendMessage(cs->SourcePlayer, "screenshot: %s: Transmission initiated.", login.c_str());
}
else
{
if (done)
{
if (!!url.size())
Printf("screenshot: %s: Transmission succeeded (saved to %s).", login.c_str(), url.c_str());
else Printf("screenshot: %s: Transmission succeeded (saved to %s).", login.c_str(), url.c_str());
}
else Printf("screenshot: %s: Transmission initiated.", login.c_str());
}
}
if (done)
ClientScreenshot_Drop(uid);
break;
}
case 0x65: // mute player packet
{
std::string login = pack.ReadString();
time_t unmutedate = pack.ReadUInt32();
struct tm parsedTime;
localtime_s(&parsedTime, &unmutedate);
byte* player = zxmgr::FindByLogin(login.c_str());
std::vector<byte*> players = zxmgr::GetPlayers();
Player* pi = PI_Get(player);
if (pi)
{
pi->UnmuteDate = unmutedate;
Printf("Player %s (login %s) should be muted until %02d.%02d.%04d %02d:%02d:%02d.",
*(const char**)(player + 0x18), login.c_str(), parsedTime.tm_mday, parsedTime.tm_mon + 1, parsedTime.tm_year + 1900, parsedTime.tm_hour, parsedTime.tm_min, parsedTime.tm_sec);
}
break;
}
default: break;
}
}
return true;
}
void Net_HatShutdown()
{
if(NetHat::Connected && !NetHat::Socket) return;
if(NetHat::Socket) SOCK_Destroy(NetHat::Socket);
NetHat::Socket = NULL;
NetHat::Connected = false;
}
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [/CONNECTION CONTROL ] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CONNECTION IMPORTS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///
void __declspec(naked) imp_NetLayer_FirstConnect()
{ // 4F0BEF
__asm
{
push ebp
mov ebp, esp
sub esp, 0x240
mov [ebp-0x230], ecx
call Net_HatPrepare
mov edx, 0x004F0BFE
jmp edx
}
}
void Net_RegularProc()
{
if(NetHat::ControlPort != 0 && NetHat::ControlAddr.length() && !NetHat::ShuttingDown)
{
if(!Net_HatProcess() && (GetTickCount()-NetHat::LastReconnect > 5000))
{
Net_HatShutdown();
Net_HatInit(); // reconnect
NetHat::LastReconnect = GetTickCount();
}
}
Sleep(1);
}
void _stdcall imp2_UpdateInfo()
{
if(!NetCmd_UpdateInfo()) NetHat::Connected = false;
}
void _stdcall imp_UpdateInfo(unsigned char a_pcount, const char* a_name, unsigned char a_level, unsigned char a_type, unsigned char a_size)
{
NetHat::Info.PlayerCount = a_pcount;
NetHat::Info.MapLevel = a_level;
NetHat::Info.MapName = a_name;
NetHat::Info.GameMode = a_type;
NetHat::Info.MapSize = a_size;
NetHat::HaveInfo = true;
if(!NetCmd_UpdateInfo()) NetHat::Connected = false;
}
void _stdcall imp2_ServerClosed()
{
if(!NetCmd_Shutdown()) NetHat::Connected = false;
}
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [/CONNECTION IMPORTS ] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ///