
    UHj                       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
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ZddZdddZddZddZefddZddZedk(  r e        yy)z6Summarize save-selector frontier leaf stream contents.    )annotationsN)Path)find_cns_stringsread_sectionsva_to_offset   outc                "    | rt        | d      S d S )N   )int)values    A/home/exedev/hwanse/tools/summarize_save_selector_leaf_streams.py	parse_hexr      s    "3ub>,,    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   dword_atr      s?    (B'F~!c#h.dC033r   c                |   g }t        |      D ]  }||dz  z   }t        | ||      }| |S |d|dd|d|dz  |dz	  d}	||v rS||   |	d<   ||   j                  d      rH||   j                  d	      s4t        | ||dz         }
d
|	d<   |
d|
dnd |	d<   nt        ||      d
|	d<   |j	                  |	        |S )Nr   0x08xi  r   )indexvaHexvalueHexu16Lou16Hicnsmapmap_TfieldMapRecordStart04x
sceneIdHexpointer)ranger   
startswithr   append)r   r   stringsr   countrowsr   item_var   itemscene_ids              r   
dword_rowsr3      s   Du uqy.h0=" K '#'U3K(V^b[
 G!%.DKu~((/8Q8QRX8Y#C7Q;?.2*+<D<Pr(3%8VZ\"(E*6"DOD)* Kr   c                l    g }| D ],  }|j                  d      }|s||vs|j                  |       . |S )Nr#   )getr,   )r/   namesrownames       r   
linked_cnsr9   7   s@    E wwu~D%LL Lr   c           	         g }| D ]e  }|j                  d      }|s|j                  d      s)|j                  d      r;|j                  |d   ||d d |j                  d      d       g |S )Nr#   r$   r%   r   r(   )recordVaHexfilenamer$   r(   )r5   r+   r,   )r/   resultr7   r8   s       r   field_recordsr?   @   ss    F wwu~DOOE*4??63JMM"7| CRy!ggl3	  Mr   c                t   t        |       }t        | |      }g }|D ]  }|j                  d      xs g D ]  }t        |j                  d            }| t	        | ||||      }	t        |	      dkD  r|	d   d   dk(  r|	d   d   nd }
g }|
r#t        |
      t	        | ||t        |
      d      }|j                  |d   |d	   |j                  d
      |j                  d      |
|j                  d      |j                  d      t        |	      t        |	      t        |      t        |      |	|d         |S )N
scenePairsleafPointerHexr   r   r    
0x0000003fr   sourcetargetselectorsourceRecordVaHextargetRecordVaHex)rD   rE   rF   rB   nestedPointerHexrG   rH   	linkedCnsfieldRecordsnestedLinkedCnsnestedFieldRecordswordsnestedWords)	r   r   r5   r   r3   r   r,   r9   r?   )r   frontier_rows
word_countr   r-   r/   frontierpairleaf_varN   nestednested_wordss               r   
build_rowsrW   N   sQ   S!HsH-GD! LL.4" 	D)9 :;GsHgw
KE-0Z!^a@TXd@dU1Xj)jnFL)F+7)#x)FBSUWXKK"8,"8, HHZ0"&((+;"<$*%)XX.A%B%)XX.A%B'. -e 4#-l#;&3L&A+ 	2 Kr   c                   dddddt        |        ddddg}| D ]  }dj                  d	 |d
   D              xs d}dj                  d |d   D              xs d}|j                  d|d    d|j                  d      xs d d|d    d|d    d| d| ddj                  |d         xs d d        |j                  d       dj                  |      S )Nz# Save Selector Leaf Streams zLeaf stream summaries for save-selector frontier candidates. These streams are script/event data candidates, not confirmed tile transitions.zLeaf streams: .z_| leaf | nested | source | target | field records in leaf | nested field records | linked CNS |z+| --- | --- | --- | --- | --- | --- | --- |, c              3  N   K   | ]  }|d     d|j                  d         ywr$    r(   Nr5   .0r1   s     r   	<genexpr>zmarkdown.<locals>.<genexpr>z   s)     ed5k]!DHH\,B+CDe   #%rK   -c              3  N   K   | ]  }|d     d|j                  d         ywr]   r_   r`   s     r   rb   zmarkdown.<locals>.<genexpr>{   s*     !rPTT%[M488L3I2J"K!rrc   rM   z| rB   z | rI   rG   rH   rJ   z |
)r   joinr,   r5   )r/   linesr7   fieldsnested_fieldss        r   markdownrk   n   s    &
 	W

