
    UHjN                    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Zddlm	Z	 ej                  j                  d e e	e      j                         j                               ddlmZmZ  e	e      j                         j&                  d   Zedz  Zh dZd	hZd
dhZg dZg dZdddgddddgddddgddg dddddgddddgdgZd.dZd/d Z	 d0	 	 	 	 	 	 	 	 	 	 	 	 	 d1d!Zd2d3d"Z	 d4	 	 	 	 	 	 	 	 	 d5d#Z d6d$Z!d7d%Z"d8d&Z#	 	 	 	 	 	 	 	 	 	 	 	 	 	 d9d'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)>z>Scan EXE references for manual map-exit candidate coordinates.    )annotationsN)Path)offset_to_varead_sections   out>   .data.text.rdatar
   r   r	   )zstrict-source-coordinate-ownerztable-text-referenceztarget-map-control-pathzmanual-review-promotion-proof)zSstrict map1_01a event coordinate or source hotspot owning the packed coordinate hitzLtext/code reference proving the coordinate hit is part of a transition tablezKcontrol-flow path linking the source coordinate to the target map2_02d loadzEmanual disassembly/review proof promoting a diagnostic coordinate refHwanse2.exezpacked coordinate byte scansz.text/.data references)pathfieldszout/map_exit_candidates.jsonexitCandidatesblockedTargetCandidatesz!out/confirmed_route_blockers.jsonsourcetargetzout/event_transitions.json)maptargetsrecordVaz$out/scene_coordinate_candidates.jsonr   zcoordinate candidatesz#out/transition_extraction_gaps.jsongapc                z    t               }g }| D ])  }||v r|j                  |       |j                  |       + |S )N)setaddappend)valuesseenresultvalues       ?/home/exedev/hwanse/tools/summarize_map_exit_coordinate_refs.pyuniquer    ,   sE    5DF D=e	
 M    c                T    | D ]#  }|d   }|d   |d   z   }||cxk  r	|k  s|c S  % y )Nrawraw_size )sectionsoffsetsectionstartends        r   section_for_offsetr+   7   sI     enwz22F S N !
 r!   c                T   g }|D ]  }|d   |vr|d   }|d   |d   z   }	| ||	 }
t        dt        |
      dz
  d      D ]e  }t        j                  d|
|      d   }||cxk  r|k  sn +|j	                  |d   |d   |z   ||z   |d	|d
d       t        |      |k\  sa|c c S   |S )Nnamer#   r$   r         <Iva0x08x)r(   refVa
fileOffsetr   valueHex)rangelenstructunpack_fromr   )datar&   start_vaend_vasection_nameslimitrefsr(   	raw_startraw_endr#   indexr   s                r   
range_refsrD   @   s     D 6?-/EN	%.7:#669W%1c#hlA. 	E&&tS%8;E//KK"6? .'%/ s,  4yE!	& Kr!   c           
        ||dz  z
  }t        d||dz  z
        }t        t        |       ||dz  z         }g }t        ||d      D ]~  }	|	dz   t        |       kD  r |S t	        ||	      }
|
&t        j                  d| |	      d   }t        j                  d| |	      \  }}|j                  |
d|
d|	d|d||g|	|k(  d        |S )Nr/   r   r0   <HHr2   r3   )r1   vaHexr5   r6   u16
isHitDword)maxminr8   r7   r   r9   r:   r   )r;   r&   r'   beforeafteralignedr)   r*   itemsitem_offsetr1   r   lohis                 r   context_dwordsrS   _   s    
#G7VaZ'(E
c$i519,
-CEUC+ ?SY& L (K0:""4{;A>##E4=B"S]%U3K(8%0
 	  Lr!   c                   i }i }g }d}d}d}		 | j                  ||	      }
|
dk  rn|
dz   }	t        ||
      }||d   t        vr9|dz  }|j                  |d   d      dz   ||d   <   |
|d   z
  dz  dk(  }|r#|dz  }|j                  |d   d      dz   ||d   <   t	        |      |k\  rt        ||
      }|t        | |t        d|dz
        |dz   t              }t        | |t        d|dz
        |dz   t              }|j                  |d   |d|d|
d|
d	|||t        | ||
      d
	       %||t        t        |j                                     t        t        |j                                     |dS )Nr   r   r-   r#   r/       r2   r3   06x)	r(   r1   rG   r5   fileOffsetHexdwordAlignedtextRefsToWindowdataRefsToWindowcontext)totalalignedTotalsectionCountsalignedSectionCountssamples)findr+   SCAN_SECTIONSgetr8   r   rD   rJ   REF_TEXT_SECTIONSREF_DATA_SECTIONSr   rS   dictsortedrO   )r;   r&   patternmax_samplessection_countsaligned_countsr`   r\   aligned_totalsearchhitr(   rN   r1   	text_refs	data_refss                   r   scan_patternrq   w   s    &(N%'NGEMF
ii(7q$Xs3?gfo]B
*8*<*<WV_a*PST*Twv''1,1QM.<.@.@RS.TWX.XN76?+w<;&(C(:tXs1b4i/@"t)M^_	tXs1b4i/@"t)M^_	v"S]!#c^# ) )%dHc:

 
	+ B %f^%9%9%;<= $VN,@,@,B%C D r!   c                   |D ch c]$  }|j                  d      |j                  d      f& }}g }| D ]H  }|j                  d      }|j                  d      xs g D ]  }|j                  d      xs i }t        g |j                  d      xs g |j                  d      xs g       }	|	D 
cg c]!  }
||
f|v s|
|j                  d      xs g v r|
# }}
|D ]  }
t        |j                  d      t              rt        |j                  d	      t              sB|j	                  ||
|j                  d
      |d   |d	   |j                  d      |j                  d      |j                  d      d         K |S c c}w c c}
w )Nr   r   r   r   sampler   selectorTargetCandidatesxyside	standable	reviewUrltrialUrl)r   r   rw   ru   rv   rx   ry   rz   )rc   r    
isinstanceintr   )map_exit_candidatesroute_blockersrowblockersrows
source_rowr   	candidaters   r   r   blocked_targetss               r   exit_candidate_rowsr      s   BPQ3"CGGH$56QHQD) 
&#(89?R 	I]]8,2F -- 9:@b-- :;Ar G &F#x/6immLe>f>ljl3m O 
 * !&**S/37z&**UX/[^?_$$%MM&1!'K!8!*{!; )j 9	 		4 K9 Rs   )F0&Fc           
        |D ]  }|d   }|d   }||dz  z  }||dz  z  }t        | |t        j                  d||            }t        | |t        j                  d||            }	t        d ||	fD              }
|d|dd||d|dd|	d	|d
<   d|d<   |d   dk(  r|	d   dk(  rd|d<   n|
dk(  rd|d<   nd|d<   d|d<    |S )Nru   rv      rF   c              3  L   K   | ]  }|d    D ]  }t        |d            yw)r`   rY   N)r8   ).0scanrs   s      r   	<genexpr>z*attach_coordinate_scans.<locals>.<genexpr>   s0     p`den`opV\S(:!;<p<ps   "$r2   r3   )r   r6   xyyxpackedScansF
promotabler\   r   zno-exact-packed-coordinate-hitstatusz*coordinate-like-hit-without-table-text-refz@coordinate-like-hit-with-table-text-ref-needs-manual-disassemblyzExact coordinate bytes alone do not prove a map transition. Promotion still needs a strict source hotspot, event coordinate, or code path that links this tile to the target map.reason)rq   r9   packsum)r;   r&   r   r   ru   rv   xy_valueyx_valuer   r   text_ref_counts              r   attach_coordinate_scansr      s/    
HHR=R=$&++eQ*BC$&++eQ*BCp"bpp " #/  " #/ 
M "Lg;!7q 0<CMq HCM^CM^ 	H7
> Kr!   c                &    |D cg c]  }|j                  d       k(  s| }}|D cg c]  }||j                  d      xs g v r| }}t        |      t        |      t         fd|D              t         fd|D              dS c c}w c c}w )Nr   r   c              3  L   K   | ]  }|j                  d       k(  sd  ywr   r   Nrc   r   r   r   s     r   r   z!scene_evidence.<locals>.<genexpr>   s&     -mCTWT[T[\aTbflTla-m   $$c              3  L   K   | ]  }|j                  d       k(  sd  ywr   r   r   s     r   r   z!scene_evidence.<locals>.<genexpr>   s      'VcSWWU^v=U'Vr   )eventTransitionCountsourceEventRecordCountsourceCoordinateCandidateCountsourceExtractionGapCount)rc   r8   r   )r   r   event_transitionscoordinate_candidatesgapsr   source_eventstarget_eventss   `       r   scene_evidencer      s    $5RS69QSRMR !cggi(.B/ 	M  !$M 2"%m"4*--m;P-m*m$''VT'V$V	  Ss   B	B	 Bc           
         t        |       }t        | |t        ||            }|D ]  }t        |d   |d   |||      |d<    ddddt        t
        t        t        t              |d	S )Nr   r   sceneEvidencezJmanual-review-only; coordinate refs are diagnostic and never auto-promotedzblocked-diagnostic-onlyF)	promotionPolicypromotionStatus
proofFoundmapExitCoordinateRefProofFound!failedMapExitCoordinateRefGateIdsmissingEvidenceevidenceRefsevidenceRefCountr   )r   r   r   r   'FAILED_MAP_EXIT_COORDINATE_REF_GATE_IDS(MAP_EXIT_COORDINATE_REF_MISSING_EVIDENCE%MAP_EXIT_COORDINATE_REF_EVIDENCE_REFSr8   )	exer}   r~   r   r   transition_gapsr&   r   r   s	            r   build_summaryr      s     S!H"32EFY[i2jkD 
-MM! 
O
 h4*/-TC= EF
 
r!   c                    dj                  d | j                  di       j                         D              }| d    d|xs d dS )N, c              3  0   K   | ]  \  }}| d |   yw)=Nr%   )r   keyr   s      r   r   zscan_summary.<locals>.<genexpr>  s     `JC#aw'`s   r^   r\   z (-))joinrc   rO   )r   countss     r   scan_summaryr     sF    YY`$((?TV:W:]:]:_``F7m_Bv}oQ//r!   c                   | j                  d      xs g }ddddddd| j                  d       dd	| j                  d
       d| j                  d       ddj                  | j                  d      xs g        dd| j                  d       dddg}|j                  d | j                  d      xs g D               |j                  g d       | j                  d      xs g D ]K  }|j                  d|j                  d       ddj                  |j                  d      xs g        d       M |j                  g d       |D ]r  }|d   }|d   }|j                  d |d!    d"|d#    d"|d$    d"|d%    d|d&    d"t	        |d'          d"t	        |d(          d)|d*    d+|d,    d-|d.    d"|d/    d0       t |j                  g d1       |D ]  }|j                  d2|d!    d3|d#    d4|d$    d4|d%    d|d&    
       d5D ]  }|d   |   }|j                  d       |j                  d6| d7|d8    d9t	        |              |d:   sH|d:   d d; D ]u  }	dj                  d< |	d=   d d; D              xs d>}
dj                  d? |	d@   d d; D              xs d>}|j                  dA|	dB    dC|	dD    dE|	dF    dG|	dH    dI|
 dJ|        w  |j                  d        dKj                  |      S )LNr   z# Map Exit Coordinate Refs zIExact packed-coordinate scan for geometry-only map-exit trial candidates.zThese rows are diagnostic only. A coordinate-like byte sequence in `Hwanse2.exe` is not enough to promote a normal transition unless it is tied to a strict source hotspot/event coordinate and a target map code path.z- promotion status: `r   `z- proof found: r   z'- map-exit coordinate ref proof found: r   z)- failed map-exit coordinate ref gates: `,r   z- evidence refs: r   z## Missing Evidencec              3  &   K   | ]	  }d |   yw)z- Nr%   r   items     r   r   zmarkdown.<locals>.<genexpr>4  s     N2dVN   r   )r   z## Evidence Refsr   z| path | fields |z| --- | --- |r   z| `r   z` | `r   r   z` |)r   zO| source | target | side | tile | xy hits | yx hits | scene evidence | status |z3| --- | --- | --- | --- | ---: | ---: | --- | --- |r   r   z| r   z | r   rw   ru   rv   r   r   z
 | events=r   	, coords=r   , gaps=r   r   z |)r   z## Sample Hitsr   z###  ->  r   z- `z` packed value `r6   z`: r`   r/   c              3  &   K   | ]	  }|d      ywr6   Nr%   r   refs     r   r   zmarkdown.<locals>.<genexpr>V       %`#c*o%`r   rY   r   c              3  &   K   | ]	  }|d      ywr   r%   r   s     r   r   zmarkdown.<locals>.<genexpr>W  r   r   rZ   z  - r(   z `rG   z` file `rW   z
` aligned=rX   
 textRefs=
 dataRefs=
)rc   r   extendr   r   )summaryr   linesr   r   scansevidenceorderr   rs   ro   rp   s               r   markdownr   "  s   ;;v$"D$
S
 	b

,= >?qA
'++l345
1'++>^2_1`a
3CHHW[[Il=m=sqs4t3uuvw
GKK(:;<=

E  
LLN5F)G)M2NN	LL   {{>*0b Zs3776?+53778;L;RPR1S0TTWXYZ	LL  
  
M"'Xs3x=/S[MSXJaPSTWPXzY\E$K()\%+-F,G H567yJjAk@llst|  ~X  uY  tZ  Z]8}oR!	

 
LL+, tCM?$s8}oQs6{m1SQTXJVWX[\_X`Wabc! 	E}%e,DLLLL3ug%5d:6F5Gs<X\K]J^_`	?y/"1-  II%`HZA[\^]^A_%``gdg	 II%`HZA[\^]^A_%``gdg	6),-Rw/@P_I`Ha b%n56j:V_U`b	 	R  99Ur!   c                   | j                  d      xs g }g }g }|D ]  }|d   }|d   }|j                  dj                  ddt        j                  |d          ddt        j                  |d	          ddt        j                  t        |d
                dd|d    d|d    ddt        j                  t        |d                ddt        j                  t        |d                dd|d    d|d    d|d    ddt        j                  |d          ddg
             g }dD ]$  }||   }	|j                  d| d|	d    dt        j                  t        |	             d       |	d   sI|j                  d       |	d   d d  D ]  }
d!j                  d" |
d#   d d  D              xs d$}d!j                  d% |
d&   d d  D              xs d$}|j                  d't        j                  |
d(          d)|
d*    d+|
d,    d-|
d.    d/t        j                  |       d0t        j                  |       d        |j                  d1       ' |j                  dj                  d2t        j                  |d          d3t        j                  |d	          d4t        j                  t        |d
                d4|d    d|d    d5dg|d1              dj                  g d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLt        j                  t        | j                  dM                   dNt        j                  t        | j                  dO                   dPt        j                  dj                  | j                  dQ      xs g              dRt        j                  t        | j                  dS                   dT	dUdVdj                  dW | j                  dX      xs g D              dYdZdVdj                  d[ | j                  d\      xs g D              dYd]d^d_|d`da|dbdcdd      S )eNr   r   r   r   z<tr>z  <td>r   z</td>r   rw   ru   r   rv   r   r   z  <td>events=r   r   r   r   r   r   z</tr>r   z
<li><code>z</code> <code>r6   	</code>: </li>r`   z<ul>r/   r   c              3  &   K   | ]	  }|d      ywr   r%   r   s     r   r   zhtml_page.<locals>.<genexpr>|       )dc#j/)dr   rY   r   c              3  &   K   | ]	  }|d      ywr   r%   r   s     r   r   zhtml_page.<locals>.<genexpr>}  r   r   rZ   z<li>r(   z <code>rG   z</code> file <code>rW   z</code> aligned=rX   r   r   z</ul>z<h2>r   r   z</h2>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>Map Exit Coordinate Refs</title>z	  <style>zg    :root { color-scheme: dark; font-family: system-ui, sans-serif; background: #101010; color: #eee; }z&    body { margin: 0; padding: 24px; }z,    h1 { margin: 0 0 8px; font-size: 24px; }z0    h2 { margin: 26px 0 10px; font-size: 18px; }zM    p { margin: 0 0 14px; color: #bbb; max-width: 980px; line-height: 1.45; }zX    table { width: 100%; border-collapse: collapse; margin: 0 0 16px; font-size: 13px; }zi    th, td { border-bottom: 1px solid #303030; padding: 7px 8px; text-align: left; vertical-align: top; }zR    th { position: sticky; top: 0; background: #181818; z-index: 1; color: #ddd; }z    code { color: #d7f0ff; }z    li { margin: 4px 0; }z
  </style>z</head>z<body>z#  <h1>Map Exit Coordinate Refs</h1>z  <p>Exact packed-coordinate scan for geometry-only map-exit trial candidates. These rows are diagnostic only and do not promote normal transitions.</p>z$  <p><b>promotion status:</b> <code>r   z</code>; <b>proof found:</b> r   z5; <b>failed map-exit coordinate ref gates:</b> <code>r   z</code>; <b>evidence refs:</b> r   z.</p>z  <h2>Missing Evidence</h2>z  <ul>c              3  N   K   | ]  }d t        j                  |       d  yw)z    <li>r   N)htmlescaper   s     r   r   zhtml_page.<locals>.<genexpr>  s#     g$HT[[./u5gs   #%r   z  </ul>z  <h2>Evidence Refs</h2>c              3     K   | ]m  }d t        j                  t        |j                  d                   dt        j                  dj	                  |j                  d      xs g              d o yw)z    <li><code>r   r   r   r   r   N)r   r   strrc   r   r   s     r   r   zhtml_page.<locals>.<genexpr>  se      
  T[[SWWV_)=>?y{{499SWWX%6%<"=>?uF
s   A3A5r   z	  <table>z    <thead><tr><th>source</th><th>target</th><th>side</th><th>tile</th><th>xy hits</th><th>yx hits</th><th>scene evidence</th><th>status</th></tr></thead>z    <tbody>z    </tbody>z
  </table>z</body>z</html>r   )rc   r   r   r   r   r   r   )r   r   bodydetailr   r   r   sample_itemsr   r   rs   ro   rp   s                r   	html_pager   `  sW   ;;v$"DDF *
M"'IIS]34E:S]34E:SV%567u=S
!CH:U3\%+%>?@F\%+%>?@F)? @A8TtKuJvv}  G  Hb  c  ~d  di  jS]34E: 	
 ! 	-E<D*UG>$zBRASS\]a]h]hiuvzi{]|\}  ~C  !D  EI##F+"9obq1 
F $		)dVL^E_`babEc)d d khkI $		)dVL^E_`babEc)d d khkI '';;vi'89:'&/AR S&&,_&=%> ?##).#9": ;$$(KK	$:#;:dkkR[F\E] 
 ##G,!	-" 	IIt{{3x=12$t{{3x=7Q6RRSTXT_T_`cdghndo`pTqSrrstwx{t|s}}~  @C  DG  @H  I  IN  O  	 	
G*
V 99 222 	2 	#	2
 	Q2 	42 	2 	r2 	12 	72 	;2 	X2 	c2 	t2 	]2  	'!2" 	$#2$ 	%2& 	'2( 	)2* 	.+2, 	c-2. /t{{3w{{K\?];^/_.` a#{{3w{{</H+IJK LSXXgkk2U&V&\Z\]^_ `!!%S=O1P-Q!R SSX		Z/28 	&92: 	;2< 			g'++N_B`Bfdfgg=2> 	?2@ 	#A2B 	C2D 			 
 {{>28b
 	
E2N 	O2P 	Q2R 	eS2T 	U2V 
W2X 	Y2Z 	[2\ 
]2^ 	_2` 	a2b 	c2 2 2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map_exit_coordinate_refs.jsonF   )ensure_asciiindentr   utf-8encodingmap_exit_coordinate_refs.html)mkdir
write_textjsondumpsr   )r   out_dirs     r   write_outputsr     sd    MM$M...::

7q9D@ ;  ..::9W;MX_:`r!   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        d
z         | j                  dt        t        dz         | j                  dt        t        dz         | j                  dt        t               | j                         }t        |j                  j                         t        j                  |j                  j                  d            t        j                  |j                  j                  d            t        j                  |j                   j                  d            t        j                  |j"                  j                  d            t        j                  |j$                  j                  d                  }t'        ||j(                         t+        dt-        |d          d|j(                  dz          y )N)descriptionz--exer   )typedefaultz--map-exitszmap_exit_candidates.jsonz--route-blockerszconfirmed_route_blockers.jsonz--event-transitionszevent_transitions.jsonz--coordinate-candidatesz scene_coordinate_candidates.jsonz--transition-gapsztransition_extraction_gaps.jsonz	--out-dirr   r   zwrote r   z! map exit coordinate ref rows -> r   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsr   r   
read_bytesr   loads	map_exits	read_textr~   r   r   r   r   r   printr8   )parserargsr   s      r   mainr    s   $$9F
dD=4HI
D#@Z:Z[
*sEd?de
-D#H`B`a
1cLnFno
+$Fg@gh
$<D

