
    UHj"B                    Z   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZ  ee      j                         j                  d   Zedz  Zdd	d
dg dg ddddddg dg ddddddg dg ddddddg dg d dd!d"d#d$g d%g d&dgZd'Zdd(d)d*d+d,d-d.dd/d)d0d+d,d1d.d2d3d)d4d+d,d5d.d6d7d)d8d+d,d9d.d:d;d)d<d=d,d>d.d?d@dAd8dBdCdDd.dEdFdGd8dHdCdId.dJdKdLd8dMdNdOd.dPdQdRd4dSd,dTd.dUdVdWd8dXdCdId.dYdZd[d<d\d,d]d.d^d_d`dadbdNdcd.gZdmddZdndeZdodfZdpdgZdqdhZdqdiZefdrdjZdsdkZedlk(  r e        yy)tz2Summarize save-selector selection buffer handlers.    )annotationsN)Path)read_sectionsva_to_offset   out0x10i@ zbuild branch-state tablezChooses primary/secondary branch-state table from stream+1, passes stream+2 to helper 0x00410c90, then advances the script stream by 4 bytes.)reads stream+1 and stream+2z1references state tables 0x0059e370 and 0x0059e360zcalls helper 0x00410c90)8a4002
b860e35900
b970e35900
e8b957000083404004)	opcodeHex	handlerVanamesummaryevidence
signatures0x11i@ zbranch on selected statezReads index (context+0xa8)[stream+2], then tests stateTable[index]. State value 1 falls through by 8 bytes; any other value jumps to dword [stream+4].)zreads context+0xa8 as a pointerr
   z3writes context+0x40 from dword [stream+4] on branch)8b80a80000008a14018a0c0283f9018b40048941400x12i_@ z&select previous/next active state slotzScans the 12-slot branch-state table for a nonzero entry, starting from (context+0xa8)[stream+2] when runtime flag 0x00457744 is set, stores the selected slot back into (context+0xa8)[stream+2], then advances by 4 bytes.)zuses 12-slot loop boundsz'reads and writes context+0xa8[stream+2]z"references runtime flag 0x00457744)
a044774500r   8a040188040a837df40cr   0x13i@ z"select matching runtime state slota  Computes the active slot base from 0x004576e9/0x00457750, clears (context+0xa8)[stream+2], scans 12 runtime slots, compares per-slot bytes from 0x0059e344 with slot fields +0x4a/+0x48, stores the matching slot index back into (context+0xa8)[stream+2], then advances by 4 bytes.)z-reads current runtime object index 0x0059e33ez#reads active order bytes 0x004576e9z/computes 0x00457750 + activeOrder[index] * 0xd8z.reads runtime comparison bytes from 0x0059e344z8writes the selected slot index to context+0xa8[stream+2])

a03ee359008a88e9764500
05507745008b89a8000000r    837df80c
a044e359008a54084a8a5c0848r   helperiA zfill branch-state tablezDispatches by stream+2 value and fills a 12-byte state table. The simplest helper path writes 1 before the requested slot and 0 after it; later paths write 0/1/2 from party, menu, item, or progress-related runtime tables.)zjump table has cases 0..11z.helper 0x00410de5 fills 12 slots with 1 then 0z1other helper paths write state values 0, 1, and 2)ff2485b00d4100c6040801c6040800c6040802iA iA iA z
constant 2z>prefix fill: slots before N become 1, remaining slots become 0z1/0z>supports predecessor fill 0x00000210; produces slots 0..1 as 1)casecaseVacalleeVaargumentpatternstateValuespersistenceRiskiA z
constant 4z*would overwrite table with slots 0..3 as 1   iA z
constant 5z*would overwrite table with slots 0..4 as 1   iA z
constant 6z*would overwrite table with slots 0..5 as 1   iA zbyte 0x004576e8zruntime prefix fillz,runtime value can overwrite the prefix width   i	A i@A zUparty/item threshold table; missing entries 0, insufficient entries 2, pass entries 1z0/1/2z8runtime party/item state can rewrite the first six slots   iA i#A z;paired runtime flags at 0x004576ec/0x004576ed; writes 0/2/1z-runtime flags can rewrite the first six slots   i/A iA z:indexed table using 0x0059e33e, 0x0059e344, and 0x0045779az0/1z-runtime table can rewrite the first six slots   iBA ihA zCcounts nonzero flags at 0x004576f8 and fills a derived prefix widthz.runtime count can rewrite the first five slots	   iUA i8A zHruntime group from 0x0059e345 plus paired flags at 0x004576f8/0x004576f9
   ihA iA z>compares object pointers from 0x0059db30 via helper 0x00433a30z4object comparison can rewrite a runtime-sized prefix   iA i}A zword 0x0059db28z3checks object table 0x0059db3c field +0x62 bit 0x80z/object flags can rewrite a runtime-sized prefixc                0    t        ||      }|y| |||z    S )N    )r   )exesectionsvasizeoffsets        D/home/exedev/hwanse/tools/summarize_save_selector_selection_state.pybytes_atrI      s'    (B'F~vv}%%rB   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )Nr9   z<Ir   )r   lenstructunpack_from)rC   rD   rE   rG   s       rH   dword_atrN     s?    (B'F~!c#h.dC033rB   c                    g }t         D ]a  }t        |d   dz  z   }t        | ||      }|j                  i |d|d   dd|dd|d   dd|d   d|d|dnd ||d   k(  d       c |S )	Nr0   r9   0x02x08xr1   r2   )caseHexjumpTableEntryVaHex	caseVaHexcalleeVaHexactualCaseVaHexjumpTableMatches)HELPER_CASESHELPER_JUMP_TABLErN   append)rC   rD   rowsr0   entry_vaactuals         rH   helper_case_rowsr_     s    D $tF|a'77#x2 

DL-.%'~#6d8nS12Z 0564:4FF3<0D &$x. 8
 	 KrB   c           
        t        |       }g }t        D ]  }t        | ||d   d      }g }|d   D ]-  }t        j	                  |      }|j                  |||v d       / |j                  i |j                         D 	ci c]  \  }}	|dk7  s||	 c}	}d|d   d|t        d |D              d        d	d
