Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =============================================================================
- # >> IMPORTS
- # =============================================================================
- # Python
- import collections
- import os
- import shlex
- import sys
- # binutils
- import binutils
- from binutils import Convention
- # =============================================================================
- # >> API
- # =============================================================================
- def register_command(name, callback):
- srv_cmds[name.lower()].add(callback)
- def unregister_command(name, callback):
- srv_cmds[name.lower()].discard(callback)
- # =============================================================================
- # >> GLOBAL VARIABLES
- # =============================================================================
- # This dict holds all callbacks for a server command
- srv_cmds = collections.defaultdict(set)
- # Get the engine binary file
- engine = binutils.find_binary('bin/engine')
- if os.name == 'nt':
- IsCommand_ptr = engine.find_signature('\x55\x8B\xEC\x81\xEC\x08\x04\x2A\x2A\x8B\x45\x08')
- else:
- IsCommand_ptr = engine['_ZN14CCvarUtilities9IsCommandERK8CCommand']
- # CCvarUtilities::IsCommand(CCommand const&)
- IsCommand = IsCommand_ptr.make_function(Convention.THISCALL, 'pp)b')
- # =============================================================================
- # >> LOAD & UNLOAD
- # =============================================================================
- def load():
- IsCommand.add_pre_hook(is_command_hook)
- def unload():
- IsCommand.remove_pre_hook(is_command_hook)
- # =============================================================================
- # >> CALLBACKS
- # =============================================================================
- def is_command_hook(args):
- arglist = shlex.split(args[1].get_string(8, False), ' ')
- name = arglist[0].lower()
- if name not in srv_cmds:
- # Continue as usual
- return
- for callback in srv_cmds[name]:
- try:
- callback(arglist[1:])
- except:
- sys.excepthook(*sys.exc_info())
- # Stop the message: "Unknown command: <cmd>"
- return True
- # =============================================================================
- # >> TESTS
- # =============================================================================
- def my_cmd_callback(args):
- print 'Args:', args
- register_command('est_rocket', my_cmd_callback)
Advertisement
Add Comment
Please, Sign In to add comment