Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deferred / automatic hooking #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

DanTheMan827
Copy link

@DanTheMan827 DanTheMan827 commented Jan 15, 2025

This adds convenience macros that mirror those in logging.hpp, but with the convenience to be installed on dlopen, or all at once with INSTALL_HOOKS(); at a user-defined location in the code.

  • Added the AutoHooks class to manage the installation of hooks.

    • Includes a private method getInstallFuncs() that returns a reference to a static vector of function pointers.
    • Includes public methods AddInstallFunc() and InstallHooks().
      • AddInstallFunc() adds an installation function to the vector.
      • InstallHooks() executes all installation functions and clears the vector.
  • Added the INSTALL_HOOKS macro.

    • Installs all hooks created with the MAKE_HOOK_AUTO macros.
  • Added MAKE_HOOK_AUTO macros.

    • Creates the desired hook and automatically registers it for later installation with the INSTALL_HOOKS macro.
  • Added the HOOK_AUTO_REGISTER macro.

    • Registers a hook for later installation with the INSTALL_HOOKS macro.
  • Added the HOOK_AUTO_REGISTER_ORIG macro.

    • Similar to HOOK_AUTO_REGISTER but uses INSTALL_HOOK_ORIG to install the hook.
  • Added the make-autohooks.ps1 PowerShell script to automate the generation of autohooks.hpp.

    • The script creates modified versions of the standard hooks that automatically register to AutoHooks.
  • Modified the build script to run make-autohooks.ps1 prior to building the project.

- Added the AutoHooks class to manage the installation of hooks.
  - Includes a private method getInstallFuncs() that returns a reference to a static vector of function pointers.
  - Includes public methods AddInstallFunc() and InstallHooks().
    - AddInstallFunc() adds an installation function to the vector.
    - InstallHooks() executes all installation functions and clears the vector.

- Added the INSTALL_HOOKS macro.
  - Installs all hooks created with the MAKE_HOOK_AUTO macros.

- Added MAKE_HOOK_AUTO macros.
  - Creates the desired hook and automatically registers it for later installation with the INSTALL_HOOKS macro.

- Added the _HOOK_AUTO_INSTALL macro for internal use.
  - Registers a hook for later installation with the INSTALL_HOOKS macro.

- Added the _HOOK_AUTO_INSTALL_ORIG macro for internal use.
  - Similar to _HOOK_AUTO_INSTALL but uses INSTALL_HOOK_ORIG to install the hook.

- Added the make-autohooks.ps1 PowerShell script to automate the generation of autohooks.hpp.
  - The script creates modified versions of the standard hooks that automatically register to AutoHooks.

- Modified the build script to run make-autohooks.ps1 prior to building the project.
@DanTheMan827
Copy link
Author

DanTheMan827 commented Jan 15, 2025

These were suggested as an alternative which would be placed after hook creation.

#define INSTALL_HOOK_ON_DLOPEN(logger_, name_) \
    __attribute((constructor)) void Hook_##name_##_Install() { \
        INSTALL_HOOK(logger_, name_); \
    }

#define INSTALL_HOOK_DIRECT_ON_DLOPEN(logger_, name_, addr_) \
    __attribute((constructor)) void Hook_Direct_##name_##_Install() { \
        INSTALL_HOOK_DIRECT(logger_, name_, addr_); \
    }

#define INSTALL_HOOK_ORIG_ON_DLOPEN(logger_, name_) \
    __attribute((constructor)) void Hook_Orig_##name_##_Install() { \
        INSTALL_HOOK_ORIG(logger_, name_); \
    }

These would provide a way to automatically install hooks at library load, however these wouldn't allow the auto hooks to be installed at a specific point in the setup() or late_load() methods if any additional setup needs to be done. The hook in that case could potentially be executed before other needed setup is done depending on what is being hooked and the execution of the game code.

If someone requires precise installation of hooks, these macros are all optional after all and primarily serve as a convenience as opposed to creating the hooks and installing them all individually, usually inside the late_load() method from what I've seen.

autohooks.hpp consists entirely of convenience macros generated from those found in hooking.hpp that accomplish roughly the same as the following, but with use of a single macro.

// Create and register a hook to be installed later with `INSTALL_HOOKS();`
MAKE_HOOK_MATCH(MyHook, ...);
HOOK_AUTO_REGISTER(MyHook);

// These can be replaced with a single use of the new macro
MAKE_HOOK_AUTO_MATCH(MyHook, ...);

or for the dlopen variant

// Create and register a hook that will be immediately installed when the library is loaded.
MAKE_HOOK_MATCH(MyHook, ...);
INSTALL_HOOK_ON_DLOPEN(MyHook);

// These can be replaced with a single use of the new macro
MAKE_HOOK_DLOPEN_MATCH(MyHook, ...);

// Also available...
INSTALL_HOOK_ON_DLOPEN_ORIG(MyHook);
INSTALL_HOOK_ON_DLOPEN_DIRECT(MyHook, addr);

// And if you want to specify the logger used...
INSTALL_HOOK_ON_DLOPEN_LOGGER(myLogger, MyHook);
INSTALL_HOOK_ON_DLOPEN_ORIG_LOGGER(myLogger, MyHook);
INSTALL_HOOK_ON_DLOPEN_DIRECT_LOGGER(myLogger, MyHook, addr);

@DanTheMan827 DanTheMan827 marked this pull request as ready for review January 16, 2025 01:08
@DanTheMan827 DanTheMan827 changed the title Add automatic hooking Add deferred and automatic hooking Jan 16, 2025
@DanTheMan827 DanTheMan827 changed the title Add deferred and automatic hooking Add deferred / automatic hooking Jan 16, 2025
- Updated to accept input and output file paths as parameters.
- Improved error handling to check for the existence of the input file before processing.
- Trimmed the extracted macro text for cleaner output.
- Converted the file header text into a here-string for better readability and maintainability.
- Add comments and slighly refactor for readability.
- Output normalized line endings depending on OS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant