
    UHjw                    r   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	 ddl
mZ ddlmZmZmZmZ ddlmZ  ee      j)                         j*                  d   Zed	z  Zd
ZdZdZ ej6                  d      Zi ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-Zh d.Zd/dd0d1d2d3dd4d5d2d6dd7d8d2d9dd:d;d2d<dd=d>d2d?dd@dAd2dBddCdDd2dEddFdGd2dHddIdJd2dKddLdMd2dNddOdPd2dQZh dRZ dxdSZ!dxdTZ"dydUZ#dzdVZ$d{dWZ%d|dXZ&d}dYZ'd~dZZ(dd[Z)dd\Z*dd]Z+dd^Z,dd_Z-	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd`Z.ddaZ/ddbZ0ddcZ1ddddZ2ddeZ3ddfZ4ddgZ5ddhZ6ddiZ7ddjZ8ddkZ9	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddlZ:ddmZ;dddnZ<ddoZ=ddpZ>ddqZ?ddrZ@ddsZAddtZBefdduZCddvZDeEdwk(  r eD        yy)zPIndex dialogue-looking event stream blocks anchored by opcode 0x0d text sources.    )annotationsN)Counter)Path)classify_cnsfind_cns_stringsread_sectionsva_to_offset)	load_json   out`   i  @   z\[\u3131-\u318e\uac00-\ud7a3A-Za-z0-9][\u3131-\u318e\uac00-\ud7a3A-Za-z0-9 !?.:_+\-,~()/]{1,}   ztext separator/continuation   zset display/font row   zset display timer/state   zwait for input release   zset object fixed-point position   zset display style triplet	   zcall event stream branch
   zreturn from event stream branch   zrender current context text id   z&spawn child object with text/source id   zset current context text id   z"set cursor relative to text origin   z move text origin/cursor relative   zselect script data bank5   zliteral CP949 text payload7   zset global timer/state>   0x020x0b0x0c0x0d
0x0041b7bbzset-display-font-rowzPhandler-grounded display/font row state; browser does not execute full VM effect)handlerVaHexdecodedLengthactionclassification
0x0041b7edzset-display-timer-statezRhandler-grounded display timer/state seed; browser does not execute full VM effect
0x0041b8eezwait-for-input-releasezNhandler-grounded input wait latch; browser does not execute original wait loop
0x0041b956zset-object-fixed-positionzOhandler-grounded object position write; browser does not execute full object VM
0x0041b9a0zset-display-style-tripletz?handler-grounded display style write with mode-dependent length
0x0041bac6zcall-event-stream-branchzQhandler-grounded VM branch call; browser records but does not follow control flow
0x0041bb15zreturn-from-event-stream-branchzQhandler-grounded VM stack return; browser records but does not pop original stack
0x0041bd4fz"set-cursor-relative-to-text-originzGhandler-grounded cursor offset; browser does not execute full layout VM
0x0041d476zmove-text-origin-and-cursorzShandler-grounded text origin/cursor offset; browser does not execute full layout VM
0x0041d89dzselect-script-data-bankzKhandler-grounded data-bank selection; browser does not execute full data VM
0x00420028zset-global-timer-statezQhandler-grounded global timer/state seed; browser does not execute full VM effect)r   r   r   r   r   r   r   r   r   r   r   >      도구   모드   무기   소지	   방어구	   이어서   시나리오   처음부터   프로듀서   맵 디자인   이어서하기   시나리오 선택c                    d| dS )N0x08x values    </home/exedev/hwanse/tools/summarize_event_dialogue_blocks.pyhex32rE          c{    c                    d| dS )Nr?   02xrA   rB   s    rD   hex8rJ      rF   rG   c                F    d| cxk  xr dk  nc xs d| cxk  xr dk  S c S )Nu   ㄱu   ㆎu   가u   힣rA   )chars    rD   	is_hangulrM      s'    t'x'G8t+Gx+GG+GGrG   c                d    |dk  s|dz   t        |       kD  ry t        j                  d| |      d   S )Nr   r   z<Ilenstructunpack_from)exeoffsets     rD   dword_atrU      s5    zVaZ#c(*dC033rG   c                d    |dk  s|dz   t        |       kD  ry t        j                  d| |      d   S )Nr   r   z<HrO   )datarT   s     rD   word_atrX      s5    zVaZ#d)+dD&1!44rG   c                N    | D ]   }|d   }||d   z   }||cxk  r	|k  s|c S  " y )Nvaraw_sizerA   )sectionsrZ   sectionstartends        rD   section_for_var`      sE     gj))BN 
 rG   c                    t        | |      xs t        | t        ||dz
              }|y t        ||d         }t        ||d   |d   z         }||k  ry t        | |      }t        | |dz
        }||y ||dz   |fS )Nr   rZ   r[   )r`   maxminr	   )r\   start_vaend_var]   clamped_startclamped_end	start_offend_offs           rD   interval_offsetsrj      s    Xx0gN8SQY[ade[eMf4gG'$-0Mfgdmgj.AABKm#X}5I8[1_5GGOgk=00rG   c                   g }| D ]o  }t        |      }|dk(  s|j                         r|j                  d       5d|cxk  rdk  sn t        |      r|j                  |       _|j                  d       q dj	                  |      S )Nu   　     ~    )ordisspaceappendrM   join)decodedcharsrL   codes       rD   normal_textrw      sv    E 4y8t||~LLT!T!Yt_LLLL 775>rG   c                n    dj                  | j                               } t        j                  dd|       S )Nrl   z,^[0-9]{1,2} (?=[\u3131-\u318e\uac00-\ud7a3])ro   )rs   splitresub)texts    rD   clean_match_textr}      s*    88DJJL!D66A2tLLrG   c                   g }t               }d}t        | dz         D ]  \  }}|dv rt        |      dk\  rt        |j	                  dd            }t
        j                  |      D ]d  }t        |j                  d            }	t        d |	D              }
t        |	      dk\  s>|
dk\  sD|j                  t        ||z         |	|
d	       f |j                          |d
z   }|s|}|j                  |        |S )Nr       >   r   r   r   cp949ignorec              3  8   K   | ]  }t        |      sd   yw)r   N)rM   ).0rL   s     rD   	<genexpr>ztext_lines.<locals>.<genexpr>   s     &MTYt_q&Ms   r   )vaHexr|   hangulCountr   )	bytearray	enumeraterP   rw   decodeTEXT_REfinditerr}   groupsumrr   rE   clear)rW   base_valinescurrentcurrent_startindexbytert   matchr|   hangul_counts              rD   
text_linesr      s    EkGM . !t<7|q %gnnWh&GH$--g6 E+EKKN;D#&&MT&M#ML4yA~,!*;%*7]+B%C$(+7& 	 MMO!AIM %NN4 %!& LrG   c                    g }| j                  d      xs g D ]`  }|j                  d      dk7  r|j                  d      dk7  s|j                  d      r>|j                  d      sP|j                  |       b |j                  d 	       |S )
Nrows	opcodeHexr"   
confidencemediumcurrentDwordPointernearbyTextSnippetsc                     t        | d   d      S )Nr      )introws    rD   <lambda>z(event_text_source_rows.<locals>.<lambda>   s    c#g,3 rG   key)getrr   sort)event_text_source_flowr   r   s      rD   event_text_source_rowsr      s    D%))&17R 77;6)77< H,8M0Nww+,C 	II3I4KrG   c                   g }| D ]}  }t        |d   d      }|t        z
  }|t        z   }|r||d   d   t        z   kD  r|j	                  |||gd       Ot        |d   d   |      |d   d<   |d   d   j	                  |        |S )Nr   r   endVa)startVar   
sourceRowsr   )r   WINDOW_BEFOREWINDOW_AFTER	MERGE_GAPrr   rb   )source_rowswindowsr   rZ   r^   r_   s         rD   merge_source_windowsr      s    G 2Wr"]"<%'"+g"6"BBNN "e  $'wr{7';S#AGBK BK%,,S12 NrG   c                    t               }t        t        dt        |       dz
              D ]'  }| |   dk(  s|t	        | |dz            xx   dz  cc<   ) |S )Nr   r   r   )r   rangerb   rP   rJ   )rW   countsr   s      rD   command_opcode_countsr      s\    "9Fs1c$i!m,- /;$4UQY()Q.)/ MrG   c           
        t        |||z
  ||z         }|g S |\  }}}	||dz  z  }g }
