
    DMjqc                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ  e	e      j                         j                  d   Zedz  Zed	z  Zed
z  Zedz  Zedz  Zedz  Zedz  Zedz  Zedz  ZdZdZdZddddddddddddddddZd.dZd/d Zd0d!Zd1d"Z d2d3d#Z!d4d$Z"d5d%Z#d6d&Z$d7d'Z%d8d(Z&d9d)Z'd:d*Z(d;d+Z)d<d,Z*e+d-k(  r e*        yy)=ai  Promote HUD runtime surface handles that can be tied to static CNS evidence.

The HUD-open runtime trace does not name CNS files directly.  It does expose
surface handles, source rectangles, destination rectangles, and call sites.
This report only promotes handles when the runtime draw pattern also matches a
static EXE/CNS consumer that was already grounded.
    )annotationsN)Counterdefaultdict)Path)Any   outwebz"runtime_trace_hud_open_review.jsonzcns_precut_catalog_review.jsonzmenu_right_panel_ui_review.jsonz%status_menu_ui_expression_review.jsonz#surface_wrapper_catalog_review.jsonz&hud_runtime_surface_handle_review.jsonz&hud_runtime_surface_handle_review.html
0x100979C0
0x1004C438
0x10001330   zleft status/menu panel)regionIndexname   zright top menu panel   zright submenu panelzbottom left HUD panel   zbottom right info panel)z0,0,416,352z416,0,640,96z416,96,640,352z0,352,416,480z416,352,640,480c                V    t        j                  | dd      S t        |       d      S )N T)quote)htmlescapestr)values    0tools/build_hud_runtime_surface_handle_review.pyhr   ,   s%    ;;U]r$GGE
$GG    c                p    | j                         si S t        j                  | j                  d            S )Nutf-8encoding)existsjsonloads	read_text)paths    r   	read_jsonr'   0   s*    ;;=	::dnngn677r   c                   | j                  d      xs i }i d| j                  d      d| j                  d      d| j                  d      d| j                  d      d| j                  d      d| j                  d      d| j                  d      d	| j                  d	      d
| j                  d
      d| j                  d      d| j                  d      d| j                  d      d| j                  d      d|j                  d      d|j                  d      d|j                  d      d|j                  d      |j                  d      | j                  d      dS )Nparsedseqframepcr   sourcedestinationsourceLabeldestinationLabelsourceResourceNamedestinationResourceNamesourceSurfaceSizedestinationSurfaceSizeflagssrcRectdstRectdstPointsrcColorKeyappliedColorKeydetail)r:   r;   get)rowr)   s     r   compact_rowr?   6   s   WWX$"Fswwu~! 	cggdm 		
 	#''(# 	sww}- 	sww}- 	CGG$67 	cgg&:; 	"377+D#E 	SWW%89 	!#''*B"C 	! 	6::i( 	6::i(  	FJJz*!" 	vzz-0#$ "::&78''(#' r   c                   t        t              }| D ]H  }|t        |j                  d            t        |j                  d            f   j	                  |       J g }t        |j                               D ]  \  \  }}}t        d |D              }t        d |D              }|j	                  ||t        |      t        t        d |D                    t        t        d |D                    t        t        d |D                    |j                  d      |j                  d      |d d	 D cg c]  }t        |       c}d
	        |S c c}w )Nr-   r.   c              3  d   K   | ](  }|j                  d       xs i j                  d       * yw)r)   r6   Nr<   .0r>   s     r   	<genexpr>z!summarize_pair.<locals>.<genexpr>V   s)     ZSWWX.4"99)DZs   .0c              3     K   | ]N  }|j                  d       xs i j                  d      xs$ |j                  d       xs i j                  d       P yw)r)   r8   r7   Nr<   rB   s     r   rD   z!summarize_pair.<locals>.<genexpr>W   sS      
 WWX$"))*5a#''(:K:Qr9V9VW`9aa
