
    UHj@                    
   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ZddlZddl	m
Z
 ddlmZ ddlmZ ej                  j!                  d e ee      j'                         j(                               ddlmZ  ee      j'                         j.                  d   Zed	z  Zd
ZdZddhZdZg dZg dZddddddgZ d%dZ!d&dZ"d&dZ#d'dZ$d(dZ%d)dZ&d*dZ'd+dZ(d,dZ)d-dZ*d.d Z+d.d!Z,efd/d"Z-d0d#Z.e/d$k(  r e.        yy)1zCSummarize global selector-root patterns for inherited gate offsets.    )annotationsN)Counter)Path)Any)read_sections   outmap1_01amap2_02d      iT )zlocal-writer-for-gate-offsetszroute-specific-root-patternzcurrent-root-fallthrough-proof)zAlocal opcode 0x12/0x13 writer for selectionBuffer[0xe8] or [0xea]z_route-specific selector-root pattern instead of reader-only inherited offsets across many rootsz8runtime fallthrough proof for the current root gate rowsHwanse2.exez;global dword-aligned scan of save-selector gate offset rows)pathdescriptionzout/save_scene_selectors.jsonzHselector root labels and field-map overlap used to group the offset rowsc                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)r   fallbacks     I/home/exedev/hwanse/tools/summarize_save_selector_gate_offset_patterns.py	load_jsonr   0   s*    ;;=::dnngn677    c                    d| dS )N0x08x values    r   hex32r#   6       c{r   c                    d| dS )Nr   02xr    r!   s    r   hex8r'   :   r$   r   c                V    | D ]  }|j                  d      dk(  s|c S  t        d      )Nnamez.dataz Hwanse2.exe has no .data section)get
ValueError)sectionssections     r   data_sectionr.   >   s5     ;;v')N 7
88r   c                    |dz  }|dvry |dz	  dz  }|dz	  dz  }|t         vry | t        |       |t        |      |t        |      ddddd	|   |d
k(  rdndt        |      |t        |      |dk(  |dv dS )N   >                  r1   fillBranchStateTablereadSelectedStateAndBranchselectActiveStateSlotselectMatchingRuntimeSlot)r1   r2   r3   r4   r   primaryBranchStatesecondaryBranchStater2   >   r3   r4   )vavaHexr"   valueHexopcode	opcodeHex	operation
stateTablestreamPlus1HexselectionBufferOffsetselectionBufferOffsetHexisReaderisWriter)OFFSETSr#   r'   )r<   r"   r?   stream_plus_1stream_plus_2s        r   
decode_rowrK   E   s    T\F--aZ4'Mb[D(MG#r%L&\(.)-	

  /<q.@*F\}-!.$($7dNl*% r   c           	        i }| D ]  }|j                  d      }t        |t              s%|j                  |t	        |      g g g d      }|j                  d       d|j                  d       }||d   vr|d   j                  |       |j                  d      xs g D ]  }||d   vs|d   j                  |         |j                  d      xs g D ]  }||d   vs|d   j                  |          t        |      }||fS )	NselectedPointer)rootHexlabels	fieldMaps	linkedCnsgroup:slotrO   rP   rQ   )r*   
isinstanceint
setdefaultr#   appendsorted)		selectorscontextsrowrootcontextlabel	field_mapcnsrootss	            r   selector_contextrc   c   s1    "H 1ww()$$%% ;	
 777#$Acggfo%67))H$$U+-3 	7I 44$++I6	7 77;'-2 	1C'+..$++C0	1'1, 8E(?r   c                    t        j                  ||       dz
  }|dk  ry ||   }|dz   t        |      k  r||dz      nd}| |k\  ry |S )Nr   r   l    )bisectbisect_rightlen)r<   rb   indexr]   	next_roots        r   root_for_varj      sZ    r*Q.Eqy<D$)AIE