t               }t        |t        ||dz
        d      D ]t  }t	        | |      }||vr||f}||v r|j                  |       t        ||      }|
j                  |t        |      nd t        |      ||   t        ||         d       v |
S )Nr   r   refVaHextargetVaHexnamekind)
rj   setr   rb   rU   addsection_for_offset_varr   rE   r   )rS   r\   stringsrd   re   radiusoffsetsrh   ri   _r   seenoffrC   r   ref_vas                   rD   cns_refs_in_intervalr     s     xF):FVOLG	#IwQID5DYIw{ ;Q? c"El$;&x5)/);f <EN 0	
 	 KrG   c                b    | D ]*  }|d   }||d   z   }||cxk  r|k  sn |d   |z   |z
  c S  y )Nrawr[   rZ   rA   )r\   rT   r]   r^   r_   s        rD   r   r   &  sO     2gj))F S 4=6)E11	2
 rG   c                    g }t               }| D ]<  }|j                  d      }|r||v r|j                  |       |j                  |       > |S )Nr   )r   r   r   rr   )r   dedupedr   r   r   s        rD   unique_by_namer   /  sT    G5D wwvtt|s NrG   c           	        g }t               }| D ]x  }|j                  d      |j                  d      |j                  d      t        |j                  d      xs g       f}||v rW|j                  |       |j	                  |       z |S )NmaprecordVaHexconditionVaHextargets)r   r   tupler   rr   )r   r   r   r   r   s        rD   unique_contextsr   ;  s    G5D 
GGENGGM"GG$%#'')$*+	
 $;s
 NrG   c                    g }t               }| D ]?  }|d   }||v r|j                  |       |j                  |       t        |      |k\  s> |S  |S )Nr|   )r   r   rr   rP   )r   limitvaluesr   r   r|   s         rD   first_unique_textr   L  sb    F5D 6{4<dv;%M MrG   c                   | d   dk  ry| j                  d      xs g D ch c]  }|d   	 }}t        |t        z        dk\  ry| j                  d      ryt        d	 | j                  d
      xs g D              ry| d   dk\  ryyc c}w )NtextLineCountr   ztext-fragment	textLinesr|   zsystem-text-like	fieldMapsmap-dialogue-likec              3  >   K   | ]  }|j                  d         yw)btl_N)
startswith)r   r   s     rD   r   z!classify_block.<locals>.<genexpr>b  s     
Pt4??6"
Ps   resourceNamesbattle-dialogue-liker   story-dialogue-like)r   rP   SYSTEM_MARKERSany)blockr   unique_texts      rD   classify_blockr   Z  s    _!*/))K*@*FBG33v;GKG
;'(A-!yy"

Puyy/I/OR
PP%_"$ Hs   Bc                    t               }| D ]6  }|j                  d      xs i j                  d      }|s*||xx   dz  cc<   8 |j                         D cg c]
  \  }}||d c}}S c c}}w )NpayloadtextSourceValueHexr   )valueHexcount)r   r   most_common)r   r   r   rC   r   s        rD   source_value_countsr   i  s|    YF #)r../CD5MQM #..0E5 U+  s   A,c                Z   g }| D ]  }|j                  d      }|t        vr|j                  d      xs i }|j                  d      ||j                  d      |j                  d      |j                  d      |j                  d      d}|d	k(  r.d
|d<   |j                  d      |d<   |j                  d      |d<   n=|dk(  rd|d<   |j                  d      |d<   n|dk(  rd|d<   |j                  d      |d<   g }|j                  d      xs g D ]g  }t        t        t	        |j                  d      xs d                  }|r#|j                  |j                  d      |d       t        |      dk\  sg n |r||d<   |j                  |        |j                  d        |S )Nr   r   r   r$   
offsetMod4r   r'   )r   r   r$   r   r   r'   r"   zset-context-text-sourcer&   r   
sourceKindr    zrender-current-context-textr!   zspawn-child-object-with-contextr   r|   ro   )r   r|   r   
nearbyTextc                R    t        t        | j                  d      xs d      d      S Nr   0r   r   strr   r   s    rD   r   zcommand_trace.<locals>.<lambda>       #c#'''*:*Ac&BB"G rG   r   )r   VM_TRACE_OPCODESr}   rw   r   rr   rP   r   )	r   commandsr   opcoder   commandsnippetssnippetr|   s	            rD   command_tracer  u  s   H #!%))'')$*WWW%GGN3'',/'',/!gg&67
 V 9GH,3KK8L,MG()$+KK$=GL!v =GH$+KK$=GL!v AGH$+KK$=GL!ww34: 	G#KGKK4G4M20N$OPD$[[1 !  8}!	 $,GL! G#!H MMGMHOrG   c                    g }t        t        dt        |       dz
              D ]=  }| |   dk7  s| |dz      dk7  r|j                  t	        ||z         ddddd	d
