To hook, use the word HOOK + AS (MS Detours tools) or HOOK + PATCH (change call by patching the calling instruction). To call the original function, the hook object can be used as a function pointer. To call a class method, you need to cast the pointer to the form of a member function (for example, this->*Hookname), or to simplify readability, use the THISCALL macro as in the example below.
- For correct hooking, it is necessary that the signature of the hook function be compatible with the signature of the hooked function. In case of incorrect hook, the engine may crash. To prevent such situations, Union at the start of the game will point out the mistake made in the hook and demand to eliminate it.
- When hooked by two or more plugins of the same function, the calls will be queued. Thus, several plug-ins can simultaneously access the execution of common procedures without disrupting the program.
- Both the name of the function and its address can be specified as a hooked function.
- Pros and cons of hooking methods:
Choosing a hook method is quite simple. You should first try the PATCH method as a completely compatible type with any other extensions. However, PATCH is not always able to recognize the address of a function in the bytecode, since it can be given implicitly. Then the console (SystemPack.ini -> ShowDebugWindow) will show a message with a hook error. Such cases are very rare, but if you are not lucky, then you should change the hook method to AS.
| AS | PATCH |
Guarantee that the function will be 100% hooked | Yes | No, if the call address is passed implicitly |
Guarantee that such a hook will get along well with other extenders and patches | No | Yes |
Hook scopes | The whole process | Module Gothic.exe |
With the SDK update, 2 hook variations appeared: HOOK + AS_IF and HOOK + PATCH_IF. The third argument in this construct can serve as a condition that determines whether hook will occur. If the third argument is False, then a delayed hook can be performed by calling the Commit method on an inactive object.