$:eai 
I	YKr   c                   t        |       }t        |      }t        |      \  }}| |d   |d   |d   z    }g }t        dt	        |      dz
  d      D ]  }t        j                  d||      d   }	|d   |z   }
t        |
|	      }|s5t        |
|      }|j                  |xs d      }|t        |      nd |d	<   t        |xs i j                  d
      xs g       |d<   t        |xs i j                  d      xs g       |d<   t        |xs i j                  d      xs g       d d |d<   t        |d   v xs t        |d   v |d<   |t        k(  |d<   |j                  |        |S )Nrawraw_sizer         z<Ir<   rN   rO   
rootLabelsrP   rootFieldMapsrQ   r5   rootLinkedCnsSamplerouteOverlapcurrentRoot)r   r.   rc   rangerg   structunpack_fromrK   rj   r*   r#   listSOURCETARGETCURRENT_ROOTrX   )exerZ   r,   r-   rb   r[   datarowsoffsetr"   r<   r\   r]   r^   s                 r   	scan_rowsr      s   S!H8$G&y1OE8wu~wu~
0CCDDD3t9q=!, ""4v6q9T]V#U#2u%,,tzr*(,(8tdI '-R!4!4X!>!D"EL#W]$7$7$D$JKO%)7=b*=*=k*J*Pb%QRTST%U!"$O(<<^#oJ^@^N!\1MC Kr   c                T   i }| D ]@  }|d   |k7  s|j                  d      s|j                  |d   g       j                  |       B g }t        |j	                         d       D ]  \  }}|d   }|j                  ||j                  d      xs g |j                  d      xs g t        |      t        d |D              t        d	 |D              t        d
 |D              t        d |D              |d d D cg c]  }|d   |d   |d   |d   |d   d c}d	        |S c c}w )NrD   rN   c                *    t        | d          | d   fS )Nr   r   )rg   )items    r   <lambda>z&root_rows_for_offset.<locals>.<lambda>   s    #dSTg,X\]^X_H` r   )keyr   rq   rr   c              3  ,   K   | ]  }|d    s	d  ywrF   r   Nr    .0r\   s     r   	<genexpr>z'root_rows_for_offset.<locals>.<genexpr>        ISZqI   
