Fully support tune params

This commit is contained in:
ChillerDragon 2023-04-02 10:39:52 +02:00
parent da6d0cfc40
commit 029cebcb25
4 changed files with 289 additions and 139 deletions

View file

@ -154,9 +154,9 @@ def generate_msg(msg: NetMessageJson, game: str) -> None:
elif member['type']['kind'] == 'boolean':
ftype = 'bool'
default = 'False'
elif member['type']['kind'] == 'tune_param': # TODO: think about tune params
ftype = 'int'
default = '0'
elif member['type']['kind'] == 'tune_param':
ftype = 'float'
default = '0.0'
elif member['type']['kind'] == 'snapshot_object':
# TODO: think about snapshot_object
ftype = 'int'
@ -196,8 +196,8 @@ def generate_msg(msg: NetMessageJson, game: str) -> None:
ftype = 'int'
elif member['type']['kind'] == 'boolean':
ftype = 'bool'
elif member['type']['kind'] == 'tune_param': # TODO: think about tune params
ftype = 'int'
elif member['type']['kind'] == 'tune_param':
ftype = 'float'
elif member['type']['kind'] == 'snapshot_object':
# TODO: think about snapshot_object
ftype = 'int'
@ -232,8 +232,8 @@ def generate_msg(msg: NetMessageJson, game: str) -> None:
unpacker = 'int()'
elif member['type']['kind'] == 'boolean':
unpacker = 'int() == 1'
elif member['type']['kind'] == 'tune_param': # TODO: think about tune params
unpacker = 'int() # TODO: this is a tune param'
elif member['type']['kind'] == 'tune_param':
unpacker = 'int() / 100.0'
elif member['type']['kind'] == 'snapshot_object':
# TODO: think about snapshot_object
unpacker = 'int() # TODO: this is a snapshot object'
@ -267,7 +267,7 @@ def get_dependencies(msg: NetMessageJson) -> str:
packer_deps.append('pack_int')
elif member['type']['kind'] == 'boolean':
packer_deps.append('pack_int')
elif member['type']['kind'] == 'tune_param': # TODO: think about tune params
elif member['type']['kind'] == 'tune_param':
packer_deps.append('pack_int')
elif member['type']['kind'] == 'snapshot_object':
# TODO: think about snapshot_object
@ -287,6 +287,7 @@ def get_dependencies(msg: NetMessageJson) -> str:
def pack_field(member: NetMessageMemberJson) -> str:
name: str = name_to_snake(member["name"])
field: str = f'self.{name}'
packer = 'int'
if member['type']['kind'] == 'string': # TODO: sanitize cc
packer = 'str'
@ -300,8 +301,9 @@ def pack_field(member: NetMessageMemberJson) -> str:
packer = 'int'
elif member['type']['kind'] == 'boolean':
packer = 'int'
elif member['type']['kind'] == 'tune_param': # TODO: think about tune params
elif member['type']['kind'] == 'tune_param':
packer = 'int'
field = f'int({field} * 100.0)'
elif member['type']['kind'] == 'snapshot_object':
# TODO: think about snapshot_object
packer = 'int'
@ -313,7 +315,7 @@ def pack_field(member: NetMessageMemberJson) -> str:
packer = 'int'
else:
raise ValueError(f"Error: unknown type {member['type']}")
return f'pack_{packer}(self.{name})'
return f'pack_{packer}({field})'
def gen_pack_return(msg: NetMessageJson) -> str:
members: list[NetMessageMemberJson] = msg['members']

View file

