Files
WoW-Hybrid-gateway/tests/spell_damage_serializer_fixture.py

49 lines
4.1 KiB
Python

"""Source non-melee damage vs original70334writer; no game/account access."""
from pathlib import Path
import ctypes as C,os,re,struct,subprocess,sys,tempfile
from native_codec_driver import require_optional_file
ROOT=Path(__file__).resolve().parents[1]
# CMake supplies the codec explicitly; out-of-tree host builds are supported.
codec=Path(sys.argv[1])
default_protocol = sys.argv[2] if len(sys.argv) > 2 else ROOT / 'build/libgateway_protocol.a'
protocol = Path(os.environ.get('GW_TEST_PROTOCOL_LIBRARY', default_protocol))
require_optional_file(protocol)
require_optional_file(codec)
codec=codec.resolve()
protocol=protocol.resolve()
names=('CRIT_DEBUG','CRIT','HIT_DEBUG','SPLIT','VICTIM_IS_ATTACKER','ATTACK_TABLE_DEBUG')
for path in (Path.home()/'Servers/tortoise-wow/src/game/Spells/SpellDefines.h',
ROOT.parent/'servers/trinity-forever/src/server/game/Miscellaneous/SharedDefines.h'):
values={name:int(value,0)for name,value in re.findall(r'\bSPELL_HIT_TYPE_(\w+)\s*=\s*(0x[0-9A-Fa-f]+|[0-9]+)',path.read_text())}
assert {name:values[name]for name in names}==dict(zip(names,(1,2,4,8,16,32))), 'Named source/destination spell hit semantics changed'
with tempfile.TemporaryDirectory(dir=ROOT/'runtime',prefix='spell-damage-oracle-')as temp:
d=Path(temp);p=d/'direct.cpp'
p.write_text("""#include \"gateway/spell_damage.hpp\"
#include <algorithm>
#include <iostream>
int main(){std::string line;while(std::getline(std::cin,line)){try{auto b=gateway::unhex(line);gateway::Reader r(b);gateway::Guid128 target{},caster{};auto t=r.take(16),c=r.take(16);std::copy(t.begin(),t.end(),target.begin());std::copy(c.begin(),c.end(),caster.begin());auto e=gateway::turtle_spell_damage_log(r.take(r.left()));if(!e){std::cout<<\"REJECT\\n\";continue;}auto out=gateway::forever_spell_damage_log(*e,{{e->target,target},{e->caster,caster}});for(auto v:out)std::cout<<"0123456789abcdef"[v>>4]<<"0123456789abcdef"[v&15];std::cout<<'\\n';}catch(const std::exception&){std::cout<<\"REJECT\\n\";}}}
""")
subprocess.run(['clang++','-std=c++20','-Wall','-Wextra','-Werror','-I'+str(ROOT/'include'),str(p),str(protocol),'-lcrypto','-lz','-o',str(d/'direct')],check=True)
def guid(v):
b=v.to_bytes(8,'little');return bytes([sum(1<<i for i,v in enumerate(b)if v)])+bytes(v for v in b if v)
target=(0xf130<<48)|(50516<<24)|2555482;caster=4518
header=struct.pack('<QQQQ',2555482,(8<<58)|(70<<42)|(1<<29),4518,(2<<58)|(70<<42))
prefix=guid(target)+guid(caster)
def case(damage=120,school=0,absorbed=5,resisted=2,blocked=3,flags=0,periodic=0,unused=0,extended=0):
return header+prefix+struct.pack('<IIBIiBBIIB',23894,damage,school,absorbed,resisted,periodic,unused,blocked,flags,extended)
cases=[]
for school in range(7):
for periodic in (0,1):
for flags in (0,2,8,0x3f):
for damage,absorbed,resisted,blocked in ((120,5,2,3),(50,0,-4,0),(0,46,0,0),(0,0,0,0)):
cases.append(case(damage,school,absorbed,resisted,blocked,flags,periodic))
rejects=[case()[:n]for n in range(len(case()))]+[case()+b'\0',case(school=7),case(periodic=2),case(unused=1),case(extended=1),case(flags=0x40),case(damage=0xffffffff),case(absorbed=0xffffffff),case(blocked=0xffffffff),case(damage=0,resisted=-100),case(damage=0x7fffffff,absorbed=1)]
all_cases=cases+rejects
result=subprocess.run([str(d/'direct')],input=''.join(v.hex()+'\n'for v in all_cases),capture_output=True,text=True,check=True).stdout.splitlines();assert len(result)==len(all_cases)
lib=C.CDLL(str(codec));fn=lib.gw_codec_serialize;fn.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)];fn.restype=C.c_int
for index,(wire,direct)in enumerate(zip(all_cases,result)):
source=C.create_string_buffer(wire);out=C.create_string_buffer(1048576);written=C.c_uint32();status=fn(38,source,len(wire),0,out,len(out),C.byref(written))
if index<len(cases):assert status==0 and direct==out.raw[:written.value].hex(),(index,status,direct,out.raw[:written.value].hex())
else:assert direct=='REJECT'and status!=0,('invalid accepted',index,status,direct)
print(len(cases),'source damage/school/mitigation/critical/periodic variants match original native writer;',len(rejects),'malformed/unsupported variants rejected. Offline only.')