4>>++W+=>

4&&00'0BC

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

4--777IJ

4''1171CDG '4<<(	F3wv'((I$,,YxJxIy
z{r!   __main__)r   	list[str]returnr  )r&   
list[dict]r'   r|   r  zdict | None)r   )r;   bytesr&   r  r<   r|   r=   r|   r>   zset[str]r?   r|   r  r  )r.   r/   )r;   r  r&   r  r'   r|   rL   r|   rM   r|   r  r  )   )
r;   r  r&   r  rh   r  ri   r|   r  rf   )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  rf   )r   r  r}   r  r~   r  r   r  r   r  r   r  r  rf   )r   rf   r  r   )r   rf   r  r   )r   rf   r   r   r  None)r  r  )+r  
__future__r   r  r   r   r9   syspathlibr   r   insertr   __file__resolveparentprobe_exe_scene_tablesr   r   r   r  r  rb   rd   re   r   r   r   r    r+   rD   rS   rq   r   r   r   r   r   r   r   r   r  __name__r%   r!   r   <module>r"     s   D "     
  3tH~--/667 8 > H~''*
Ul,I w' + ', ( 'EG_&`a+8HJc7de0Xx<PQ)5ST3G^?_`2uenM) % 
  	
   >8 	2
22 2 	2
 
2j@ F	#  "	
 &   
>0
;|aH 25 a|. zF r!   