Archived

This topic is now archived and is closed to further replies.

Kafra

[2016-05-01] HPMHooking improvements

Recommended Posts

Rationale:
This changeset offers improvements to the HPMHooking, making it capable to detect, at compile time, an error in the type of a hook function, as well as allowing pre-hooks to be more powerful when it comes to pointer-type arguments.

Contents:
The HPMHooking macros addHookPre() and addHookPost() have been slightly edited, and they can now detect if the type of the passed function is the correct type for the hooked function. In order to do so, the HPMHookingGen script produces one more header (HPMHooking.Defs.inc) that lists the hook function types.
This means that, if a plugin hooks into a function through HPMHooking, and the core function changes, the plugin will show a compile-time warning instead of silently compiling (and crashing at runtime or causing undesired effects).
The post-hook function types have been simplified, dropping all the extra indirection levels that were added originally.
The pre-hook function types have been changed, increasing the indirection level for pointers (now all variable types require an extra '*' in pre-hooks). This makes it possible to override const pointers from pre-hooks.

Impact:
Scripts that use the HPMHooking will need some small syntax changes.

Details:
All plugins that want to use the HPMHooking will need to #include "plugins/HPMHooking.h" (it's recommended to include it just above HPMDataCheck.h)
#include "plugins/HPMHooking.h"   // Included by plugins that use the HPMHooking
#include "common/HPMDataCheck.h"  // Included by all plugins

Then the addHookPre() and addHookPost() calls need to be updated to the new syntax, separating interface name and function name:
/* Before */
HPExport void plugin_init (void)
{
    addHookPre("pc->dropitem", my_pc_dropitem_pre);
    addHookPost("pc->dropitem", my_pc_dropitem_post);
}
 
/* Now */
HPExport void plugin_init (void)
{
    addHookPre(pc, dropitem, my_pc_dropitem_pre);
    addHookPost(pc, dropitem, my_pc_dropitem_post);
}

Pre-hook functions will need an additional indirection level in their pointer-type arguments:
/* Hooked function: */
int (*dropitem) (struct map_session_data *sd, int n, int amount);
 
/* Pre-hook (before) */
int my_pc_dropitem_pre(struct map_session_data *sd, int *n, int *amount) // Only adds '*' to the non-pointers
 
/* Pre-hook (after) */
int my_pc_dropitem_pre(struct map_session_data **sd, int *n, int *amount) // Adds '*' to everything

Post-hook functions will no longer need any additional indirection level in their arguments:
/* Hooked function: */
int (*dropitem) (struct map_session_data *sd, int n, int amount);
 
/* Post-hook (before) */
int my_pc_dropitem_post(int retVal, struct map_session_data *sd, int *n, int *amount) // Adds '*' to the non-pointers
 
/* Post-hook (after) */
int my_pc_dropitem_post(int retVal, struct map_session_data *sd, int n, int amount) // No longer adds any '*'

Merge Date:
Sun, 1 May 2016 20:22:03 +0300

Related Pull Requests:
- #1253 - https://github.com/HerculesWS/Hercules/pull/1253 - HPMHooking improvements [Haru]

Related Commits:
1ec9328 - https://github.com/HerculesWS/Hercules/commit/1ec9328 - Sun, 28 Feb 2016 02:12:48 +0100 Moved HPMHooking-related definitions to plugins/HPMHooking.h [Haru]
5db7c79 - https://github.com/HerculesWS/Hercules/commit/5db7c79 - Sun, 28 Feb 2016 02:17:21 +0100 Added type-checking for the addHookPre() and addHookPost() macros [Haru]
4e49441 - https://github.com/HerculesWS/Hercules/commit/4e49441 - Sun, 28 Feb 2016 02:20:40 +0100 HPM Hooks Update [Haru]
2788afc - https://github.com/HerculesWS/Hercules/commit/2788afc - Sun, 28 Feb 2016 02:40:15 +0100 Replaced memset with braced initializers in the HPMHooking hook handlers [Haru]
fa2f2f4 - https://github.com/HerculesWS/Hercules/commit/fa2f2f4 - Sun, 28 Feb 2016 02:41:01 +0100 HPM Hooks Update [Haru]
8aacecc - https://github.com/HerculesWS/Hercules/commit/8aacecc - Fri, 15 Apr 2016 19:37:54 +0200 Removed extra indirection level in HPMHooking post-hooks [Haru]
7eb4ae4 - https://github.com/HerculesWS/Hercules/commit/7eb4ae4 - Sun, 17 Apr 2016 00:38:37 +0200 HPM Hooks Update [Haru]
89e0550 - https://github.com/HerculesWS/Hercules/commit/89e0550 - Sun, 28 Feb 2016 02:48:47 +0100 Added one level of indirection to all variables in pre-hook functions [Haru]
e9c98a1 - https://github.com/HerculesWS/Hercules/commit/e9c98a1 - Sun, 28 Feb 2016 02:50:40 +0100 HPM Hooks Update [Haru]
95b4e32 - https://github.com/HerculesWS/Hercules/commit/95b4e32 - Sun, 1 May 2016 20:22:03 +0300 Merge pull request #1253 from HerculesWS/hpmhooking [Andrei Karas]

View the full article

Share this post


Link to post
Share on other sites