-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol.pl
executable file
·118 lines (108 loc) · 2.37 KB
/
protocol.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
my $state = 'none';
my @fields;
my $special;
my $debug = (shift || '') eq '-v';
my %types = (
'bool' => 'FIELD_BYTE',
'boolean' => 'FIELD_BYTE',
'byte' => 'FIELD_BYTE',
'ubyte' => 'FIELD_UBYTE',
'unsigned byte' => 'FIELD_UBYTE',
'short' => 'FIELD_SHORT',
'int' => 'FIELD_INT',
'long' => 'FIELD_LONG',
'float' => 'FIELD_FLOAT',
'double' => 'FIELD_DOUBLE',
'string' => 'FIELD_STRING',
'[[slot_data|slot]]' => 'FIELD_ITEM',
'metadata' => 'FIELD_ENTITY_DATA',
);
my %specials = (
'ADD_OBJECT_OR_VEHICLE' => [ 5, 'FIELD_OBJECT_DATA' ],
'MAP_CHUNK' => [ 6, 'FIELD_BYTE_ARRAY' ],
'MULTI_BLOCK_CHANGE' => [ 2, 'FIELD_BLOCK_ARRAY' ],
'EXPLOSION' => [ 4, 'FIELD_EXPLOSION_ARRAY' ],
'WINDOW_ITEMS' => [ 1, 'FIELD_ITEM_ARRAY' ],
'ITEM_DATA' => [ 2, 'FIELD_MAP_ARRAY' ],
);
sub sanitise
{
my $_ = uc shift;
s/\// OR /g;
s/&/AND/g;
s/&/AND/g;
s/[^\w]+/_/g;
s/^_+|_+$//g;
return $_;
}
while (my $line = <>)
{
if ($state eq 'none' && $line =~ /^=== (.*) \((0x..)\) ===$/)
{
my $name = sanitise $1;
my $id = $2;
print "PACKET($id, $name, ";
print STDERR "$name: $id\n" if $debug;
$state = 'packet';
$special = $specials{$name};
}
elsif ($state eq 'packet' && $line =~ /^\|- class="row1"$/)
{
$state = 'fields';
@fields = ();
}
# if, not elsif; we want the row1 header from the previous transition
if ($state eq 'fields')
{
if ($line =~ /^\|- class="row.*"$/)
{
<> if @fields == 0;
$line = <>;
if ($line ne "|}\n")
{
$line =~ /^\| class=".*" \| (.*)$/ or next;
my $name = sanitise($1) || 'UNKNOWN';
$line = <>;
$line =~ /^\| class=".*" \| (.*)$/ or next;
my $type = $types{lc $1} or die "unknown field type $1";
next if $1 =~ /^\d/;
push @fields, { 'type' => $type, 'name' => $name };
}
}
if (defined $special)
{
my ($pos, $type) = @$special;
if (@fields == $pos)
{
push @fields, { 'type' => $type, 'name' => 'DATA' };
while ($line ne "|}\n")
{
$line = <>;
}
}
}
# again if for fall-through
if ($line eq "|}\n")
{
print scalar @fields;
if (@fields)
{
foreach my $field (@fields)
{
print STDERR "- $field->{type} $field->{name}\n" if $debug;
print ", FIELD($field->{type}, $field->{name})";
}
}
else
{
print ", 0";
}
print STDERR "\n" if $debug;
print ")\n";
$state = 'none';
}
}
}