88 lines
3.5 KiB
Python
88 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Decode initial owned-item identities, counts and charges through the real codec.
|
|
|
|
Constructed Source UpdateObject fixtures, using the existing item-instance flags
|
|
fixture layout. These prove packet parity, not a live action-button reproduction.
|
|
"""
|
|
import os
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
from native_codec_driver import ROOT, load, serialize
|
|
|
|
if not Path(os.environ.get('GW_TEST_CODEC_PATH', ROOT/'build/forever-schema/libforever_codec.so')).is_file():
|
|
sys.exit(77)
|
|
library = load()
|
|
SELF = 4509
|
|
BAG = (0x4000 << 48) | 76
|
|
|
|
|
|
def packed(value, width):
|
|
raw = value.to_bytes(width, 'little')
|
|
mask = sum(1 << i for i, byte in enumerate(raw) if byte)
|
|
return mask.to_bytes(width // 8, 'little') + bytes(byte for byte in raw if byte)
|
|
|
|
|
|
def unpack(data, at):
|
|
mask = int.from_bytes(data[at:at+2], 'little')
|
|
at += 2
|
|
value = 0
|
|
for i in range(16):
|
|
if mask & (1 << i):
|
|
value |= data[at] << (8*i)
|
|
at += 1
|
|
return value, at
|
|
|
|
|
|
def initial_item(container, entry, count, charges):
|
|
fields = {22:80, 28:80, 34:60, 35:1, 36:0x01000101, 718:98, 719:0x012c012c}
|
|
mask = [0]*23
|
|
for index in fields:
|
|
mask[index//32] |= 1 << (index%32)
|
|
body = struct.pack('<IBB', 2, 0, 3) + packed(SELF, 8) + bytes([4, 0x21])
|
|
body += struct.pack('<II4fI', 0, 100, 1, 2, 3, 0, 0)
|
|
body += struct.pack('<6f', 2.5, 7, 4.5, 4.72, 2.5, 3.14)
|
|
body += bytes([23]) + struct.pack('<23I', *mask)
|
|
body += b''.join(struct.pack('<I', fields[i]) for i in sorted(fields))
|
|
item = {3:entry, 6:SELF, 8:container & 0xffffffff, 9:container >> 32, 14:count}
|
|
item.update({16+i:charge & 0xffffffff for i, charge in enumerate(charges)})
|
|
body += bytes([2]) + packed((0x4000 << 48) | 75, 8) + bytes([1, 0, 1])
|
|
body += struct.pack('<I', sum(1 << i for i in item))
|
|
body += b''.join(struct.pack('<I', item[i]) for i in sorted(item))
|
|
header = struct.pack('<QHIIIII', SELF, 70, 0x46010046, 0, 12, 0, 0)
|
|
header += b'Fixture\0' + bytes([1, 1, 0]) + struct.pack('<I', 0xa9)
|
|
wire = serialize(library, 0, header+body)
|
|
assert struct.unpack_from('<I', wire, 2)[0] == 3
|
|
assert wire[11] == 2
|
|
_, at = unpack(wire, 12)
|
|
at += 8 # type and movement header
|
|
size = struct.unpack_from('<I', wire, at)[0]
|
|
data = wire[at+4:at+4+size]
|
|
at = 5+12 # owner visibility/type list, ObjectData
|
|
owner, at = unpack(data, at)
|
|
contained, at = unpack(data, at)
|
|
for _ in range(2):
|
|
_, at = unpack(data, at)
|
|
stack, expiration, *actual_charges = struct.unpack_from('<II5i', data, at)
|
|
return owner, contained, stack, actual_charges
|
|
|
|
|
|
player = ((2 << 58) | (70 << 42)) << 64 | SELF
|
|
bag = (3 << 122) | (BAG & 0xffffffff)
|
|
# No item-ID branching: consumables and an arbitrary charged item use one path.
|
|
for entry in (20520, 61675, 25):
|
|
for container, expected in ((SELF, player), (BAG, bag), (0, 0)):
|
|
charges = [-1, 0, 3, -5, 0]
|
|
owner, actual, count, actual_charges = initial_item(container, entry, 7, charges)
|
|
assert owner == player
|
|
assert actual == expected, f'item {entry}: Source contained={container:#x}, native={actual:#x}, expected={expected:#x}'
|
|
assert count == 7 and actual_charges == charges
|
|
# Malformed Source containment is rejected consistently with inventory updates.
|
|
try:
|
|
initial_item(1234, 20520, 7, [-1, 0, 0, 0, 0])
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError('foreign player containment accepted')
|
|
print('PASS: initial item containment, owner, stack and five signed charges preserved')
|