-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_DecPacket.php
87 lines (80 loc) · 2.54 KB
/
_DecPacket.php
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
#!/usr/bin/env php
<?php
/*
* DecMcpe
*
* Copyright (C) 2016 PEMapModder
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PEMapModder
*/
$opts = getopt("", ["in:", "out:"]);
$SUBJECT = $opts["in"] ?? "in/mpe.asm";
$OUTPUT = $opts["out"] ?? "out/pkdump.json";
/** @noinspection PhpUsageOfSilenceOperatorInspection */
@mkdir(dirname($OUTPUT), 0777, true);
spl_autoload_register(function ($class){
require_once __DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
});
$pkColl = new PacketCollection;
echo "\rOpening stream to read $SUBJECT";
$is = fopen($SUBJECT, "r");
$linesCnt = 0;
while(!feof($is)){
$linesCnt++;
// echo "\rLine $linesCnt: Reading...";
$line = rtrim(fgets($is));
if(strpos($line, "SharedConstants::NetworkProtocolVersion") !== false){
echo "\rLine $linesCnt: Reading...";
echo " Detecting protocol version...";
$linesCnt++;
$next = trim(fgets($is));
if(preg_match('/[0-9a-f]+:[ \t]+([0-9a-f]+)/', $next, $match)){
$pkColl->setProtocolVersion(hexdec($match[1]));
echo " Detected 0x$match[1]";
}
}elseif(preg_match('/([A-Za-z0-9_]+Packet)::getId\(\)/', $line, $match)){
echo "\rLine $linesCnt: Reading...";
echo " Detecting packet-to-ID declaration...";
$name = $match[1];
$linesCnt++;
$next = trim(fgets($is));
if(preg_match('/[0-9a-f]{4}[ \t]+movs[ \t]+r0, #([0-9]+)/', $next, $match2)){
$id = hexdec($match2[1]) + 0x8e;
printf(" Detected 0x%02x", $id);
$pkColl->get($name)->id = $id;
$pkColl->get($name)->idHex = sprintf("0x%02x", $id);
}
}elseif(preg_match('/^[0-9a-f]+ <([A-Za-z0-9_]+Packet)::read\(RakNet::BitStream\*\)>:/', $line, $match)){
echo "\rLine $linesCnt: Reading...";
$pkName = $match[1];
// $fields = [];
// $pkSize = 0;
$pk = $pkColl->get($pkName);
$pk->startAnalyze();
while(!feof($is)){
$linesCnt++;
echo "\rLine $linesCnt: Reading $pkName packet structure...";
$line = rtrim(fgets($is));
if(strlen($line) - 2 !== strlen(ltrim($line, " "))){
break;
}
// $field = new PacketField($line, $pkSize);
// if($field->isValid()){
// $fields[] = $field;
// }
$pk->analyze($line);
}
$pk->stopAnalyze();
$pkColl->free();
echo "\rFreed packet instance (memory: " . round(memory_get_usage() / 1024, 2) . " KB)", PHP_EOL;
}
}
echo "\rWriting to $OUTPUT";
$pkColl->write($OUTPUT);
echo PHP_EOL;
exit(0);