@ -0,0 +1,117 @@
from twnet_parser.messages7.game.sv_tune_params import MsgSvTuneParams
def test_tune_repack_custom_gravity() -> None:
# chunk header: b'A\x86\x82\x0c'
data = \
b'\xa8\x0f\x88\x032\xa8\x14\xb0\x12\xb4\x07\x96\x02' \
b'\x9f\x01\xb0\xd1\x04\x80}\xac\x04\x9c\x17\x90\x1f\x98\xdb\x06' \
b'\x80\xb5\x18\x8c\x02\xbd\x01\xa0\xed\x1a\x88\x03\xbd\x01\xb8' \
b'\xc8!\x90\x01\x14\xbc\n\xa0\x9a\x0c\x88\x03\x80\xe2\t' \
b'\x98\xea\x01\xa4\x01\x00\xa4\x01\xa4\x01'
tune = MsgSvTuneParams()
tune.unpack(data)
assert tune.message_name == 'sv_tune_params'
assert tune.ground_control_speed == 10
assert tune.ground_control_accel == 2
assert tune.ground_friction == 0.5
assert tune.ground_jump_impulse == 13.2
assert tune.air_jump_impulse == 12
assert tune.air_control_speed == 5
assert tune.air_control_accel == 1.5
assert tune.air_friction == 0.95
assert tune.hook_length == 380
assert tune.hook_fire_speed == 80
assert tune.hook_drag_accel == 3
assert tune.hook_drag_speed == 15
assert tune.gravity == 20
assert tune.velramp_start == 550
assert tune.velramp_range == 2000
assert tune.velramp_curvature == 1.4
assert tune.gun_curvature == 1.25
assert tune.gun_speed == 2200
assert tune.gun_lifetime == 2
assert tune.shotgun_curvature == 1.25
assert tune.shotgun_speed == 2750
assert tune.shotgun_speeddiff == 0.8
assert tune.shotgun_lifetime == 0.2
assert tune.grenade_curvature == 7
assert tune.grenade_speed == 1000
assert tune.grenade_lifetime == 2
assert tune.laser_reach == 800
assert tune.laser_bounce_delay == 150
assert tune.laser_bounce_num == 1
assert tune.laser_bounce_cost == 0
assert tune.player_collision == 1
assert tune.player_hooking == 1
repack = tune.pack()
assert repack == data
def test_change_values() -> None:
tune = MsgSvTuneParams()
tune.ground_control_speed = 400
tune.gravity = 0.25
data = tune.pack()
new_tune = MsgSvTuneParams()
new_tune.unpack(data)
assert new_tune.ground_control_speed == 400
assert new_tune.gravity == 0.25
def test_tune_reset() -> None:
# chunk header: b'\x41\x85\xbc\x0c'
data = \
b'\xa8\x0f\x88\x03\x32\xa8\x14' \
b'\xb0\x12\xb4\x07\x96\x02\x9f' \
b'\x01\xb0\xd1\x04\x80\x7d\xac' \
b'\x04\x9c\x17\x32\x98\xdb\x06' \
b'\x80\xb5\x18\x8c\x02\xbd\x01' \
b'\xa0\xed\x1a\x88\x03\xbd\x01' \
b'\xb8\xc8\x21\x90\x01\x14\xbc' \
b'\x0a\xa0\x9a\x0c\x88\x03\x80' \
b'\xe2\x09\x98\xea\x01\xa4\x01' \
b'\x00\xa4\x01\xa4\x01'
tune = MsgSvTuneParams()
tune.unpack(data)
assert tune.message_name == 'sv_tune_params'
assert tune.ground_control_speed == 10
assert tune.ground_control_accel == 2
assert tune.ground_friction == 0.5
assert tune.ground_jump_impulse == 13.2
assert tune.air_jump_impulse == 12
assert tune.air_control_speed == 5
assert tune.air_control_accel == 1.5
assert tune.air_friction == 0.95
assert tune.hook_length == 380
assert tune.hook_fire_speed == 80
assert tune.hook_drag_accel == 3
assert tune.hook_drag_speed == 15
assert tune.gravity == 0.5
assert tune.velramp_start == 550
assert tune.velramp_range == 2000
assert tune.velramp_curvature == 1.4
assert tune.gun_curvature == 1.25
assert tune.gun_speed == 2200
assert tune.gun_lifetime == 2
assert tune.shotgun_curvature == 1.25
assert tune.shotgun_speed == 2750
assert tune.shotgun_speeddiff == 0.8
assert tune.shotgun_lifetime == 0.2
assert tune.grenade_curvature == 7
assert tune.grenade_speed == 1000
assert tune.grenade_lifetime == 2
assert tune.laser_reach == 800
assert tune.laser_bounce_delay == 150
assert tune.laser_bounce_num == 1
assert tune.laser_bounce_cost == 0
assert tune.player_collision == 1
assert tune.player_hooking == 1
repack = tune.pack()
assert repack == data