s   AAc              3  >   K   | ]  }|j                  d         yw)r,   Nr<   rB   s     r   rD   z!summarize_pair.<locals>.<genexpr>`   s     (L3(L   c              3  >   K   | ]  }|j                  d         yw)r   Nr<   rB   s     r   rD   z!summarize_pair.<locals>.<genexpr>a   s     *Ps3776?*PrG   c              3  >   K   | ]  }|j                  d         yw)r5   Nr<   rB   s     r   rD   z!summarize_pair.<locals>.<genexpr>b   s     +RCGGG,<+RrG   
      )	r-   r.   countpcCounts
nameCountsflagsCounts
srcRectTopdstTop
sampleRows)r   listr   r=   appendsorteditemsr   lendictmost_commonr?   )	rowsgroupedr>   r	   r-   r.   	pair_rows	src_rects
dst_valuess	            r   summarize_pairr_   O   s?   ;Ft;LG SSWWX&'SWW]-C)DEFMMcRS !#C,27==?,C 
(yZPYZZ	 
 
 

 	

 *Y (L)(L!LM"7*Pi*P#PQ#G+R	+R$RS'33B7$004;DRa=IC{3/I
	

& J Js   1Ec                *   g }| D ]  }|r:t        |j                  d            j                         |j                         k7  r?|r:t        |j                  d            j                         |j                         k7  r{|j                  |        |S )Nr-   r.   )r   r=   lowerrT   )rZ   r-   r.   r	   r>   s        r   rows_forrb   k   s    
C c#''(+,224F3sww}56<<>+BSBSBUU

3 Jr   c                j    | j                  d      xs g D ]  }|j                  d      |k(  s|c S  i S )NrZ   assetr<   )catalogrd   r>   s      r   cns_rowrf   v   s<    {{6"(b 777u$J Ir   c           
         t        | D ch c]/  }t        |j                  d      xs i j                  d            1 c}      S c c}w )Nr)   r6   )rU   r   r=   )rZ   r>   s     r   rect_setrh   }   s9    DQS3)/R44Y?@QRRQs   4Ac                f   i }| j                  d      xs g D ]  }t        |j                  d      xs d      }|s%|j                  |i       j                  ||j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d
      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      d       
 | j                  d      xs g D ]  }t        |j                  d      xs d      }|s$|j                  |i       j                  ||j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d
      |j                  d      |j                  d      |j                  d      |j                  d      d        |S )NsurfaceSnapshotRowssurfaceHandler   r/   r1   r3   resourceNameresourceTypearchiveOffset
imageWidthimageHeightcolorKey	pixelHashpaletteHashnotes)rk   r/   r1   r3   rl   rm   rn   ro   rp   rq   rr   rs   rt   resourceSurfaceLabelRows)rk   r/   r1   r3   rl   rm   rn   ro   rp   rq   rr   rt   )r=   r   
setdefaultupdate)tracelabelsr>   handles       r   handle_labelsr{      s   (*Fyy./52 
SWW_-34&"%,,!'"ww}5&)gg.B&C%(WW-@%A # 7 # 7!$!9!ggl3"ww}5GGJ/ WW[1"ww}5)	
	
* yy34: 
SWW_-34&"%,,!'"ww}5&)gg.B&C%(WW-@%A # 7 # 7!$!9!ggl3"ww}5GGJ/ WW[1)	
	
( Mr   c                    | j                  d      xs i g}|D cg c]&  }|j                  di       j                  d      s%|( }}|rt        |d       d   S |r|d   S i S c c}w )NtracesruntimeSignalsresourceSurfaceLabelCountc                d    t        | j                  d      xs i j                  d      xs d      S )Nmanifestexported_atr   r   r=   )rx   s    r   <lambda>zchoose_trace.<locals>.<lambda>   s+    eii
6K6Qr5V5VWd5e5kik1l r   )key)r=   rU   )runtimer}   rx   labeleds       r   choose_tracer      sr    [["*rdF"(mEII6F,K,O,OPk,lumGmg#lmnpqq6":'R' ns   &A$A$c                &    | j                  |i       S Nr<   )ry   rz   s     r   resource_label_forr      s    ::fb!!r   c                >   t        | dt              }t        | dt              }t        | t        t              }t        | t        t              }t        | dt              }t        | dt              }g }	dD ]"  }
|	j	                  t        | |
t                     $ t        |d      }|j                  d      xs t        |d      }dd	d
dg ddd|j                  d      |j                  d      |j                  d      |j                  d      dt        |d      t        |      t        |      |d d D cg c]  }t        |       c}ddddd
dg ddd|j                  d      |j                  d      |j                  d      |j                  d      g ddt        |d      t        |      t        |      |D cg c]  }t        |       c}ddt        dd
dddgd t        it        |      t        |      |d d! D cg c]  }t        |       c}dd"t        d#d
dd$d%gt        |      t        |      |d d& D cg c]  }t        |       c}dd't        d(d)d*d+d,gt        |      |d d& D cg c]  }t        |       c}d-d'dd.d/d0g d1t        |d      t        |      t        |      |d d! D cg c]  }t        |       c}dd2dd3d/d0g d4t        |d      t        |      t        |      |d d! D cg c]  }t        |       c}dd2d5d6d7d0g d8t        |d9      t        |d:      t        |d;      dt        |	      t        |	      |	d d! D cg c]  }t        |       c}dd2g}|S c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )<N
0x1025D108
0x1025D038
0x102113D0
0x10211458)
0x10211280
0x10211284
0x10211288statuswindowCatalogwindowzwindow.cns source surfaceHighzruntime-confirmed)zRThe handle only draws into the HUD text/window surface 0x100979C0 in this capture.zThe first draw sequence is the exact 16x16 window-template top row: left cap 0,0,16,16, repeated middle 16,0,32,16, right cap 32,0,48,16.ziThe static catalog classifies window.cns as a template/region system rather than a raw source-rect table.z
window.cnsmodetemplateCountregionCount)rd   cnsr   r   r   r      )rowCountsourceRectsrR   )rz   classification
confidence	promotionwhyPromotedstaticEvidenceresourceLabelruntimeEvidencezstatus.cns source surfacez,runtime-confirmed-with-static-consumer-match)zcThe handle draws only status-sized cursor/header/arrow rectangles into the HUD text/window surface.zQRuntime rect 96,0,112,16 at 440,72 matches the grounded top-menu cursor consumer.z\Runtime rect 192,16,224,32 at 600,328 matches the grounded detail-page right-arrow consumer.zWRuntime rect 32,32,64,48 at 600,104 matches the grounded MP/header status.cns consumer.zjThe static CNS catalog classifies status.cns as descriptor +4 source-rect table 0x0047e3e0 with 23 frames.z
status.cnsbestTableStartVaHexbestFrameCount)zPmenu_right_panel_ui_review: status.cns #3 top menu cursor at x=440+index*32,y=72zUmenu_right_panel_ui_review: status.cns #15/#16/#17/#18 page arrows at x=424/600,y=328z?menu_right_panel_ui_review: status.cns MP header at x=600,y=104)rd   r   r   r   r   r   knownConsumersz!assembled HUD text/window surfacezHwindow.cns/status.cns draws and GDI TextOutA writes target this surface.zUThe surface is copied to frame surface 0x1004C438 by the known HUD region rectangles.regions   )rz   r   r   r   r   r   r   z!frame/backing composition surfacez4Field/map/sprite/HUD sources draw onto this surface.zHThe completed 640x480 frame is copied to 0x10001330 every visible frame.r   )rz   r   r   r   r   r   zscreen/backbuffer destinationMediumzruntime-observed-destinationzKThe handle receives repeated full-frame 640x480 Blt copies from 0x1004C438.zThe trace does not include DirectDraw surface creation labels, so the exact front/back role remains runtime-observed rather than statically named.)incomingRowsrR   z=title.cns source surface used by field/title-tile draw helperzHigh filename / Medium rolez$resource-label-confirmed-role-review)zYThe new runtime trace labels this handle as title.cns with archive offset and pixel hash.z]It draws 16x16 source rects directly to the frame surface before the HUD surface is composed.zThe CNS filename is now confirmed; the engine role is still review-grade because this field HUD trace does not explain why title.cns is used as a tile-like source here.)rz   r   r   r   r   r   r   z9btl_efc.cns source surface used by 48x64 color-keyed draw)z[The new runtime trace labels this handle as btl_efc.cns with archive offset and pixel hash.zZIt draws a 48x64 color-keyed rectangle to the frame surface in the same field HUD capture.zThe CNS filename is confirmed; the exact field role of this battle-effect surface needs a targeted draw-context trace if it matters.z$0x10211280 / 0x10211284 / 0x10211288z'labeled actor-like draw source surfacesz%High filenames / Medium semantic role)zqThe new runtime trace labels 0x10211280 as cara_sm1.cns, 0x10211284 as cara_rs1.cns, and 0x10211288 as title.cns.z]All three draw the same 48x64 frame-sized rect to the frame surface with separate color keys.zThe file labels are confirmed. The title.cns entry in an actor-like slot is intentionally left as a role question, not overwritten by older assumptions.r   r   r   )rb   TEXT_SURFACEFRAME_SURFACESCREEN_SURFACEextendrf   r=   r   rW   rh   r?   REGION_BY_RECT)	draw_rowsre   ry   window_rowsstatus_rowshud_to_frameframe_to_screentitle_tile_rowseffect_rowsactor_like_rowsrz   status_catalogwindow_catalogr>   rZ   s                  r   build_handle_rowsr      s'   9lLAK9lLAKI|]CLy-HOy,FO9lMBKO< Kx	6=IJK Wh/N[[1OWWh5ON #9 , "#&**62(,,X6!/!3!3O!D-11-@ 0E,'4;Fs;KLC{3/L '	
4 #9 G "#&**62(,,X6'5'9'9:O'P"0"4"45E"F# 0E,'4;FGC{3/G 5	
B #A ,Zg
 >  -'5;G;LMC{3/M 	
& $A ,FZ
  0'8;J2A;NOC{3/O 	
  %="7] e
 !$O 4;J2A;NOC{3/O 	
 #]7?
 0E0'8;J3B;OPC{3/P 	
$ #Y7?
 0E,'4;Fs;KLC{3/L 	
$ =GA? 1F0F0F  0'8;J3B;OPC{3/P 	
W`DB KU M@ H$ N P P" Q" M* Qs0   4M7=M<N N4N9N>N N"c                    t        t              } t        t              }t        t              }t        t              }t        t
              }t        |       }|j                  d      xs g }|j                  d      xs g }t        |      }t        |||      }	|	D 