T1%
i5	E  
eQTUcQdeelil		!rX[\pXq!rryvy%&'s3773E+F+M#*NcRUViRjQkkn&'(F83}oSSVWbScIdIkhkHllnp	

 
LL99Ur   c                   g }| D ]  }dj                  d |d   D              xs d}dj                  d |d   D              xs d}|j                  dt        j                  |d          d	t        j                  |j	                  d
      xs d       d	t        j                  |d   xs d       d	t        j                  |d   xs d       d	| d	| d	t        j                  dj                  |d         xs d       d        dj                  dddddddddddddddd d!t        |        d"d#d$d%dj                  |      xs d&d'd(d)d*d+g      S ),Nz<br>c           
   3     K   | ]6  }t        j                  |d     d|d    d|j                  d              8 ywr<   r^   r$   r(   Nhtmlescaper5   r`   s     r   rb   zhtml_page.<locals>.<genexpr>   sF      
 KK4./qeQtxx?U>VWX
   <>rK   rd   c           
   3     K   | ]6  }t        j                  |d     d|d    d|j                  d              8 ywrn   ro   r`   s     r   rb   zhtml_page.<locals>.<genexpr>   sF      $
 KK4./qeQtxx?U>VWX$
rr   rM   z<tr><td>rB   z	</td><td>rI   rG   rH   r[   rJ   z
</td></tr>rf   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>Save Selector Leaf Streams</title>z	  <style>z^    body { margin: 24px; background: #101010; color: #eee; font: 14px system-ui, sans-serif; }z5    table { border-collapse: collapse; width: 100%; }zM    th, td { border: 1px solid #333; padding: 6px 8px; vertical-align: top; }z9    th { background: #1d1d1d; position: sticky; top: 0; }z
  </style>z</head>z<body>z%  <h1>Save Selector Leaf Streams</h1>z  <p>Leaf stream summaries for save-selector frontier candidates. These streams are script/event data candidates, not confirmed tile transitions.</p>z  <p>Leaf streams: z.</p>z	  <table>z    <thead><tr><th>leaf</th><th>nested</th><th>source record</th><th>target record</th><th>field records in leaf</th><th>nested field records</th><th>linked CNS</th></tr></thead>z    <tbody>z.<tr><td colspan="7">No leaf streams.</td></tr>z    </tbody>z
  </table>z</body>z</html>rY   )rg   r,   rp   rq   r5   r   )r/   	body_rowsr7   ri   rj   s        r   	html_pageru      s   I 
 
N+
 
   	  $
01$
 
   	 	;;s#3456 7;;sww'9:AcBC D;;s#67>3?@ A;;s#67>3?@ A(  / ";;tyy[)9:AcBC D
	

( 99"P5h?WC/ 	`
c$i[. 	}		)P P
5  r   c                    |j                  dd       |dz  j                  t        j                  | dd      dz   d	       y )
NT)parentsexist_oksave_selector_leaf_streams.jsonF   )ensure_asciiindentrf   utf-8encoding)mkdir
write_textjsondumps)r/   out_dirs     r   write_outputsr      sC    MM$M.00<<

4eA6= = r   c                 (   t        j                  t              } | 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                          t#        d
t%        |       d|j                   dz          y )N)descriptionz--exezHwanse2.exe)typedefaultz
--frontierzsave_selector_frontier.jsonz	--out-dirr}   r~   zwrote z# save selector leaf stream rows -> ry   )argparseArgumentParser__doc__add_argumentr   ROOTOUT
parse_argsrW   r   
read_bytesr   loadsrR   	read_textr   r   printr   )parserargsr/   s      r   mainr      s    $$9F
dD=4HI
4?\9\]
$<D

4==**G*<=D $%	F3t9+@PqAq@r
str   __main__)r   z
str | Nonereturn
int | None)r   bytesr   
list[dict]r   r   r   r   )r   r   r   r   r-   zdict[int, str]r   r   r.   r   r   r   )r/   r   r   z	list[str])r/   r   r   r   )P   )r   r   rP   r   rQ   r   r   r   )r/   r   r   str)r/   r   r   r   r   None)r   r   )r   
__future__r   r   rp   r   r   pathlibr   probe_exe_scene_tablesr   r   r   __file__resolverw   r   r   r   r   r3   r9   r?   rW   rk   ru   r   r   __name__ r   r   <module>r      s    < "      P P H~''*
Ul-44@,1h 58 u zF r   