ddddddd|dt        ddt        | |      ddS c c}	}w )Nr   i   r   )signatureHexfoundrP   rR   c              3  &   K   | ]	  }|d      yw)rb   N ).0checks     rH   	<genexpr>z build_summary.<locals>.<genexpr>)  s     %IeGn%Is   )handlerVaHexsignatureChecksallSignaturesFoundz?save-selector script selection buffer and branch-state handlers
0x000000a8   zPointer to a per-script selection buffer addressed by byte offsets from stream+2. Opcodes 0x11, 0x12, and 0x13 use stream+2 as an offset into this buffer; the resulting byte selects one of 12 branch-state table slots.)contextFieldHex
stateSlotsinterpretationzinactive/unavailablezactive/pass branch testz&visible but blocked or unmet condition)012z
0x00..0x0b)
tableVaHexvalidCaseRangeHexcases)scopeselectionBufferbranchStateValueshandlershelperJumpTable)
r   HANDLERSrI   bytesfromhexr[   itemsallrZ   r_   )
rC   rD   r\   handlercodecheckssig_hexsigkeyvalues
             rH   build_summaryr     s6   S!HD Xw{';UC|, 	G--(CMM ' 	 	 
,3MMOSjc5sl?RsEzS
 !5c :;%"%%I&%I"I	
 	  S+C	
 (*9

 056!-%c84
#  Ts   >CCc                   dd| d   dd| d   d    d| d   d    d	| d   d
   dddg	}| d   D ]8  }|d   rdnd}|j                  d|d    d|d    d|d    d|d    d| d       : |j                  g d       | d   j                         D ]  \  }}|j                  d| d|         |j                  dddd| d    d!    d"| d    d#    d$dd%d&g       | d    d'   D ]J  }|j                  d|d(    d|d)    d|d*    d|d+    d|d,    d|d-    d|d.    d|d/    d|d0    d       L d1j                  |      d1z   S )2Nz# Save Selector Selection State rv   z!Selection buffer: context field `rw   rm   z`, values select one of rn   z branch-state slots.ro   z2| opcode | handler | name | summary | signatures |z| --- | --- | --- | --- | --- |ry   rj   okmissingz| `r   z` | `rh   z` | r   z | r   z |)r   z## State Valuesr   rx   z- `z`: z## Helper CaseszHelper jump table `rz   rs   z` accepts cases `rt   z`.zj| case | entry | target | callee | argument | state values | pattern | persistence/reset note | verified |z7| --- | --- | --- | --- | --- | --- | --- | --- | --- |ru   rS   rT   rU   rV   r3   r5   r4   r6   rX   
)r[   extendr~   join)r   linesrowr   r   labels         rH   markdownr   D  s=   )

/8I0JK\0]/^ _$$+,=$>|$L#MMac 	!"#34
<)E z" 
 !56TI
#k"#5^)<(=T#f+c9~c*R1	

 
LL,- 34::< .us5'UG,-.	LL