c              3  ,   K   | ]  }|d    s	d  ywrG   r   Nr    r   s     r   r   z'root_rows_for_offset.<locals>.<genexpr>   r   r   c              3  >   K   | ]  }|j                  d         yw)rt   Nr*   r   s     r   r   z'root_rows_for_offset.<locals>.<genexpr>   s     MC 7M   c              3  >   K   | ]  }|j                  d         yw)ru   Nr   r   s     r   r   z'root_rows_for_offset.<locals>.<genexpr>   s     K#sww}5Kr   r5   r=   r>   r@   rA   rB   r=   r>   r@   rA   rB   )	rN   rO   rP   rowCountreaderCountwriterCountrt   ru   
sampleRows)r*   rW   rX   rY   itemsrg   sumany)r   r   by_rootr\   	summariesroot_hex	root_rowsfirsts           r   root_rows_for_offsetr      sO   %'G ;&'619K3y>2.55c:; I%gmmo;`a )!ii-3?39rIIIIIIIIIM9MMKKK %Ra=	  !\ #J!$[!1!$[!1"%l"3	
 	, 	s   :D%c                   t        | |      }g }t        d |D              }t        t              D ]  }|D cg c]  }|d   |k(  s| }}t	        ||      }|D cg c]  }|j                  d      s| }	}|D cg c]  }|j                  d      s| }
}|j                  t        |      t        |      t        d |D              t        d |D              t        |      t        |
      t        |	      |	D cg c]  }|d   |d   |d	   |d
   |d   d c}|d d |
d
        t        d |D              }d}i dddt        dt        dt        t              dt        |      dt        d |D              d|dt        |      d|dddddt        d t        d!t         d"t        t               d#d$d%|S c c}w c c}w c c}w c c}w )&Nc              3  ,   K   | ]  }|d    s	d  ywr   r    r   s     r   r   z build_summary.<locals>.<genexpr>   s     =cS_=r   rD   ru   rt   c              3  ,   K   | ]  }|d    s	d  ywr   r    r   s     r   r   z build_summary.<locals>.<genexpr>        KS3z?qKr   c              3  ,   K   | ]  }|d    s	d  ywr   r    r   s     r   r   z build_summary.<locals>.<genexpr>   r   r   r=   r>   r@   rA   rB   r      )
	offsetHexr   r   r   	rootCountrouteOverlapRootCountcurrentRootRowCountcurrentRootRowstopRootsrouteOverlapRootsc              3  b   K   | ]'  }|j                  d       s|j                  d        ) yw)rN   Nr   r   s     r   r   z build_summary.<locals>.<genexpr>   s$     T#AS3779-Ts   //a  Offsets 0xe8 and 0xea are reader-only inherited gate offsets across many selector roots, not map1_01a-specific local setup. The global scan finds 981 reader rows and 0 opcode 0x12/0x13 writers for these offsets; current root 2:0 contributes only reader rows. This keeps the gate fallthrough blocked on runtime/inherited selection-buffer state rather than promoting map1_01a->map2_02d.scopez_global dword-aligned save-selector rows for gate offsets 0xe8 and 0xea grouped by selector rootsourcetargetcurrentRootHextotalRowCounttotalReaderCountc              3  ,   K   | ]  }|d    s	d  ywr   r    r   s     r   r   z build_summary.<locals>.<genexpr>   s     EcS_Er   totalWriterCountr   offsets
proofFoundFgateOffsetPatternProofFoundfailedGateOffsetPatternGateIdsmissingEvidenceevidenceRefsevidenceRefCountpromotionStatusblocked
conclusion)r   r   rY   rH   r   r*   rX   r'   rg   r   rz   r{   r#   r|   #FAILED_GATE_OFFSET_PATTERN_GATE_IDS$GATE_OFFSET_PATTERN_MISSING_EVIDENCE!GATE_OFFSET_PATTERN_EVIDENCE_REFS)r}   rZ   r   r   total_writersr   r\   offset_rowsrb   current_root_rowsroute_overlap_rootsroot_counterr   s                r   build_summaryr      si   S)$DG=T==M/ &*Usc2I.Jf.TsUU$T62,7RS377=;QSRR.3Ossww~7NsOOfK(KKKKKKKKU%()<%=#&'8#9 -	   !\ #J!$[!1!$[!1"%l"3	  cr
!4'
 	4 TTTL	h r& 	& 	%-	
 	T 	CETEE 	M 	S& 	7 	e 	&u 	)*M 	? 	9 	C AB  	9!" 	j# A VRO	 s)   GGG2G<GG<Gc                   ddd| d    ddd| d    d| d	    d
d| d    d
d| d    d| d    d| d    d| d    d| d    d
d| d    d
d| d    d| d   dddg}|j                  d | d   D               |j                  g d       |j                  d  | d!   D               |j                  g d"       |j                  d# | d$   D               |j                  g d%       | d&   D ]>  }|j                  d'|d(    d)|d*    d+|d,    d+|d-    d+|d    d+|d.    d+|d/    d0       @ | d&   D ]  }|j                  dd1|d(    d2dd3d4g       |d5   D ]p  }|j                  d'|d6    d)d7j                  |d8   d d9       xs d: d+d7j                  |d;   d d<       xs d: d+|d*    d+|d,    d+|d-    d+|d=    d+|d>    d0       r  |j                  d       d?j                  |      S )@Nz$# Save Selector Gate Offset Patterns zScope: r   .z
- route: `r   z -> r   `z- current root: `r   z- total rows: r   z- total readers: r   z- total writers: r   z- roots with rows: r   z- proofFound: `r   z - gateOffsetPatternProofFound: `r   z- promotion status: r   r   z## Failed Gatesc              3  (   K   | ]
  }d | d  yw)- `r   Nr    r   r   s     r   r   zmarkdown.<locals>.<genexpr>  s     U43tfAUs   r   )r   z## Missing Evidencer   c              3  &   K   | ]	  }d |   yw)z- Nr    r   s     r   r   zmarkdown.<locals>.<genexpr>  s     D2dVDs   r   )r   z## Evidence Refsr   c              3  8   K   | ]  }d |d    d|d      yw)r   r   z`: r   Nr    r   s     r   r   zmarkdown.<locals>.<genexpr>  s/       c&k]#c-012s   r   )r   z## Offset Summaryr   zW| offset | rows | readers | writers | roots | route-overlap roots | current-root rows |z1| --- | ---: | ---: | ---: | ---: | ---: | ---: |r   z| `r   z` | r   z | r   r   r   r   z |z## Offset `z` Top RootszM| root | labels | maps | rows | readers | writers | route overlap | current |z4| --- | --- | --- | ---: | ---: | ---: | --- | --- |r   rN   , rO   ro   -rP      rt   ru   
)extendrX   join)summarylinesr   r]   s       r   markdownr      s   .

