Files
WoW-Hybrid-gateway/tests/honor_fields_serializer_test.py
2026-11-23 03:28:32 +02:00

53 lines
2.1 KiB
Python

"""Exercise self honor through sparse Source updates, including explicit clears."""
import os
from pathlib import Path
import struct
import sys
from native_codec_driver import MODES, 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)
codec = load()
guid = 0x119D
realm = 70
def packed(data):
mask = sum(1 << i for i, value in enumerate(data) if value)
return mask.to_bytes(len(data) // 8, "little") + bytes(value for value in data if value)
def delta(fields):
words = [0] * (max(fields) // 32 + 1)
for index in fields:
words[index // 32] |= 1 << (index % 32)
mask = bytes([len(words)]) + struct.pack(f"<{len(words)}I", *words)
values = b"".join(struct.pack("<I", fields[i]) for i in sorted(fields))
update = struct.pack("<IBB", 1, 0, 0) + packed(struct.pack("<Q", guid)) + mask + values
context = struct.pack("<HIQBHI", realm, 0, guid, 1, 0, 0xA9)
return serialize(codec, MODES.index("--unit-values-profile"), context + update)
def expected(type_mask, encoded):
values = struct.pack("<BBBI", 1, 0, 3, type_mask) + bytes.fromhex(encoded)
identity = packed(struct.pack("<QQ", guid, (2 << 58) | (realm << 42)))
block = bytes([0]) + identity + struct.pack("<I", len(values)) + values
return struct.pack("<HIBI", 0, 1, 0x80, len(block)) + block
# Full Trinity PlayerData and ActivePlayerData writers, build 70334.
fields = {195: 0x09007766, 1222: 0x0C007712, 1250: 0xCAFEBEEF, 1251: 0xBAAD1234, 1255: 0x1234ABCD}
for index, mask, reference in (
(195, 64, "00200800002009"),
(1222, 128, "00300000010000000980120000000c"),
(1250, 128, "002000008100efbe"),
(1251, 128, "0020000101003412"),
(1255, 128, "002000040100cdab3412"),
):
assert delta({index: fields[index]}) == expected(mask, reference)
assert delta(fields) == expected(192, "0020080000200900300000010000058980120000000cefbe3412cdab3412")
assert delta(dict.fromkeys(fields, 0)) == expected(192, "002008000020000030000001000005898000000000000000000000000000")