cg c]"  }
dt        |
j                  dd            v s!|
$ }}
|	D 
cg c]"  }
dt        |
j                  dd            vs!|
$ }}
t        d |D              }t        |      }ddd	t        t        t              j                  t                     t        t        j                  t                     t        t        j                  t                     t        t        j                  t                     t        t        j                  t                     t        t
        j                  t                     d
|j                  d      |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      xs i j                  d      di dt#        |      dt#        |      dt#        |j                  d      xs g       dt#        |j                  d      xs g       dt#        |      dt#        |      dt#        |      dt#        |      dt$        dt&        dt(        dt+        |      d|j                  d      xs i j                  d      d |j                  d!      d"|j                  d!      d#|j                  d!      |	||g d$g d%d&S c c}
w c c}
w )'NdrawSurfaceRowstextDrawRows	confirmedr   r   c              3  P   K   | ]  }t        |j                  d                yw)r.   Nr   rB   s     r   rD   zbuild.<locals>.<genexpr>|  s     QCGGM$: ;Qs   $&r   z(hwanse-hud-runtime-surface-handle-reviewz*hud-window-status-surface-handles-promoted)runtimeTrace
cnsCatalogmenuRightPanelstatusMenuExpressionsurfaceWrapperCatalogtraceId
eventCount
frameRanger   	jsonlPathr   r   )r   r   r   r   r   
exportedAtrj   ru   resourceLabelCountsurfacePairCountpromotedHandleCountpendingHandleCounttextSurfaceframeSurfacescreenSurfacetextDestinationswindowCatalogModer   r   menuRightPanelStatusr   statusMenuStatussurfaceWrapperStatus)zThe new HUD-open trace directly labels loaded CNS/resource surfaces, so window.cns/status.cns and previously pending field/actor-like source handles now have confirmed filenames.zThe trace still separates filename confirmation from semantic role confirmation; title.cns and btl_efc.cns appearances in field HUD drawing remain role-review items.zqMenu navigation, deeper submenu writes, and dynamic HP/MP/EXP numeric glyph producers still need targeted traces.)zcCapture menu navigation after HUD open: arrows, Enter, ESC with mem-write and object-stream-change.zeCapture changing HP/MP/EXP values or equipment selection preview to identify numeric glyph producers.zIf the title.cns actor-like draw role matters, capture a short pre-HUD field idle trace with active object/resource-slot labels.)versionkindr   r-   inputsrx   summary
handleRowssurfacePairsresourceSurfaceLabelsconclusionsnextTraceRequirements)r'   RUNTIME_REVIEWCNS_CATALOGMENU_RIGHT_PANELSTATUS_MENUSURFACE_CATALOGr   r=   r{   r   r   r   r_   r   __file__relative_toROOTrW   r   r   r   rX   )r   re   
menu_rightstatus_menusurface_catalogrx   r   	text_rowsry   handle_rowsr>   promotedpendingtext_destinationssurface_pairss                  r   buildr   m  s>   'G$G+,JK(K0O!E		+,2I		.)/RI5!F#Iw?K*[kSVXAY=Z.Z[H[)^s[CGGKY[D\@]-]s^G^QyQQ"9-M :>d8n0067 : :4 @Ak55d;<!"2">">t"DE$'(?(?(E$F%()D)DT)J%K
 yy+))L1))L1#ii(89;/ 99Z06B;;MJ

