This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_distribution.cpp
607 lines (489 loc) · 14.7 KB
/
packet_distribution.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//////////////////////////////////////////////////
//
// This work is licensed under the MIT license.
//
// You are free to copy, modify and distribute
// this work freely given that proper attribution
// is supplied and the author is not held liable.
// See LICENSE for details.
//
// Copyright (c) 2016-2018 David "OmniTroid" Skoland
//
//////////////////////////////////////////////////
#include "aoapplication.h"
#include "lobby.h"
#include "courtroom.h"
#include "networkmanager.h"
#include "encryption_functions.h"
#include "hardware_functions.h"
#include "misc_functions.h"
#include <QDebug>
void AOApplication::ms_packet_received(AOPacket *p_packet)
{
p_packet->net_decode();
QString header = p_packet->get_header();
QStringList f_contents = p_packet->get_contents();
if (header != "CHECK")
qDebug() << "R(ms):" << p_packet->to_string();
if (header == "ALL")
{
server_list.clear();
for (QString i_string : p_packet->get_contents())
{
server_type f_server;
QStringList sub_contents = i_string.split("&");
if (sub_contents.size() < 4)
{
qDebug() << "W: malformed packet";
continue;
}
f_server.name = sub_contents.at(0);
f_server.desc = sub_contents.at(1);
f_server.ip = sub_contents.at(2);
f_server.port = sub_contents.at(3).toInt();
server_list.append(f_server);
}
if (lobby_constructed)
{
w_lobby->list_servers();
}
}
else if (header == "CT")
{
QString f_name, f_message;
if (f_contents.size() == 1)
{
f_name = "";
f_message = f_contents.at(0);
}
else if (f_contents.size() >= 2)
{
f_name = f_contents.at(0);
f_message = f_contents.at(1);
}
else
goto end;
if (lobby_constructed)
{
w_lobby->append_chatmessage(f_name, f_message);
}
if (courtroom_constructed && courtroom_loaded)
{
w_courtroom->append_ms_chatmessage(f_name, f_message);
}
}
else if (header == "AO2CHECK")
{
send_ms_packet(new AOPacket("ID#AO2#" + get_version_string() + "#%"));
send_ms_packet(new AOPacket("HI#" + get_hdid() + "#%"));
if (f_contents.size() < 1)
goto end;
QStringList version_contents = f_contents.at(0).split(".");
if (version_contents.size() < 3)
goto end;
int f_release = version_contents.at(0).toInt();
int f_major = version_contents.at(1).toInt();
int f_minor = version_contents.at(2).toInt();
if (get_release() > f_release)
goto end;
else if (get_release() == f_release)
{
if (get_major_version() > f_major)
goto end;
else if (get_major_version() == f_major)
{
if (get_minor_version() >= f_minor)
goto end;
}
}
call_notice("Outdated version! Your version: " + get_version_string()
+ "\nPlease go to aceattorneyonline.com to update.");
destruct_courtroom();
destruct_lobby();
}
else if (header == "DOOM")
{
call_notice("You have been exiled from AO."
"Have a nice day.");
destruct_courtroom();
destruct_lobby();
}
end:
delete p_packet;
}
void AOApplication::server_packet_received(AOPacket *p_packet)
{
p_packet->net_decode();
QString header = p_packet->get_header();
QStringList f_contents = p_packet->get_contents();
QString f_packet = p_packet->to_string();
if (header != "checkconnection")
qDebug() << "R:" << f_packet;
if (header == "decryptor")
{
if (f_contents.size() == 0)
goto end;
//you may ask where 322 comes from. that would be a good question.
s_decryptor = fanta_decrypt(f_contents.at(0), 322).toUInt();
//default(legacy) values
encryption_needed = true;
yellow_text_enabled = false;
prezoom_enabled = false;
flipping_enabled = false;
custom_objection_enabled = false;
improved_loading_enabled = false;
desk_mod_enabled = false;
evidence_enabled = false;
//workaround for tsuserver4
if (f_contents.at(0) == "NOENCRYPT")
encryption_needed = false;
QString f_hdid;
f_hdid = get_hdid();
AOPacket *hi_packet = new AOPacket("HI#" + f_hdid + "#%");
send_server_packet(hi_packet);
}
else if (header == "ID")
{
if (f_contents.size() < 2)
goto end;
s_pv = f_contents.at(0).toInt();
server_software = f_contents.at(1);
send_server_packet(new AOPacket("ID#AO2#" + get_version_string() + "#%"));
}
else if (header == "CT")
{
if (f_contents.size() < 2)
goto end;
if (courtroom_constructed)
w_courtroom->append_server_chatmessage(f_contents.at(0), f_contents.at(1));
}
else if (header == "FL")
{
if (f_packet.contains("yellowtext",Qt::CaseInsensitive))
yellow_text_enabled = true;
if (f_packet.contains("flipping",Qt::CaseInsensitive))
flipping_enabled = true;
if (f_packet.contains("customobjections",Qt::CaseInsensitive))
custom_objection_enabled = true;
if (f_packet.contains("fastloading",Qt::CaseInsensitive))
improved_loading_enabled = true;
if (f_packet.contains("noencryption",Qt::CaseInsensitive))
encryption_needed = false;
if (f_packet.contains("deskmod",Qt::CaseInsensitive))
desk_mod_enabled = true;
if (f_packet.contains("evidence",Qt::CaseInsensitive))
evidence_enabled = true;
}
else if (header == "PN")
{
if (f_contents.size() < 2)
goto end;
w_lobby->set_player_count(f_contents.at(0).toInt(), f_contents.at(1).toInt());
}
else if (header == "SI")
{
if (f_contents.size() != 3)
goto end;
char_list_size = f_contents.at(0).toInt();
evidence_list_size = f_contents.at(1).toInt();
music_list_size = f_contents.at(2).toInt();
if (char_list_size < 1 || evidence_list_size < 0 || music_list_size < 0)
goto end;
loaded_chars = 0;
loaded_evidence = 0;
loaded_music = 0;
destruct_courtroom();
construct_courtroom();
courtroom_loaded = false;
QString window_title = "Attorney Online 2";
int selected_server = w_lobby->get_selected_server();
if (w_lobby->public_servers_selected)
{
if (selected_server >= 0 && selected_server < server_list.size())
window_title += ": " + server_list.at(selected_server).name;
}
else
{
if (selected_server >= 0 && selected_server < favorite_list.size())
window_title += ": " + favorite_list.at(selected_server).name;
}
w_courtroom->set_window_title(window_title);
w_lobby->show_loading_overlay();
w_lobby->set_loading_text("Loading");
w_lobby->set_loading_value(0);
AOPacket *f_packet;
if(improved_loading_enabled)
f_packet = new AOPacket("RC#%");
else
f_packet = new AOPacket("askchar2#%");
send_server_packet(f_packet);
}
else if (header == "CI")
{
if (!courtroom_constructed)
goto end;
for (int n_element = 0 ; n_element < f_contents.size() ; n_element += 2)
{
if (f_contents.at(n_element).toInt() != loaded_chars)
break;
//this means we are on the last element and checking n + 1 element will be game over so
if (n_element == f_contents.size() - 1)
break;
QStringList sub_elements = f_contents.at(n_element + 1).split("&");
if (sub_elements.size() < 2)
break;
char_type f_char;
f_char.name = sub_elements.at(0);
f_char.description = sub_elements.at(1);
f_char.evidence_string = sub_elements.at(3);
//temporary. the CharsCheck packet sets this properly
f_char.taken = false;
++loaded_chars;
w_lobby->set_loading_text("Loading chars:\n" + QString::number(loaded_chars) + "/" + QString::number(char_list_size));
w_courtroom->append_char(f_char);
}
int total_loading_size = char_list_size + evidence_list_size + music_list_size;
int loading_value = (loaded_chars / static_cast<double>(total_loading_size)) * 100;
w_lobby->set_loading_value(loading_value);
if (improved_loading_enabled)
send_server_packet(new AOPacket("RE#%"));
else
{
QString next_packet_number = QString::number(((loaded_chars - 1) / 10) + 1);
send_server_packet(new AOPacket("AN#" + next_packet_number + "#%"));
}
}
else if (header == "EI")
{
if (!courtroom_constructed)
goto end;
// +1 because evidence starts at 1 rather than 0 for whatever reason
//enjoy fanta
if (f_contents.at(0).toInt() != loaded_evidence + 1)
goto end;
if (f_contents.size() < 2)
goto end;
QStringList sub_elements = f_contents.at(1).split("&");
if (sub_elements.size() < 4)
goto end;
evi_type f_evi;
f_evi.name = sub_elements.at(0);
f_evi.description = sub_elements.at(1);
//no idea what the number at position 2 is. probably an identifier?
f_evi.image = sub_elements.at(3);
++loaded_evidence;
w_lobby->set_loading_text("Loading evidence:\n" + QString::number(loaded_evidence) + "/" + QString::number(evidence_list_size));
w_courtroom->append_evidence(f_evi);
int total_loading_size = char_list_size + evidence_list_size + music_list_size;
int loading_value = ((loaded_chars + loaded_evidence) / static_cast<double>(total_loading_size)) * 100;
w_lobby->set_loading_value(loading_value);
QString next_packet_number = QString::number(loaded_evidence);
send_server_packet(new AOPacket("AE#" + next_packet_number + "#%"));
}
else if (header == "EM")
{
if (!courtroom_constructed)
goto end;
for (int n_element = 0 ; n_element < f_contents.size() ; n_element += 2)
{
if (f_contents.at(n_element).toInt() != loaded_music)
break;
if (n_element == f_contents.size() - 1)
break;
QString f_music = f_contents.at(n_element + 1);
++loaded_music;
w_lobby->set_loading_text("Loading music:\n" + QString::number(loaded_music) + "/" + QString::number(music_list_size));
w_courtroom->append_music(f_music);
}
int total_loading_size = char_list_size + evidence_list_size + music_list_size;
int loading_value = ((loaded_chars + loaded_evidence + loaded_music) / static_cast<double>(total_loading_size)) * 100;
w_lobby->set_loading_value(loading_value);
QString next_packet_number = QString::number(((loaded_music - 1) / 10) + 1);
send_server_packet(new AOPacket("AM#" + next_packet_number + "#%"));
}
else if (header == "CharsCheck")
{
if (!courtroom_constructed)
goto end;
for (int n_char = 0 ; n_char < f_contents.size() ; ++n_char)
{
if (f_contents.at(n_char) == "-1")
w_courtroom->set_taken(n_char, true);
else
w_courtroom->set_taken(n_char, false);
}
}
else if (header == "SC")
{
if (!courtroom_constructed)
goto end;
for (int n_element = 0 ; n_element < f_contents.size() ; ++n_element)
{
QStringList sub_elements = f_contents.at(n_element).split("&");
char_type f_char;
f_char.name = sub_elements.at(0);
if (sub_elements.size() >= 2)
f_char.description = sub_elements.at(1);
//temporary. the CharsCheck packet sets this properly
f_char.taken = false;
++loaded_chars;
w_lobby->set_loading_text("Loading chars:\n" + QString::number(loaded_chars) + "/" + QString::number(char_list_size));
w_courtroom->append_char(f_char);
}
int total_loading_size = char_list_size + evidence_list_size + music_list_size;
int loading_value = (loaded_chars / static_cast<double>(total_loading_size)) * 100;
w_lobby->set_loading_value(loading_value);
send_server_packet(new AOPacket("RM#%"));
}
else if (header == "SM")
{
if (!courtroom_constructed)
goto end;
for (int n_element = 0 ; n_element < f_contents.size() ; ++n_element)
{
++loaded_music;
w_lobby->set_loading_text("Loading music:\n" + QString::number(loaded_music) + "/" + QString::number(music_list_size));
w_courtroom->append_music(f_contents.at(n_element));
}
int total_loading_size = char_list_size + evidence_list_size + music_list_size;
int loading_value = (loaded_chars / static_cast<double>(total_loading_size)) * 100;
w_lobby->set_loading_value(loading_value);
send_server_packet(new AOPacket("RD#%"));
}
else if (header == "DONE")
{
if (!courtroom_constructed)
goto end;
if (lobby_constructed)
w_courtroom->append_ms_chatmessage("", w_lobby->get_chatlog());
w_courtroom->done_received();
courtroom_loaded = true;
destruct_lobby();
}
else if (header == "BN")
{
if (f_contents.size() < 1)
goto end;
if (courtroom_constructed)
w_courtroom->set_background(f_contents.at(0));
}
//server accepting char request(CC) packet
else if (header == "PV")
{
if (f_contents.size() < 3)
goto end;
if (courtroom_constructed)
w_courtroom->enter_courtroom(f_contents.at(2).toInt());
}
else if (header == "MS")
{
if (courtroom_constructed && courtroom_loaded)
w_courtroom->handle_chatmessage(&p_packet->get_contents());
}
else if (header == "MC")
{
if (courtroom_constructed && courtroom_loaded)
w_courtroom->handle_song(&p_packet->get_contents());
}
else if (header == "RT")
{
if (f_contents.size() < 1)
goto end;
if (courtroom_constructed)
w_courtroom->handle_wtce(f_contents.at(0));
}
else if (header == "HP")
{
if (courtroom_constructed && f_contents.size() > 1)
w_courtroom->set_hp_bar(f_contents.at(0).toInt(), f_contents.at(1).toInt());
}
else if (header == "LE")
{
if (courtroom_constructed)
{
QVector<evi_type> f_evi_list;
for (QString f_string : f_contents)
{
QStringList sub_contents = f_string.split("&");
if (sub_contents.size() < 3)
continue;
evi_type f_evi;
f_evi.name = sub_contents.at(0);
f_evi.description = sub_contents.at(1);
f_evi.image = sub_contents.at(2);
f_evi_list.append(f_evi);
}
w_courtroom->set_evidence_list(f_evi_list);
}
}
else if (header == "IL")
{
if (courtroom_constructed && f_contents.size() > 0)
w_courtroom->set_ip_list(f_contents.at(0));
}
else if (header == "MU")
{
if (courtroom_constructed && f_contents.size() > 0)
w_courtroom->set_mute(true, f_contents.at(0).toInt());
}
else if (header == "UM")
{
if (courtroom_constructed && f_contents.size() > 0)
w_courtroom->set_mute(false, f_contents.at(0).toInt());
}
else if (header == "KK")
{
if (courtroom_constructed && f_contents.size() > 0)
{
int f_cid = w_courtroom->get_cid();
int remote_cid = f_contents.at(0).toInt();
if (f_cid != remote_cid && remote_cid != -1)
goto end;
call_notice("You have been kicked.");
construct_lobby();
destruct_courtroom();
}
}
else if (header == "KB")
{
if (courtroom_constructed && f_contents.size() > 0)
w_courtroom->set_ban(f_contents.at(0).toInt());
}
else if (header == "BD")
{
call_notice("You are banned on this server.");
}
else if (header == "ZZ")
{
if (courtroom_constructed && f_contents.size() > 0)
w_courtroom->mod_called(f_contents.at(0));
}
end:
delete p_packet;
}
void AOApplication::send_ms_packet(AOPacket *p_packet)
{
p_packet->net_encode();
QString f_packet = p_packet->to_string();
net_manager->ship_ms_packet(f_packet);
qDebug() << "S(ms):" << f_packet;
delete p_packet;
}
void AOApplication::send_server_packet(AOPacket *p_packet, bool encoded)
{
if (encoded)
p_packet->net_encode();
QString f_packet = p_packet->to_string();
if (encryption_needed)
{
qDebug() << "S(e):" << f_packet;
p_packet->encrypt_header(s_decryptor);
f_packet = p_packet->to_string();
}
else
{
qDebug() << "S:" << f_packet;
}
net_manager->ship_server_packet(f_packet);
delete p_packet;
}