
    UHjw.                    p   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m	Z	 ddl
mZ ddlmZ ddlmZmZ  ee      j%                         j&                  d   Zed	z  Zdd
ZddZddZ	 d	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZd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)$zJList callers of Korean text routine candidates and nearby text-table refs.    )annotationsN)Counterdefaultdict)Pathdirect_text_calls)read_sectionsva_to_offset   outc                    d| dS )N0x08x )values    ;/home/exedev/hwanse/tools/summarize_text_routine_callers.pyhex32r      s    c{    c                L    t        j                  | j                  d            S )Nutf-8encoding)jsonloads	read_text)paths    r   	load_jsonr      s    ::dnngn677r   c                F   i }| j                  d      xs g D ]m  }t        |j                  d      t              s#|j                  d      xs t        |d         |j                  dd      |j                  dd      d||d   <   o g }| j                  d      xs g D ]  }g }|j                  d	      xs g D ][  }t        |d
   d      }|j	                  ||d
   |j                  dd      |j                  dd      d       |j                  |       ] t        |d   d      }t        |d   d      }	|j                  ||	t        d|dz
        |	dz   |d   |d   |j                  dd      |j                  d      xs i |j                  d      xs g |d
        ||fS )N
candidatesvavaHextext classification)	textVaHexr"   r$   refClustersentriesr%      
startVaHexendVaHexr   @   
entryCountclassificationCountssampleTexts)
startVaendVamatchStartVa
matchEndVar)   r*   r,   r-   r.   r'   )get
isinstanceintr   
setdefaultappendmax)
korean_summarytextsrowclustersclusterr'   entrytext_vastart_vaend_vas
             r   
text_indexrB      s   E!!,/52 cggdmS) WWW-As4y1A+"%''*:B"? E#d) H!%%m4: [[+1r 	"E%,b1GW";/		&"-"')),<b"A' 
 NN5!	" w|,b1WZ("-8d?3 4-!,/
