307 lines
21 KiB
Python
307 lines
21 KiB
Python
#!/usr/bin/env python3
|
|
"""Pure 70334 Item adapter for source-authoritative scalar compatibility.
|
|
|
|
No DB, client, network, header generation or installation. An inaccessible
|
|
destination record is not evidence that its ID is absent. New scalar records
|
|
are explicit source-authoritative overrides requiring actual-client acceptance.
|
|
"""
|
|
from copy import deepcopy
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import re
|
|
import struct
|
|
from item_sparse_compat import fields as _fields,check_db2 as _check_db2
|
|
from item_sparse_compat import native_item_material
|
|
from item_package_preflight import scalar_exclusions
|
|
from item_icon_preflight import SOURCE_EMPTY_ICON, empty_icon_reference, normalize_icon
|
|
from octo_client_data import canonical_sha256
|
|
|
|
TABLE=0x50238ec2
|
|
LAYOUT=0x9a2a4834
|
|
WIRE_BYTES=43
|
|
# Item.dbd LAYOUT9A2A4834 / DB2Meta::ItemMeta / ItemLoadInfo: ID is
|
|
# noninline. Retain all 17 exported columns; only these16 are serialized.
|
|
SCHEMA=(('ClassID','int',32,False,1),('SubclassID','int',8,True,1),
|
|
('Material','int',8,True,1),('InventoryType','int',8,False,1),
|
|
('SheatheType','int',8,True,1),('ItemPetFoodID','int',32,False,1),
|
|
('Sound_override_subclassID','int',8,False,1),('IconFileDataID','int',32,False,1),
|
|
('ItemGroupSoundsID','int',32,True,1),('ContentTuningID','int',32,False,1),
|
|
('ModifiedCraftingReagentItemID','int',32,False,1),('AmmunitionType','int',8,True,1),
|
|
('CraftingQualityID','int',32,False,1),('ItemSquishEraID','int',32,False,1),
|
|
('RecraftReagentCountPercentage','float',32,False,1),('OrderSource','int',8,True,1))
|
|
ROW_KEYS=frozenset(('ID',)+tuple(field[0] for field in SCHEMA))
|
|
# Source ItemPrototype.h enums versus target ItemTemplate.h. Numeric matches
|
|
# with changed meanings are deliberately absent (class8, weapon9, armor5,
|
|
# container8/9). Gem0 has no reviewed source enum meaning and stays deferred.
|
|
REVIEWED_SUBCLASSES={0:frozenset(range(9)),1:frozenset(range(8)),
|
|
2:frozenset(set(range(21))-{9}),4:frozenset(set(range(10))-{5}),
|
|
5:frozenset((0,)),6:frozenset(range(5)),
|
|
7:frozenset((0,1,2,3,5,6,7,8,9,10,11,12)),9:frozenset(range(11)),
|
|
10:frozenset((0,)),11:frozenset(range(4)),12:frozenset((0,)),
|
|
13:frozenset((0,1)),14:frozenset((0,)),15:frozenset(range(6))}
|
|
SCALAR_CLASSES=frozenset((5,7,12,13,15))
|
|
DEFAULTS=dict(ItemPetFoodID=0,Sound_override_subclassID=-1,ItemGroupSoundsID=0,
|
|
ContentTuningID=0,ModifiedCraftingReagentItemID=0,CraftingQualityID=0,
|
|
ItemSquishEraID=0,RecraftReagentCountPercentage=0,OrderSource=0)
|
|
|
|
def fields(path):
|
|
result=_fields(path,LAYOUT,len(SCHEMA))
|
|
if tuple(result)!=SCHEMA:raise ValueError('Unreviewed70334 Item layout')
|
|
return result
|
|
|
|
def check_db2(path):_check_db2(path,TABLE,LAYOUT)
|
|
|
|
def _integer(value,low,high,label):
|
|
if type(value) is not int or not low<=value<=high:raise ValueError('Item basic integer bound: '+label)
|
|
return value
|
|
|
|
def _source_identity(proto):
|
|
if not isinstance(proto,dict):raise ValueError('Source item object required')
|
|
ident=_integer(proto.get('entry'),1,0x7fffffff,'entry')
|
|
cls=_integer(proto.get('class'),0,15,'class');sub=_integer(proto.get('subclass'),0,255,'subclass')
|
|
inv=_integer(proto.get('inventory_type'),0,28,'inventory_type')
|
|
if sub not in REVIEWED_SUBCLASSES.get(cls,()):raise ValueError('Source item class/subclass semantic conversion is unreviewed')
|
|
return ident,cls,sub,inv
|
|
|
|
def encode(row,schema=SCHEMA):
|
|
if tuple(schema)!=SCHEMA or not isinstance(row,dict) or set(row)!=ROW_KEYS:raise ValueError('Complete reviewed17-column Item row required')
|
|
_integer(row['ID'],1,0x7fffffff,'ID');result=bytearray()
|
|
for name,kind,bits,unsigned,_ in SCHEMA:
|
|
value=row[name]
|
|
if kind=='float':
|
|
if type(value) not in (int,float) or not math.isfinite(value):raise ValueError('Nonfinite Item basic float')
|
|
try:result.extend(struct.pack('<f',value))
|
|
except (OverflowError,struct.error):raise ValueError('Item basic float bound') from None
|
|
else:
|
|
_integer(value,0 if unsigned else -(1<<(bits-1)),(1<<bits)-1 if unsigned else (1<<(bits-1))-1,name)
|
|
result.extend(value.to_bytes(bits//8,'little',signed=not unsigned))
|
|
if len(result)!=WIRE_BYTES:raise ValueError('Item basic serialized width drift')
|
|
return bytes(result)
|
|
|
|
def _icon(proto,evidence):
|
|
"""Consume the existing icon-preflight row, retaining its naming-only limit."""
|
|
_integer(proto.get('display_id'),1,0x7fffffff,'display_id')
|
|
if (not isinstance(evidence,dict) or type(evidence.get('entry')) is not int or evidence['entry']!=proto['entry'] or
|
|
type(evidence.get('source_display_id')) is not int or evidence['source_display_id']!=proto.get('display_id')):raise ValueError('Item icon evidence has a different source identity')
|
|
if evidence.get('policy')=='verified-exact-captured-source-icon-member-v1':
|
|
from exact_source_icon_compat import verify
|
|
return verify(proto,evidence)
|
|
if evidence.get('policy') == SOURCE_EMPTY_ICON:
|
|
if (type(evidence.get('native_icon_file_data_id')) is not int or
|
|
evidence != empty_icon_reference(proto['entry'], proto['display_id'])):
|
|
raise ValueError('Empty source icon evidence differs')
|
|
return 0
|
|
source=normalize_icon(evidence.get('source_icon'),source=True)
|
|
if evidence.get('policy')=='source-icon-reference-deferred-metadata-only':
|
|
# Explicitly import known identity/stat text while artwork is pending.
|
|
# Zero means absent; it is never a selected texture or a runtime handle.
|
|
if (type(evidence.get('native_icon_file_data_id'))is not int
|
|
or evidence['native_icon_file_data_id']!=0
|
|
or evidence.get('normalized_source')!=source
|
|
or evidence.get('source_icon_presentation_pending')is not True
|
|
or evidence.get('reason')!='source-icon-reference-unresolved'):
|
|
raise ValueError('Explicit deferred icon metadata differs')
|
|
return 0
|
|
if evidence.get('policy')=='verified-source-artwork-native-icon-unregistered':
|
|
# Zero is an explicit absent native reference, never a process texture
|
|
# handle or an unrelated icon. The source-artwork UI provider owns the
|
|
# separate presentation binding; engine icon registration stays pending.
|
|
digest=evidence.get('source_artwork_sha256');manifest=evidence.get('source_artwork_manifest_sha256')
|
|
if (evidence.get('native_icon_file_data_id')!=0 or type(evidence.get('native_icon_file_data_id'))is not int
|
|
or not isinstance(digest,str)or re.fullmatch('[0-9a-f]{64}',digest)is None
|
|
or not isinstance(manifest,str)or re.fullmatch('[0-9a-f]{64}',manifest)is None
|
|
or evidence.get('source_artwork_path')!='interface/icons/gwhybrid_'+digest+'.blp'
|
|
or evidence.get('normalized_source')!=source or evidence.get('source_artwork_bytes_verified')is not True):
|
|
raise ValueError('Explicit source artwork binding differs')
|
|
return 0
|
|
ident=_integer(evidence.get('native_icon_file_data_id'),1,0x7fffffff,'IconFileDataID')
|
|
names=evidence.get('target_listfile_names')
|
|
if not isinstance(names,list) or len(names)!=1:raise ValueError('One explicit target icon reference required')
|
|
target=normalize_icon(names[0])
|
|
if not target['extension']:raise ValueError('Target icon extension missing')
|
|
exact=bool(source['extension']) and source['path']==target['path']
|
|
stem=not source['extension'] and source['stem']==target['stem']
|
|
status='filename-equivalent' if exact else 'stem-equivalent-extension-unverified' if stem else None
|
|
if (status is None or evidence.get('status')!=status or evidence.get('normalized_source')!=source or
|
|
evidence.get('normalized_target')!=target or evidence.get('logical_stem_equivalent') is not True or
|
|
evidence.get('filename_equivalent') is not exact):raise ValueError('Source/target icon naming evidence differs')
|
|
return ident
|
|
|
|
def convert(proto,native=None,*,icon_evidence=None,source_revision=None,source_authoritative=False,ordinary_container=False):
|
|
"""Preserve compatible known rows or stage a narrow scalar source override.
|
|
|
|
New rows deliberately omit sound-group association and all unrelated modern
|
|
references. Those zero references do not claim sound or mechanic parity.
|
|
Source scripts, spells, food, equipment and unreviewed fields stay blocked
|
|
by the existing exhaustive scalar policy rather than guessed defaults.
|
|
"""
|
|
ident,cls,sub,inv=_source_identity(proto)
|
|
if type(source_authoritative) is not bool or type(ordinary_container) is not bool:raise ValueError('Explicit boolean source-authoritative/category policy required')
|
|
container=ordinary_container and (cls,sub,inv)==(1,0,18)
|
|
if ordinary_container and not container:raise ValueError('Ordinary container category differs')
|
|
if source_authoritative and native is None:raise ValueError('Known basic correction requires its real same-ID baseline')
|
|
if native is not None:
|
|
encode(native)
|
|
if native['ID']!=ident:raise ValueError('Same-ID native Item basic collision')
|
|
if not source_authoritative:
|
|
if (native['ClassID'],native['SubclassID'],native['InventoryType'])!=(cls,sub,inv):raise ValueError('Same-ID native Item basic collision')
|
|
if icon_evidence is not None:raise ValueError('Compatible existing Item rows must remain unchanged')
|
|
return deepcopy(native)
|
|
if native['InventoryType']!=0 and not (container and (native['ClassID'],native['SubclassID'],native['InventoryType'])==(1,0,18)):
|
|
raise ValueError('Known basic correction requires a reviewed same-category baseline')
|
|
if not container and (cls not in SCALAR_CLASSES or inv!=0):raise ValueError('New Item basic override is outside reviewed non-equippable scalar categories')
|
|
icon=_icon(proto,icon_evidence)
|
|
from item_package_preflight import container_exclusions
|
|
policy=container_exclusions if container else scalar_exclusions
|
|
reasons=policy(proto,{'IconFileDataID':icon},set(),source_revision)
|
|
if reasons:raise ValueError('New Item basic source dependencies: '+'; '.join(reasons))
|
|
row=dict(ID=ident,ClassID=cls,SubclassID=sub,Material=native_item_material(proto, native),InventoryType=inv,
|
|
SheatheType=proto['sheath'],IconFileDataID=icon,AmmunitionType=proto['ammo_type'],**DEFAULTS)
|
|
encode(row)
|
|
return row
|
|
|
|
def record(proto,native=None,*,icon_evidence=None,source_revision=None,source_authoritative=False,ordinary_container=False):
|
|
"""Return one ordinary component record, never an active installation."""
|
|
row=convert(proto,native,icon_evidence=icon_evidence,source_revision=source_revision,source_authoritative=source_authoritative,ordinary_container=ordinary_container);wire=encode(row)
|
|
return dict(table_name='Item',table_hash=hex(TABLE),id=row['ID'],record=row,wire_hex=wire.hex(),sha256=hashlib.sha256(wire).hexdigest(),
|
|
source_record_sha256=canonical_sha256(proto),policy='source-authoritative-known-basic-correction' if source_authoritative else 'preserve-existing-complete-record' if native is not None else 'source-authoritative-basic-override',
|
|
unknown_native_definition=native is None,target_complete_absence_verified=False,
|
|
preserved_native_fields=[key for key in ('ID',)+tuple(field[0] for field in SCHEMA) if native is not None and native[key]==row[key]],
|
|
defaults_applied=sorted(DEFAULTS) if native is None or source_authoritative else [],
|
|
baseline_record_sha256=hashlib.sha256(encode(native)).hexdigest() if native is not None else None,
|
|
baseline_identity={key:native[key] for key in ('ID','ClassID','SubclassID','InventoryType','IconFileDataID')} if native is not None else None,
|
|
changed_fields=[dict(field=key,native=native[key],source=row[key]) for key in ('ID',)+tuple(field[0] for field in SCHEMA) if native is not None and native[key]!=row[key]],
|
|
icon_reference_evidence_sha256=canonical_sha256(icon_evidence) if icon_evidence is not None else None,
|
|
icon_asset_bytes_verified=False,engine_accepted=False,gameplay_verified=False)
|
|
|
|
|
|
def type_override_record(proto,native,*,source_authoritative=False):
|
|
"""Correct only reviewed scalar type fields; retain every other native column.
|
|
|
|
The caller owns the pinned source-entry/native-ItemID authority. This is a
|
|
partial field projection, never a full source-template or icon claim.
|
|
"""
|
|
if source_authoritative is not True:raise ValueError('Explicit source-authoritative type projection required')
|
|
ident,cls,sub,inv=_source_identity(proto)
|
|
if native is None or type(native.get('ID'))is not int or native['ID']!=ident:raise ValueError('Same-ID complete native Item baseline required')
|
|
encode(native)
|
|
if cls not in SCALAR_CLASSES or inv!=0 or native['InventoryType']!=0:
|
|
raise ValueError('Type-only projection requires reviewed unequipped scalar categories')
|
|
row=deepcopy(native)
|
|
row.update(ClassID=cls,SubclassID=sub,InventoryType=inv)
|
|
wire=encode(row)
|
|
source_fields={k:proto[k]for k in ('entry','class','subclass','inventory_type')}
|
|
return dict(table_name='Item',table_hash=hex(TABLE),id=ident,record=row,
|
|
wire_hex=wire.hex(),sha256=hashlib.sha256(wire).hexdigest(),
|
|
policy='source-authoritative-scalar-type-only-v1',partial_source_authority=True,
|
|
source_type_fields=source_fields,
|
|
source_type_fields_sha256=hashlib.sha256(json.dumps(source_fields,sort_keys=True,separators=(',',':')).encode()).hexdigest(),
|
|
source_field_names=['ClassID','SubclassID','InventoryType'],
|
|
native_baseline_row_sha256=hashlib.sha256(json.dumps(native,sort_keys=True,separators=(',',':')).encode()).hexdigest(),
|
|
complete_source_template_verified=False,engine_accepted=False)
|
|
|
|
|
|
def equipment_record(proto,native=None,*,icon_evidence,replace_existing=False,replace_category=False):
|
|
"""Compile equipment identity only; callers must resolve its other components.
|
|
|
|
The scalar/container exclusion policy describes *complete narrow items*,
|
|
and must not be weakened to admit equipment. Item basics have no stats,
|
|
armor, damage or effect fields. Emit a separately labelled component so
|
|
those dependencies cannot be mistaken for completed conversion.
|
|
"""
|
|
ident,cls,sub,inv=_source_identity(proto)
|
|
# Tortoise ItemPrototype.h ItemSubclassWeapon/Armor and InventoryType;
|
|
# target ItemTemplate.h retains these reviewed category meanings.
|
|
weapon_slots={0:{13,21,22},1:{17},2:{15},3:{26},4:{13,21,22},5:{17},
|
|
6:{17},7:{13,21,22},8:{17},10:{17},11:{13,21,22},12:{17},
|
|
13:{13,21,22},14:{13,21,22,23},15:{13,21,22},16:{25},17:{17},18:{26},19:{26},20:{17}}
|
|
# Original70334 Item280317 proves miscellaneous weapon14/held-offhand23
|
|
# identity. Damage/speed remain separate components, never inferred here.
|
|
# Both pinned ItemPrototype.h and70334 ItemTemplate.h define miscellaneous
|
|
# armor0 with BODY4/TABARD19. These are real wearable identity components;
|
|
# appearance/armor/effect conversion remains separately required below.
|
|
armor_slots={0:{2,4,11,12,19,23},1:{1,3,4,5,6,7,8,9,10,16,20},
|
|
2:{1,3,5,6,7,8,9,10,20},3:{1,3,5,6,7,8,9,10,20},
|
|
4:{1,3,5,6,7,8,9,10,20},6:{14},7:{28},8:{28},9:{28}}
|
|
slots=weapon_slots if cls==2 else armor_slots if cls==4 else {}
|
|
if inv not in slots.get(sub,set()):raise ValueError('Unreviewed equipment class/subclass/slot combination')
|
|
if type(replace_existing) is not bool:raise ValueError('Explicit equipment replacement policy required')
|
|
if type(replace_category)is not bool or (replace_category and not replace_existing):raise ValueError('Category replacement requires explicit same-ID correction')
|
|
if native is not None:
|
|
encode(native)
|
|
if native['ID']!=ident or (not replace_category and (native['ClassID'],native['SubclassID'])!=(cls,sub)):
|
|
raise ValueError('Equipment baseline identity/class/subclass collision')
|
|
if not replace_existing:raise ValueError('Existing equipment requires explicit source-authoritative correction')
|
|
elif replace_existing:raise ValueError('Equipment correction requires a real same-ID baseline')
|
|
icon=_icon(proto,icon_evidence)
|
|
row=deepcopy(native) if native is not None else dict(ID=ident,**DEFAULTS)
|
|
row.update(ClassID=cls,SubclassID=sub,InventoryType=inv,Material=native_item_material(proto, native),
|
|
SheatheType=proto['sheath'],IconFileDataID=icon,AmmunitionType=proto['ammo_type'])
|
|
wire=encode(row)
|
|
return dict(table_name='Item',table_hash=hex(TABLE),id=ident,record=row,
|
|
wire_hex=wire.hex(),sha256=hashlib.sha256(wire).hexdigest(),
|
|
policy='source-authoritative-equipment-identity-component',
|
|
source_record_sha256=canonical_sha256(proto),
|
|
baseline_record_sha256=hashlib.sha256(encode(native)).hexdigest() if native else None,
|
|
changed_fields=[dict(field=key,native=native[key],source=row[key]) for key in sorted(ROW_KEYS)
|
|
if native is not None and native[key]!=row[key]],
|
|
icon_reference_evidence_sha256=canonical_sha256(icon_evidence),
|
|
native_icon_registration_pending=icon==0 and icon_evidence.get('policy')!=SOURCE_EMPTY_ICON,
|
|
dependency_complete=False,installable_alone=False,icon_asset_bytes_verified=False,
|
|
engine_accepted=False,gameplay_verified=False,
|
|
remaining_components=['ItemSparse and source flags/restrictions/stats',
|
|
'weapon damage or armor/block semantics where applicable',
|
|
'item effects and referenced spells/sets where applicable',
|
|
'appearance and asset dependency validation'])
|
|
|
|
|
|
def catalog_record(proto,native=None,*,icon_evidence,replace_category=False,
|
|
material_retention=None,native_sparse=None,sparse_schema=None):
|
|
"""Identity component for bulk conversion, never a complete item policy.
|
|
|
|
Reuse equipment slot checks and the reviewed enum intersection. Other
|
|
categories retain their spells/recipes/quests as unresolved dependencies in
|
|
the catalogue; admitting their identity does not admit their mechanics.
|
|
"""
|
|
ident,cls,sub,inv=_source_identity(proto)
|
|
if type(replace_category)is not bool:raise ValueError('Explicit catalogue category replacement policy required')
|
|
retention_requested=any(value is not None for value in (material_retention,native_sparse,sparse_schema))
|
|
if cls in (2,4):
|
|
if retention_requested:raise ValueError('Equipment material retention requires a separate qualified path')
|
|
return equipment_record(proto,native,icon_evidence=icon_evidence,replace_existing=native is not None,replace_category=replace_category and native is not None)
|
|
# Pinned source ItemPrototype.h and target ItemTemplate.h both define
|
|
# PROJECTILE6, ARROW2/BULLET3 and AMMO24. Admit their identity pair without
|
|
# claiming that source ammunition damage or ranged execution is converted.
|
|
projectile=cls==6 and sub in(2,3)and inv==24
|
|
#35 original70334 Item rows prove miscellaneous15/subclass0 with held23.
|
|
# This admits only the existing identity, never a weapon/armor remap or an
|
|
# implicit spell/use policy for miscellaneous held items.
|
|
miscellaneous_held=(cls,sub,inv)==(15,0,23)
|
|
if inv!=0 and not (cls==1 and inv==18)and not projectile and not miscellaneous_held:raise ValueError('Unreviewed catalogue category/inventory combination')
|
|
if native is not None:
|
|
encode(native)
|
|
if native['ID']!=ident or (not replace_category and (native['ClassID'],native['SubclassID'],native['InventoryType'])!=(cls,sub,inv)):
|
|
raise ValueError('Catalogue baseline identity/category collision')
|
|
material=native_item_material(proto, native)
|
|
if retention_requested:
|
|
if native is None or material_retention is None or native_sparse is None or sparse_schema is None:
|
|
raise ValueError('Complete same-item native material retention evidence required')
|
|
from item_material_retention import verify
|
|
material=verify(proto,native,native_sparse,sparse_schema,material_retention)
|
|
row=deepcopy(native) if native is not None else dict(ID=ident,**DEFAULTS)
|
|
row.update(ClassID=cls,SubclassID=sub,InventoryType=inv,Material=material,
|
|
SheatheType=proto['sheath'],IconFileDataID=_icon(proto,icon_evidence),AmmunitionType=proto['ammo_type'])
|
|
wire=encode(row)
|
|
return dict(table_name='Item',table_hash=hex(TABLE),id=ident,record=row,wire_hex=wire.hex(),
|
|
sha256=hashlib.sha256(wire).hexdigest(),source_record_sha256=canonical_sha256(proto),
|
|
policy='source-authoritative-item-identity-component',
|
|
baseline_identity={key:native[key]for key in ('ID','ClassID','SubclassID','InventoryType')}if native is not None else None,
|
|
changed_fields=[dict(field=key,native=native[key],source=row[key])for key in sorted(ROW_KEYS)if native is not None and native[key]!=row[key]],
|
|
baseline_record_sha256=hashlib.sha256(encode(native)).hexdigest() if native is not None else None,
|
|
icon_reference_evidence_sha256=canonical_sha256(icon_evidence),
|
|
**({'material_retention':material_retention,'source_material_conversion':'deferred'} if retention_requested else {}),
|
|
native_icon_registration_pending=row['IconFileDataID']==0 and icon_evidence.get('policy')!=SOURCE_EMPTY_ICON,
|
|
dependency_complete=False,installable_alone=False,icon_asset_bytes_verified=False,
|
|
engine_accepted=False,gameplay_verified=False)
|