Ayuto

Dynamic server commands

Oct 31st, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # =============================================================================
  2. # >> IMPORTS
  3. # =============================================================================
  4. # Python
  5. import collections
  6. import os
  7. import shlex
  8. import sys
  9.  
  10. # binutils
  11. import binutils
  12.  
  13. from binutils import Convention
  14.  
  15.  
  16. # =============================================================================
  17. # >> API
  18. # =============================================================================
  19. def register_command(name, callback):
  20.     srv_cmds[name.lower()].add(callback)
  21.  
  22. def unregister_command(name, callback):
  23.     srv_cmds[name.lower()].discard(callback)
  24.  
  25.  
  26. # =============================================================================
  27. # >> GLOBAL VARIABLES
  28. # =============================================================================
  29. # This dict holds all callbacks for a server command
  30. srv_cmds = collections.defaultdict(set)
  31.  
  32. # Get the engine binary file
  33. engine = binutils.find_binary('bin/engine')
  34.  
  35. if os.name == 'nt':
  36.     IsCommand_ptr = engine.find_signature('\x55\x8B\xEC\x81\xEC\x08\x04\x2A\x2A\x8B\x45\x08')
  37. else:
  38.     IsCommand_ptr = engine['_ZN14CCvarUtilities9IsCommandERK8CCommand']
  39.  
  40. # CCvarUtilities::IsCommand(CCommand  const&)
  41. IsCommand = IsCommand_ptr.make_function(Convention.THISCALL, 'pp)b')
  42.  
  43.  
  44. # =============================================================================
  45. # >> LOAD & UNLOAD
  46. # =============================================================================
  47. def load():
  48.     IsCommand.add_pre_hook(is_command_hook)
  49.  
  50. def unload():
  51.     IsCommand.remove_pre_hook(is_command_hook)
  52.  
  53.  
  54. # =============================================================================
  55. # >> CALLBACKS
  56. # =============================================================================
  57. def is_command_hook(args):
  58.     arglist = shlex.split(args[1].get_string(8, False), ' ')
  59.     name = arglist[0].lower()
  60.     if name not in srv_cmds:
  61.         # Continue as usual
  62.         return
  63.        
  64.     for callback in srv_cmds[name]:
  65.         try:
  66.             callback(arglist[1:])
  67.         except:
  68.             sys.excepthook(*sys.exc_info())
  69.            
  70.     # Stop the message: "Unknown command: <cmd>"
  71.     return True
  72.    
  73.    
  74. # =============================================================================
  75. # >> TESTS
  76. # =============================================================================
  77. def my_cmd_callback(args):
  78.     print 'Args:', args
  79.    
  80. register_command('est_rocket', my_cmd_callback)
Advertisement
Add Comment
Please, Sign In to add comment