
    JjK                    n   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m	Z	 ej                  j                  d e e	e      j                         j                               ddlmZmZmZ ddlmZmZmZ  e	e      j                         j0                  d   Zedz  Zg d	Zg d
Zg dZdg dddg dddg ddgZd#dZd$dZ d%dZ!	 	 	 	 	 	 	 	 	 	 d&dZ"d'dZ#d(dZ$d)d*dZ%	 	 	 	 	 	 	 	 	 	 	 	 d+dZ&	 	 	 	 	 	 	 	 	 	 	 	 d,dZ'	 	 	 	 	 	 	 	 d-dZ(d.dZ)d/dZ*d/dZ+edfd0d Z,d1d!Z-e.d"k(  r e-        yy)2zJSummarize why the current save-selector frontier is a scene/resource list.    )annotationsN)Path)find_cns_stringsread_sectionsva_to_offset)HANDLER_TABLE_VAdword_atsection_name_for_va   out)zresource-branch-not-field-mapzselector-adjacency-onlyzstrict-hotspot-missing)z7direct field-map branch target for map1_01a -> map2_02dz?strict or confirmed transition backing selector scene adjacencyz,strict map1_01a source coordinate or hotspot)zSprove the 0x00542b0c branch targets a field-map transition, not a CNS resource gatezJback selector scene-record adjacency with a strict or confirmed transitionz3find a strict map1_01a source coordinate or hotspotHwanse2.exe).textz.rdataz.data)pathfieldsz(out/save_selector_frontier_branches.json)branchStepssourcetargetzout/scene_manifest.json)maprecordVa
sceneIdHextilesetsc                     | sy t        | d      S )N   )intvalues    G/home/exedev/hwanse/tools/summarize_save_selector_scene_list_context.py	parse_hexr   3   s    ub>    c                    | d| dS d S )N0x08x r   s    r   hex32r$   9   s    $0Rc{:d:r   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   z<Ir   )r   lenstructunpack_from)exesectionsvaoffsets       r   read_u32r.   =   s?    (B'F~!c#h.dC033r   c                D   |ddiS ||v ry||   }d||j                  d      xr% |j                  d      xr |j                  d       |j                  d      xr% |j                  d      xr |j                  d        dS t        ||      dt        |      d	S dd
iS )Nkind
unreadablecnsr   z.cnsmap_)r0   r2   
isFieldMap
isResourcepointer)r0   targetVaHexscalar)
startswithendswithr   r$   )r*   r+   stringsr   names        r   
value_kindr=   D   s     }%%u~//%0jT]]65JjSWSbSbciSjOj#u5o$--:OoX\XgXghnXoTop	
 	
 He$0!%,??Hr   c                    t         |dz  z   }t        | ||      }|t        ||      nd }d|dt        |      t        |      ||dk(  dS )Nr&   r!   02xr   )	opcodeHex
entryVaHexhandlerVaHexhandlerSectionisCodeHandler)r   r	   r
   r$   )r*   r+   opcodeentry_va
handler_vasections         r   handler_kindrI   Y   sc    &1*,H#x2J;E;Q!(J7W[G&&Hoj)! G+ r   c                `   g }| D ]  }|j                  d      |k7  r|j                  d      }t        |t              s:|j                  |t	        |      |j                  d      |j                  d      |j                  d      xs g |j                  d      d        t        |d       S )	Nr   r   r   r   resourceSource)r   recordVaHexr   r   r   rK   c                    | d   S )Nr   r#   )items    r   <lambda>z&manifest_records_for.<locals>.<lambda>v   s    j)9 r   )key)get
isinstancer   appendr$   sorted)manifestmap_namerowsrow	record_vas        r   manifest_records_forrZ   f   s    D 775>X%GGJ'	)S)! +775>'',/
+1r!gg&67
 	 $9::r   c                    |y | D cg c]'  }|d   |k\  r|d   |z
  |k  ri |dd|d   |z
  di) }}|r|d   S d S c c}w )Nr   distanceHexr!   xr   r#   )recordsr,   limitrecord
candidatess        r   nearest_record_afterrb   y   s    	z *#z(:R(?5(H 	D6C=Bvj'9B'>q&A"BCJ 
 ':a=0D0s   ,?c           
         g }t        ||d      D ]j  }t        | ||      }|t        |      t        |      d|dz  dt        | |||      d}|dz  dk  rt	        | ||dz        |d<   |j                  |       l |S )Nr&   r!      r?   )vaHexvalueHex
lowByteHex	valueKindhandlerCandidate)ranger.   r$   r=   rI   rS   )	r*   r+   r;   startendrW   r,   r   rX   s	            r   dword_windowrm      s     DE3" h+=2Yeut|C01#C7EB	
 DLT!&23%$,&OC"#C Kr   c                J   t        ||j                  d            }t        ||j                  d            }g }|j                  d      xs g D ]5  }t        |j                  d            }	t        |j                  d            }
t        |j                  d            }|
t        | ||
      nd }t	        | |||      }t	        | |||      }|t        | ||dz        ni }t        ||	      }t        ||	      }|j                  i |||j                  d      d	k(  xr |j                  d
      du t        |      |||j                  d      du |||j                  d      d	k(  r|j                  d
      du r|rdndd	       8 t        |j                  d      xs g D cg c]7  }t        |j                  d            t        |j                  d            9 c}xs |r|d   d   ndg      }|r|d   d   n|dz   }t        t        |dz   |dz         |dz         }i d|j                  d      d|j                  d      dddddddt        dt        dt        dt        dt        t              ddd dd|d!|d"|d#t!        | ||||      d$d%S c c}w )&Nr   r   r   streamVaHexfallthroughVaHexbranchTargetHexrd   r0   r2   r5   TrD   z!resource-gate-before-scene-record
unresolved)	branchTargetValueKindbranchTargetIsResourcefallthroughValueHexfallthroughValueKindfallthroughHandlerCandidatefallthroughLooksExecutablenearestSourceRecordAfterBranchnearestTargetRecordAfterBranchclassificationr   r      P   i  
proofFoundFsceneListResourceGateProofFoundstrictTransitionProofFoundfailedSceneListGateIdsmissingEvidenceremainingProofsevidenceRefsevidenceRefCountpromotionStatusblockedselectorOnlySceneListsourceRecordstargetRecordswindow
conclusionaX  The save-selector branch around the current frontier gates CNS resource loading before field-map scene records. For map1_01a -> map2_02d, the 0x00542b0c branch target is cara_01.cns, not the map2_02d field map. map2_02d appears later as another scene record in the same selector list, so this remains scene-list evidence, not a tile transition.)rZ   rQ   r   r.   r=   rI   rb   rS   r$   minmaxFAILED_SCENE_LIST_GATE_IDSSCENE_LIST_MISSING_EVIDENCESCENE_LIST_REMAINING_PROOFSSCENE_LIST_EVIDENCE_REFSr'   rm   )r*   r+   r;   edgerU   source_recordstarget_recordsstepsstep	branch_vafallthrough_va	target_vafallthrough_valuetarget_kindfallthrough_kindfallthrough_handlersource_recordtarget_recordwindow_startfirst_target_record
window_ends                        r   branch_contextr      sS    *(DHHX4FGN)(DHHX4FGNE'-2 dhh}56	"488,>#?@dhh'89:	GUGaHS(NCgk hC%c8W>OPWhWtl3:Kd:RSz|,^YG,^YG 

%0&1oof&=&F&p;??[gKhlpKp#():#;$4+>*=*A*A/*RVZ*Z.;.; ??6*e3OOL1T9! 4 "
 	8 8<8O8USU  	IYbcgckcklyczY{  ZH488M*	+  	I 	D-;N1j)CL <J.+J7|^bObS,t3\D5HI<Z_K_`J$((8$$((8$ 	e 	*5	
 	%e 	!"< 	6 	6 	0 	C 89 	9 	  	u 	 	  	,sHg|ZP!" 	G%  	Is   #J J c                   t        |       }t        | |      }|D cg c]  }t        | ||||       }}t        d |D        |r|d   ni       }ddddt        t
        t        t        t        t              t        |      ||dddS c c}w )Nc              3  p   K   | ].  }|j                  d       dk(  r|j                  d      dk(  r| 0 yw)r   map1_01ar   map2_02dNrQ   .0rX   s     r   	<genexpr>z build_summary.<locals>.<genexpr>   s9      	
wwx J.37783D
3R 	
s   46r   zFsave-selector scene-list/resource context for confirmed-route blockersFr   zSave-selector field-map adjacency is kept out of normal gameplay because the nearby branches resolve to resource gates and scene-record lists rather than strict source coordinates.)scoper~   r   r   r   r   r   r   r   rowCountrW   currentFrontierr   r   )	r   r   r   nextr   r   r   r   r'   )r*   frontier_branchesrU   r+   r;   r   rW   currents           r   build_summaryr      s    
 S!HsH-G & 	sHgtX>D  	
	
 QRG Z+0&+"<660 89I"$f s   Bc                    | sydj                  | j                  d      xs g       xs d}| j                  d       d| j                  d       d| j                  d       d| S )N-,r   rL    r   r   )joinrQ   )r`   r   s     r   format_recordr   	  sg    xx

:.4"5<Hjj'(&**U*;)<Afjj>V=WWXYaXbccr   c                	   dd| d   dddg}| j                  d      xs g D ]  }|j                  d      xs g D ]i  }|j                  d      xs i }|j                  d	      xs i }|j                  d
      xs$ |j                  d      xs |j                  d      }|j                  d       d|j                  d
      xs$ |j                  d      xs |j                  d       }|j                  d|j                  d       d|j                  d       d|j                  d       d|j                  d       d|j                  d       d| d| dt        |j                  d             dt        |j                  d             d|j                  d       d       l  t        d | j                  d      xs g D              s|j                  d       |j	                  g d        | j                  d!      xs i }|j	                  d"|j                  d       d#d$|j                  d       d#d%|j                  d&       d'|j                  d(       d)|j                  d*       d+d,j                  |j                  d-      xs g       xs d. d/t        |j                  d0      xs g        d1|j                  d2      xs | j                  d2       d3|j                  d4       d#d5|j                  d6       d|j                  d      xs ddd7dg       |j	                  d8 |j                  d0      xs g D               |j	                  g d9       |j                  d:      xs g d d; D ]  }	|	j                  d<      xs i }
|	j                  d=      xs i }|
j                  d
      xs$ |
j                  d      xs |
j                  d      }|j                  d>      xs d. d|j                  d?      xs d. }|j                  d@|	j                  dA       d|	j                  dB       d|	j                  dC       dD| d| d        |j                  d       dEj                  |      S )FNz"# Save Selector Scene List Context r   z| source | target | branch | target value | fallthrough value | nearest source record | nearest target record | classification |z1| --- | --- | --- | --- | --- | --- | --- | --- |rW   r   rs   rv   r2   r7   r0   ru   r   z| r   z | r   z | `ro   z` `	conditionz` | `rq   z` ry   rz   r{   z |c              3  >   K   | ]  }|j                  d         yw)r   Nr   r   s     r   r   zmarkdown.<locals>.<genexpr>(  s     K#sww}%Ks   z!| - | - | - | - | - | - | - | - |)r   z## Current Frontier Windowr   r   z- source: ``z- target: `z- proof found: r~   z(- scene-list resource gate proof found: r   z!- strict transition proof found: r   z- failed scene-list gates: z, r   r   z- missing evidence count: r   z- evidence refs: r   z- promotion status: `r   z- selector-only scene list: r   z## Missing Evidencec              3  &   K   | ]	  }d |   yw)z- Nr#   r   rN   s     r   r   zmarkdown.<locals>.<genexpr>=  s     N2dVNs   )r   z:| va | value | low byte | value kind | handler candidate |z| --- | --- | --- | --- | --- |r   0   rh   ri   rB   rC   z| `re   rf   rg   z` | 
)rQ   rS   r   anyextendr   r'   )summarylinesrX   r   r   	fall_kindtarget_text	fall_textr   wordr0   handler	kind_texthandler_texts                 r   markdownr     s   ,

 	K;E {{6"(b GGM*0b 	D((#:;ArK!78>BI%//%0mKOOM4RmVaVeVeflVmK881231Y]]55I  6CY]][hMi  6Cmvmzmz  |B  nC  5D  E  LLSWWX&'s3778+<*= >HH]+,C0E/F GHH./0;-s+Stxx8X/Y!Z [[^ *J!KLMSQUQYQYZjQkPlln	p	 KV1D1JKK89	LL78kk+,2G	LL
gkk(+,A.
gkk(+,A.
'++l345
27;;?`3a2bc
+GKK8T,U+VW
%dii<T0U0[Y[&\&c`c%de
$S5F)G)M2%N$OP
GKK(:;^w{{K]?^_`
,= >?qA
&w{{3J'K&LM
L!'R

 " 
LLN5F)G)M2NN	LL  
 X&,"cr2 
xx$*((-.4"HHUORtxx'>R$((6BR	!++n5<=Qw{{K[?\?c`c>de$((7#$E$((:*>)?uTXXlE[D\\`k\N".	

 
LL99Ur   c           %        g }| j                  d      xs g D ]d  }|j                  d      xs g D ]H  }|j                  d      xs i }|j                  d      xs i }|j                  d      xs$ |j                  d      xs |j                  d      }|j                  d       d	|j                  d      xs$ |j                  d      xs |j                  d       }|j                  d
t        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  |j                  d      xs d       dt        j                  t	        |             dt        j                  |       dt        j                  t        |j                  d                   dt        j                  t        |j                  d                   dt        j                  |j                  d      xs d       d       K g | j                  d      xs i }dj                  d |j                  d      xs g D              }	dj                  d |j                  d      xs g D              }
g }|j                  d      xs g d d D ]S  }|j                  d       xs i }|j                  d!      xs i }|j                  d      xs$ |j                  d      xs |j                  d      }|j                  d"      xs d d	|j                  d#      xs d }|j                  d$t        j                  |j                  d%      xs d       d&t        j                  |j                  d'      xs d       d&t        j                  |j                  d(      xs d       d)t        j                  t	        |             dt        j                  |       d       V d*j                  d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;t        j                  | j                  d<      xs d       d=d>|j                  d?       d@|j                  dA       dB|j                  dC       dDt        |j                  d      xs g        dE|j                  dF      xs | j                  dF       dGt        j                  t	        |j                  dH      xs | j                  dH                   dIdJ|	 dKdL|
 dKdMdNdj                  |      xs dO dPdQd;t        j                  |j                  d<      xs d       d=dRdNdj                  |      xs dS dPdTdUdg      S )VNrW   r   rs   rv   r2   r7   r0   ru   r   z<tr><td>r   r   z	</td><td>r   z</td><td><code>ro   z</code><br>r   rq   ry   rz   r{   z
</td></tr>r   r   c              3  N   K   | ]  }d t        j                  |       d  ywz<li>z</li>Nhtmlescaper   s     r   r   zhtml_page.<locals>.<genexpr>e  s(      ,0$t{{4 !'   #%r   c              3  N   K   | ]  }d t        j                  |       d  ywr   r   r   s     r   r   zhtml_page.<locals>.<genexpr>h  s#     g$T[[./u5gr   r   r   r   rh   ri   rB   rC   z<tr><td><code>re   z</code></td><td><code>rf   rg   z</code></td><td>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">z1  <title>Save Selector Scene List Context</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; }z9    th { background: #1d1d1d; position: sticky; top: 0; }z    code { color: #f5d76e; }z
  </style>z</head>z<body>z+  <h1>Save Selector Scene List Context</h1>z  <p>r   z</p>z  <p>proofFound <code>r~   z/</code>; sceneListResourceGateProofFound <code>r   z*</code>; strictTransitionProofFound <code>r   z$</code>; missingEvidenceCount <code>z</code>; evidence refs <code>r   z </code>; promotion status <code>r   z</code>.</p>z&  <h2>Failed Scene-List Gates</h2><ul>z</ul>z  <h2>Missing Evidence</h2><ul>z  <table><thead><tr><th>source</th><th>target</th><th>branch</th><th>target value</th><th>fallthrough value</th><th>nearest source record</th><th>nearest target record</th><th>classification</th></tr></thead>z	  <tbody>z&<tr><td colspan="8">No rows.</td></tr>z</tbody></table>z"  <h2>Current Frontier Window</h2>zx  <table><thead><tr><th>va</th><th>value</th><th>low byte</th><th>value kind</th><th>handler candidate</th></tr></thead>z&<tr><td colspan="5">No rows.</td></tr>z</body>z</html>)rQ   rS   r   r   strr   r   r'   )r   rW   rX   r   r   r   r   r   r   failed_gatesmissingwindow_rowsr   r0   r   r   r   s                    r   	html_pager   P  s   D{{6"(b GGM*0b 	D((#:;ArK!78>BI%//%0mKOOM4RmVaVeVeflVmK88$9:;1Y]]5=Q  >KU^UbUbcpUq  >Ku~  vC  vC  DJ  vK  =L  MIKK{{3778#4#;<= >{{3778#4#;<= >![[-)@)GCHIUYU`U`aeaiaijuava}z}U~T @![[2C)D)KLM[Y]YdYdehiteuYvXw x{{9-. /{{=:Z1[#\]^ _{{=:Z1[#\]^ _{{488,<#=#DEF G		$ kk+,2G77 4;KK@X4Y4_]_ L ggg'++N_B`BfdfggGKX&,"cr2 
xx$*((-.4"HHUORtxx'>R$((6BR	!++n5<=Qw{{K[?\?c`c>deTXXg%6%=#>? @TXXj%9%@SAB CTXXl%;%BsCD E;;s9~./ 0;;|,- .	

 99"P;hTWC&5
GKK5;<=TB
 \!: ;;jkrkvkv  xY  lZ  k[  [E  FM  FQ  FQ  Rn  Fo  Ep  pT  UX  Y`  Yd  Yd  ev  Yw  Y}  {}  U~  T  \  ]d  ]h  ]h  i{  ]|  ]_  @G  @K  @K  L^  @_  \`  `@  AE  AL  AL  MP  QX  Q\  Q\  ]n  Qo  QQ  sz  s~  s~  P  sQ  MR  AS  @T  T`  	a
0eD
)'%8 	[
BGGDMO%OPP`a,
GKK5;<=TB 	C
BGGK(V,VWWgh
;  r   c                   |j                  dd       |dz  }|j                  t        j                  | dd      dz   d	       |9|j                  j                  dd       |j                  t        |       d	       |S )
NT)parentsexist_okz%save_selector_scene_list_context.jsonF   )ensure_asciiindentr   utf-8encoding)mkdir
write_textjsondumpsparentr   )r   out_dirhtml_outjson_outs       r   write_outputsr     s    MM$M.@@H

7q9D@   dT:Ig.AOr   c            	        t        j                  t              } | j                  dt        t
        dz         | j                  dt        t        dz         | j                  dt        t        dz         | j                  d	t        t               | j                  d
t               | j                         }t        |j                  j                         t        j                  |j                  j                  d            t        j                  |j                  j                  d                  }t!        ||j"                  |j$                        }t'        d|        y )N)descriptionz--exer   )typedefaultz--frontier-branchesz$save_selector_frontier_branches.jsonz--scene-manifestzscene_manifest.jsonz	--out-dirz
--html-out)r   r   r   z*wrote save selector scene-list context -> )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsr   r*   
read_bytesr   loadsr   	read_textscene_manifestr   r   r   print)parserargsr   r   s       r   mainr     s   $$9F
dD=4HI
-D#HnBno
*sEZ?Z[
$<
40D

4))33W3EF

