Download: https://github.com/KirillMysnik/sp-controlled-cvars-package/releases/latest
What's this?
Sometimes I feel kind of uncomfortable with reading config cvars values right from the ConVar instances in my code.
Because imagine if you store some RGB value or another list in the cvar. You'll probably do something like this
Syntax: Select all
r, g, b = map(int, cvar.get_string().split(','))
But then you should also be ready to handle invalid values:
Syntax: Select all
try:
r, g, b = map(int, cvar.get_string().split(','))
except (TypeError, ValueError):
r, g, b = 0, 0, 0
Or, instead of setting r, g, b to zeros, you could've reverted them to their previous (valid) values - if any. That's even more code.
Now what if you require this list every tick? All these checks, try-excepts, resetting to default values would happen every tick.
While you could've just saved valid cvar value once it's changed in some variable:
Syntax: Select all
@Event('server_cvar')
def on_server_cvar(game_event):
if game_event['cvarname'] != cvar.name:
return
global rgb
try:
rgb = list(map(int, cvar.get_string().split(',')))
except (TypeError, ValueError):
rgb = 0, 0, 0
And then you're free from doing all these unnecessary checks every tick.
But what if you have tens of such variables? Longer server_cvar handler with more global variables? Or you could go with a dictionary - still I'm not happy.
So I made this ControlledCvars package which basically includes one class - ControlledConfigManager.
Usage
Syntax: Select all
from commands.server import ServerCommand
from core import echo_console
from controlled_cvars import ControlledConfigManager, InvalidValue
config_manager = ControlledConfigManager("cvars_test", cvar_prefix='test_')
def test_cvar_handler(cvar):
try:
r, g, b = map(int, cvar.get_string().split(','))
return r, g, b
except (TypeError, ValueError):
raise InvalidValue
test_cvar = config_manager.controlled_cvar(test_cvar_handler, "my_cvar", default="255,255,255")
config_manager.write()
config_manager.execute()
@ServerCommand('print_my_cvar')
def srv_print_my_cvar(command):
echo_console("Saved value: {}".format(config_manager["my_cvar"]))
Code: Select all
] print_my_cvar
Saved value: (255, 255, 255)
] test_my_cvar 150,150,150
Variable was updated successfully
] print_my_cvar
Saved value: (150, 150, 150)
] test_my_cvar some_invalid_value
Failed to update variable with the given value
] print_my_cvar
Saved value: (150, 150, 150)
You create a cvar with a handler that:
1. Returns value you'd want to be associated with your variable
2. Raises InvalidValue if new value doesn't fit your requirements
And then just require that value this way
Syntax: Select all
config_manager["my_cvar"]
This package also includes some pre-defined cvar handlers that may be useful
You can find them in controlled_cvars.handlers module.
bool_handler
Tries to convert variable to an integer, then to boolean.
Fails if it's impossible to convert variable to an integer.
Valid cvar example: my_boolean_cvar 1
color_handler
Tries to convert a comma-separated triple (or quadruple) variable to an Color object.
Fails if:
1. Too many integers in a list
2. Impossible to convert one of the list items to an integer
3. List integers overflow valid color component value (e.g. > 255 or < 0).
Valid cvar example: my_color_cvar 255,0,0
float_handler
Tries to convert variable to a float value.
Fails if it's impossible to do so.
Valid cvar example: my_float_cvar 42.0
int_handler
Tries to convert variable to an integer value.
Fails if it's impossible to do so.
Valid cvar example: my_integer_cvar 7
list_handler
Converts a comma-separated variable to a list object.
Does not fail.
Valid cvar example: my_list_cvar item1,item2,item3
sound_handler
Converts a path variable to a Sound object.
Does not fail.
Valid cvar example: my_sound_cvar player/suit_sprint.wav
sound_nullable_handler
Converts variable to None if it contains empty string, otherwise to Sound object.
Does not fail.
Valid cvar example: my_sound_nullable_cvar ""
string_handler
Converts variable to a string.
Does not fail.
Valid cvar example: my_string_cvar Hello, world
Hope somebody finds this useful.