Files

85 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Test adapter: run existing serializer fixtures through the loaded C ABI.
No child codec process, sockets or game-engine calls in this adapter.
"""
import ctypes as C
import os
import shutil
import stat
from pathlib import Path
import sys
ROOT=Path(__file__).resolve().parents[1]
MODES=['','--self-talents','--npcs','--npcs-types','--gameobjects','--creature-template',
'--unit-values','--unit-values-profile','--monster-move','--vendor-inventory',
'--gossip-menu','--melee-damage','--reputation-initial','--attack-error',
'--power-update','--features','--trait-values','--talent-commit','--spell-event',
'--gateway-spell-event','--gateway-monster-move','--glue-features','--native-auth-success','--native-client-initialization','--native-db-reply-missing','--inventory-objects','--native-spell-cast','--native-hotfix-reply','--sell-error','--buy-result','--trainer-list','--spell-learning','--periodic-aura-log','--guild-info','--aura-update','--unit-target','--npcs-combat','--loot-event','--spell-damage-log','--item-appearance','--corpse-event','--corpses','--guild-invite','--guild-command-result','--players','--player-values','--peer-movement','--party-update','--world-transfer','--native-exploration-experience','--native-explored-zone-values','--gameobject-values','--quest-packet','--quest-log-values',
'--social-packet','--contact-names','--companions','--companion-values','--companion-name',
'--profession-values','--party-invite','--random-roll','--guild-permissions','--persistent-ground','--spell-channel','--spell-feedback',
'--owned-stat-pet','--controlled-unit','--owned-stat-pet-values',
'--group-event','--trade-event','--who','--pvp-statistics','--self-party-type','--auction-buckets','--auction-items','--pet-stable','--auction-personal','--commodity-quote','--combo-target','--farsight','--forced-reactions']
BOUND=1048576
def codec_library_path():
return Path(os.environ.get('GW_TEST_CODEC_PATH', ROOT / 'build/forever-schema/libforever_codec.so'))
def optional_file_available(path):
path = Path(path)
try:
mode = path.stat().st_mode
except FileNotFoundError:
if path.is_symlink():
raise
return False
if not stat.S_ISREG(mode):
raise ValueError(f'Expected a regular fixture file: {path}')
return True
def require_optional_file(path):
if not optional_file_available(path):
print(f'Optional fixture input unavailable: {path}', file=sys.stderr)
raise SystemExit(77)
def require_optional_cli(command):
# Existing subprocess callers can supply an executable on PATH or a path.
if not os.path.dirname(command):
command = shutil.which(command, mode=os.F_OK) or command
require_optional_file(command)
def load():
library=C.CDLL(str(codec_library_path()))
library.gw_codec_abi.restype=C.c_uint32
function=library.gw_codec_serialize
function.argtypes=[C.c_uint32,C.c_void_p,C.c_uint32,C.c_uint32,C.c_void_p,C.c_uint32,C.POINTER(C.c_uint32)]
function.restype=C.c_int
assert library.gw_codec_abi()==1
return library
def serialize(library,mode,data,flags=0):
source=C.create_string_buffer(data)
output=C.create_string_buffer(BOUND);written=C.c_uint32()
result=library.gw_codec_serialize(mode,source,len(data),flags,output,BOUND,C.byref(written))
if result:raise ValueError('Bounded serializer rejected packet')
return output.raw[:written.value]
if __name__=='__main__':
if len(sys.argv) == 4 and sys.argv[1] == '--fixture':
if not sys.argv[2]:
print('Optional fixture codec is not configured', file=sys.stderr)
sys.exit(77)
library = Path(sys.argv[2]).absolute()
require_optional_file(library)
os.environ['GW_TEST_CODEC_PATH'] = str(library)
os.execv(sys.executable, [sys.executable, sys.argv[3], str(Path(__file__).resolve())])
try:
mode=MODES.index(sys.argv[1] if len(sys.argv)==2 else '')
if len(sys.argv)>2:raise ValueError()
data=sys.stdin.buffer.read(BOUND+1)
if len(data)>BOUND:raise ValueError()
sys.stdout.buffer.write(serialize(load(),mode,data,1 if 'GATEWAY_TALENT_EXPERIMENT' in os.environ else 0))
except (ValueError,AssertionError):sys.exit(1)