Description
This Python module package will contain some advanced functions to interact more with the Source server itself. For any ideas on what to add, please post in [url=http://forums.mattie.info/cs/forums/viewtopic.php?t=25426]the forum topic[/url].
Console commands available when loaded as a script (es_load srcds):
[list]
[*][b]srcds_ent[/b] {classname | entity_index}
[list]
[*]If called with a entity classname, displays list of attributes and methods supported for such entities
[*]If called with an entity_index, dumps all attributes of such entity to console
[/list]
[/list]
Currently usable modules are:
[b]entities[/b]
[list]
[*]This module contains high-level interface for accessing and manipulating all entities in Source engine
[*][b]get_entity[/b](entity_index)
[list]
[*]Returns an entity instance corresponding to the entity_index
[*]Raises srcds.SRCDSError if the entity does not exist (or has empty classname for any reason)
[/list]
[*][b]get_entities[/b](criteria...)
[list]
[*]Return a dictionary of form {entity_index: entity_instance, ...}
[*]Criteria can be used to limit which entities populate the dictionary, expressed in form attribute=value
[*]Wildcard * can be used in value for partial matches, for color and vector properties, a tuple can be used as value
[/list]
[*][b]<entity classname>[/b]()
[list]
[*]Create a new entity to the game with the specified classname
[*]To spawn the entity into the game world, once attributes are set up, call the instance's spawn() method
[/list]
[/list]
[i][b]EXAMPLES OF srcds.entities[/b][/i]
[syntax="python"]import es
import playerlib
import srcds.entities
# some examples of get_entities
# get all entities currently in game:
all_entities = srcds.entities.get_entities()
# get all entities with classname prop_physics
all_prop_physics = srcds.entities.get_entities('prop_physics')
# get all entities whose classname starts with prop_
all_props = srcds.entities.get_entities('prop_*')
# get all entities whose rendercolor is red
all_red_entities = srcds.entities.get_entities(rendercolor=(255,0,0))
# get all entities whose rendercolor is blue and that are precisely in the map origin
all_blue_entities_at_origin = srcds.entities.get_entities(rendercolor=(0,0,255), origin=(0,0,0))
# an example of getting an entity instance and doing something with it
random_user = es.getuserid()
player = playerlib.Player(random_user)
player_index = player.index
player_entity = srcds.entities.get_entity(player_index)
# fire Ignite input to set the player on fire
player_entity.Ignite()
# set targetname for next example
player_entity.targetname = 'random_%s' % random_user
# an example of creating an entity and using it
hurt = srcds.entities.point_hurt()
hurt.Damage = 100
hurt.DamageType = 'FALL'
hurt.DamageTarget = player_entity.targetname
hurt.spawn() # come forth, mighty damaging entity!
hurt.Hurt() # OUCH!
hurt.Kill() # remove from game now that we used it
[/syntax]
[b]console[/b]
[list]
[*]This module contains functions for directly interacting with the server console.
[*][color=red]NOTE: Console functions work on Linux servers only![/color]
[*][color=red]NOTE 2: Console functions do not work correctly on Linux servers even, do not use them![/color]
[*][b]dbgmsg[/b](text)
[list]
[*]Prints selected text on stderr stream. This will make the text appear in the console even if output capture was active.
[*][i]text[/i] - the text you want to print, a new line character is appended by the function
[/list]
[*][b]get_concmd_output[/b](command, callback, args=(), kw={}, bufsize=1024)
[list]
[*]Executes a console command and captures whatever it outputs to the server console and calls the callback function with the data. The captured output will not visible in the server console.
[*][i]command[/i] - the command string to be executed using es.ServerCommand (equal to es.server.queuecmd)
[*][i]callback[/i] - a Python callable object that should accept at least one parameter, the captured text string
[*][i]args[/i] - optional parameter containing extra parameters that should be used with the callback function in a tuple
[*][i]kw[/i] - optional parameter containing the keyword parameters that should be used with the callback function in a dict
[*][i]bufsize[/i] - optional parameter specifying the maximum length of output to be saved from the console (no, there is no 'unlimited' option)
[/list]
[*][b]get_console_output[/b](function, args=(), kw={}, bufsize=1024)
[list]
[*]Calls a Python function with given parameters, captures any console output the function creates and returns a tuple of type (return_value, captured_output) where [i]return_value[/i] is whatever the Python function returned and [i]captured_output[/i] is the text that would had appeared on the server console during executing the function.
[*][i]function[/i] - the Python callable object to execute
[*][i]args[/i] - optional parameter being a tuple of parameters for the function
[*][i]kw[/i] - optional parameter being a dictionary of keyword arguments for the function
[*][i]bufsize[/i] - optional parameter specifying the maximum length of output to be saved from the console (no, there is still no 'unlimited' option)
[/list]
[/list]
In other words, currently this has any value only on Linux servers. Valve has made the Windows servers not use standard ways to input and output data in the console, so it is not possible to directly manipulate them without hacking the function in the engine level, which in turn is not possible with EventScripts.
[i][b]EXAMPLES[/b][/i]
A simple example demonstrating how to capture the output of a Python function and that the text is not visible in console at all:
[syntax="python"]import es
import srcds.console # IMPORT THE console MODULE FROM srcds PACKAGE!
# print some message
es.dbgmsg(0, 'First message!')
# capture the output of the next message:
retval, output = srcds.console.get_console_output(es.dbgmsg, (0, 'Second message!'))
# print another message
es.dbgmsg(0, 'Third message!')
# now print what was captured before
es.dbgmsg(0, 'es.dbgmsg returned %s and the text it put was: "%s"'%(
retval,
output[:-1],
)
)
'''
Note that since es.dbgmsg prints a line feed,
that line feed is captured also,
so we are removing it in this example using slice operator [:-1]
'''[/syntax]
The above example will output this on a Linux server:
[quote]First message!
Third message!
es.dbgmsg returned None and the text it put was: "Second message!"[/quote]
Another example that makes some real use of console output capture feature, it captures output of the console command "status" and parses it.
[syntax="python"]import es
import srcds.console # IMPORT THE console MODULE FROM srcds PACKAGE!
'''
Some script could call get_server_info from this module
and then receive the server info as a dictionary to its callback function
'''
def get_server_info(callback):
# capture the output of "status" and parse it in our callback:
srcds.console.get_concmd_output('status', status_callback, callback)
# Note that we are passing the callback function as extra parameter here
def status_callback(text, callback):
# this callback is called when the output of "status" is saved
# the parameter callback is the extra parameter from previous function
# The layout of the output, as saved in the text parameter is like this:
'''
hostname: Counter-Strike: Source
version : 1.0.0.34/7 3224 secure
udp/ip : 192.168.11.150:27015
map : de_dust at: 0 x, 0 y, 0 z
players : 0 (32 max)
# userid name uniqueid connected ping loss state adr
'''
# Create a dictionary into which to save the info
info = {}
# Split the text into lines and parse them based on the info above
lines = text.split('\n')
info['hostname'] = lines[0].split(':', 1)[1].strip()
version_info = lines[1].split(':', 1)[1].strip().split(' ', 2)
info['version'] = version_info[0]
info['build'] = version_info[1]
info['secure'] = (version_info[2] == 'secure')
info['address'] = lines[2].split(':', 1)[1].strip()
info['map'] = lines[3].split(':', 1)[1].strip().split(' ', 1)[0]
player_info = lines[4].split(':', 1)[1].strip().split(' ', 2)
info['players'] = int(player_info[0])
info['maxplayers'] = int(player_info[1][1:])
# Now call the callback function with our parsed dict info
callback(info)
# test the script:
import pprint # pretty printer for nicer output of the dictionary
def print_it(info):
pprint.pprint(info)
get_server_info(print_it)
[/syntax]
With the test part in previous example this is the output in console:
[quote]{'address': '192.168.11.150:27015',
'build': '3224',
'hostname': 'Counter-Strike: Source',
'map': 'de_dust',
'maxplayers': 32,
'players': 0,
'secure': True,
'version': '1.0.0.34/7'}
[/quote]
Note: This has been tested only on Linux Source Dedicated Server for Counter-Strike: Source. It should work in Orange Box version of the dedicated server too, but I have not been able to test it on one yet.