4&&00'0BCG
 WdllDMMBH	6xj
ABr   __main__)r   
str | Nonereturn
int | None)r   r  r  r  )r*   bytesr+   
list[dict]r,   r   r  r  )
r*   r  r+   r  r;   dict[int, str]r   r  r  dict)r*   r  r+   r  rE   r   r  r  )rU   r  rV   r   r  r  )i   )r^   r  r,   r  r_   r   r  dict | None)r*   r  r+   r  r;   r  rk   r   rl   r   r  r  )r*   r  r+   r  r;   r  r   r  rU   r  r  r  )r*   r  r   r  rU   r  r  r  )r`   r	  r  r   )r   r  r  r   )r   r  r   r   r   zPath | Noner  r   )r  None)/r   
__future__r   r   r   r   r(   syspathlibr   r   insertr   __file__resolver   probe_exe_scene_tablesr   r   r   summarize_script_handler_tabler   r	   r
   r   r   r   r   r   r   r   r   r$   r.   r=   rI   rZ   rb   rm   r   r   r   r   r   r   r   __name__r#   r   r   <module>r     s   P "     
  3tH~--/667 8 P P Z Z H~''*
Ul 
 
  .
 ;5
 *?  ;4	  	
 
*
;&1	  	
 
 0C	CC C 	C
 C 
CL$	$!$ $ 
	$Nd=@FR 25d 
C" zF r   