Jump to content
Haru

[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
Note: arguments of type va_list do not require an additional indirection level. 'va_list ap' remains 'va_list ap' and does not become 'va_list *ap'

 

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]

Edited by Haru
Added note about va_list arguments

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.