From 5db7c799055c6ae9c4463f6cf4c88a35597d5d31 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 28 Feb 2016 02:17:21 +0100 Subject: Added type-checking for the addHookPre() and addHookPost() macros - The macros will now throw a warning at compile time if a plugin is using a wrong function type for a pre or post hook. This avoids some very subtle, hard to detect, issues. - The macros now require 3 arguments instead of 2. Example: old code: addHookPre("ifname->function" my_hook); becomes: addHookPre(ifname, function, my_hook); Signed-off-by: Haru --- src/plugins/HPMHooking.h | 14 ++- src/plugins/HPMHooking/HPMHooking.Defs.inc | 24 ++++ src/plugins/sample.c | 4 +- tools/HPMHookGen/HPMHookGen.pl | 177 ++++++++++------------------- 4 files changed, 96 insertions(+), 123 deletions(-) create mode 100644 src/plugins/HPMHooking/HPMHooking.Defs.inc diff --git a/src/plugins/HPMHooking.h b/src/plugins/HPMHooking.h index b2d95dd8d..44970863c 100644 --- a/src/plugins/HPMHooking.h +++ b/src/plugins/HPMHooking.h @@ -43,8 +43,18 @@ struct HPMHooking_core_interface { #else // ! HERCULES_CORE HPExport struct HPMHooking_interface HPMHooking_s; -#define addHookPre(tname,hook) (HPMi->hooking->AddHook(HOOK_TYPE_PRE,(tname),(hook),HPMi->pid)) -#define addHookPost(tname,hook) (HPMi->hooking->AddHook(HOOK_TYPE_POST,(tname),(hook),HPMi->pid)) +#include "HPMHooking/HPMHooking.Defs.inc" + +#define addHookPre(ifname, funcname, hook) ( \ + (void)((HPMHOOK_pre_ ## ifname ## _ ## funcname)0 == (hook)), \ + HPMi->hooking->AddHook(HOOK_TYPE_PRE, #ifname "->" #funcname, (hook), HPMi->pid) \ + ) + +#define addHookPost(ifname, funcname, hook) ( \ + (void)((HPMHOOK_post_ ## ifname ## _ ## funcname)0 == (hook)), \ + HPMi->hooking->AddHook(HOOK_TYPE_POST, #ifname "->" #funcname, (hook), HPMi->pid) \ + ) + /* need better names ;/ */ /* will not run the original function after pre-hook processing is complete (other hooks will run) */ #define hookStop() (HPMi->hooking->HookStop(__func__,HPMi->pid)) diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc new file mode 100644 index 000000000..72490c7c1 --- /dev/null +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -0,0 +1,24 @@ +/** + * This file is part of Hercules. + * http://herc.ws - http://github.com/HerculesWS/Hercules + * + * Copyright (C) 2013-2016 Hercules Dev Team + * + * Hercules is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* + * NOTE: This file was auto-generated and should never be manually edited, + * as it will get overwritten. + */ diff --git a/src/plugins/sample.c b/src/plugins/sample.c index d6036dd70..1e45f9afd 100644 --- a/src/plugins/sample.c +++ b/src/plugins/sample.c @@ -200,13 +200,13 @@ HPExport void plugin_init (void) { /* in this sample we add a PreHook to pc->dropitem */ /* to identify whether the item being dropped is on amount higher than 1 */ /* if so, it stores the amount on a variable (my_pc_dropitem_storage) and changes the amount to 1 */ - addHookPre("pc->dropitem",my_pc_dropitem_pre); + addHookPre(pc, dropitem, my_pc_dropitem_pre); /* in this sample we add a PostHook to pc->dropitem */ /* if the original pc->dropitem was successful and the amount stored on my_pc_dropitem_storage is higher than 1, */ /* our posthook will display a message to the user about the cap */ /* - by checking whether it was successful (retVal value) it allows for the originals conditions to take place */ - addHookPost("pc->dropitem",my_pc_dropitem_post); + addHookPost(pc, dropitem, my_pc_dropitem_post); } } /* triggered when server starts loading, before any server-specific data is set */ diff --git a/tools/HPMHookGen/HPMHookGen.pl b/tools/HPMHookGen/HPMHookGen.pl index 0e33fd002..4dedb48c2 100755 --- a/tools/HPMHookGen/HPMHookGen.pl +++ b/tools/HPMHookGen/HPMHookGen.pl @@ -460,31 +460,13 @@ foreach my $file (@files) { # Loop through the xml files } my $year = (localtime)[5] + 1900; -foreach my $servertype (keys %keys) { - my $keysref = $keys{$servertype}; - # Some interfaces use different names - my %exportsymbols = map { - $_ => &{ sub ($) { - return 'battlegrounds' if $_ =~ /^bg$/; - return 'pc_groups' if $_ =~ /^pcg$/; - return $_; - }}($_); - } @$keysref; - my ($maxlen, $idx) = (0, 0); - my $fname; - - if ($servertype eq 'all') { - $fname = "../../src/common/HPMSymbols.inc.h"; - open(FH, ">", $fname) - or die "cannot open > $fname: $!"; - - print FH <<"EOF"; +my $fileheader = <<"EOF"; /** * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2015-$year Hercules Dev Team + * Copyright (C) 2013-$year Hercules Dev Team * * Hercules is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -504,7 +486,29 @@ foreach my $servertype (keys %keys) { * NOTE: This file was auto-generated and should never be manually edited, * as it will get overwritten. */ +EOF +foreach my $servertype (keys %keys) { + my $keysref = $keys{$servertype}; + # Some interfaces use different names + my %exportsymbols = map { + $_ => &{ sub ($) { + return 'battlegrounds' if $_ =~ /^bg$/; + return 'pc_groups' if $_ =~ /^pcg$/; + return $_; + }}($_); + } @$keysref; + + my ($maxlen, $idx) = (0, 0); + my $fname; + + if ($servertype eq 'all') { + $fname = "../../src/common/HPMSymbols.inc.h"; + open(FH, ">", $fname) + or die "cannot open > $fname: $!"; + + print FH <<"EOF"; +$fileheader #if !defined(HERCULES_CORE) EOF @@ -538,6 +542,36 @@ EOF } EOF close FH; + + $fname = "../../src/plugins/HPMHooking/HPMHooking.Defs.inc"; + open(FH, ">", $fname) + or die "cannot open > $fname: $!"; + + print FH <<"EOF"; +$fileheader +EOF + + foreach my $key (@$keysref) { + print FH <<"EOF"; +#ifdef $fileguards{$key}->{guard} /* $key */ +EOF + + foreach my $if (@{ $ifs{$key} }) { + my ($predef, $postdef) = ($if->{predef}, $if->{postdef}); + $predef =~ s/preHookFunc/HPMHOOK_pre_${key}_$if->{name}/; + $postdef =~ s/postHookFunc/HPMHOOK_post_${key}_$if->{name}/; + + print FH <<"EOF"; +typedef $predef +typedef $postdef +EOF + } + print FH <<"EOF"; +#endif // $fileguards{$key}->{guard} +EOF + } + close FH; + next; } @@ -546,30 +580,7 @@ EOF or die "cannot open > $fname: $!"; print FH <<"EOF"; -/** - * This file is part of Hercules. - * - * Copyright (C) 2013-$year Hercules Dev Team - * - * Hercules is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* - * NOTE: This file was auto-generated and should never be manually edited, - * as it will get overwritten. - */ - +$fileheader struct HookingPointData HookingPoints[] = { EOF @@ -597,31 +608,7 @@ EOF or die "cannot open > $fname: $!"; print FH <<"EOF"; -/** - * This file is part of Hercules. - * http://herc.ws - http://github.com/HerculesWS/Hercules - * - * Copyright (C) 2013-$year Hercules Dev Team - * - * Hercules is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* - * NOTE: This file was auto-generated and should never be manually edited, - * as it will get overwritten. - */ - +$fileheader EOF foreach my $key (@$keysref) { @@ -636,31 +623,7 @@ EOF or die "cannot open > $fname: $!"; print FH <<"EOF"; -/** - * This file is part of Hercules. - * http://herc.ws - http://github.com/HerculesWS/Hercules - * - * Copyright (C) 2013-$year Hercules Dev Team - * - * Hercules is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* - * NOTE: This file was auto-generated and should never be manually edited, - * as it will get overwritten. - */ - +$fileheader struct { EOF @@ -711,31 +674,7 @@ EOF or die "cannot open > $fname: $!"; print FH <<"EOF"; -/** - * This file is part of Hercules. - * http://herc.ws - http://github.com/HerculesWS/Hercules - * - * Copyright (C) 2013-$year Hercules Dev Team - * - * Hercules is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/* - * NOTE: This file was auto-generated and should never be manually edited, - * as it will get overwritten. - */ - +$fileheader EOF foreach my $key (@$keysref) { -- cgit v1.2.3-70-g09d2