s9~
C	N
 "3uyy1F'G'M2#N
 'EII6P,Q,WUW(X	

 !#f+
 M 2
 "3x=
 !#g,
 <
 M
 ^
 %6 7
  '++o">"D"!I!I&!Q
 #JNN8$<
  9
  #O$7$7$A!
$ "%!'

"
]3 3 \^s   "M( M(
"M--M-c                   | d   }g d}dD ];  }|j                  dt        |       dt        |j                  |             d       = |j                  d       |j                  d       | d	   D ]4  }t        |j                  d
            }|dk(  rdn|dk(  rdnd}dj	                  d |j                  d      xs g D              }|j                  d      xs i }dj	                  d |j                  d      xs g D              }	t        j                  |dd      }
|j                  dt        |j                  d             dt        |j                  d             d| dt        |       dt        |j                  d              d| d!t        |
       d"|	 d#       7 |j                  d$       |j                  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/        |j                  d$       |j                  d0       | d1   D ]   }|j                  d2t        |       d3       " |j                  d4       | d5   D ]   }|j                  d2t        |       d3       " |j                  d6       t        j                  d7j	                  |      d7z   d89       y ):Nr   )z%<!doctype html><meta charset="utf-8">z0<title>HUD Runtime Surface Handle Review</title>a  