''"#1%

WX&'tGH,=+>a@
G$456a8
123
G$6789
G$6789
gk234
',/02
*73P+Q*RRST
w'89:;


%E( 
LLU73S+TUU	LL  
 
LLD1B)CDD	LL  
 
LL >*  
LL   	" 
${#$Dj)9(:#d=>Q=RRUVZ[hViUjjmK !T*A%B$C3tLaGbFcceg	


 	" ${+,K8[B
 	 $ 	DLLd9o&d499T(^BQ5G+H+OC*PPS99T+.r23:s;3tJ?O>PPS&'s4+>*?s4CWBXX[\`an\o[pprt	 
LL99Ur   c                p   dj                  d | d   D              }dj                  d | d   D              }dj                  d | d   D              }dj                  d	 | d
   D              }g }| d
   D ]K  }dj                  d |d   D              }|j                  dt        j                  |d          d| d       M dj                  ddddt        j                  | d          dddt        j                  | d          dt        j                  | d          ddt        j                  | d          dd| d     d!d"| d#    d!d$| d%    d!d&| d'    d!d(| d)    dd*t        j                  | d+          d!d,d-t        j                  | d.          d/d0d| d,d1d| d,d2d| d,d3d4|dg|      S )5Nr   c              3  N   K   | ]  }d t        j                  |       d  yw)
<li><code></code></li>Nhtmlescaper   s     r   r   zhtml_page.<locals>.<genexpr>@  s+       T[[&'|4   #%r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li></li>Nr   r   s     r   r   zhtml_page.<locals>.<genexpr>D  s+       t{{4 !'r   r   c              3     K   | ]9  }d t        j                  |d          dt        j                  |d          d ; yw)r   r   z	</code>: r   r   Nr   r   s     r   r   zhtml_page.<locals>.<genexpr>H  sE       T[[V-.iCDV8W7XX]^s   ?Ar   r   c              3     K   | ]D  }d t        j                  |d          d|d    d|d    d|d    d|d    d|d    d|d	    d
 F yw)<tr><td><code>r   </code></td><td>r   	</td><td>r   r   r   r   r   
</td></tr>Nr   r   s     r   r   zhtml_page.<locals>.<genexpr>L  s       	[[k!234 5J  !M"# $M"# $K ! "+,- .)*+ ,	s   A
Ar   c              3  L  K   | ]  }d t        j                  |d          dt        j                  dj                  |d   dd       xs d       dt        j                  dj                  |d	   dd
       xs d       d|d    d|d    d|d    d|d    d|d    d  yw)r   rN   r   r   rO   Nro   r   r   rP   r   r   r   r   rt   ru   r   )r   r   r   )r   r]   s     r   r   zhtml_page.<locals>.<genexpr>Z  s      
 T)_56 7;;tyyh);<CDE F;;tyyk):2A)>?F3GH I