ddd	       ? |S )Nr   r   r   r   r   
0x0041b771r   0x4handler-grounded-storage-scanzXdialogue cursor/separator command; copies context+0xce to 0xd6 and advances context+0xdaz$advance-dialogue-cursor-or-separatorzcontext-font-row)	r   r   r$   r%   decodedLengthHexr   r'   r&   r   )r   rb   rP   rr   rE   )rW   r   r   r   s       rD   separator_command_tracer    s    Hs1c$i!m,- ;$$uqy/T"97U?+( %9x<,

 
	 OrG   c                   g }t        t        dt        |       dz
              D ]  }| |   dk7  r| |dz      }t        j	                  |      }|s-|j	                  d      }t        ||z         t        |      |d   |t        |t              rd|dnd d|d	   |d
   dd	}|dv r |dz   t        |       k  r| |dz      nd |d<   n"|dv r%t        | |dz         |d<   t        | |dz         |d<   n|dk(  r%t        | |dz         |d<   t        | |dz         |d<   n|dk(  r|dz   t        |       k  r| |dz      nd }||d<   |dk(  r)d|d<   d|d<   |dz   t        |       k  r| |dz      nd |d<   n||dk(  r7d|d<   d|d<   dD 	cg c]  }	||	z   t        |       k  r| ||	z      nd  c}	|d<   n@d |d<   n:|d!k(  r5t        | |dz         }
|
t        |
      nd |d"<   t        ||z   dz         |d#<   |j                  |        |S c c}	w )$Nr   r   r   r%   r$   r?   xr  r'   r&   F)	r   r   r$   r%   r  r   r'   r&   browserExecutesEffect>   r   r   r   r   r   operandByte2>   r   r   r   operandWord2r   operandWord4r   xWordyWordr   moder  r  r   tableIndexByte0x8)   r   r   immediateStyleByteszmode0=0x4/mode1=0x8r   branchTargetVaHexcontinuationVaHex)r   rb   rP   GROUNDED_CONTROL_OPCODESr   rE   rJ   
