
    jRjiE                    x   d Z ddlm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  ee      j                         j                  d   Zedz  Zed	z  Zed
z  ZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"g dZ#d(d)dZ$d*dZ%d+dZ&d,dZ'd-dZ(d.d Z)d/d!Z*d0d"Z+d0d#Z,d1d$Z-d2d%Z.d3d&Z/e0d'k(  r e/        yy)4a  Summarize the grounded selected-root scenario VM pattern.

This report consolidates the scattered selected-root/selector-root evidence into
one narrow page.  It intentionally distinguishes proven VM primitives from the
still-missing field/scene trigger producer.
    )annotationsN)Path)Any)read_sectionsva_to_offset   zHwanse2.exeoutwebi8D i!#@ i`#@ im@ i@ iɭ@ i@ iYC i0Y i U @   D   \         ))selected_root_live_writer_frontier_review"selected_root_opcode_matrix_reviewselector_root_structure_review%scene_event_vm_execution_route_review(scene_event_vm_command_stream_candidatesc                    | y d| d| dS )N0x0x )valuewidths     4tools/build_scenario_selected_root_pattern_review.pyhxr   3   s"    }awaZ !!    c                V    t        j                  | dd      S t        |       d      S )N T)quote)htmlescapestr)r   s    r   hr%   9   s%    ;;U]r$GGE
$GGr   c                    t         |  dz  }|j                         si S t        j                  |j	                  d            S )N.jsonutf-8encoding)OUTexistsjsonloads	read_text)namepaths     r   	load_jsonr2   =   s:    D6D;;=	::dnngn677r   c                N    t        ||      }|y| |||z    j                  d      S )Nr     )r   hex)exesectionsvasizeoffs        r   
read_bytesr;   D   s2    
x
$C
{sS4Z $$S))r   c                p   t        j                  d|      }g }| j                  |      }|dk\  rd }|D ]E  }t        |d         }t        |d         }	||cxk  r	||	z   k  s.n 1t        |d         ||z
  z   } n ||j	                  t        |      xs d       | j                  ||dz         }|dk\  r|S )N<Ir   rawraw_sizer8   r    r   )structpackfindintappendr   )
r6   r7   r   needlerefsposr8   sectionr>   r9   s
             r   
dword_refsrI   K   s    [[u%FD
((6
C
( 	Ggen%Cwz*+Dc&C$J&'395	 >KK2"%hhvsQw' ( Kr   c                v    t        ||      }||dz   t        |       kD  ry t        j                  d| |      d   S )N   r=   r   )r   lenr@   unpack_from)r6   r7   r8   r:   s       r   dword_atrN   ]   s?    
x
$C
{cAgC(dC-a00r   c           	         t        | ||      }g }|D ]S  }t        |d      }t        |cxk  rt        dz   k  s&n )|t        z
  dz  }|j                  ||t	        |d      d       U t	        |      ||dS )N   i   rK      )
entryVaHexopcodeIndexopcodeIndexHex)handlerVaHexdispatchRefsallDwordRefs)rI   rC   GENERAL_DISPATCH_TABLE_VArD   r   )r6   r7   
handler_varF   dispatch_refsrefref_intindexs           r   dispatch_entryr^   d   s    c8Z0DM 
c2,$S2Ke2SS88Q>E  "%#(&(l	
 :% r   c                 6   t         D  ci c]  } | t        |        }} |d   }|d   }|d   }|d   }|d   }|j                  di       }|j                  di       }|j                  di       }	|j                  d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i       j                  d      d|j                  di       j                  d      d|j                  d      d|j                  d      d|j                  d      d|j                  d      d|	j                  d      |
