
    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Zddlm	Z	 ej                  j                  d e e	e      j                         j                               ddlmZmZ ddlmZmZmZ  ej.                  d      ZddZdd	Zdd
ZdddZddZddZedk(  r e        yy)a6  Scan scene records for pointer fields that look like coordinate tables.

This is intentionally broader than extract_scene_events.py. Many hits are
script/object argument tables rather than confirmed map triggers, but the output
is useful when comparing repeated scene records and looking for true transitions.
    )annotationsN)Path)load_map_sizesread_point_table)find_cns_stringsoffset_to_varead_sectionszmap\d+_\d+[a-z]\.cns$c                6    t        j                  d| |      d   S )N<Ir   )structunpack_from)dataoffsets     >/home/exedev/hwanse/tools/probe_scene_coordinate_candidates.pydword_atr      s    dD&1!44    c                4    d| cxk  xr dk  nc xr | dz  dv S )Nr   i      >   r       )values    r   looks_like_scene_idr      s     uxBUT\l$BBr   c                    t        d|      D ]U  }||dz  z   }|dz   t        |       kD  r |S |j                  t        | |            }|s=t        j                  |      sS|c S  |S )N      )rangelengetr   MAP_RE	fullmatch)r   stringshit
max_dwordsindexoffnames          r   next_map_field_indexr'   "   st    q*% EAIo7SY  {{8D#./F$$T*L r   c                   t        |       }t        | |      }t        |      }g }|j                         D ]  \  }}t        j                  |      s|d d }	|j                  |	      }
|
s6t        j                  d|      }d}	 | j                  ||      }|dk  rg|dz   }|dz   t        |       kD  r0t        | |dz         }t        | |dz         }|dk7  st        |      s_t        | |||      }g }t        d|      D ]  }||dz  z   }|dz   t        |       kD  r npt        | |      }d	|cxk  rd
k  sn 8t        | ||g|
 \  }}t        |      dk  rX|j!                  |t#        ||      |t        |      t        |      |d        |st#        ||      }||j!                  |	|d|d|||d       : |j%                  d        |S )Nr   r         r      r   i  @ i  `    )
fieldIndexfieldVapointTableVarawPointCount
pointCountpoints0x04x)mapsceneId
sceneIdHexrecordVarecordFileOffset
candidatesc                    | d   | d   fS )Nr9   r6   r   )items    r   <lambda>z$extract_candidates.<locals>.<lambda>q   s    4
#3T%["A r   )key)r	   r   r   itemsr   r    r   r   packfindr   r   r   r'   r   r   appendr   sort)r   extract_dirr#   sectionsr!   	map_sizesrecordsmap_vafilenamer&   sizeneedlesearchr"   scene_idzero
scan_limitr;   r$   r%   ptr_va
raw_pointsr3   	record_vas                           r   extract_candidatesrT   -   s   T"HtX.G{+IG#MMO <)}}}T"T6*))FF+CQw1WFRx#d)#cAg.HD#'*Dqy 3H =-dGS*MJJq*- EAIo7SY&!$,"f:
:%5dHf%Tt%T"
Fv;?!!&+#/##>(.),Z&)&k"(	* $Xs3I NN'$&xn"5 )(+",	S <| LLALBNr   c                   g d}| D ]  }dj                  d |d   d d D              }g }|d   d d D ]:  }dj                  d |d	   d d
 D              }|j                  d|d    d|        < |j                  d|d    d|d    d|d   dd| ddj                  |       d        |j                  d       dj                  |      S )N)z# Scene Coordinate Candidates zbGenerated by scanning scene records for pointer fields that decode as in-bounds coordinate tables.rV   zmThese are broad candidates, not confirmed transition triggers. Many short tables are script/object arguments.rV   z0| map | scene | record | fields | first points |z| --- | --- | --- | --- | --- |, c              3  8   K   | ]  }d |d    d|d      yw)ir.    n=r2   Nr   .0	candidates     r   	<genexpr>zmarkdown.<locals>.<genexpr>   s1      
 	,'(Il,C+DE
s   r;   r,   r    c              3  6   K   | ]  }|d     d|d      yw)x,yNr   )r\   points     r   r^   zmarkdown.<locals>.<genexpr>   s+       :,ac
|,s   r3      rY   r.   z: z| r6   z | r8   z | `0xr9   08xz` | z; z |rV   
)joinrC   )rH   linesr=   fieldsr3   r]   coordss          r   markdownrl   u   s   	E  
 
!,/3
 
 l+BQ/ 	CIXX &x0!4 F MMAi56bAB	C 	eSl!3 4 5z"3'tF83tyy7H6IM	

  
LL99Ur   c            
        t        j                  t              } | j                  dt        t	        d             | j                  dt        t	        d             | j                  dt        d	       | j                  d
t        d	       | j                  dt
        d       | j                  ddd       | j                         }t        |j                  j                         |j                  |j                        }|j                  r]|j                  j                  j                  dd       |j                  j                  t!        j"                  |dd      dz   d       |j$                  rM|j$                  j                  j                  dd       |j$                  j                  t'        |      d       |D cg c]#  }|j(                  r|d   |j(                  k(  s"|% }}t+        dt-        |       d       |d d D ]@  }dj/                  d |d    d d! D              }t+        |d"    d#|d    d$|d%   d&d#|        B t-        |      dkD  rt+        d't-        |      dz
   d(       y y c c}w ))N)descriptionz--exezHwanse2.exe)typedefaultz--extract-dirextract_fldz--outzwrite JSON output)ro   helpz--md-outzwrite Markdown summaryz--max-dwords(   z--map
map_filterz#only print records for one map stem)destrr   T)parentsexist_okFr-   )ensure_asciiindentrg   zutf-8)encodingr6   zfound z6 scene records with coordinate-like pointer candidatesx   rW   c              3  F   K   | ]  }d |d    d|d    d|d   d  yw)rY   r.   rZ   r2   z ptr=0xr0   rf   Nr   r[   s     r   r^   zmain.<locals>.<genexpr>   sH      
  	,'(Il,C+D E~.s35
s   !r;      r8   r_   z
 record=0xr9   rf   z... z more)argparseArgumentParser__doc__add_argumentr   int
parse_argsrT   exe
read_bytesrE   r#   outparentmkdir
write_textjsondumpsmd_outrl   rt   printr   rh   )parserargsrH   r=   shownsummarys         r   mainr      sI   $$9F
dD4GH
dD<OP
d1DE

4LM
S"=
l9^_D !4!4!68H8H$//ZGxxdT:DJJwU1MPTT_fg{{   =x07C%_dT__Ut@^T_E_	F3w<. V
WXds 	
)) 
 ",/3
 

 	L!"!DK= 1Z(-Qwi9	
	
 5zCSZ#%&e,-  `s   #I=,I=__main__)r   bytesr   r   returnr   )r   r   r   bool)
r   r   r!   zdict[int, str]r"   r   r#   r   r   r   )rs   )r   r   rE   r   r#   r   r   
list[dict])rH   r   r   str)r   None) r   
__future__r   r~   r   rer   syspathlibr   pathinsertr   __file__resolver   extract_scene_eventsr   r   probe_exe_scene_tablesr   r   r	   compiler   r   r   r'   rT   rl   r   __name__r   r   r   <module>r      s    #   	  
  3tH~--/667 8 A P P 
,	-5CEP>.D zF r   