367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
218 lines
10 KiB
Python
218 lines
10 KiB
Python
"""Exact-identity Warrior flat cooldown modifier mapping and pure wire state.
|
|
|
|
Source 0x266 is a replacement aggregate, not a delta. This module never sends
|
|
packets, installs definitions, accesses accounts, or changes source gameplay.
|
|
Callers provide complete pinned class inventories and authenticated ownership.
|
|
"""
|
|
from dataclasses import dataclass
|
|
import hashlib
|
|
import json
|
|
import struct
|
|
|
|
FAMILY = 4
|
|
OPERATION = 11
|
|
SOURCE_OPCODE = 0x266
|
|
NATIVE_OPCODE = 0x690027 # Original ClassicOpcodes70334 normalization, not raw670027 enum.
|
|
POLICY = 'warrior-flat-cooldown-exact-bit-identity-v1'
|
|
|
|
|
|
def _integer(value, low, high, label):
|
|
if type(value) is not int or not low <= value <= high:
|
|
raise ValueError(label)
|
|
return value
|
|
|
|
|
|
def _sha(value):
|
|
return hashlib.sha256(json.dumps(value, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
|
|
|
|
|
|
def _inventories(source, target, *, family=FAMILY):
|
|
if type(source) is not list or not 1 <= len(source) <= 100000:
|
|
raise ValueError('complete bounded source inventory required')
|
|
if type(target) is not list or not 1 <= len(target) <= 100000:
|
|
raise ValueError('complete bounded native class4 inventory required')
|
|
old, new = {}, {}
|
|
for row in source:
|
|
if type(row) is not dict or set(row) != {'id', 'family', 'mask'}:
|
|
raise ValueError('typed source identity required')
|
|
ident = _integer(row['id'], 1, 0x7fffffff, 'source identity')
|
|
source_family = _integer(row['family'], 0, 0xffffffff, 'source family')
|
|
mask = _integer(row['mask'], 0, 0xffffffffffffffff, 'source class mask')
|
|
if ident in old:
|
|
raise ValueError('duplicate source identity')
|
|
old[ident] = (source_family, mask)
|
|
native_ids = set()
|
|
for row in target:
|
|
if type(row) is not dict or set(row) != {'ID', 'SpellID', 'ModalNextSpell', 'SpellClassSet', 'SpellClassMask'}:
|
|
raise ValueError('typed native class row required')
|
|
rid = _integer(row['ID'], 1, 0x7fffffff, 'native row identity')
|
|
ident = _integer(row['SpellID'], 1, 0x7fffffff, 'native spell identity')
|
|
_integer(row['ModalNextSpell'], 0, 0x7fffffff, 'native modal spell')
|
|
_integer(row['SpellClassSet'], family, family, 'native family')
|
|
words = row['SpellClassMask']
|
|
if type(words) is not list or len(words) != 4:
|
|
raise ValueError('four native class words required')
|
|
mask = 0
|
|
for i, word in enumerate(words):
|
|
mask |= (_integer(word, -0x80000000, 0xffffffff, 'native class word') & 0xffffffff) << (i * 32)
|
|
if rid in native_ids or ident in new:
|
|
raise ValueError('duplicate native class identity')
|
|
native_ids.add(rid)
|
|
new[ident] = mask
|
|
return old, new
|
|
|
|
|
|
def effective_native_class(base, overlays, *, family=FAMILY):
|
|
"""Apply complete relevant overlay identities, including moves out of class4."""
|
|
_inventories([{'id': 1, 'family': family, 'mask': 1}], base, family=family)
|
|
if type(overlays) is not list or len(overlays) > 1000000:
|
|
raise ValueError('bounded package rows required')
|
|
rows = {row['ID']: dict(row) for row in base}
|
|
seen = set()
|
|
for overlay in overlays:
|
|
if type(overlay) is not dict:
|
|
raise ValueError('typed overlay required')
|
|
if overlay.get('table_name') != 'SpellClassOptions':
|
|
continue
|
|
rid = _integer(overlay.get('id'), 1, 0x7fffffff, 'overlay identity')
|
|
if rid in seen:
|
|
raise ValueError('duplicate class overlay identity')
|
|
seen.add(rid)
|
|
status = overlay.get('record_status', 'valid')
|
|
if status == 'removed':
|
|
rows.pop(rid, None)
|
|
continue
|
|
if status != 'valid':
|
|
raise ValueError('nonvalid class overlay')
|
|
record = overlay.get('record')
|
|
if type(record) is not dict:
|
|
raise ValueError('class overlay record identity')
|
|
if _integer(record.get('ID', rid), 1, 0x7fffffff, 'class overlay record identity') != rid:
|
|
raise ValueError('class overlay record identity')
|
|
if set(record) not in ({'ID', 'SpellID', 'ModalNextSpell', 'SpellClassSet', 'SpellClassMask'},
|
|
{'SpellID', 'ModalNextSpell', 'SpellClassSet', 'SpellClassMask'}):
|
|
raise ValueError('class overlay fields')
|
|
record_family = _integer(record['SpellClassSet'], 0, 255, 'overlay family')
|
|
_integer(record['SpellID'], 1, 0x7fffffff, 'overlay spell identity')
|
|
_integer(record['ModalNextSpell'], 0, 0x7fffffff, 'overlay modal spell')
|
|
words = record['SpellClassMask']
|
|
if type(words) is not list or len(words) != 4:
|
|
raise ValueError('four overlay class words required')
|
|
for word in words:
|
|
_integer(word, -0x80000000, 0xffffffff, 'overlay class word')
|
|
# Even a move away from the selected family must not leave stale base data.
|
|
if record_family == family:
|
|
rows[rid] = dict(record, ID=rid)
|
|
else:
|
|
rows.pop(rid, None)
|
|
result = sorted(rows.values(), key=lambda row: row['ID'])
|
|
_inventories([{'id': 1, 'family': family, 'mask': 1}], result, family=family)
|
|
return result
|
|
|
|
|
|
def effective_native_class4(base, overlays):
|
|
return effective_native_class(base, overlays, family=FAMILY)
|
|
|
|
|
|
def derive(source, target, bits=None, *, family=FAMILY):
|
|
"""Admit only unique bit-to-bit equal complete spell-identity sets.
|
|
|
|
A static union mask is insufficient for replacement aggregates. Subset covers,
|
|
colliding source bits, target-only collateral and multiple native candidates
|
|
remain held; no arbitrary choice or merging of indistinguishable aggregates.
|
|
"""
|
|
old, new = _inventories(source, target, family=family)
|
|
if bits is None:
|
|
bits = [bit for bit in range(64) if any(source_family == family and mask & (1 << bit) for source_family, mask in old.values())]
|
|
if type(bits) is not list or not 1 <= len(bits) <= 64:
|
|
raise ValueError('bounded explicit source bits required')
|
|
checked = [_integer(bit, 0, 63, 'source bit') for bit in bits]
|
|
if len(set(checked)) != len(checked):
|
|
raise ValueError('duplicate source bit')
|
|
native_sets = {bit: {ident for ident, mask in new.items() if mask & (1 << bit)} for bit in range(128)}
|
|
admitted, held = [], []
|
|
for bit in sorted(checked):
|
|
wanted = {ident for ident, (source_family, mask) in old.items() if source_family == family and mask & (1 << bit)}
|
|
missing = sorted(wanted - new.keys())
|
|
exact = [nbit for nbit, identities in native_sets.items() if identities == wanted and wanted]
|
|
if not wanted:
|
|
reason = 'empty-source-bit'
|
|
elif missing:
|
|
reason = 'missing-native-class-identities'
|
|
elif len(exact) != 1:
|
|
reason = 'no-exact-native-bit' if not exact else 'ambiguous-native-bits'
|
|
else:
|
|
admitted.append({'source_bit': bit, 'native_bit': exact[0], 'spell_ids': sorted(wanted)})
|
|
continue
|
|
held.append({'source_bit': bit, 'reason': reason, 'spell_ids': sorted(wanted),
|
|
'missing_native_spell_ids': missing, 'exact_native_candidates': exact})
|
|
collisions = {row['native_bit'] for row in admitted if sum(other['native_bit'] == row['native_bit'] for other in admitted) > 1}
|
|
for row in list(admitted):
|
|
if row['native_bit'] in collisions:
|
|
admitted.remove(row)
|
|
held.append(dict(row, reason='colliding-source-bits'))
|
|
return {'policy': POLICY, 'family': family, 'operation': OPERATION,
|
|
'source_inventory_sha256': _sha(source), 'native_inventory_sha256': _sha(target),
|
|
'source_bits': sorted(checked), 'admitted': admitted,
|
|
'held': sorted(held, key=lambda row: row['source_bit'])}
|
|
|
|
|
|
def validate_profile(profile, source, target):
|
|
if type(profile) is not dict:
|
|
raise ValueError('typed mapping profile required')
|
|
rebuilt = derive(source, target, profile.get('source_bits'))
|
|
if profile != rebuilt:
|
|
raise ValueError('mapping profile does not match pinned complete inventories')
|
|
return profile
|
|
|
|
|
|
def parse_flat_cooldown(opcode, body):
|
|
if type(opcode) is not int or opcode != SOURCE_OPCODE:
|
|
raise ValueError('source flat cooldown opcode required')
|
|
if not isinstance(body, (bytes, bytearray)) or len(body) != 6:
|
|
raise ValueError('six-byte source spell modifier required')
|
|
bit, operation, value = struct.unpack('<BBi', body)
|
|
if bit >= 64 or operation != OPERATION:
|
|
raise ValueError('unsupported source modifier bit/operation')
|
|
# Native flat field is float32. No implicit integer precision loss.
|
|
if int(struct.unpack('<f', struct.pack('<f', float(value)))[0]) != value:
|
|
raise ValueError('flat modifier cannot be represented exactly')
|
|
return bit, value
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Packet:
|
|
opcode: int
|
|
body: bytes
|
|
|
|
|
|
class FlatCooldownState:
|
|
"""Pure generation-bound replacement/removal state, with no live sender."""
|
|
def __init__(self, profile, source, target, owner, generation, *, character_class):
|
|
_integer(character_class, 1, 1, 'authenticated Warrior class required')
|
|
validate_profile(profile, source, target)
|
|
self.owner = _integer(owner, 1, 0xffffffffffffffff, 'authenticated owner')
|
|
self.generation = _integer(generation, 1, 0xffffffffffffffff, 'world generation')
|
|
self.routes = {row['source_bit']: row['native_bit'] for row in profile['admitted']}
|
|
self.values = {}
|
|
|
|
def apply(self, opcode, body, owner, generation):
|
|
if type(owner) is not int or type(generation) is not int or (owner, generation) != (self.owner, self.generation):
|
|
raise ValueError('modifier outside authenticated generation')
|
|
bit, value = parse_flat_cooldown(opcode, body)
|
|
if bit not in self.routes:
|
|
raise ValueError('source class bit has no exact native mapping')
|
|
if bit in self.values and self.values[bit] == value:
|
|
return []
|
|
packet = Packet(NATIVE_OPCODE, struct.pack('<IBIfB', 1, OPERATION, 1, float(value), self.routes[bit]))
|
|
self.values[bit] = value
|
|
return [packet]
|
|
|
|
def clear(self, owner, generation):
|
|
if type(owner) is not int or type(generation) is not int or (owner, generation) != (self.owner, self.generation):
|
|
raise ValueError('clear outside authenticated generation')
|
|
packets = [Packet(NATIVE_OPCODE, struct.pack('<IBIfB', 1, OPERATION, 1, 0.0, self.routes[bit]))
|
|
for bit, value in sorted(self.values.items()) if value != 0]
|
|
self.values.clear()
|
|
return packets
|