367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
81 lines
6.0 KiB
Python
81 lines
6.0 KiB
Python
"""Retain an already identical complete item-effect relationship list.
|
|
|
|
No spell definition, use/proc behavior or effect record is created or changed.
|
|
The proof is suitable only for independent ItemSparse metadata corrections.
|
|
"""
|
|
import copy,hashlib,math
|
|
from item_material_retention import evidence as material_evidence,digest
|
|
from item_sparse_compat import encode
|
|
from item_effect_compat import TABLES,effective_item_links
|
|
|
|
POLICY='retain-exact-existing-item-effect-references-v1'
|
|
SOURCE_FIELDS=frozenset(f'{field}_{i}' for i in range(1,6)for field in
|
|
('spellid','spelltrigger','spellcharges','spellppmrate','spellcooldown','spellcategory','spellcategorycooldown'))
|
|
|
|
def context(package,effects,links,schemas):
|
|
if set(schemas)!=set(TABLES):raise ValueError('Exact item effect/link schemas required')
|
|
indices={};pins={}
|
|
for table,rows in(('ItemEffect',effects),('ItemXItemEffect',links)):
|
|
if not isinstance(rows,list)or not 1<=len(rows)<=100000:raise ValueError('Bounded native relationship rows required')
|
|
names={f[0]for f in schemas[table]};index={}
|
|
for row in rows:
|
|
if not isinstance(row,dict)or set(row)!={'ID',*names}or any(type(v)is not int for v in row.values())or not 0<row['ID']<2**32 or row['ID']in index:
|
|
raise ValueError('Complete unique typed native relationship row required')
|
|
encode(row,schemas[table]);index[row['ID']]=copy.deepcopy(row)
|
|
indices[table]=index;pins[table]=digest(rows)
|
|
by_effect=indices['ItemEffect']
|
|
for row in package['rows']:
|
|
table=row['table_name']
|
|
if table not in TABLES:continue
|
|
if int(row['table_hash'],16)!=TABLES[table][0]:raise ValueError('Relationship table namespace differs')
|
|
if row.get('record_status','valid')=='removed':
|
|
if table!='ItemXItemEffect':raise ValueError('Removed shared effect is unreviewed')
|
|
continue
|
|
record=row['record'];names={f[0]for f in schemas[table]}
|
|
if set(record)!=names or any(type(v)is not int for v in record.values()):raise ValueError('Exact typed published relationship record required')
|
|
raw=encode(record,schemas[table])
|
|
if raw.hex()!=row['wire_hex']or hashlib.sha256(raw).hexdigest()!=row['sha256']:raise ValueError('Published relationship bytes differ')
|
|
if table=='ItemEffect':by_effect[row['id']]=dict(record,ID=row['id'])
|
|
by_item={}
|
|
for link in effective_item_links(links,package['rows']):
|
|
effect=by_effect.get(link['ItemEffectID'])
|
|
by_item.setdefault(link['ItemID'],[]).append(dict(link=copy.deepcopy(link),effect=copy.deepcopy(effect)))
|
|
return dict(policy=POLICY,package_push_id=package['push_id'],native_record_sha256=pins,schemas=copy.deepcopy(schemas),by_item=by_item)
|
|
|
|
def source_slots(source):
|
|
records=[]
|
|
for i in range(1,6):
|
|
vals={key:source.get(key+'_'+str(i))for key in('spellid','spelltrigger','spellcharges','spellppmrate','spellcooldown','spellcategory','spellcategorycooldown')}
|
|
if any(type(v)is not int for k,v in vals.items()if k!='spellppmrate')or type(vals['spellppmrate'])not in(int,float)or not math.isfinite(vals['spellppmrate'])or vals['spellppmrate']!=0:
|
|
raise ValueError('Typed source effect fields and zero unmapped PPM required')
|
|
if vals['spellid']==0:
|
|
if any(vals[k]!=0 for k in('spelltrigger','spellcharges','spellcategory'))or any(vals[k]not in(-1,0)for k in('spellcooldown','spellcategorycooldown')):
|
|
raise ValueError('Orphan source effect slot fields')
|
|
continue
|
|
if not 0<vals['spellid']<2**31 or vals['spelltrigger']not in(0,1,2)or not -32768<=vals['spellcharges']<=32767 or not 0<=vals['spellcategory']<=65535 or any(not -1<=vals[k]<=86400000 for k in('spellcooldown','spellcategorycooldown')):
|
|
raise ValueError('Existing source relationship bounds/trigger differs')
|
|
records.append(dict(LegacySlotIndex=i-1,TriggerType=vals['spelltrigger'],Charges=vals['spellcharges'],CoolDownMSec=vals['spellcooldown'],CategoryCoolDownMSec=vals['spellcategorycooldown'],SpellCategoryID=vals['spellcategory'],SpellID=vals['spellid'],ChrSpecializationID=0,PlayerConditionID=0))
|
|
return records
|
|
|
|
def evidence(source,basic,native,sparse_schema,ctx):
|
|
identity=material_evidence(source,basic,native,sparse_schema)
|
|
if not isinstance(ctx,dict)or ctx.get('policy')!=POLICY:raise ValueError('Explicit existing effect context required')
|
|
wanted=source_slots(source);actual=ctx['by_item'].get(source['entry'],[])
|
|
if len(wanted)!=len(actual):raise ValueError('Complete existing effect slot count differs')
|
|
slots=set();proofs=[]
|
|
if any(pair['effect']is None for pair in actual):raise ValueError('Existing item link has no native/published effect')
|
|
for pair in sorted(actual,key=lambda p:p['effect']['LegacySlotIndex']):
|
|
effect=pair['effect'];slot=effect['LegacySlotIndex']
|
|
if slot in slots:raise ValueError('Duplicate existing effect slot')
|
|
slots.add(slot);record={k:v for k,v in effect.items()if k!='ID'}
|
|
if record not in wanted:raise ValueError('Existing effect slot fields differ (including sentinels, conditions and specialization)')
|
|
proofs.append(dict(link=copy.deepcopy(pair['link']),effect=copy.deepcopy(effect),link_wire_sha256=hashlib.sha256(encode(pair['link'],ctx['schemas']['ItemXItemEffect'])).hexdigest(),effect_wire_sha256=hashlib.sha256(encode(effect,ctx['schemas']['ItemEffect'])).hexdigest()))
|
|
return dict(policy=POLICY,source_item=source['entry'],source_record_sha256=digest(source),native_identity=identity,
|
|
package_push_id=ctx['package_push_id'],native_relationship_records_sha256=ctx['native_record_sha256'],complete_effect_slots=proofs,
|
|
source_ppm=0,new_or_changed_relationships=0,spell_definition_or_execution_support_added=False)
|
|
|
|
def verify(source,basic,native,schema,ctx,proof):
|
|
actual=evidence(source,basic,native,schema,ctx)
|
|
if not isinstance(proof,dict)or proof!=actual or digest(proof)!=digest(actual):raise ValueError('Existing effect reference provenance changed')
|
|
return actual
|