+!++lA6$+KK0F$G$M2";;}5;
 	0 (?r   c                `    t        | ||      \  }}|D cg c]  }|d   |v s| c}|fS c c}w )NtargetVar   )exesectionstargetsexe_pathcallsmethodcalls          r   scan_calls_to_targetsrL   A   s;     &c8X>ME6"BTd:&6'&ADBFJJBs   ++c                   t        d||z
        }||z   }t        ||      }t        ||      }	||	|	|k  rg g dfS | ||	 }
g }g }t               }t               }t        dt        dt	        |
      dz
              D ]  }t        j                  d|
|      d   }||z   }||v rD||f|vr>|j                  ||f       |j                  t        |      t        |      d||          |D ]t  }|d   |cxk  r	|d   k  sn ||d   f|vs"|j                  ||d   f       |j                  t        |      t        |      |d	   |d
   |d   |d   |d   d d d       v  |||
j                  d      fS )Nr   r#      z<I)refVaHexvalueHexr1   r2   r/   r)   r*   r,   r-   r.      )rO   rP   clusterStartVaHexclusterEndVaHexr,   r-   r.    )r8   r
   setrangelenstructunpack_fromaddr7   r   hex)rE   rF   	center_var:   r<   radiusr@   rA   	start_offend_offdata	text_refs
table_refs	seen_text
seen_tableindexr   ref_var=   s                      r   scan_context_refsrg   K   s    1i&()HFXx0I8V,GGOw)/C2rzy!DIJIJq#aTQ/0 ""4u5a8E!E>vuoY>MM65/*!&M!%L , 
   	G~&%H7<3HHfV]^gVhMiq{M{	(:;<!! %f %e)0)>'.z':"),"7,34J,K#*=#9"1#=# 	, j$((3-//r   c                V   g }| D ]#  }|j                  |j                  dd             % |D ]&  }|j                  |j                  d      xs g        ( dj                  |      t	        fddD              ryt	        fdd	D              ry
t	        fddD              ry|ryy)Nr"   r#   r.   rT   c              3  &   K   | ]  }|v  
 y wNr   .0wordjoineds     r   	<genexpr>z"classify_caller.<locals>.<genexpr>~   s     
Xd46>
X   )u   파일u   드라이브u   환세취호전 verz	system-uic              3  &   K   | ]  }|v  
 y wrj   r   rk   s     r   ro   z"classify_caller.<locals>.<genexpr>   s     
_d46>
_rp   )u   약초u	   해독초u   마법의 물약u	   마수석z	item-menuc              3  &   K   | ]  }|v  
 y wrj   r   rk   s     r   ro   z"classify_caller.<locals>.<genexpr>   s     
bd46>
brp   )u   정권u   돌려차기u	   던지기u   도주u   방어zbattle-actionz
text-tableunclassified)r7   r3   extendjoinany)ra   rb   samplesrefrn   s       @r   classify_callerry   w   s    G ,swwvr*+, 5sww}-345XXgF

X&W
XX

_&^
__

b&a
bbr   c                   t        |       }t        |      \  }}|j                  d      xs g D ch c]"  }|j                  d      rt        |d   d      $ }}g }t	        | |||      \  }	}
|	D ]X  }t        | ||d   ||      \  }}}t        ||      }|j                  i ||t        |      t        |      |d d |d d |d       Z t        t              }|D ]  }||d      j                  |        g }|j                         D ]V  \  }}t        d |D              }|j                  |t        |      t        t        |j                                     |d       X |j                  d	 
       t        d |D              }dt        |      t        |      |
t        t        |j                                     d|dS c c}w )NtextRoutineCandidatestargetVaHexr(   callVa   )r$   directTextRefCounttextTableRefCountdirectTextRefstextTableRefscontextBytesc              3  &   K   | ]	  }|d      ywr$   Nr   )rl   r;   s     r   ro   z build_summary.<locals>.<genexpr>   s     Es#34E   )r|   callerCountr-   callersc                    | d    | d   fS )Nr   r|   r   )r;   s    r   <lambda>zbuild_summary.<locals>.<lambda>   s    C$6#6M8J"K r   )keyc              3  &   K   | ]	  }|d      ywr   r   )rl   rK   s     r   ro   z build_summary.<locals>.<genexpr>   s     Dd4 01Dr   zQCallers of Korean text routine candidates discovered near executable text tables.zKnown Korean text routine callers are currently tied to system, item, and battle/action text-table contexts. No story-dialogue caller class has been identified from these direct callsites.)scoperoutineCountr   directTextCallScanMethodr-   
conclusionroutines)r	   rB   r3   r5   rL   rg   ry   r7   rW   r   listitemsr   dictsortedsort)rE   r9   rH   rF   r:   r<   r;   routine_targetsrI   matched_callscall_scan_methodrK   ra   rb   
code_bytesr$   groupedr   
target_hexrowsclass_countss                        r   build_summaryr      s   S!H 0OE8 "%%&=>D"77=! 	C#O 
 E&;C?\d&e#M# ,=c8TRZ^]bdl,m)	:z(J? 

,"%i.!$Z'n'_&
 	 $G 2]#$++D12H#MMO 
DEEE%t9$(0B0B0D)E$F	
 	 MMKMLDeDDLdH5z$4 $VL,>,>,@%A B^  Cs   'Gc                (   g }| j                  d      xs g D ]#  }|j                  |j                  dd             % | j                  d      xs g D ]&  }|j                  |j                  d      xs g        ( dj                  |d d       xs dS )	Nr   r"   r#   r   r.   z,    -)r3   r7   rt   ru   )rK   rw   rx   s      r   caller_samplesr      s    Gxx()/R ,swwvr*+,xx(.B 5sww}-34599WRa[!(S(r   c                   dd| d   dd| d    d| d    d| j                  d	d
       dt        j                  | d   d       dddddg}| j                  d      xs g D ];  }|j                  d|d    d|d    dt        j                  |d   d       d       = |j	                  g d       | j                  d      xs g D ]j  }|j                  d      xs g D ]P  }t        |      j                  dd      }|j                  d|d    d|d    d|d    d|d    d|d     d| d       R l d!j                  |      d!z   S )"Nz# Text Routine Callersr#   r   z- routine targets: r   z- caller count: r   z- direct call scan: r   r   z- classes: r-   Fensure_asciiz## Routinesz| routine | callers | classes |z| --- | ---: | --- |r   z| `r|   z` | z | z |)r#   z
## Callersr#   z>| call | target | class | direct text | table refs | samples |z'| --- | --- | --- | ---: | ---: | --- |r   |z\|	callVaHexz` | `r$   r   r   
)r3   r   dumpsr7   rt   r   replaceru   )summarylinesroutinerK   rw   s        r   markdownr      s    


gn567
7=123
w{{+EsKLM
djj)?!@uUVW

)E ;;z*0b 
'-()gm.D-ESzz'"89NOrS	


 
LL   ;;z*0b KK	*0b 	D$T*223>GLLd;'(d=.A-B$tL\G]F^^a,-.c$7J2K1LCPWyXZ\	 99Ud""r   c                   dj                  d | j                  d      xs g D              }g }| j                  d      xs g D ]  }|j                  d      xs g D ]  }|j                  dt        j                  |d          dt        j                  |d          d	t        j                  |d
          d|d    d|d    dt        j                  t        |             d         dj                  dddddddddddt        j                  | d          ddt        j                  | j                  dd             d | d!    d"d#d$d%| d&d'd(d%dj                  |       d&d)d*dg      S )+Nr#   c              3     K   | ]T  }d t        j                  |d          d|d    dt        j                  t        j                  |d   d             d V y	w)
<tr><td><code>r|   </code></td><td>r   	</td><td>r-   Fr   
</td></tr>N)htmlescaper   r   )rl   r   s     r   ro   zhtml_page.<locals>.<genexpr>   so       	[[!789 :}%& '{{4::g.D&ETYZ[\ ]	s   AAr   r   r   r   z</code></td><td><code>r|   r   r$   r   r   r   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">z%  <title>Text Routine Callers</title>z  <style>body{font-family:system-ui,sans-serif;background:#101010;color:#eee;margin:24px}table{border-collapse:collapse;width:100%}td,th{border:1px solid #333;padding:6px 8px;vertical-align:top}th{background:#1d1d1d}code{color:#f5d76e}</style>z</head>z<body>z  <h1>Text Routine Callers</h1>z  <p>r   z</p>z  <p>Direct call scan: r   r   z; callers: r   z.</p>z  <h2>Routines</h2>zQ  <table><thead><tr><th>routine</th><th>callers</th><th>classes</th></tr></thead>z	  <tbody>z</tbody></table>z  <h2>Callers</h2>z  <table><thead><tr><th>call</th><th>target</th><th>class</th><th>direct text</th><th>table refs</th><th>samples</th></tr></thead>z</body>z</html>)ru   r3   r7   r   r   r   )r   routine_rowscaller_rowsr   rK   s        r   	html_pager      s   77  {{:.4" L K;;z*0b KK	*0b 
	D![[k):;< =![[m)<=> ?{{4(8#9:; <012 3/01 2{{>$#789 :	
	 99"P/ 	~)
GL12348
!$++gkk:TVY.Z"[!\\ghop}h~g  @E  	F[
L>!12 	M
BGGK())9:
+  r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       y )
NT)parentsexist_oktext_routine_callers.jsonF   )r   indentr   r   r   )mkdir
write_textr   r   )r   out_dirs     r   write_outputsr     sC    MM$M.**66

7q9D@ 7 r   c                    t        j                         } | j                  dt        t        dz         | j                  dt        t
        dz         | j                  dt        t
               | j                         }t        |j                  j                         t        |j                        |j                        }t        ||j                         t        d|j                  dz   d	|d
    d       y )Nz--exezHwanse2.exe)typedefaultz--korean-textzkorean_text_candidates.jsonz	--out-dirzwrote text routine callers -> r   z (r   z	 callers))argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr   rE   
read_bytesr   korean_textr   r   print)parserargsr   s      r   mainr   !  s    $$&F
dD=4HI
dCB_<_`
$<DDHH//19T=M=M3NPTPXPXYG'4<<(	(<<55
6 7M"#9	.r   __main__)r   r5   returnstr)r   r   )r9   r   r   z"tuple[dict[int, dict], list[dict]]rj   )
rE   bytesrF   
list[dict]rG   zset[int]rH   Path | Noner   ztuple[list[dict], str])`   )rE   r   rF   r   r\   r5   r:   zdict[int, dict]r<   r   r]   r5   r   z"tuple[list[dict], list[dict], str])ra   r   rb   r   r   r   )rE   r   r9   r   rH   r   r   r   )rK   r   r   r   )r   r   r   r   )r   r   r   r   r   None)r   r   )#__doc__
__future__r   r   r   r   rX   collectionsr   r   pathlibr   exe_call_scannerr   probe_exe_scene_tablesr	   r
   __file__resolver   r   r   r   r   rB   rL   rg   ry   r   r   r   r   r   r   __name__r   r   r   <module>r      s%   P "     ,  . > H~''*
Ul8"R !	K	KK K 	K
 K  )0	)0)0 )0 	)0
 )0 )0 ()0X$/d)##L,^ 25  zF r   