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
|
#!/usr/bin/perl
# Copyright (c) Hercules Dev Team, licensed under GNU GPL.
# See the LICENSE file
use strict;
use warnings;
use XML::Simple;
# XML Parser hint (some are faster than others)
#local $ENV{XML_SIMPLE_PREFERRED_PARSER} = ''; # 0m7.138s
local $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser'; # 0m2.674s
#local $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::SAX::Expat'; # 0m7.026s
#local $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::LibXML::SAX'; # 0m4.152s
sub trim($) {
my $s = $_[0];
$s =~ s/^\s+//; $s =~ s/\s+$//;
return $s;
}
sub parse($$) {
my ($p, $d) = @_;
# Strip gcc attributes
$p =~ s/\s*__attribute__\s*\(\(.*$//;
$d =~ s/\s*__attribute__\s*\(\(.*$//;
$p =~ s/^.*?\)\((.*)\).*$/$1/; # Clean up extra parentheses )(around the arglist)
# Retrieve return type
unless ($d =~ /^(.+)\(\*\s*[a-zA-Z0-9_]+_interface::([^\)]+)\s*\)\s*\(.*\)$/) {
print "Error: unable to parse '$d'\n";
return {};
}
my $rt = trim($1); #return type
my $name = trim($2); #function name
my @args;
my ($anonvars, $variadic) = (0, 0);
my ($lastvar, $notes) = ('', '');
$p = ' ' unless $p; # ensure there's at least one character (we don't want a do{} block)
while ($p) { # Scan the string for variables
my $current = '';
my ($paren, $needspace) = (0, 0);
while ($p) { # Parse tokens
$p =~ s|^(?:\s*(?:/\*.*?\*/)?)*||; # strip heading whitespace and c-style comments
last unless $p;
if ($p =~ s/^([a-zA-Z0-9_]+)//) { # Word (variable, type)
$current .= ' ' if $needspace;
$current .= $1;
$needspace = 1;
next;
}
if ($p =~ s/^(,)//) { # Comma
last unless $paren; # Argument terminator unless inside parentheses
$current .= $1;
$needspace = 1;
next;
}
if ($p =~ s/^(\*)//) { # Pointer
$current .= ' ' if $needspace;
$current .= $1;
$needspace = 0;
next;
}
if ($p =~ s/^([\[\].])//) { # Array subscript
$current .= $1;
$needspace = 0;
next;
}
if ($p =~ s/^(\()//) { # Open parenthesis
$current .= ' ' if $needspace;
$current .= $1;
$needspace = 0;
$paren++;
next;
}
if ($p =~ s/^(\))//) { # Closed parenthesis
$current .= $1;
$needspace = 1;
if (!$paren) {
$notes .= "\n/* Error: unexpected ')' */";
print "Error: unexpected ')' at '$p'\n";
} else {
$paren--;
}
next;
}
$p =~ s/^(.)//; # Any other symbol
$notes .= "\n/* Error: Unexpected character '$1' */";
print "Error: Unexpected character '$1' at '$p'\n";
$current .= $1;
$needspace = 0;
}
$current =~ s/^\s+//; $current =~ s/\s+$//g; # trim
next if (!$current or $current =~ /^void$/); # Skip if empty
my ($array, $type1, $var, $type2, $indir) = ('', '', '', '', '');
if ($current =~ qr/^
([\w\s\[\]*]+\() # Capture words, spaces, array subscripts, up to the first '(' (return type)
\s* # Skip spaces
(\*) # Capture the '*' from the function name
\s* # Skip spaces
([\w]*) # Capture words (function name)
\s* # Skip spaces
(\)\s*\([\w\s\[\]*,]*\)) # Capture first ')' followed by a '( )' block containing arguments
\s* # Skip spaces
$/x
) { # Match a function pointer
$type1 = trim($1);
$indir = $2;
$var = trim($3 // '');
$type2 = trim($4);
} elsif ($current eq '...') { # Match a '...' variadic argument
$type1 = '...';
$indir = '';
$var = '';
$type2 = '';
} else { # Match a "regular" variable
$type1 = '';
while(1) {
if ($current =~ /^(const)\s+(.*)$/) { # const modifier
$type1 .= "$1 ";
$current = $2 // '';
next;
}
if ($current =~ /^((?:un)?signed)\s+((?:char|int|long|short)[*\s]+.*)$/
or $current =~ /^(long|short)\s+((?:int|long)[*\s]+.*)$/
) { # signed/unsigned/long/short modifiers
$current = $2;
$type1 .= "$1 ";
next;
}
if ($current =~ /^(struct|enum)\s+(.*)$/) { # enum and struct names
$current = $2 // '';
$type1 .= "$1 ";
}
last; # No other modifiers
}
if ($current =~ /^\s*(\w+)((?:const|[*\s])*)(\w*)\s*((?:\[\])?)$/) { # Variable type and name
$type1 .= trim($1);
$indir = trim($2 // '');
$var = trim($3 // '');
$array = trim($4 // '');
$type2 = '';
} else { # Unsupported
$notes .= "\n/* Error: Unhandled var type '$current' */";
print "Error: Unhandled var type '$current'\n";
push(@args, { var => $current });
next;
}
}
unless ($var) {
$anonvars++;
$var = "p$anonvars";
}
my ($callvar, $pre_code, $post_code, $dereference, $addressof) = ($var, '', '', '', '');
my $indirectionlvl = () = $indir =~ /\*/;
if ($type1 eq 'va_list') { # Special handling for argument-list variables
$callvar = "${var}___copy";
$pre_code = "va_list ${callvar}; va_copy(${callvar}, ${var});";
$post_code = "va_end(${callvar});";
} elsif ($type1 eq '...') { # Special handling for variadic arguments
unless ($lastvar) {
$notes .= "\n/* Error: Variadic function with no fixed arguments */";
print "Error: Variadic function with no fixed arguments\n";
next;
}
$pre_code = "va_list ${callvar}; va_start(${callvar}, ${lastvar});";
$post_code = "va_end(${callvar});";
$var = '';
$variadic = 1;
} elsif (!$indirectionlvl) { # Increase indirection level when necessary
$dereference = '*';
$addressof = '&';
}
$indirectionlvl++ if ($array); # Arrays are pointer, no matter how cute you write them
push(@args, {
var => $var,
callvar => $callvar,
type => $type1.$array.$type2,
orig => $type1 eq '...' ? '...' : trim("$type1 $indir$var$array $type2"),
indir => $indirectionlvl,
hookf => $type1 eq '...' ? "va_list ${var}" : trim("$type1 $dereference$indir$var$array $type2"),
hookc => trim("$addressof$callvar"),
origc => trim($callvar),
pre => $pre_code,
post => $post_code,
});
$lastvar = $var;
}
my $rtmemset = 0;
my $rtinit = '';
foreach ($rt) { # Decide initialization for the return value
my $x = $_;
if ($x =~ /^const\s+(.+)$/) { # Strip const modifier
$x = $1;
}
if ($x =~ /\*/g) { # Pointer
$rtinit = ' = NULL';
} elsif ($x eq 'void') { # void
$rtinit = '';
} elsif ($x eq 'bool') { # bool
$rtinit = ' = false';
} elsif ($x =~ /^(?:enum\s+)?damage_lv$/) { # Known enum damage_lv
$rtinit = ' = ATK_NONE';
} elsif ($x =~ /^(?:enum\s+)?sc_type$/) { # Known enum sc_type
$rtinit = ' = SC_NONE';
} elsif ($x =~/^(?:enum\s+)?c_op$/) { # Known enum c_op
$rtinit = ' = C_NOP';
} elsif ($x =~ /^enum\s+BATTLEGROUNDS_QUEUE_ACK$/) { # Known enum BATTLEGROUNDS_QUEUE_ACK
$rtinit = ' = BGQA_SUCCESS';
} elsif ($x =~ /^enum\s+bl_type$/) { # Known enum bl_type
$rtinit = ' = BL_NUL';
} elsif ($x =~ /^enum\s+homun_type$/) { # Known enum homun_type
$rtinit = ' = HT_INVALID';
} elsif ($x =~ /^enum\s+channel_operation_status$/) { # Known enum channel_operation_status
$rtinit = ' = HCS_STATUS_FAIL';
} elsif ($x =~ /^enum\s+bg_queue_types$/) { # Known enum bg_queue_types
$rtinit = ' = BGQT_INVALID';
} elsif ($x =~ /^struct\s+.*$/ or $x eq 'DBData') { # Structs
$rtinit = '';
$rtmemset = 1;
} elsif ($x =~ /^(?:(?:un)?signed\s+)?(?:char|int|long|short)$/
or $x =~ /^(?:long|short)\s+(?:int|long)$/
or $x =~ /^u?int(?:8|16|32|64)$/
or $x eq 'defType'
) { # Numeric variables
$rtinit = ' = 0';
} else { # Anything else
$notes .= "\n/* Unknown return type '$rt'. Initializing to '0'. */";
print "Unknown return type '$rt'. Initializing to '0'.\n";
$rtinit = ' = 0';
}
}
return {
name => $name,
vname => $variadic ? "v$name" : $name,
type => $rt,
typeinit => $rtinit,
memset => $rtmemset,
variadic => $variadic,
args => \@args,
notes => $notes,
};
}
my %key2original;
my @files = grep { -f } glob 'doxyoutput/xml/*interface*.xml';
my %ifs;
my %keys = (
login => [ ],
char => [ ],
map => [ ],
);
foreach my $file (@files) { # Loop through the xml files
my $xml = new XML::Simple;
my $data = $xml->XMLin($file, ForceArray => 1);
my $filekey = (keys %{ $data->{compounddef} })[0];
my $loc = $data->{compounddef}->{$filekey}->{location}->[0];
next unless $loc->{file} =~ /src\/(map|char|login)\//;
my $servertype = $1;
my $key = $data->{compounddef}->{$filekey}->{compoundname}->[0];
my $original = $key;
# Some known interfaces with different names
if ($key =~ /battleground/) {
$key = "bg";
} elsif ($key =~ /guild_storage/) {
$key = "gstorage";
} elsif ($key =~ /inter_homunculus/) { # to avoid replace to homun
$key = "inter_homunculus";
} elsif ($key =~ /homunculus/) {
$key = "homun";
} elsif ($key =~ /irc_bot/) {
$key = "ircbot";
} elsif ($key =~ /log_interface/) {
$key = "logs";
} elsif ($key =~ /pc_groups_interface/) {
$key = "pcg";
} elsif ($key =~ /char_interface/) {
$key = "chr";
} else {
$key =~ s/_interface//;
}
foreach my $v ($data->{compounddef}->{$filekey}->{sectiondef}->[0]) { # Loop through the sections
my $memberdef = $v->{memberdef};
foreach my $f (sort { # Sort the members in declaration order according to what the xml says
my $astart = $a->{location}->[0]->{bodystart} || $a->{location}->[0]->{line};
my $bstart = $b->{location}->[0]->{bodystart} || $b->{location}->[0]->{line};
$astart <=> $bstart
} @$memberdef) { # Loop through the members
my $t = $f->{argsstring}->[0];
next unless ref $t ne 'HASH' and $t =~ /^[^\[]/; # If it's not a string, or if it starts with an array subscript, we can skip it
my $if = parse($t, $f->{definition}->[0]);
next unless scalar keys %$if; # If it returns an empty hash reference, an error must've occurred
# Skip variadic functions, we only allow hooks on their arglist equivalents.
# i.e. you can't hook on map->foreachinmap, but you hook on map->vforeachinmap
# (foreachinmap is guaranteed to do nothing other than call vforeachinmap)
next if ($if->{variadic});
# Some preprocessing
$if->{hname} = "HP_${key}_$if->{name}";
$if->{hvname} = "HP_${key}_$if->{vname}";
$if->{handlerdef} = "$if->{type} $if->{hname}(";
$if->{predef} = "$if->{type} (*preHookFunc) (";
$if->{postdef} = "$if->{type} (*postHookFunc) (";
if ($if->{type} eq 'void') {
$if->{precall} = '';
$if->{postcall} = '';
$if->{origcall} = '';
} else {
$if->{precall} = "retVal___ = ";
$if->{postcall} = "retVal___ = ";
$if->{origcall} = "retVal___ = ";
}
$if->{precall} .= "preHookFunc(";
$if->{postcall} .= "postHookFunc(";
$if->{origcall} .= "HPMHooks.source.$key.$if->{vname}(";
$if->{before} = []; $if->{after} = [];
my ($i, $j) = (0, 0);
if ($if->{type} ne 'void') {
$j++;
$if->{postdef} .= "$if->{type} retVal___";
$if->{postcall} .= "retVal___";
}
foreach my $arg (@{ $if->{args} }) {
push(@{ $if->{before} }, $arg->{pre}) if ($arg->{pre});
push(@{ $if->{after} }, $arg->{post}) if ($arg->{post});
if ($i) {
$if->{handlerdef} .= ', ';
$if->{predef} .= ', ';
$if->{precall} .= ', ';
$if->{origcall} .= ', ';
}
if ($j) {
$if->{postdef} .= ', ';
$if->{postcall} .= ', ';
}
$if->{handlerdef} .= $arg->{orig};
$if->{predef} .= $arg->{hookf};
$if->{precall} .= $arg->{hookc};
$if->{postdef} .= $arg->{hookf};
$if->{postcall} .= $arg->{hookc};
$if->{origcall} .= $arg->{origc};
$i++; $j++;
}
if (!$i) {
$if->{predef} .= 'void';
$if->{handlerdef} .= 'void';
}
if (!$j) {
$if->{postdef} .= 'void';
}
$if->{handlerdef} .= ')';
$if->{predef} .= ");";
$if->{precall} .= ");";
$if->{postdef} .= ");";
$if->{postcall} .= ");";
$if->{origcall} .= ");";
$key2original{$key} = $original;
$ifs{$key} = [] unless $ifs{$key};
push(@{ $ifs{$key} }, $if);
}
}
push(@{ $keys{$servertype} }, $key) if $key2original{$key};
}
foreach my $servertype (keys %keys) {
my $keysref = $keys{$servertype};
# Some interfaces use different names
my %exportsymbols = map {
$_ => &{ sub ($) {
return 'battlegrounds' if $servertype eq 'map' and $_ =~ /^bg$/;
return 'pc_groups' if $servertype eq 'map' and $_ =~ /^pcg$/;
return $_;
}}($_);
} @$keysref;
my ($maxlen, $idx) = (0, 0);
my $fname;
$fname = "../../src/plugins/HPMHooking/HPMHooking_${servertype}.HookingPoints.inc";
open(FH, ">", $fname)
or die "cannot open > $fname: $!";
print FH <<"EOF";
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
//
// NOTE: This file was auto-generated and should never be manually edited,
// as it will get overwritten.
struct HookingPointData HookingPoints[] = {
EOF
foreach my $key (@$keysref) {
print FH "/* ".$key." */\n";
foreach my $if (@{ $ifs{$key} }) {
print FH <<"EOF";
{ HP_POP($key\->$if->{name}, $if->{hname}) },
EOF
$idx += 2;
$maxlen = length($key."->".$if->{name}) if( length($key."->".$if->{name}) > $maxlen )
}
}
print FH <<"EOF";
};
int HookingPointsLenMax = $maxlen;
EOF
close FH;
$fname = "../../src/plugins/HPMHooking/HPMHooking_${servertype}.sources.inc";
open(FH, ">", $fname)
or die "cannot open > $fname: $!";
print FH <<"EOF";
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
//
// NOTE: This file was auto-generated and should never be manually edited,
// as it will get overwritten.
EOF
foreach my $key (@$keysref) {
print FH <<"EOF";
memcpy(&HPMHooks.source.$key, $key, sizeof(struct $key2original{$key}));
EOF
}
close FH;
$fname = "../../src/plugins/HPMHooking/HPMHooking_${servertype}.GetSymbol.inc";
open(FH, ">", $fname)
or die "cannot open > $fname: $!";
print FH <<"EOF";
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
//
// NOTE: This file was auto-generated and should never be manually edited,
// as it will get overwritten.
EOF
foreach my $key (@$keysref) {
print FH <<"EOF";
if( !($key = GET_SYMBOL("$exportsymbols{$key}") ) ) return false;
EOF
}
close FH;
$fname = "../../src/plugins/HPMHooking/HPMHooking_${servertype}.HPMHooksCore.inc";
open(FH, ">", $fname)
or die "cannot open > $fname: $!";
print FH <<"EOF";
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
//
// NOTE: This file was auto-generated and should never be manually edited,
// as it will get overwritten.
struct {
EOF
foreach my $key (@$keysref) {
foreach my $if (@{ $ifs{$key} }) {
print FH <<"EOF";
struct HPMHookPoint *$if->{hname}_pre;
struct HPMHookPoint *$if->{hname}_post;
EOF
}
}
print FH <<"EOF";
} list;
struct {
EOF
foreach my $key (@$keysref) {
foreach my $if (@{ $ifs{$key} }) {
print FH <<"EOF";
int $if->{hname}_pre;
int $if->{hname}_post;
EOF
}
}
print FH <<"EOF";
} count;
struct {
EOF
foreach my $key (@$keysref) {
print FH <<"EOF";
struct $key2original{$key} $key;
EOF
}
print FH <<"EOF";
} source;
EOF
close FH;
$fname = "../../src/plugins/HPMHooking/HPMHooking_${servertype}.Hooks.inc";
open(FH, ">", $fname)
or die "cannot open > $fname: $!";
print FH <<"EOF";
// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// See the LICENSE file
//
// NOTE: This file was auto-generated and should never be manually edited,
// as it will get overwritten.
EOF
foreach my $key (@$keysref) {
print FH <<"EOF";
/* $key */
EOF
foreach my $if (@{ $ifs{$key} }) {
my ($initialization, $beforeblock3, $beforeblock2, $afterblock3, $afterblock2, $retval) = ('', '', '', '', '', '');
unless ($if->{type} eq 'void') {
$initialization = "\n\t$if->{type} retVal___$if->{typeinit};";
$initialization .= "\n\tmemset(&retVal___, '\\0', sizeof($if->{type}));" if $if->{memset};
}
$beforeblock3 .= "\n\t\t\t$_" foreach (@{ $if->{before} });
$afterblock3 .= "\n\t\t\t$_" foreach (@{ $if->{after} });
$beforeblock2 .= "\n\t\t$_" foreach (@{ $if->{before} });
$afterblock2 .= "\n\t\t$_" foreach (@{ $if->{after} });
$retval = ' retVal___' unless $if->{type} eq 'void';
print FH <<"EOF";
$if->{handlerdef} {$if->{notes}
int hIndex = 0;${initialization}
if( HPMHooks.count.$if->{hname}_pre ) {
$if->{predef}
*HPMforce_return = false;
for(hIndex = 0; hIndex < HPMHooks.count.$if->{hname}_pre; hIndex++ ) {$beforeblock3
preHookFunc = HPMHooks.list.$if->{hname}_pre[hIndex].func;
$if->{precall}$afterblock3
}
if( *HPMforce_return ) {
*HPMforce_return = false;
return$retval;
}
}
{$beforeblock2
$if->{origcall}$afterblock2
}
if( HPMHooks.count.$if->{hname}_post ) {
$if->{postdef}
for(hIndex = 0; hIndex < HPMHooks.count.$if->{hname}_post; hIndex++ ) {$beforeblock3
postHookFunc = HPMHooks.list.$if->{hname}_post[hIndex].func;
$if->{postcall}$afterblock3
}
}
return$retval;
}
EOF
}
}
close FH;
}
|