#$ %&' (&' ('( )&' (	
s   B"B$r   z<h2>Offset <code>r   z</code> Top Roots</h2><table><thead><tr><th>root</th><th>labels</th><th>maps</th><th>rows</th><th>readers</th><th>writers</th><th>route overlap</th><th>current</th></tr></thead><tbody>z</tbody></table>zV<!doctype html><meta charset="utf-8"><title>Save Selector Gate Offset Patterns</title>a  <style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;max-width:1280px;margin:24px auto}table{border-collapse:collapse;width:100%;margin:16px 0 28px}td,th{border:1px solid #444;padding:6px 8px;vertical-align:top}code{color:#9bd4ff}</style>z+<h1>Save Selector Gate Offset Patterns</h1>z
<p>Scope: r   z.</p>z<ul>z<li>route: <code>r   z -&gt; r   r   z<li>current root: <code>r   z<li>total rows: r   r   z<li>total readers: r   z<li>total writers: r   z<li>roots with rows: r   z<li>proofFound: <code>r   z<li>promotion status: r   z</ul>z<p>r   z</p>z<h2>Failed Gates</h2>z<h2>Missing Evidence</h2>z<h2>Evidence Refs</h2>z<h2>Offset Summary</h2>z<table><thead><tr><th>offset</th><th>rows</th><th>readers</th><th>writers</th><th>roots</th><th>route-overlap roots</th><th>current-root rows</th></tr></thead><tbody>)r   rX   r   r   )r   failed_gatesmissing_evidenceevidence_refsr   r,   r   rb   s           r   	html_pager   ?  sp   77 <= L ww -.  GG >* M ))  I& K H	" 
		 
 Z(
 
 	D,= >? @qg%'	

& 99b 	L5
T[[!12359
DKK(9:;74;;wW_O`CaBbbno
"4;;w7G/H#I"J,W
7?34E:
g&89:%@
g&89:%@
 45U;
 !6 7|D
 W5F-G!H IO
dkk',/016
|nE"#
 & 
}oU#! 	q34 
5  r   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_gate_offset_patterns.jsonF   )ensure_asciiindentr   r   r   'save_selector_gate_offset_patterns.html)mkdir
write_textr   dumpsr   )r   out_dirs     r   write_outputsr     se    MM$M.88DD

7q9D@ E  88DDYwEWbiDjr   c                    t        j                         } | j                  dt        t        dz         | j                  dt        t
        dz         | j                  dt        t
               | j                         }t        |j                  j                         t        |j                  g             }t        ||j                         t        d|j                  dz          y )	Nz--exer   )typedefaultz--selectorszsave_scene_selectors.jsonz	--out-dirzwrote gate offset patterns -> r   )argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr   r}   
read_bytesr   rZ   r   r   print)parserargsr   s      r   mainr    s    $$&F
dD=4HI
D#@[:[\
$<D$.."%G '4<<(	*4<<:c+c*d
efr   __main__)r   r   r   r   returnr   )r"   rV   r  str)r,   
list[dict]r  dict)r<   rV   r"   rV   r  zdict | None)rZ   r  r  z!tuple[list[int], dict[int, dict]])r<   rV   rb   z	list[int]r  z
int | None)r}   bytesrZ   r  r  r  )r   r  r   rV   r  r  )r}   r  rZ   r  r  r  )r   r  r  r  )r   r  r   r   r  None)r  r  )0__doc__
__future__r   r  re   r   r   rw   syscollectionsr   pathlibr   typingr   r   insertr  __file__resolveparentprobe_exe_scene_tablesr   r   r  r  rz   r{   rH   r|   r   r   r   r   r#   r'   r.   rK   rc   rj   r   r   r   r   r   r   r  __name__r    r   r   <module>r     s,   I "      
    3tH~--/667 8 0 H~''*
Ul		,' #
( $ T
 0a	% !89<80@7t@FHV 25 kg zF r   