wovencode / OpenMMO-Other

Ideas, Tasks and others
2 stars 0 forks source link

[ongoing] Hook Calling Protocol #26

Open DX4D opened 4 years ago

DX4D commented 4 years ago

Here is an example of a properly called hook:

protected override void UpdateServer() { base.UpdateServer(); this.InvokeInstanceDevExtMethods(nameof(UpdateServer)); //HOOK }

And this is a properly declared hook method. [DevExtMethods(nameof(UpdateServer))] void UpdateServer_MyHookedMethod() { }

Normally Hooks are called by passing in a string with the name of the hook you wish to call. This poses multiple issues:

  1. If the name of the method is ever renamed, the hooks that are called by string will not be renamed. Worse yet, this will not trigger any errors or warnings, your hooks will simply fail silently and it may be a long time before you realize it.
  2. When debugging, it is much more difficult to trace back a Hook that was called via string. When using the nameof method, you are able to right click the method in any IDE and select "Find All References" to see where that hook is called.
  3. When using the helper functions in your IDE to mass rename a method, hooks that are called using the nameof method will also be renamed.

In addition to the use of nameof to call and declare hooks, be sure to add the comment //HOOK after calling each hook. This will allow users to search the project for all instances of the word HOOK (matching case) to get a full list off all the hooks they can use.

wovencode commented 4 years ago

doing that since a while already, works very well