j                  d      |
j                  d      |
j                  d      |
j                  d      dS c c} w ) Nr   r   r   r   r   summaryselectorRootCountselectorRowsrootsWithSceneSeqCountrootsWithResourceRefCount&sequenceRootWithResourceStructureCountunclassifiedSelectorRootCount rootsWithSelectedRootOpcodeCountopcode4fMode1WriterCountopcode4fSelfWriterCountopcode4fSlotAliasCountselectedRootDirectRefCountdirectRefCountselectedRootExecutorFoundliveWriterStatusstatusopcodeMatrixStatusselectorStructureStatuspromotionStatusrouteReviewStatusdirectExecutionRootFoundcommandBlockCountdialogueLikeBlockCountrouteLinkedCommandBlockCountmapLinkedCommandBlockCount)ru   rv   rw   rx   )RELATED_REPORTSr2   get)r0   relatedselector_structureopcode_matrixlive_writerroute_reviewcommand_candidatesselector_summaryopcode_summaryroute_summarycommand_summarys              r   summarize_relatedr   y   s   1@AtYt_$AGA !AB@AMEFKBCL !KL)--i<"&&y"5N $$Y3M(,,Y;O-112EF(,,^< 	!"2"6"67O"P 	$%5%9%9:U%V	
 	12B2F2FGo2p 	()9)=)=>])^ 	+N,>,>?a,b 	#N$6$67Q$R 	">#5#56O#P 	!."4"45M"N 	%kooi&D&H&HIY&Z 	$[__Y%C%G%GHc%d 	KOOH5 	m//9 	"#5#9#9:K#L  	\--.?@!" 	#M$5$56P$Q#$ -001DE"1"5"56N"O(7(;(;<Z([&5&9&9:V&W+  Bs   Hc                    t         j                         } t        |       }t               }t	        | |t
              t	        | |t              t	        | |t              t	        | |t              t	        | |t              d}dt        t
              t        | |t
        d      ddddt        t              t        | |t        d      dddd	t        t              t        | |t        d
      ddddt        t              t        | |t        d      ddddt        t              t        | |t        d      ddddt        t              t        | |t        d      ddddt        t              t        | |t        d      dddg}t        t              t        | |t              dt        t              t        | |t              dd}g d}g d}g d}ddd d!t        t              t        t              t        t         d"      t        t"        d"      t        t$        d"      t        t&        d"      t        t(        d"      d#|||||||t*        D 	cg c]  }	d$|	 d%d&|	 d'd( c}	g d)d*S c c}	w )+N)vmDispatcherselectorByteWriterselectedRootIndexedWriterselectedRootExecutorselectedRootDirectWriterzgeneric VM dispatcher?   z\reads object+0x40 as current stream, reads opcode byte, dispatches through table 0x00440538.	confirmed)labelvaHexbytesdecodedro   znested VM stream runnerzltemporarily replaces object+0x40 with an argument stream, runs the dispatcher, then restores the old stream.zselector byte writer   zzmode 1 writes selector bytes from stream operands and jumps to a fixed root pointer; modes 2/3 delegate save/load restore.zselected root indexed writer.   zqselected_root = dword[streamTable + stream[1] * 4], where streamTable is dword at stream+4; advances stream by 8.zselected root executorE   zif selected_root is nonzero, pushes current continuation to object+0x44 return stack, increments object+0x5c, and writes selected_root to object+0x40.zselected root direct writerp   zamode 0 stores inline stream+4 as selected_root; mode 1 stores dword at stream+4 as selected_root.zroot object pool initializerJ   zoinitializes the object pool/list around 0x0055be00, matching the VM object base seen in selected-root handlers.zconfirmed-base)r   	dwordRefs)selectedRoot
rootObject)zKThe VM dispatcher at 0x00402321 executes bytecode streams from object+0x40.zTselected_root at 0x0059de30 is a real global consumed by the selected-root executor.zk0x0040ad9b writes selected_root by table index; 0x0040ae0e writes it directly or as an inline continuation.zg0x0040adc9 pushes the current stream to object+0x44/object+0x5c and jumps object+0x40 to selected_root.z0x00406dbb writes selector bytes and jumps to fixed roots, so selector bytes and selected roots are part of the same VM family.ziExisting selector-root reports already expose dozens of roots and command blocks that fit this mechanism.)zfield/NPC/region triggers likely enter this VM through active descriptors or inline stream pointers, but the exact live producer is not promoted here.zobject+0xa8 appears repeatedly in VM handlers as a context/base pointer family; object+0xb0 remains a script-pointer candidate, not a confirmed stream slot in this review.zselectedRoot indexed tables and direct inline roots are good static enumeration targets, but each entry still needs owner/trigger binding.)zNo static proof yet maps a specific field entrance, talk target, or region descriptor to one selected_root write in normal gameplay.zscene/event command streams are found, but route-linked command block count remains zero in the current command-stream candidate report.zyautomatic map transition and scenario branch producer remain separate problems from the selected-root executor mechanism.z,hwanse-scenario-selected-root-pattern-reviewz4tools/build_scenario_selected_root_pattern_review.pyz;scenario-selected-root-vm-pattern-grounded-producer-pendinga  The web-runtime observation is consistent with the EXE.  A common VM runner, selected-root writers, and selected-root executor are statically grounded.  The remaining missing evidence is the upstream producer that chooses those streams from concrete field/NPC/region conditions.rQ   )selectedRootVaHexrootObjectVaHexcurrentStreamOffsetHexreturnStackOffsetHexreturnDepthOffsetHexvmContextOffsetCandidateHexscriptPointerOffsetCandidateHexzout/r'   zweb/z.html)r-   r"   )zOEnumerate opcode 0x81 indexed-writer command rows and group them by owner root.ztEnumerate opcode 0x83 direct-writer inline roots and split mode 0 inline continuation from mode 1 dword-root writes.zCross-link selected-root writer rows to prompt/text groups and resource records, but keep them below route-proof until a producer is found.zeUse future runtime traces to tag which writer row fired before a known talk/region/event interaction.)kindsourcero   decisionglobalsdispatchbyteEvidence
globalRefsr   
candidatesblockedrelatedSummaryrelatedReportsnextStaticActions)EXEr;   r   r   r^   VM_DISPATCHER_VASELECTOR_BYTE_WRITER_VASELECTED_ROOT_INDEXED_WRITER_VASELECTED_ROOT_EXECUTOR_VASELECTED_ROOT_DIRECT_WRITER_VAr   VM_NESTED_RUNNER_VAOBJECT_POOL_INIT_VASELECTED_ROOT_VArI   ROOT_OBJECT_VACURRENT_STREAM_OFFSETRETURN_STACK_OFFSETRETURN_DEPTH_OFFSETVM_CONTEXT_OFFSET_CANDIDATESCRIPT_POINTER_OFFSET_CANDIDATEry   )
r6   r7   related_summaryr   byte_evidenceglobal_refsr   r   r   r0   s
             r   build_reportr      s   