View file

@ -203,7 +203,38 @@ def test_parse_7_real_tune_params_rcon_line_input_timing_snap_empty() -> None:
snap = packet.messages[3]
assert tune.message_name == 'sv_tune_params'
# assert tune.ground_control_speed == 10
assert tune.ground_control_speed == 10
assert tune.ground_control_accel == 2
assert tune.ground_friction == 0.5
assert tune.ground_jump_impulse == 13.2
assert tune.air_jump_impulse == 12
assert tune.air_control_speed == 5
assert tune.air_control_accel == 1.5
assert tune.air_friction == 0.95
assert tune.hook_length == 380
assert tune.hook_fire_speed == 80
assert tune.hook_drag_accel == 3
assert tune.hook_drag_speed == 15
assert tune.gravity == 20
assert tune.velramp_start == 550
assert tune.velramp_range == 2000
assert tune.velramp_curvature == 1.4
assert tune.gun_curvature == 1.25
assert tune.gun_speed == 2200
assert tune.gun_lifetime == 2
assert tune.shotgun_curvature == 1.25
assert tune.shotgun_speed == 2750
assert tune.shotgun_speeddiff == 0.8
assert tune.shotgun_lifetime == 0.2
assert tune.grenade_curvature == 7
assert tune.grenade_speed == 1000
assert tune.grenade_lifetime == 2
assert tune.laser_reach == 800
assert tune.laser_bounce_delay == 150
assert tune.laser_bounce_num == 1
assert tune.laser_bounce_cost == 0
assert tune.player_collision == 1
assert tune.player_hooking == 1
assert rcon.message_name == 'rcon_line'
assert rcon.line == '[08:52:58][tuning]: gravity changed to 20.00'

View file