g&78FGGXY`arYs  uH  ZI  YJ  JL  	M
tA  ()'2 
#i.!s+@'A&B%KHXGY ZM"#4J'8C<N;Os9~c#&7"8!9SAS=T<UUWY	

 99Ud""rB   c                N   g }| d   D ]  }|d   rdnd}|j                  dt        j                  |d          dt        j                  |d          d	t        j                  |d
          dt        j                  |d          dt        j                  |       d        dj                  d | d   j	                         D              }g }| d   d   D ]  }|j                  dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          d	t        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d          d|d    d        dj                  dddd d!d"d#d$d%d&d'd(d)d*d+d,d-t        j                  | d.          d/d0t        j                  | d1   d2          d3| d1   d4    d5d-t        j                  | d1   d6          d/d7d8dj                  |       d9d:| d;d<t        j                  | d   d=          d>t        j                  | d   d?          d@dAd8dj                  |       d9dBdCdg      S )DNry   rj   r   r   z<tr><td><code>r   z</code></td><td><code>rh   z</code></td><td>r   z	</td><td>r   z
</td></tr>r   c              3     K   | ]6  \  }}d t        j                  |       dt        j                  |       d 8 yw)z
<li><code>z	</code>: z</li>N)htmlescape)re   r   r   s      rH   rg   zhtml_page.<locals>.<genexpr>{  s?      E5 T[['(	$++e2D1EUKs   <>rx   rz   ru   rS   rT   rU   rV   r3   r5   r4   r6   rX   r   z<!doctype html>z<html lang="en">z<head>z  <meta charset="utf-8">zF  <meta name="viewport" content="width=device-width, initial-scale=1">z.  <title>Save Selector Selection State</title>z	  <style>z^    body { margin: 24px; background: #101010; color: #eee; font: 14px system-ui, sans-serif; }zJ    table { border-collapse: collapse; width: 100%; margin-bottom: 24px; }zM    th, td { border: 1px solid #333; padding: 6px 8px; vertical-align: top; }z    th { background: #1d1d1d; }z    code { color: #f5d76e; }z
  </style>z</head>z<body>z(  <h1>Save Selector Selection State</h1>z  <p>rv   z</p>z+  <p>Selection buffer: context field <code>rw   rm   z</code>; values select one of rn   z branch-state slots.</p>ro   zp  <table><thead><tr><th>opcode</th><th>handler</th><th>name</th><th>summary</th><th>signatures</th></tr></thead>z	  <tbody>z</tbody></table>z  <h2>State Values</h2><ul>z</ul>z2  <h2>Helper Cases</h2><p>Helper jump table <code>rs   z</code> accepts cases <code>rt   z</code>.</p>z  <table><thead><tr><th>case</th><th>entry</th><th>target</th><th>callee</th><th>argument</th><th>state values</th><th>pattern</th><th>persistence/reset note</th><th>verified</th></tr></thead>z</body>z</html>)r[   r   r   r   r~   )r   r\   r   r   valueshelper_rowss         rH   	html_pager   n  s   Dz" 

 !56TI
S%567 8S%89: ;;;s6{+, -;;s9~./ 0;;z*+ ,	


 WW #$78>>@ F K()'2 
S^45 6S)>%?@A BS%567 8S%789 :;;s:/0 1;;s=123 4;;s9~./ 0;;s#4567 8)*+ ,
	

 99"P8hTW)&2
GG,-.d3
5dkk'J[B\]nBo6p5q  rP  QX  Yj  Qk  lx  Qy  Pz  zR  	S
G$567GHIJ$Oz
BGGDM?"23
%fXU3
<T[[QbIcdpIq=r<s  tP  QU  Q\  Q\  ]d  ev  ]w  xK  ]L  QM  PN  NZ  	[ 	K
BGGK())9:
9  rB   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       y )NT)parentsexist_okz"save_selector_selection_state.jsonFr7   )ensure_asciiindentr   zutf-8)encoding"save_selector_selection_state.html)mkdir
write_textjsondumpsr   )r   out_dirs     rH   write_outputsr     se    MM$M.33??

7q9D@ @  33??	'@R]d?erB   c                 v   t        j                  t              } | j                  dt        t
        dz         | j                  dt        t               | j                         }t        |j                  j                               }t        ||j                         t        d|j                  dz          y )N)descriptionz--exezHwanse2.exe)typedefaultz	--out-dirz'wrote save selector selection state -> r   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsr   rC   
read_bytesr   r   print)parserargsr   s      rH   mainr     s    $$9F
dD=4HI
$<DDHH//12G'4<<(	3DLLCg4g3h
ijrB   __main__)
rC   r|   rD   
list[dict]rE   intrF   r   returnr|   )rC   r|   rD   r   rE   r   r   z
int | None)rC   r|   rD   r   r   r   )rC   r|   r   dict)r   r   r   str)r   r   r   r   r   None)r   r   )r   
__future__r   r   r   r   rL   pathlibr   probe_exe_scene_tablesr   r   __file__resolver   r   r   r{   rZ   rY   rI   rN   r_   r   r   r   r   r   __name__rd   rB   rH   <module>r      s   8 "      > H~''*
Ul *P


, *a


. 8Z


0 4L


#> )>


Cvp    S[  SG  SG  SG %(I  jU  PJ  OJ  XK  ]J %SQ %HLIm`&4"(V'#T=@ 25 fk zF rB   