<style>
body{font-family:system-ui,-apple-system,Segoe UI,sans-serif;margin:24px;background:#f5f3ef;color:#1f2328}
a{color:#145f8f}.top{display:flex;gap:10px;flex-wrap:wrap;margin:12px 0 20px}
.chip{display:inline-block;padding:6px 10px;border:1px solid #bbb;border-radius:999px;background:#fff;text-decoration:none;color:#222}
.panel{background:#fff;border:1px solid #d7d0c4;border-radius:8px;padding:16px;margin:14px 0;box-shadow:0 1px 3px #0001}
h1{margin:0 0 8px;font-size:26px}h2{font-size:18px;margin:0 0 12px}
table{border-collapse:collapse;width:100%;font-size:13px}th,td{border:1px solid #ddd;padding:7px;vertical-align:top}
th{background:#ece5d7;text-align:left}code{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}
.ok{color:#0f6b2f;font-weight:700}.warn{color:#916400;font-weight:700}.low{color:#8a2d2d;font-weight:700}
.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:10px}.small{font-size:12px;color:#555}
details{margin-top:6px}
</style>
z*<h1>HUD Runtime Surface Handle Review</h1>u   <p>HUD-open runtime trace의 DirectDraw surface handle을 기존 정적 CNS 근거와 대조해 승격 가능한 것만 정리합니다.</p>a)  <div class="top"><a class="chip" href="index.html">index</a><a class="chip" href="runtime_trace_hud_open_review.html">runtime trace</a><a class="chip" href="hud_runtime_static_bridge_review.html">runtime bridge</a><a class="chip" href="../out/hud_runtime_surface_handle_review.json">json</a></div>z9<section class="panel"><h2>Summary</h2><div class="grid">)r   r   rj   ru   r   r   r   r   r   r   r   r   r   r   r   z<div><b>z</b><br><code>z</code></div>z</div></section>z<section class="panel"><h2>Handle Classification</h2><table><tr><th>handle</th><th>classification</th><th>confidence</th><th>promotion</th><th>evidence</th><th>runtime rects</th></tr>r   r   r   okr   warnlowz<br>c              3  2   K   | ]  }t        |        y wr   r   )rC   items     r   rD   zwrite_html.<locals>.<genexpr>  s     P4qwPs   r   r   c              3  :   K   | ]  }d t        |       d  yw)z<code>z</code>Nr   )rC   rects     r   rD   zwrite_html.<locals>.<genexpr>  s     c$fQtWIW5cs   r   Fr   ensure_asciiindentz<tr><td><code>rz   z</code></td><td>r   z</td><td class="z">z	</td><td>r   z$<details><summary>raw</summary><pre>z</pre></details></td><td>z
</td></tr>z</table></section>z<section class="panel"><h2>Surface Pair Summary</h2><table><tr><th>source</th><th>destination</th><th>count</th><th>PCs</th><th>src rect top</th><th>dst top</th></tr>r   r-   z</code></td><td><code>r.   rL   z</td><td><code>rM   rP   rQ   z</code></td></tr>z/<section class="panel"><h2>Conclusions</h2><ul>r   z<li>z</li>z)</ul><h2>Next Trace Requirements</h2><ul>r   z</ul></section>
r   r    )	rT   r   r=   r   joinr#   dumpsHTML_OUT
write_text)datar   rZ   r   r>   r   clsevidencer   rectsdetailsr   s               r   
write_htmlr
    sJ   9oGD2 Y" 	hqvhnQw{{37G5H4IWX#Y$ 	KK"#KK  J  KL! 