..
CS!H')O 'sH6FG,S(<ST%3CCb%c .sH>W X$23B`$aH -()X/?Fu!	
 /+,X/BDI F!	
 ,/0X/FM T!	
 478X/NPTU K!	
 .12X/H$O p!	
 367X/MtTz!	
 4+,X/BDI I&	
W2Ml ()#C3CD

 '#C>B
	KIJ
G ?HO]
 "$$4!5!.1&()>&B$&':A$>$&':A$>+-.I1+M/12QST/U
 %! )`opX\d4&$6$tfE@RSp
5   2 qs   I"c                    dj                  d |D              }g }| D ]1  dj                  fd|D              }|j                  d| d       3 d| ddj                  |       dS )	Nr    c              3  @   K   | ]  \  }}d t        |       d  yw)z<th>z</th>Nr%   ).0_r   s      r   	<genexpr>zrender_table.<locals>.<genexpr>!  s!     BhaT!E(5)Bs   c              3  `   K   | ]%  \  }}d t        j                  |             d ' yw)z<td>z</td>N)r%   rz   )r   keyr   rows      r   r   zrender_table.<locals>.<genexpr>$  s+     L&#q$q/u5Ls   +.z<tr>z</tr>z<table><thead><tr>z</tr></thead><tbody>z</tbody></table>)joinrD   )rowscolumnsheadbodycellsr   s        @r   render_tabler      su    77B'BBDD )LGLLd5''()  v%9"''$-HXYYr   c                0   | d   D cg c]  }|d   |d   |d   |d   d }}| d   j                         D cg c]
  \  }}||d }}}g }| d	   j                         D ]k  \  }}|j                  d
      xs g }dj                  d |D              xs d}	dj                  d |D              xs d}