isinstancer   rX   rU   rr   )rW   r   eventsr   r   metadecoded_lengtheventr  itemtargets              rD   grounded_control_eventsr     s   Fs1c$i!m,- /;$eai'++F3/27U?+f 0+;EnVY;Z"^A$6 7`d9"#348n%*

 --7<qy3t97LDORVE.!))$+D%!)$<E.!$+D%!)$<E.!t^$T5195E'N$T5195E'Nt^&+ai#d)&;4	?D E&Mqy)*o&,1()=BQYT=R$uqy/X\&')*o&,1() !*0 +0$,T*BD&L0+,
 -B()t^dEAI.F:@:LvRVE%&).w/B)CE%&e_/` M0s   ?"G>c           
        t        | j                  d      xs i j                  d      xs d      }|dk  rg S g }t               }| j                  d      xs g D ]c  }|j                  d      }|r||v r|j                  |       |j	                  |j                  d      dd|dd	d
       t        |      |k\  sb |S  |S )NcommandOpcodeCounts0x35r   r   r|   r   zliteral-cp949-text-payloadzstorage-inferredzdialogue-block-text-lines)r   r   r&   r|   r   source)r   r   r   r   rr   rP   )r   r   r  r   r   r|   s         rD   literal_text_eventsr%    s    017R<<VDIJEz	F5Dyy%+ wwvtt|WWW%2,1
 	 v;%M MrG   c           	        | j                  d      xs i t        |      t        ||      z   }|j                  d        t	        |       }t        ||      }|D cg c]  }|j                  d      dk(  r| }}h d}	t        j                               D 
ci c]  \  }
}|
|	vr|
| }}
}i ddd	g d
dt        j                               dt        |      dj                  dd      dt        |      d|d d dt        fdt        D              dt        |      dt        |D ch c]  }|d   	 c}      d|d d d| j                  dd      d| j                  dd      dj                  dd      dt        |      d|d||t        | j                  d            d d!d"S c c}w c c}}
w c c}w )#Nr"  c                R    t        t        | j                  d      xs d      d      S r   r   r   s    rD   r   zvm_trace.<locals>.<lambda>  r   rG   r   r   r   >   r   r    r!   r"   r#  r$  zpartial-opcode-replayimplementedOpcodes)r   r"   r    r#  observedCommandCounttraceCommandCountseparatorCommandCountr   separatorEventCountseparatorEventsrm   groundedControlCommandCountc              3  T   K   | ]  }j                  t        |      d        ! yw)r   N)r   rJ   )r   r   command_countss     rD   r   zvm_trace.<locals>.<genexpr>  s#     *vSY>+=+=d6lA+N*vs   %(groundedControlEventCountgroundedControlOpcodeSetgroundedControlEventsr   sourceCommandCountrenderCommandCountliteralTextCommandCountr#  literalTextEventCountliteralTextEventsunsupportedOpcodeCountsrouteContextsTF)r   routeLinkedEventVmExecution&browserEventVmPartialReplayImplemented browserEventVmFullImplementation)r   r  r  r   r%  r   sorteditemsr   r   rP   r  bool)r   rows_in_windowrW   r   r   literal_eventscontrol_eventsr   separator_events	supportedr   r   unsupportedr  r0  s                 @rD   vm_tracerG    sQ   YY45;N^,/FtW/UUHMMGMH(/N,T7;N  ;;{#v- 	 
 9I $N$8$8$:;FE" 	K 
)> 	N$9$9$; < 	S]	
 	 !3!3FA!> 	s#34 	+CR0 	&s*v]u*v'v 	$S%8 	#FN+[5E+,>+[$\ 	 !4 	eii(<a@ 	eii(<a@ 	">#5#5fa#@ 	 ^!4  	^!" 	";#$ '+EIIo,F'G26,1+  ,\s   F7F<3Gc                   |d   }|d   }t        |||      }|y |\  }	}
}| |	|
 }|D cg c],  }|t        |j                  dd      d      cxk  r|k  rn n|. }}t        | ||||      }|D ]i  }|j                  d      xs g D ]O  }|j	                  |j                  d      d |j                  d      t        |j                  dd	            d
       Q k t        |      }|D cg c]  }|j                  d      dk(  s|d    }}|D cg c]  }|j                  d      dk(  s|d    }}t        |D cg c]  }|j                  d      xs g D ]  }|   c}}      }t        ||      }t        |      }|D cg c]  }|j                  d      dk(  s| }}|D cg c]  }|j                  d      dk(  s| }}i d|dd|dd|dt        |      d|dt        |      d||z
  dt        |      d|D cg c]  }|d   	 c}dt        |      d|d d D cg c]  }|d   	 c}dt        |      dt        t        |j                                     d t        t         j                               D ci c]+  \  }}|j                  t#        |            rt#        |      |- c}}d!|D cg c]  }|d   	 c}d"|d#|||t        |      t        |D ch c]  }|d$   	 c}      |t%        |      d%}t'        |      |d&<   t)        ||||      |d'<   |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 c c}}w c c}w c c}w )(Nr   r   r   r   r   	nearbyCnsr   r   ro   r   r   r   tilesetr:  r   r"   r    r   blockIdzevent-dialogue-block-03d
startVaHexendVaHex	byteCountr4  sourceCommandVasr5  renderCommandVasrm   sourceValuesr"  commandLabelsr   	resourcesr   r|   )tilesetsr:  r   uniqueTextLineCountr   
sampleTextr'   vmTrace)rj   r   r   r   rr   r   r   r   r   r   rE   rP   r   dictr>  r?  COMMAND_LABELSrJ   r   r   rG  )rS   r\   r   
event_rowswindowr   rd   re   r   rh   ri   r   rW   r   rA  cns_refscnsrT  
field_mapsrU  contextroute_contexts	line_rowsr0  r   render_rowsr   labelr   s                                rD   build_blockre  )  s    i HG_Fx6:G")Iwy!D s3777C0"5>> 	N 
 $C7HfMH 77;'-2 	COOGGJ/#$SWWVR%89	 	 x(I)2O#cggfo6N#f+OJO'0QCGGFOy4PFQHQ$!&06B&  	&& N
 4)I*40N"0S3CGGK4HF4R3SKS"0S3CGGK4HF4R3SKS*5+6 	8 	eHo	
 	 	E&M 	Vh& 	c+. 	[AcS\A 	c+. 	["5EFcS\F 	+K8 	tF>+?+?+A$BC 	!'(<(<(>!?
!!$v,/ L%
& 	;##f+;'( 	Y)* 	Z+, 'Y"9#ECCK#EF'	27E: -U3E
~tWEE)Ls PQ& TS BF

 < $FsY   1L.)L3L3L8*L8=#L=
MM'MMM*M	0M
M,M"c                   t        |       }t        | |      }|j                  d      xs g }t        |      }t	        |      }g }t        |d      D ]/  \  }}	t        | ||||	|      }
|
|
d   s|j                  |
       1 t        d |D              }t               }t               }|D ]Q  }
|j                  |
j                  d      xs i        |j                  d |
j                  d      xs g D               S |D 
cg c]  }
|
j                  d	      s|
 }}
|D 
cg c]  }
|
j                  d
      dv r|
 }}
dt        t        t        t        |      t        |      t        |      t        |      t        d |D              t        d |D              t        |      t!        t#        |j%                                     t!        t#        |j%                                     d|dS c c}
w c c}
w )Nr   r   )r^   r   c              3  &   K   | ]	  }|d      yw)r'   NrA   r   r   s     rD   r   z build_summary.<locals>.<genexpr>  s     Gu5!12G   r"  c              3  &   K   | ]	  }|d      yw)r|   NrA   r   r   s     rD   r   z build_summary.<locals>.<genexpr>  s     PCCKPri  r   r:  r'   >   r   r   r   z\event/object VM dialogue-looking CP949 blocks anchored by opcode 0x0d context+0x28 producersc              3  D   K   | ]  }|j                  d       sd  yw)r   r   N)r   rh  s     rD   r   z build_summary.<locals>.<genexpr>  s     "SEIIk<R1"Ss     c              3  &   K   | ]	  }|d      yw)r   NrA   rh  s     rD   r   z build_summary.<locals>.<genexpr>  s     HU?3Hri  a  Opcode 0x0d source commands now anchor a reusable index of nearby CP949 story-dialogue-looking blocks, including their adjacent 0x0b render commands and literal text payload markers. Opcode 0x02 cursor/separator commands are now preserved in the partial replay trace. This identifies dialogue storage candidates for playback work, but it still does not prove route-linked event execution or implement the event VM in the browser.)scopewindowBeforewindowAftermergeGapsourceCommandSeedCount
blockCountdialogueLikeBlockCountrouteLinkedBlockCountmapLinkedBlockCountr   rV  classificationCountsr"  
conclusionblocks)r   r   r   r   r   r   re  rr   r   r   updater   r   r   rP   r   rY  r>  r?  )rS   r   r\   r   r[  r   r   ry  r   r\  r   class_countsr0  unique_linesroute_linked_blocksdialogue_likes                   rD   build_summaryr  t  s   S!HsH-G'++F39rJ()?@K";/GF"7!4 !vC7JN!7MM% ! GGGL#*9N5L Qeii(=>D"EP599[3I3ORPPQ /5SU		/8R5SS 99%& +
 
 	M  p%#"%k"2&k"%m"4!$%8!9""S&"SSHHH"<0 $VL,>,>,@%A B#F>+?+?+A$BCH +  Ts   G.G.%G3c                V    dj                  | j                  dg       d |       }|xs dS )Nz / rW  -)rs   r   )r   r   samples      rD   text_sampler    s,    ZZ		,3FU;<F=SrG   c                h    | j                  d      xs g }|sydj                  d |d d D              S )NrR  r  , c              3  6   K   | ]  }|d     d|d      yw)r   r
  r   NrA   rk  s     rD   r   z'source_value_summary.<locals>.<genexpr>  s,       z?
1S\N+s   r   r   rs   )r   r   s     rD   source_value_summaryr    s?    YY~&,"F99 "1:  rG   c                \    | j                  d      xs g }dj                  |d d       xs dS )Nr   r  r   r  r  )r   namess     rD   resource_summaryr    s/    IIo&,"E99U2AY&3&rG   c                    | j                  d      xs i dD cg c]  }j                  |      r| }}dj                  fd|D              xs dS c c}w )Nr"  )r"   r    r#  r   r!   r  c              3  2   K   | ]  }| d |      yw):NrA   )r   r   r   s     rD   r   z!opcode_summary.<locals>.<genexpr>  s!     Lfxq 01Ls   r  r  )r   r   interestingr   s      @rD   opcode_summaryr    sd    YY,-3F ?::f 	K 
 99LLLSPSSs   Ac                   dj                  d | j                  dg       d d D              }| j                  dd      dkD  r|d| d   dz
   dz  }d	t        j                  | d
          dt        j                  | d          dt        j                  | d          dt        j                  | d          d| d    d| d    dt        j                  t	        |              dt        j                  t        |              d| dS )N
c              3     K   | ]9  }d t        j                  |d          dt        j                  |d          d ; yw)z
<li><code>r   z</code> r|   z</li>N)htmlescaperk  s     rD   r   z$block_detail_html.<locals>.<genexpr>  sD       T[[W./xCK8P7QQVWs   ?Ar   P   r   r   z<li>... z more lines</li>z<details><summary>rK  rl   r'   rM  ..rN  z</summary><p>source/render: r4  /r5  z; source values: z; resources: z</p><ol>z</ol></details>)rs   r   r  r  r  r  )r   r   s     rD   block_detail_htmlr    s&   II 99["-cr2 E yy!$r)8E/2R788HII
T[[y)9:;1;;u-./
0;;u\*+
,Bt{{5;L/M.N O"#7895AU;V:W X++&:5&ABC Dkk"25"9:; <g_	&rG   c                   g }| j                  d      xs g D ]  }|j                  dt        j                  |d          dt        j                  |d          dt        j                  |d          dt        j                  |d	          d
|d    d|d    d
t        j                  t	        |             d
t        j                  t        |             d
t        j                  t        |             d
t        j                  t        |d             d        dj                  d | j                  d      xs g D              }dj                  dddddddddddt        j                  | d          dd| d     d!| d"    d#| d$    d%| d&    d'| d(    d)d*d+d,j                  |      xs d- d.d/|xs d0d1d2d,g      S )3Nry  z<tr><td><code>rK  z</code></td><td><code>rM  r  rN  z</code></td><td>r'   z	</td><td>r4  r  r5  r   z
</td></tr>r  c              3  2   K   | ]  }t        |        y w)N)r  rh  s     rD   r   zhtml_page.<locals>.<genexpr>  s     ^-e4^s   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>Event Dialogue Blocks</title>a    <style>body{font-family:system-ui,sans-serif;background:#101010;color:#eee;margin:24px}table{border-collapse:collapse;width:100%;margin-bottom:24px}td,th{border:1px solid #333;padding:6px 8px;vertical-align:top}th{background:#1d1d1d}code{color:#f5d76e}details{border:1px solid #333;margin:10px 0;padding:8px;background:#161616}summary{cursor:pointer;font-weight:600}li{margin:3px 0}</style>z</head>z<body>z   <h1>Event Dialogue Blocks</h1>z  <p>rx  z</p>z  <p>Blocks: rs  z; dialogue-like: rt  z; route-linked: ru  z; text lines: r    (rV  z unique).</p>z  <table><thead><tr><th>block</th><th>range</th><th>class</th><th>source/render</th><th>source values</th><th>opcodes</th><th>resources</th><th>sample text</th></tr></thead>z	  <tbody>ro   z9<tr><td colspan="8">No dialogue-looking blocks.</td></tr>z</tbody></table>z  <h2>Block Text</h2>z<p>No block text.</p>z</body>z</html>)	r   rr   r  r  r  r  r  r  rs   )summaryr   r   detail_rowss       rD   	html_pager    s$   DX&," 
U9%567 8U<%89:"T[[zIZ=[<\ ];;u%5678 9-./q7K1L0M N;;3E:;< =;;~e456 7;;/678 9;;{5!456 7		

 ))^'++hBWB][]^^K99"P0 	S*
GL12348
-..?H`@a?b c !89:.Q`IaHb c)*+=	: 	x
BGGDMb%bccst..
+  rG   c                   g }| j                  d      xs g D ]  }|j                  |d   |d   |d   |d   |j                  d      xs g |j                  d      xs g |j                  d      xs g |j                  d	      xs g |j                  d
      xs g |j                  d      xs i |j                  d      xs g D cg c]  }|d   	 c}d        |S c c}w )Nry  rK  r'   rM  rN  rP  rR  r   r   rU  rX  r   r|   )rK  r'   rM  rN  rP  rR  r   r   rU  rX  r   )r   rr   )r  ry  r   r   s       rD   runtime_blocksr    s    FX&," Y'#$45-j) %		*< = C!IIn5;"YY7=2;/52		*-3yy+1r-2YY{-C-IrJcc&kJ
 	 M Ks   Cc                <   |j                  dd       |dz  j                  t        j                  | dd      dz   d	       |d
z  j                  t	        |       d	       |dz  j                  dt        j                  t        |       dd      z   dz   d	       y )NT)parentsexist_okzevent_dialogue_blocks.jsonF),r  )ensure_ascii
separatorsr  zutf-8)encodingevent_dialogue_blocks.htmlz event_dialogue_blocks_runtime.jsz&window.HWANSE_EVENT_DIALOGUE_BLOCKS = z;
)mkdir
write_textjsondumpsr  r  )r  out_dirs     rD   write_outputsr    s    MM$M.++77

7:FM 8  ++77	'8JU\7]11==0
**^G,5Z
X	Y
	 	 > rG   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                  i             }t        ||j                         t        d|j                  dz   d	|d
    d       y )Nz--exezHwanse2.exe)typedefaultz--event-text-source-flowzevent_text_source_flow.jsonz	--out-dirzwrote event dialogue blocks -> r  r  rs  z blocks))argparseArgumentParseradd_argumentr   ROOTOUT
parse_argsr  rS   
read_bytesr
   r   r  r  print)parserargsr  s      rD   mainr  #  s    $$&F
dD=4HI
2sMjGjk
$<D$--r2G '4<<(	)<<66
7 8L!"(	,rG   __main__)rC   r   returnr   )rL   r   r  r@  )rS   bytesrT   r   r  
int | None)rW   r  rT   r   r  r  )r\   
list[dict]rZ   r   r  dict | None)r\   r  rd   r   re   r   r  ztuple[int, int, int] | None)rt   r   r  r   )r|   r   r  r   )rW   r  r   r   r  r  )r   rY  r  r  )r   r  r  r  )rW   r  r  r   )   )rS   r  r\   r  r   dict[int, str]rd   r   re   r   r   r   r  r  )r\   r  rT   r   r  r  )r   r  r  r  )r   )r   r  r   r   r  z	list[str])r   rY  r  r   )r   rY  r  r  )
r   rY  rA  r  rW   r  r   r   r  rY  )rS   r  r\   r  r   r  r[  r  r\  rY  r   r   r  r  )rS   r  r   rY  r  rY  )r   )r   rY  r   r   r  r   )r  rY  r  r   )r  rY  r  r  )r  rY  r  r   r  None)r  r  )F__doc__
__future__r   r  r  r  rz   rQ   collectionsr   pathlibr   probe_exe_scene_tablesr   r   r   r	    summarize_event_text_source_flowr
   __file__resolver  r  r  r   r   r   compiler   rZ  r   r  r   rE   rJ   rM   rU   rX   r`   rj   rw   r}   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r%  rG  re  r  r  r  r  r  r  r  r  r  r  __name__rA   rG   rD   <module>r     s   V "    	    ^ ^ 6 H~''*
Ul	
"**?
'
  	
# 	
"	
 	
+ 	
% 	
$ 	
+ 	
* 	
2 	
' 	
. 	
, 	
# 	
&  	
"!$ 4  %(l	 %+n	 %*j	 %-k	 %-[	 %,m	 %3m	 %6c	 %/o	 %+g	 %*m	{C H H451
M
4$ 	  	
   B	"	'T&2j0'TH	HH H 	H
 H H HV1h
'
T$&R& 25 $ zF rG   