./
 F*d*:PV[;;P377=3I3ORPP''+,2c'++mB\Bb`bcc**SuQ?3778,-. /SWW-./0 1%s1Z=/ 2SWW[)*+ ,*@7 M' 		

" 	KK$%KK  y  zN# 

3778,-. /377=123 4SWWW%&' (377:./0 1377<012 33778,-. /		


 	KK$%KKAB]# +d1T7)5)*+KK;<,- +d1T7)5)*+KK!"		$$.Ar   c                 ~   t         j                  dd       t        j                  dd       t               } t        j                  t        j                  | dd      dz   d       t        |        t        d	t        j                  t                      t        d	t        j                  t                      y )
NT)parentsexist_okFr   r   r   r   r    zwrote )OUTmkdirWEBr   JSON_OUTr  r#   r  r
  printr   r   r  )r  s    r   mainr    s    IIdTI*IIdTI*7D

4eAFMX_`t	F8''-.
/0	F8''-.
/0r   __main__)r   r   returnr   )r&   r   r  dict[str, Any])r>   r  r  r  )rZ   list[dict[str, Any]]r  r  )NN)rZ   r  r-   
str | Noner.   r  r  r  )re   r  rd   r   r  r  )rZ   r  r  z	list[str])rx   r  r  dict[str, dict[str, Any]])r   r  r  r  )ry   r  rz   r   r  r  )r   r  re   r  ry   r  r  r  )r  r  )r  r  r  None)r  r  ),__doc__
__future__r   r   r#   collectionsr   r   pathlibr   typingr   r   resolver  r   r  r  r   r   r   r   r   r  r  r   r   r   r   r   r'   r?   r_   rb   rf   rh   r{   r   r   r   r   r
  r  __name__ r   r   <module>r#     sF   #   ,   H~''*
Ul
Ul;;44:: ;;==9999 $%.FG$%/EF&'1FG%&0GH'(2KLH828S+\("odEPYBx1 zF r   