Skip to content

Added the possibility to access pre-registers in a post-hook #384

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

Merged
merged 6 commits into from
Feb 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added context manager to temporarily set use_pre_registers
  • Loading branch information
Ayuto committed Feb 1, 2021
commit eef7b840e343411f249e5c4f68650c6920fd8cde
53 changes: 52 additions & 1 deletion addons/source-python/packages/source-python/memory/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
'PreHook',
'set_hooks_disabled',
'get_hooks_disabled',
'hooks_disabled'
'hooks_disabled',
'use_pre_registers',
)


Expand Down Expand Up @@ -86,6 +87,56 @@ class PostHook(_Hook):
# =============================================================================
# >> FUNCTIONS
# =============================================================================
@contextmanager
def use_pre_registers(stack_data, value=True):
"""Temporarily set ``StackData.use_pre_registers`` to the given value.
When the context ends, the previous value is restored.

Some functions overwrite CPU registers during their execution with values
from their internal calculations. In a post-hook, you have access to the
modified CPU registers, but in some cases you might want to access the
registers that were saved before the pre-hook was called. In that case you
can use this context manager to get access to the previous state of the
registers. On Windows this is often required when hooking THISCALL
functions, because the this-pointer is saved in the CPU register ``ECX``,
but gets overwritten during the execution of the hooked function. So, in a
post-hook you won't have access to the this-pointer anymore.

Example (CS:S/Windows):

.. code:: python

from entities.hooks import EntityCondition
from entities.hooks import EntityPostHook
from entities.hooks import EntityPreHook

from memory.hooks import use_pre_registers

@EntityPreHook(EntityCondition.is_player, 'drop_weapon')
def pre_on_drop_weapon(stack_data):
print(f'PRE: this = {stack_data[0].address}')

@EntityPostHook(EntityCondition.is_player, 'drop_weapon')
def post_on_drop_weapon(stack_data, ret_val):
print(f'POST FALSE: this = {stack_data[0].address}')
with use_pre_registers(stack_data):
print(f'POST CORRECT: this = {stack_data[0].address}')

Output:

.. code::

PRE: this = 546778280
POST FALSE: this = 16439007
POST CORRECT: this = 546778280
"""
old = stack_data.use_pre_registers
stack_data.use_pre_registers = value
try:
yield
finally:
stack_data.use_pre_registers = old

@contextmanager
def hooks_disabled(disabled=True):
"""Temporarily disable or enable all hook callbacks. By default hooks are
Expand Down