Advertisement
KirillMysnik

OnPlayerRunCommand fix

Dec 5th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. # ../listeners/_run_command.py
  2.  
  3. """Provides a run command listener."""
  4.  
  5. # =============================================================================
  6. # >> IMPORTS
  7. # =============================================================================
  8. # Source.Python Imports
  9. #   Core
  10. from core import SOURCE_ENGINE_BRANCH
  11. #   Entities
  12. from entities.hooks import EntityPreHook
  13. from entities.hooks import EntityCondition
  14. #   Listeners
  15. from listeners import on_player_run_command_listener_manager
  16. from listeners import on_button_state_changed_listener_manager
  17. #   Players
  18. from players import UserCmd
  19. from players.bots import BotCmd
  20. from players.entity import Player
  21. #   Memory
  22. from memory import make_object
  23. from memory import get_object_pointer
  24. from memory import get_size
  25.  
  26.  
  27. # =============================================================================
  28. # >> CONSTANTS
  29. # =============================================================================
  30. BOT_CMD_SIZE = get_size(BotCmd)
  31.  
  32.  
  33. # =============================================================================
  34. # >> CALLBACKS
  35. # =============================================================================
  36. @EntityPreHook(EntityCondition.is_player, 'run_command')
  37. def _pre_player_run_command(args):
  38.     if (not on_player_run_command_listener_manager
  39.             and not on_button_state_changed_listener_manager):
  40.         return
  41.  
  42.     player = make_object(Player, args[0])
  43.  
  44.     # https://github.com/Source-Python-Dev-Team/Source.Python/issues/149
  45.     use_rtti_fix = False
  46.  
  47.     if player.is_fake_client():
  48.         if SOURCE_ENGINE_BRANCH == 'tf2':
  49.             use_rtti_fix = True
  50.             user_cmd = BotCmd()
  51.             user_cmd_ptr = get_object_pointer(user_cmd)
  52.             args[1].copy(user_cmd_ptr, BOT_CMD_SIZE)
  53.         else:
  54.             user_cmd = make_object(BotCmd, args[1])
  55.  
  56.     else:
  57.         user_cmd = make_object(UserCmd, args[1])
  58.  
  59.     if on_player_run_command_listener_manager:
  60.         on_player_run_command_listener_manager.notify(player, user_cmd)
  61.  
  62.     if on_button_state_changed_listener_manager:
  63.         _handle_button_state_changed(player, player.buttons, user_cmd.buttons)
  64.  
  65.     if use_rtti_fix:
  66.         user_cmd_ptr.copy(args[1], BOT_CMD_SIZE)
  67.  
  68.  
  69. def _handle_button_state_changed(player, old_buttons, new_buttons):
  70.     if old_buttons == new_buttons:
  71.         return
  72.  
  73.     on_button_state_changed_listener_manager.notify(
  74.         player, old_buttons, new_buttons)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement