
    UHj@                    F   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
  ee      j                         j                  d   Zedz  ZdZd	Zd
ZdZ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d'dZ d'dZ!efd(dZ"d)dZ#e$dk(  r e#        yy)*zAScan all save-scene selector map sets for recomposition patterns.    )annotationsN)Path)Any   outmap1_01amap2_02dz0:0z1:0z2:0c                p    | j                         s|S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)pathfallbacks     J/home/exedev/hwanse/tools/summarize_save_selector_recomposition_lattice.py	load_jsonr      s*    ;;=::dnngn677    c                L    | j                  d       d| j                  d       S )Ngroup:slot)getrows    r   selector_keyr      s&    gggq 122r   c                h    t        | D cg c]  }|j                  d      s| c}d       S c c}w )N	fieldMapsc                z    t        | j                  d      xs d      t        | j                  d      xs d      fS )Nr   r   r   )intr   r   s    r   <lambda>z$ordered_field_rows.<locals>.<lambda>#   s0    SWWW-23S9MA5NO r   key)sortedr   )	selectorsr   s     r   ordered_field_rowsr(       s.    !:SWW[%9:O :s   //c                R    | D ]  }t        |      |k(  s|c S  t        d| d      )Nz	selector z
 not found)r   
ValueError)rowsr%   r   s      r   find_selectorr,   '   s9     #J yZ0
11r   c                    t        |       | j                  d      | j                  d      xs g t        | j                  d      xs g       dS )NselectedPointerHexr    )selectorrootHexr    fieldMapCount)r   r   lenr   s    r   compact_selectorr3   .   sH     %77/0WW[)/RSWW[17R8	 r   c                   g }t        |       D ]  \  }}t        |j                  d      xs g       }| d | D ]  }t        |j                  d      xs g       }t        ||z
        }t        ||z
        }t	        |      dk(  sL|rO|j                  t        |      |j                  d      t        |      |j                  d      |d   t	        |      t	        |      d         |S )Nr    r   r.   r   )r/   r0   baseSelectorbaseRootHexaddedMapcurrentMapCountbaseMapCount)	enumeratesetr   r&   r2   appendr   )	r+   outputindexr   current_mapspreviousprevious_mapsaddedremoveds	            r   one_map_augmentation_rowsrD   7   s    Fo 
s377;/526Ve 	H[ 9 ?R@M<-78E]\9:G5zQw ,S 1"ww';<$0$:#+<<0D#E %a'*<'8$'$6 	  Mr   c                   g }t        |       D ]  \  }}t        |j                  d      xs g       }t        j                  | d | d      D ]  \  }}t        |j                  d      xs g       t        |j                  d      xs g       z  }||k(  sI|j                  t        |      |j                  d      t        |      t        |      |j                  d      |j                  d      t        |      d         |S )Nr       r.   )r/   r0   leftSelectorrightSelectorleftRootHexrightRootHexmapCount)r:   r;   r   	itertoolscombinationsr<   r   r2   )r+   r=   r>   r   r?   leftrightunions           r   exact_pair_union_rowsrQ   L   s    Fo 
s377;/526$11$v,B 	KD%-34s599[;Q;WUW7XXE$ ,S 1"ww';<$0$6%1%%8#'88,@#A$)II.B$C #L 1 	 Mr   c                   t        | |      }t        |j                  d      xs d      t        |j                  d      xs d      f}t        |j                  d      xs g       }| D cg c]C  }t        |j                  d      xs d      t        |j                  d      xs d      f|k  r|E }}g }t	        j
                  |d      D ]  \  }}	t        |j                  d      xs g       t        |	j                  d      xs g       z  }
||
k  sI|j                  t        |      t        |	      t        |
|z
        t        ||
z
        |
|k(  t        |
      d        t        |d       S c c}w )	Nr   r   r   r    rF   )rG   rH   extraVsCurrentmissingVsCurrentunionEqualsCurrentunionMapCountc                0    t        | d         | d   | d   fS )NrS   rG   rH   )r2   r   s    r   r#   z$covering_pair_rows.<locals>.<lambda>t   s$    3s3C/D+Es>GZ\_`o\p*q r   r$   )
r,   r"   r   r;   rL   rM   r<   r   r&   r2   )r+   r/   current_rowcurrent_orderr?   r   previous_rowsr=   rN   rO   rP   s              r   covering_pair_rowsr[   _   sg   h/K16Q7[__V=T=YXY9Z[M{{39r:L  %A&CGGFO,@q(AB]R 	M 
 F --mQ? 
eDHH[)/R03uyy7M7SQS3TT5 MM ,T 2!-e!4"()=">$*<%+?$@&+|&;!$U 
 &qrr#s   +AE4c           	         t        |      }| D cg c].  }|t        |j                  d      xs g       k  rt        |      0 c}S c c}w )Nr    )r;   r   r3   )r+   mapswantedr   s       r   selectors_containingr_   w   sM    YF S-344 	  s   3Ac                   t        | t              }t        | t              }t        | t              }t	        |j                  d      xs g       }t	        |j                  d      xs g       }t	        |j                  d      xs g       }t        | t              }t        |       D cg c]  }|d   t        k(  r| }	}t        |       D cg c]  }|d   t        k(  r| }
}t        |      t        |      t        |      t        | t        t              t        | t              t        | t              ||t        hz  k(  |||z  k(  |||z  k  t        ||z  |z
        t        |||z  z
        |	|
|dS c c}w c c}w )Nr    r/   )sourceSelectorpredecessorSelectorcurrentSelector"selectorsContainingSourceAndTargetselectorsContainingSourceselectorsContainingTarget"currentEqualsPredecessorPlusSource#currentEqualsSourcePredecessorUnion#sourcePredecessorUnionCoversCurrentsourcePredecessorUnionExtraMaps!sourcePredecessorUnionMissingMapscurrentOneMapAugmentationscurrentExactPairUnionscurrentCoveringPairUnions)r,   SOURCE_SELECTORPREDECESSOR_SELECTORCURRENT_SELECTORr;   r   r[   rD   rQ   r3   r_   SOURCETARGETr&   )r+   
source_rowpredecessor_rowrX   source_mapspredecessor_mapsr?   covering_pairsr   one_map_rowsexact_pair_rowss              r   current_summaryr{      s   t_5J#D*>?O&67Kjnn[17R8K?..{;ArB{{39r:L'.>?N06z?.. 	L 
 -T2z?.. 	O 
 +:6/@+K8.B4QW.X%9$%G%9$%G.:?OSYRZ?Z.[/;N^@^/_/;N^@^/_+1;AQ3QUa2a+b-3LKRbDb4c-d&2"1%3 s   (E3E8c                N   t        |       }t        |      }t        |      }t        |      }|d   }|D cg c]  }|d   	 c}t        gk(  }|d   }dt        |       dt         dt         dt         dt         d	t         d
dj                  |d         xs d d}	i dt        dt        dt        |      dt        |      dt        |      dt        |D ch c]  }|d   	 c}      dt        |      d|d|d|d|ddddddd|	d g d!d"|d d# S c c}w c c}w )$Nrd   r/   rn   zAcross z* selectors with field maps, only selector z contains both z and z!. Its map set equals predecessor z plus z7, while the source+predecessor union over-covers it by , rj   zno mapsz. This strengthens the selector-recomposition diagnosis, but it still does not prove runtime execution order, selected-pointer activation, or a strict map1_01a source hotspot.sourcetargetselectorWithFieldMapCountoneMapAugmentationCountexactPairUnionCountexactPairUnionSelectorCountroutePairSelectorCountroutePairOnlyCurrentSelectorcurrentoneMapAugmentationsexactPairUnionspromotionStatusblockedexecutionOrderProvenFstrictHotspotFound
conclusionremainingProofs)zOcapture a real selector 2:0 gameplay savedata or selected-pointer runtime tracezHprove VM/control-flow execution into selector 2:0 on the confirmed routez@find a strict map1_01a source coordinate or hotspot for map2_02dcurrentTopCoveringPairs
   )
r(   rD   rQ   r{   rq   r2   rr   rs   rp   join)
r'   r+   ry   rz   r   route_pair_selectorsr   route_pair_only_currentcurrent_covering_pairsr   s
             r   build_summaryr      s   i(D,T2L+D1Od#G"#GH:NO3s:OTdSee$%@A
#d)FGWFXXg(%x@AU@VV\]c\d e@99W>?@MI
N OS	S && 	$SY 	"3|#4	
 	s?3 	&s+WC
O+W'X 	!#&:"; 	'(? 	7 	| 	? 	9 	 	e 	j  	 
!* 	"#9#2#>+  P  ,Xs   DD"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t        |d          ddj                  |d         xs d d| d    dd| d    dd!dd"d#g}|d$   D ];  }|j                  d%|d&    d'|d(    d)|d*    d+dj                  |d,          d-	       = |j                  g d.       | d/   D ]X  }|j                  d%|d0    d'|d1    d)|d2    d+dj                  |d3         xs d4 d+dj                  |d5         xs d4 d-       Z |j                  g d6       | d7   D ]2  }|j                  d%|d&    d'|d8    d'|d9    d)|d:    d+|d;    d-       4 |j                  g d<       | d=   D ],  }|j                  d%|d&    d'|d0    d'|d1    d)|d>    d-	       . |j                  g d?       |j                  d@ | dA   D               |j                  d       dBj                  |      S )CNr   z%# Save Selector Recomposition Lattice z
- route: `r~   z -> r   `z- selectors with field maps: r   z- one-map augmentation rows: r   z- exact previous pair unions: r   z across r   z selector(s)z- route-pair selector count: r   z$- route pair only current selector: r   z*- current equals predecessor plus source: rg   z"- current exact pair union count: rm   z)- current source+predecessor extra maps: r}   rj   nonez- promotion status: `r   r   z## Route Pair Selectorsz&| selector | root | map count | maps |z| --- | --- | ---: | --- |rd   z| `r/   z` | `r0   z` | r1   z | r    z |)r   z## Current Covering Pair Unionsr   z3| left | right | equals current | extra | missing |z| --- | --- | --- | --- | --- |r   rG   rH   rU   rS   -rT   )r   z## One-Map Augmentationsr   z:| selector | base | added map | current maps | base maps |z!| --- | --- | --- | ---: | ---: |r   r5   r7   r8   r9   )r   z## Exact Pair Unionsr   z'| selector | left | right | map count |z| --- | --- | --- | ---: |r   rK   )r   z## Remaining Proofsr   c              3  &   K   | ]	  }d |   yw)z- N .0items     r   	<genexpr>zmarkdown.<locals>.<genexpr>
  s     D2dVDs   r   
)r2   r   r<   extend)summaryr   linesr   s       r   markdownr      sJ   i G/

WX&'tGH,=+>a@
'0K(L'MN
'0I(J'KL
(1F)G(HQXYvQwPx  yE  	F
'0H(I'JK
.w7U/V.WX
4W=a5b4cd
,S9Q1R-S,TU
3DIIgFg>h4i4sms3tu
(9 :;1=

!
0$'E* ;< 
#j/"%I'7tC<P;QQTUYU^U^_bcn_oUpTqqst	

 
LL   01 
#n%&eC,@+AcJ^F_E``cyy-./637s499SI[E\;];dad:eegi	


 
LL   ,- 
#j/"%N(;'<E#j/ARRV$%&c#n*=)>bB	


 
LL   () 
#j/"%N(;'<E#oBVAWW[\_`j\k[llno	

 
LL01	LLD1B)CDD	LL99Ur   c                j     d   d?fd}d? fd}d? fd}d? fd}dj                  d  d   D              }d	j                  g 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't        d(          d)t        j                  d*j                  d+         xs d,       d-d.t        j                   d/          d0d1d2 |       d3d4d5 |       d3d6d7 |       d3d8d9 |       d3d:d;| d<d=d>d      S )@Nr   c                 :    dj                  d  d   D              S )Nr   c              3     K   | ]g  }d t        j                  |d          dt        j                  |d          d|d    dt        j                  dj                  |d                d		 i y
w)<tr><td><code>r/   </code></td><td><code>r0   </code></td><td>r1   	</td><td>r}   r    
</td></tr>Nhtmlescaper   r   r   s     r   r   z5html_page.<locals>.route_pair_rows.<locals>.<genexpr>  s      
 S_56 7S^45 6'( );;tyy[)9:;< =	
s   A-A/rd   r   )r   s   r   route_pair_rowsz"html_page.<locals>.route_pair_rows  s)    yy 
 CD
 
 	
r   c                 :    dj                  d  d   D              S )Nr   c              3  B  K   | ]  }d t        j                  |d          dt        j                  |d          d|d    dt        j                  dj                  |d         xs d	       dt        j                  dj                  |d
         xs d	       d  yw)r   rG   r   rH   r   rU   r   r}   rS   r   rT   r   Nr   r   s     r   r   z3html_page.<locals>.covering_rows.<locals>.<genexpr>  s      	
 S%89: ;S%9:; <+,- .;;tyy-=)>?F3GH I;;tyy-?)@AHSIJ K	
s   BBr   r   r   s   r   covering_rowsz html_page.<locals>.covering_rows  s)    yy 	
 89	
 	
 		
r   c                 :    dj                  d  d   D              S )Nr   c              3     K   | ]^  }d t        j                  |d          dt        j                  |d          dt        j                  |d          d|d    d|d    d	 ` y
w)r   r/   r   r5   r7   r   r8   r   r9   r   Nr   r   r   s     r   r   z2html_page.<locals>.one_map_rows.<locals>.<genexpr>*  s      	
 S_56 7S%89: ;S_56 7()* +~&' (	
s   A$A&r   r   r   s   r   ry   zhtml_page.<locals>.one_map_rows)  s)    yy 	
 45	
 	
 		
r   c                 :    dj                  d  d   D              S )Nr   c           
   3     K   | ]X  }d t        j                  |d          dt        j                  |d          dt        j                  |d          d|d    d	 Z yw)	r   r/   r   rG   rH   r   rK   r   Nr   r   s     r   r   z0html_page.<locals>.exact_rows.<locals>.<genexpr>6  sx      
 S_56 7S%89: ;S%9:; <z?# $	
s   AA r   r   r   s   r   
exact_rowszhtml_page.<locals>.exact_rows5  s)    yy 
 01
 
 	
r   r   c              3  N   K   | ]  }d t        j                  |       d  yw)z<li>z</li>Nr   r   s     r   r   zhtml_page.<locals>.<genexpr>@  s#     adDT!2 359as   #%r   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">z4  <title>Save Selector Recomposition Lattice</title>a    <style>body{font-family:system-ui,sans-serif;background:#111;color:#eee;margin:24px}table{border-collapse:collapse;width:100%;max-width:1280px}td,th{border:1px solid #333;padding:6px 8px;text-align:left;vertical-align:top}th{background:#1f1f1f}code{color:#9bd4ff}</style>z</head>z<body>z.  <h1>Save Selector Recomposition Lattice</h1>z  <p>route <code>r~   z -&gt; r   z#</code>; selectors with field maps r   z; one-map augmentations r   z; exact previous pair unions r   z; route pair selector count r   z#; route pair only current selector r   z; promotion <code>r   z</code>.</p>z-  <p>current equals predecessor plus source: rg   z"; current exact pair union count: rm   z); current source+predecessor extra maps: r}   rj   r   z.</p>z  <p>r   z</p>z  <h2>Route Pair Selectors</h2>ze  <table><thead><tr><th>selector</th><th>root</th><th>map count</th><th>maps</th></tr></thead><tbody>z  </tbody></table>z'  <h2>Current Covering Pair Unions</h2>zx  <table><thead><tr><th>left</th><th>right</th><th>equals current</th><th>extra</th><th>missing</th></tr></thead><tbody>z   <h2>One-Map Augmentations</h2>z  <table><thead><tr><th>selector</th><th>base</th><th>added map</th><th>current maps</th><th>base maps</th></tr></thead><tbody>z  <h2>Exact Pair Unions</h2>zf  <table><thead><tr><th>selector</th><th>left</th><th>right</th><th>map count</th></tr></thead><tbody>z  <h2>Remaining Proofs</h2>z  <ul>z</ul>z</body>z</html>)returnstr)r   r2   r   r   )r   r   r   ry   r   proof_itemsr   s   `     @r   	html_pager     sx   i G	




	
 ''agN_F`aaK99 ### 	# 	#	#
 	Q# 	?# 	\# 	# 	# 	9# GH-.ggh6G5HHkls  uP  mQ  lR  Rj  kr  sL  kM  jN  Nk  ls  tI  lJ  kK  Kg  ho  pH  hI  gJ  Jm  nu  vT  nU  mV  Vh  ip  qB  iC  hD  DP  	Q# 8@d8e7f  gI  JM  NU  Vn  No  Jp  Iq  qZ  [_  [f  [f  gk  gp  gp  qx  yZ  q[  g\  gf  `f  [g  Zh  hm  	n# GL12348# 	*# 	p#  	!#" 	##$ 	2%#& 	C'#( 	)#* 	+#, 	+-#. 	J/#0 	1#2 	3#4 	'5#6 	q7#8 	9#: 	;#< 	&=#> U#?#@ 	A#B 	C#D 	E# # #r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       y )
NT)parentsexist_ok(save_selector_recomposition_lattice.jsonFrF   )ensure_asciiindentr   r   r   )mkdir
write_textr   dumps)r   out_dirs     r   write_outputsr   g  sD    MM$M.99EE

7q9D@ F r   c                 6   t        j                  t              } | j                  dt        t
               | j                         }t        t        |j                  dz  g             }t        ||j                         t        d|j                  dz          y )N)descriptionz	--out-dir)typedefaultzsave_scene_selectors.jsonz-wrote save selector recomposition lattice -> r   )argparseArgumentParser__doc__add_argumentr   OUT
parse_argsr   r   r   r   print)parserargsr   s      r   mainr   o  sz    $$9F
$<DIdll5P&PRTUVG'4<<(	9$,,Is:s9t
uvr   __main__)r   r   r   r   r   r   )r   dictr   r   )r'   
list[dict]r   r   )r+   r   r%   r   r   r   )r   r   r   r   )r+   r   r   r   )r+   r   r/   r   r   r   )r+   r   r]   r   r   r   )r+   r   r   r   )r'   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )%r   
__future__r   r   r   rL   r   pathlibr   typingr   __file__resolver   ROOTr   rr   rs   ro   rp   rq   r   r   r(   r,   r3   rD   rQ   r[   r_   r{   r   r   r   r   r   __name__r   r   r   <module>r      s    G "       H~''*
Ul		  832*&s0D&RAHUp 25 w zF r   