85 lines
4.1 KiB
Python
85 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Serialize creature and player weapons through the native host codec's ordinary create and values entry points."""
|
|
from pathlib import Path
|
|
import os
|
|
import struct
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
from native_codec_driver import load, serialize, MODES, ROOT
|
|
|
|
P = struct.pack
|
|
SELF = 4509
|
|
NPC_GUID = bytes.fromhex('cb9d110630f1')
|
|
SELF_GUID = bytes.fromhex('039d11')
|
|
HEADER = P('<QHIIIII', SELF, 70, 0x46010046, 0, 12, 0, 0) + b'Fixture\0' + bytes((1, 1, 0)) + P('<I', 0xa9)
|
|
MOVE = P('<II4fI', 0, 100, 1, 2, 3, 0, 0) + P('<6f', 2.5, 7, 4.5, 4.72, 2.5, 3.14)
|
|
# Two existing modifier-0 wands with different item, modified appearance and source display IDs.
|
|
WANDS = ((21096, 11287, 160241), (21097, 11288, 160242))
|
|
|
|
|
|
def fields(values):
|
|
masks = [0] * (max(values) // 32 + 1)
|
|
for index in values:
|
|
masks[index // 32] |= 1 << (index % 32)
|
|
return (bytes((len(masks),)) + b''.join(P('<I', mask) for mask in masks)
|
|
+ b''.join(P('<I', values[k]) for k in sorted(values)))
|
|
|
|
|
|
def full_item(item, appearance):
|
|
return P('<4iHHIBB', item, 0, 0, 0, 0, 0, appearance, 0, 0) + b'\0'
|
|
|
|
|
|
def validate(lib):
|
|
def run(mode, body):
|
|
return serialize(lib, MODES.index(mode), body)
|
|
def creature_values(values):
|
|
body = P('<IBB', 1, 0, 0) + NPC_GUID + fields(values)
|
|
return run('--unit-values', P('<HIQI', 70, 0, SELF, 0xa9) + body)
|
|
def owner_update(slot, item):
|
|
body = P('<QHII', SELF, 70, 0, 1) + P('<QBBHHI', SELF, 4, 0, 1, 260 + 12*slot, item)
|
|
return run('--inventory-objects', body)
|
|
def owner_create(slot, shown):
|
|
values = {22:80, 28:80, 34:60, 35:1, 36:0x01000101, 718:98, 719:0x012c012c,
|
|
486 + 2*slot:75, 487 + 2*slot:0x40000000}
|
|
if shown is not None:
|
|
values[260 + 12*slot] = shown
|
|
body = P('<IBB', 2, 0, 3) + SELF_GUID + bytes((4, 0x21)) + MOVE + fields(values)
|
|
body += bytes((2, 0x81, 75, 0x40, 1, 0)) + fields({3:11287, 6:SELF, 8:SELF, 14:1})
|
|
return run('', HEADER + body)
|
|
# Nested sparse mask bits 0, 3 and 8 become 0x209 with the Classic inserted bit.
|
|
for slot in range(3):
|
|
for display, item, appearance in (*WANDS, (0, 0, 0)):
|
|
wire = creature_values({37 + slot: display})
|
|
expected = bytes.fromhex('2090') + P('<iI', item, appearance)
|
|
assert wire.endswith(expected), (slot, display, 'creature values', wire.hex())
|
|
assert creature_values({37 + slot: WANDS[0][0]}) != creature_values({37 + slot: WANDS[1][0]})
|
|
assert creature_values({22:55, 37 + slot:0x7fffffff}) == creature_values({22:55})
|
|
assert creature_values({22:55, 37 + slot:0}) != creature_values({22:55})
|
|
for _, item, appearance in (*WANDS, (0, 0, 0)):
|
|
wire = owner_update(15 + slot, item)
|
|
expected = bytes.fromhex('3090') + P('<iHI', item, 0, appearance)
|
|
assert wire.count(expected) == 2, (slot, item, 'owner values', wire.hex())
|
|
|
|
# Both native structures reflect the known visible transmog independently of the owned item object.
|
|
for slot in (15, 16, 17):
|
|
for shown, expected in ((11288, 11288), (11287, 11287), (None, 11287),
|
|
(0x70000000, 11287), (0, 0)):
|
|
wire = owner_create(slot, shown)
|
|
for _, item, appearance in WANDS:
|
|
count = wire.count(full_item(item, appearance))
|
|
assert count == (2 if expected == item else 0), (slot, shown, item, 'owner create', count)
|
|
|
|
npc_values = {3:6, 22:80, 28:80, 34:1, 35:1, 36:0x01000101,
|
|
37:WANDS[0][0], 38:WANDS[1][0], 39:0, 131:19723, 132:19723}
|
|
npc = P('<IBB', 1, 0, 3) + NPC_GUID + bytes((3, 0x20)) + MOVE + fields(npc_values)
|
|
wire = run('--npcs', HEADER + npc)
|
|
assert b''.join(full_item(item, appearance) for _,item,appearance in WANDS) + full_item(0, 0) in wire
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if not Path(os.environ.get('GW_TEST_CODEC_PATH', ROOT/'build/forever-schema/libforever_codec.so')).is_file():
|
|
sys.exit(77)
|
|
validate(load())
|
|
print('Passed creature weapon create, swap and clear plus owner visible/virtual weapon projection in all three slots.')
|