@ -8,145 +8,145 @@ from twnet_parser.packer import pack_int
class MsgSvTuneParams(PrettyPrint):
def __init__(
self,
ground_control_speed: int = 0,
ground_control_accel: int = 0,
ground_friction: int = 0,
ground_jump_impulse: int = 0,
air_jump_impulse: int = 0,
air_control_speed: int = 0,
air_control_accel: int = 0,
air_friction: int = 0,
hook_length: int = 0,
hook_fire_speed: int = 0,
hook_drag_accel: int = 0,
hook_drag_speed: int = 0,
gravity: int = 0,
velramp_start: int = 0,
velramp_range: int = 0,
velramp_curvature: int = 0,
gun_curvature: int = 0,
gun_speed: int = 0,
gun_lifetime: int = 0,
shotgun_curvature: int = 0,
shotgun_speed: int = 0,
shotgun_speeddiff: int = 0,
shotgun_lifetime: int = 0,
grenade_curvature: int = 0,
grenade_speed: int = 0,
grenade_lifetime: int = 0,
laser_reach: int = 0,
laser_bounce_delay: int = 0,
laser_bounce_num: int = 0,
laser_bounce_cost: int = 0,
player_collision: int = 0,
player_hooking: int = 0
ground_control_speed: float = 0.0,
ground_control_accel: float = 0.0,
ground_friction: float = 0.0,
ground_jump_impulse: float = 0.0,
air_jump_impulse: float = 0.0,
air_control_speed: float = 0.0,
air_control_accel: float = 0.0,
air_friction: float = 0.0,
hook_length: float = 0.0,
hook_fire_speed: float = 0.0,
hook_drag_accel: float = 0.0,
hook_drag_speed: float = 0.0,
gravity: float = 0.0,
velramp_start: float = 0.0,
velramp_range: float = 0.0,
velramp_curvature: float = 0.0,
gun_curvature: float = 0.0,
gun_speed: float = 0.0,
gun_lifetime: float = 0.0,
shotgun_curvature: float = 0.0,
shotgun_speed: float = 0.0,
shotgun_speeddiff: float = 0.0,
shotgun_lifetime: float = 0.0,
grenade_curvature: float = 0.0,
grenade_speed: float = 0.0,
grenade_lifetime: float = 0.0,
laser_reach: float = 0.0,
laser_bounce_delay: float = 0.0,
laser_bounce_num: float = 0.0,
laser_bounce_cost: float = 0.0,
player_collision: float = 0.0,
player_hooking: float = 0.0
) -> None:
self.message_name = 'sv_tune_params'
self.system_message = False
self.header: ChunkHeader
self.ground_control_speed: int = ground_control_speed
self.ground_control_accel: int = ground_control_accel
self.ground_friction: int = ground_friction
self.ground_jump_impulse: int = ground_jump_impulse
self.air_jump_impulse: int = air_jump_impulse
self.air_control_speed: int = air_control_speed
self.air_control_accel: int = air_control_accel
self.air_friction: int = air_friction
self.hook_length: int = hook_length
self.hook_fire_speed: int = hook_fire_speed
self.hook_drag_accel: int = hook_drag_accel
self.hook_drag_speed: int = hook_drag_speed
self.gravity: int = gravity
self.velramp_start: int = velramp_start
self.velramp_range: int = velramp_range
self.velramp_curvature: int = velramp_curvature
self.gun_curvature: int = gun_curvature
self.gun_speed: int = gun_speed
self.gun_lifetime: int = gun_lifetime
self.shotgun_curvature: int = shotgun_curvature
self.shotgun_speed: int = shotgun_speed
self.shotgun_speeddiff: int = shotgun_speeddiff
self.shotgun_lifetime: int = shotgun_lifetime
self.grenade_curvature: int = grenade_curvature
self.grenade_speed: int = grenade_speed
self.grenade_lifetime: int = grenade_lifetime
self.laser_reach: int = laser_reach
self.laser_bounce_delay: int = laser_bounce_delay
self.laser_bounce_num: int = laser_bounce_num
self.laser_bounce_cost: int = laser_bounce_cost
self.player_collision: int = player_collision
self.player_hooking: int = player_hooking
self.ground_control_speed: float = ground_control_speed
self.ground_control_accel: float = ground_control_accel
self.ground_friction: float = ground_friction
self.ground_jump_impulse: float = ground_jump_impulse
self.air_jump_impulse: float = air_jump_impulse
self.air_control_speed: float = air_control_speed
self.air_control_accel: float = air_control_accel
self.air_friction: float = air_friction
self.hook_length: float = hook_length
self.hook_fire_speed: float = hook_fire_speed
self.hook_drag_accel: float = hook_drag_accel
self.hook_drag_speed: float = hook_drag_speed
self.gravity: float = gravity
self.velramp_start: float = velramp_start
self.velramp_range: float = velramp_range
self.velramp_curvature: float = velramp_curvature
self.gun_curvature: float = gun_curvature
self.gun_speed: float = gun_speed
self.gun_lifetime: float = gun_lifetime
self.shotgun_curvature: float = shotgun_curvature
self.shotgun_speed: float = shotgun_speed
self.shotgun_speeddiff: float = shotgun_speeddiff
self.shotgun_lifetime: float = shotgun_lifetime
self.grenade_curvature: float = grenade_curvature
self.grenade_speed: float = grenade_speed
self.grenade_lifetime: float = grenade_lifetime
self.laser_reach: float = laser_reach
self.laser_bounce_delay: float = laser_bounce_delay
self.laser_bounce_num: float = laser_bounce_num
self.laser_bounce_cost: float = laser_bounce_cost
self.player_collision: float = player_collision
self.player_hooking: float = player_hooking
# first byte of data
# has to be the first byte of the message payload
# NOT the chunk header and NOT the message id
def unpack(self, data: bytes) -> bool:
unpacker = Unpacker(data)
self.ground_control_speed = unpacker.get_int() # TODO: this is a tune param
self.ground_control_accel = unpacker.get_int() # TODO: this is a tune param
self.ground_friction = unpacker.get_int() # TODO: this is a tune param
self.ground_jump_impulse = unpacker.get_int() # TODO: this is a tune param
self.air_jump_impulse = unpacker.get_int() # TODO: this is a tune param
self.air_control_speed = unpacker.get_int() # TODO: this is a tune param
self.air_control_accel = unpacker.get_int() # TODO: this is a tune param
self.air_friction = unpacker.get_int() # TODO: this is a tune param
self.hook_length = unpacker.get_int() # TODO: this is a tune param
self.hook_fire_speed = unpacker.get_int() # TODO: this is a tune param
self.hook_drag_accel = unpacker.get_int() # TODO: this is a tune param
self.hook_drag_speed = unpacker.get_int() # TODO: this is a tune param
self.gravity = unpacker.get_int() # TODO: this is a tune param
self.velramp_start = unpacker.get_int() # TODO: this is a tune param
self.velramp_range = unpacker.get_int() # TODO: this is a tune param
self.velramp_curvature = unpacker.get_int() # TODO: this is a tune param
self.gun_curvature = unpacker.get_int() # TODO: this is a tune param
self.gun_speed = unpacker.get_int() # TODO: this is a tune param
self.gun_lifetime = unpacker.get_int() # TODO: this is a tune param
self.shotgun_curvature = unpacker.get_int() # TODO: this is a tune param
self.shotgun_speed = unpacker.get_int() # TODO: this is a tune param
self.shotgun_speeddiff = unpacker.get_int() # TODO: this is a tune param
self.shotgun_lifetime = unpacker.get_int() # TODO: this is a tune param
self.grenade_curvature = unpacker.get_int() # TODO: this is a tune param
self.grenade_speed = unpacker.get_int() # TODO: this is a tune param
self.grenade_lifetime = unpacker.get_int() # TODO: this is a tune param
self.laser_reach = unpacker.get_int() # TODO: this is a tune param
self.laser_bounce_delay = unpacker.get_int() # TODO: this is a tune param
self.laser_bounce_num = unpacker.get_int() # TODO: this is a tune param
self.laser_bounce_cost = unpacker.get_int() # TODO: this is a tune param
self.player_collision = unpacker.get_int() # TODO: this is a tune param
self.player_hooking = unpacker.get_int() # TODO: this is a tune param
self.ground_control_speed = unpacker.get_int() / 100.0
self.ground_control_accel = unpacker.get_int() / 100.0
self.ground_friction = unpacker.get_int() / 100.0
self.ground_jump_impulse = unpacker.get_int() / 100.0
self.air_jump_impulse = unpacker.get_int() / 100.0
self.air_control_speed = unpacker.get_int() / 100.0
self.air_control_accel = unpacker.get_int() / 100.0
self.air_friction = unpacker.get_int() / 100.0
self.hook_length = unpacker.get_int() / 100.0
self.hook_fire_speed = unpacker.get_int() / 100.0
self.hook_drag_accel = unpacker.get_int() / 100.0
self.hook_drag_speed = unpacker.get_int() / 100.0
self.gravity = unpacker.get_int() / 100.0
self.velramp_start = unpacker.get_int() / 100.0
self.velramp_range = unpacker.get_int() / 100.0
self.velramp_curvature = unpacker.get_int() / 100.0
self.gun_curvature = unpacker.get_int() / 100.0
self.gun_speed = unpacker.get_int() / 100.0
self.gun_lifetime = unpacker.get_int() / 100.0
self.shotgun_curvature = unpacker.get_int() / 100.0
self.shotgun_speed = unpacker.get_int() / 100.0
self.shotgun_speeddiff = unpacker.get_int() / 100.0
self.shotgun_lifetime = unpacker.get_int() / 100.0
self.grenade_curvature = unpacker.get_int() / 100.0
self.grenade_speed = unpacker.get_int() / 100.0
self.grenade_lifetime = unpacker.get_int() / 100.0
self.laser_reach = unpacker.get_int() / 100.0
self.laser_bounce_delay = unpacker.get_int() / 100.0
self.laser_bounce_num = unpacker.get_int() / 100.0
self.laser_bounce_cost = unpacker.get_int() / 100.0
self.player_collision = unpacker.get_int() / 100.0
self.player_hooking = unpacker.get_int() / 100.0
return True
def pack(self) -> bytes:
return pack_int(self.ground_control_speed) + \
pack_int(self.ground_control_accel) + \
pack_int(self.ground_friction) + \
pack_int(self.ground_jump_impulse) + \
pack_int(self.air_jump_impulse) + \
pack_int(self.air_control_speed) + \
pack_int(self.air_control_accel) + \
pack_int(self.air_friction) + \
pack_int(self.hook_length) + \
pack_int(self.hook_fire_speed) + \
pack_int(self.hook_drag_accel) + \
pack_int(self.hook_drag_speed) + \
pack_int(self.gravity) + \
pack_int(self.velramp_start) + \
pack_int(self.velramp_range) + \
pack_int(self.velramp_curvature) + \
pack_int(self.gun_curvature) + \
pack_int(self.gun_speed) + \
pack_int(self.gun_lifetime) + \
pack_int(self.shotgun_curvature) + \
pack_int(self.shotgun_speed) + \
pack_int(self.shotgun_speeddiff) + \
pack_int(self.shotgun_lifetime) + \
pack_int(self.grenade_curvature) + \
pack_int(self.grenade_speed) + \
pack_int(self.grenade_lifetime) + \
pack_int(self.laser_reach) + \
pack_int(self.laser_bounce_delay) + \
pack_int(self.laser_bounce_num) + \
pack_int(self.laser_bounce_cost) + \
pack_int(self.player_collision) + \
pack_int(self.player_hooking)
return pack_int(int(self.ground_control_speed * 100.0)) + \
pack_int(int(self.ground_control_accel * 100.0)) + \
pack_int(int(self.ground_friction * 100.0)) + \
pack_int(int(self.ground_jump_impulse * 100.0)) + \
pack_int(int(self.air_jump_impulse * 100.0)) + \
pack_int(int(self.air_control_speed * 100.0)) + \
pack_int(int(self.air_control_accel * 100.0)) + \
pack_int(int(self.air_friction * 100.0)) + \
pack_int(int(self.hook_length * 100.0)) + \
pack_int(int(self.hook_fire_speed * 100.0)) + \
pack_int(int(self.hook_drag_accel * 100.0)) + \
pack_int(int(self.hook_drag_speed * 100.0)) + \
pack_int(int(self.gravity * 100.0)) + \
pack_int(int(self.velramp_start * 100.0)) + \
pack_int(int(self.velramp_range * 100.0)) + \
pack_int(int(self.velramp_curvature * 100.0)) + \
pack_int(int(self.gun_curvature * 100.0)) + \
pack_int(int(self.gun_speed * 100.0)) + \
pack_int(int(self.gun_lifetime * 100.0)) + \
pack_int(int(self.shotgun_curvature * 100.0)) + \
pack_int(int(self.shotgun_speed * 100.0)) + \
pack_int(int(self.shotgun_speeddiff * 100.0)) + \
pack_int(int(self.shotgun_lifetime * 100.0)) + \
pack_int(int(self.grenade_curvature * 100.0)) + \
pack_int(int(self.grenade_speed * 100.0)) + \
pack_int(int(self.grenade_lifetime * 100.0)) + \
pack_int(int(self.laser_reach * 100.0)) + \
pack_int(int(self.laser_bounce_delay * 100.0)) + \
pack_int(int(self.laser_bounce_num * 100.0)) + \
pack_int(int(self.laser_bounce_cost * 100.0)) + \
pack_int(int(self.player_collision * 100.0)) + \
pack_int(int(self.player_hooking * 100.0))