|j                  ||d   |	|
d       m d(d}dt	        | d          dt	        | d          d || d          d || d          d || d          dt        | d   j                         D cg c]
  \  }}||d c}}ddg       dt        |g d        d!t        |g d"       d#t        |d$dg       d% || d&          d'S c c}w c c}}w c c}}w ))Nr   r   r   ro   r   )r   r8   ro   r   r   )r   r   r   rV   z, c              3  F   K   | ]  }|j                  d       xs d  yw)rT   r    Nrz   r   r[   s     r   r   zrender_html.<locals>.<genexpr>:  s"     LCGG$45;;L   !-c              3  F   K   | ]  }|j                  d       xs d  yw)rR   r    Nr   r   s     r   r   zrender_html.<locals>.<genexpr>;  s!     HCCGGL17R7Hr   rU   )r0   handleropcodesentriesc                >    ddj                  d | D              z   dz   S )Nz<ul>r    c              3  :   K   | ]  }d t        |       d  yw)z<li>z</li>Nr   )r   items     r   r   z/render_html.<locals>.bullets.<locals>.<genexpr>?  s     H$$qwiu 5Hs   z</ul>)r   )itemss    r   bulletszrender_html.<locals>.bullets>  s!    H%HHH7RRr   a  <!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Scenario selected-root pattern review</title>
  <style>
    body { margin: 0; background: #f6f7f9; color: #20242b; font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; line-height: 1.45; }
    main { width: min(1180px, calc(100vw - 28px)); margin: 0 auto; padding: 22px 0 36px; }
    h1 { margin: 0 0 8px; font-size: 26px; }
    h2 { margin: 18px 0 8px; font-size: 18px; }
    .panel { background: #fff; border: 1px solid #d9dee7; border-radius: 8px; padding: 14px; margin: 12px 0; }
    .status { display: inline-block; padding: 3px 8px; border-radius: 999px; background: #fff3cd; color: #7a5600; font-size: 12px; font-weight: 700; }
    code { background: #eef2f7; padding: 1px 4px; border-radius: 4px; }
    table { border-collapse: collapse; width: 100%; background: #fff; font-size: 13px; }
    th, td { border: 1px solid #dfe4ec; padding: 7px 8px; vertical-align: top; text-align: left; }
    th { background: #f0f3f8; }
    .grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
    .small { color: #687080; font-size: 13px; }
    @media (max-width: 900px) { .grid { grid-template-columns: 1fr; } table { display: block; overflow-x: auto; } }
  </style>
</head>
<body>
<main>
  <h1>Scenario selected-root pattern review</h1>
  <p><span class="status">z)</span></p>
  <div class="panel">
    <p>r   u   </p>
    <p class="small">JSON: <code>out/scenario_selected_root_pattern_review.json</code></p>
  </div>

  <div class="grid">
    <section class="panel">
      <h2>확정</h2>
      r   uH   
    </section>
    <section class="panel">
      <h2>후보</h2>
      r   uK   
    </section>
    <section class="panel">
      <h2>차단점</h2>
      r   u8   
    </section>
  </div>

  <h2>전역/오프셋</h2>
  r   )r   r0   )r   r   u&   

  <h2>디스패치 엔트리</h2>
  ))r0   r0   )r   z
handler VA)r   zopcode index)r   zdispatch entryu    

  <h2>바이트 근거</h2>
  ))r   r   )r8   VA)ro   ro   )r   zdecoded semanticsu'   

  <h2>관련 산출물 요약</h2>
  )r   metricu$   

  <h2>다음 정적 분석</h2>
  r   z
</main>
</body>
</html>)r   	list[str]returnr$   )r   rz   r   rD   r%   r   )reportr   evidence_rowskvr{   dispatch_rowsr0   rF   r   r   r   s               r   render_htmlr   )  sJ    .)  \g,(m9~		
M  +,224Aq AG  MJ'--/ u	cww~&,"))LtLLSPS))H4HHOCds>7JW^krst	uS2 VH-./ 0	&
  vk"#$ % vl#$% & vi !" #
 F94E4K4K4MNDAqQ'NQ`btPuvw x    I  J  K K yz{ | +-?@AB C 6%&'( )w> >/z Os   FF1F c                 j   t               } t        j                  d       t        j                  d       t        dz  j	                  t        j                  | dd      d       t        |       }t        d	z  j	                  |d       t        d	z  j	                  |d       t        | d
          y )NT)exist_okz*scenario_selected_root_pattern_review.jsonFrQ   )ensure_asciiindentr(   r)   z*scenario_selected_root_pattern_review.htmlro   )	r   r+   mkdirWEB
write_textr-   dumpsr   print)r   	html_texts     r   mainr     s    ^FIItIIItI77CC

6a8 D  F#I77CCIX_C`77CCIX_C`	&
r   __main__)   )r   
int | Noner   rC   r   z
str | None)r   r   r   r$   )r0   r$   r   dict[str, Any])
r6   r   r7   list[dict[str, Any]]r8   rC   r9   rC   r   r$   )r6   r   r7   r   r   rC   r   r   )r6   r   r7   r   r8   rC   r   r   )r6   r   r7   r   rY   rC   r   r   )r   r   )r   r   r   zlist[tuple[str, str]]r   r$   )r   r   r   r$   )r   None)1__doc__
__future__r   r"   r-   r@   pathlibr   typingr   probe_exe_scene_tablesr   r   __file__resolveparentsROOTr   r+   r   rX   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   r%   r2   r;   rI   rN   r^   r   r   r   r   r   __name__r   r   r   <module>r     s   #      > H~''*
]
Ul
Ul&    $ ", & !+       " "& "H8*$1*"JDZVr zF r   