
    2hR?                        d Z ddlZej                            dddd          Zej                            dd          Zej                            d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ddlZddlZddlZddlmc mZ ddlZddlZd Zd Zd Z d%dZ!d Z"eeeefdZ#d Z$d Z%d Z&eefd Z'eefd!Z(d" Z)d# Z*d$ Z+dS )&z
This script can be used to help validate that all of the:
(1) animations used in animation groups are in fact available
(2) audio events used in animations are in fact available.
    Nzvictor-audio-assetsmetadataDev_MaczSoundbanksInfo.xmlzanimation-assets
animationsanimationGroupsNameRobotAudioKeyFrame	audioNameaudioEventIdtriggerTime_msdurationTime_mseventGroupseventIds
SoundBanksIncludedEventsId
Animationsc                     g }t          j        |           D ]3\  }}d |D             }|                    fd|D                        4|S )Nc                 <    g | ]}|                     d           |S )z.tar)endswith.0xs     E/home/metal/girl-os-victor/project/buildScripts/validate_anim_data.py
<listcomp>z!get_tar_files.<locals>.<listcomp>5   s)    @@@1QZZ-?-?@Q@@@    c                 P    g | ]"}t           j                            |          #S  )ospathjoin)r   r   dir_names     r   r   z!get_tar_files.<locals>.<listcomp>6   s)    KKKAbgll8Q77KKKr   )r   walkextend)root_dirall_tar_filessubdir_list	file_list	tar_filesr!   s        @r   get_tar_filesr)   2   so    M,.GH,=,= M M(+y@@	@@@	KKKKKKKLLLLr   c                     i }| D ]b}t           j                            |          }||v r7||                             |           t	          d|d||                    \|g||<   c|S )NzALERT: Multiple 'z' files found: )r   r   basenameappendprint)r'   	file_dictone_file	file_names       r   fill_file_dictr1   :   s    I . .G$$X..		!!i ''111EYYY	R[H\H\]____$,:Ii  r   c                    g }| rt           j                            |           st          d| z            t	          j                    }t          j        |           5 }|                    |           |D ]<}t           j        	                    ||j
                  }|                    |           =	 d d d            n# 1 swxY w Y   |S )NzInvalid TAR file provided: %s)r   r   isfile
ValueErrortempfilemkdtemptarfileopen
extractallr    namer,   )tar_fileunpacked_filesdest_dirtarmembers        r   unpack_tarballr@   F   s   N E27>>(33 E88CDDD!!H	h		 *3x    	* 	*FW\\(FK88F!!&))))	** * * * * * * * * * * * * * *
 s   AB??CCTc                    t          j        |           }g }|D ]}|                    t           j                  r"t           j                            | |          }t           j                            |          r$|                    t          ||                     |r|	                    |           |	                    |           |S )z
    Given a directory path, eg. "EXTERNALS/animation-assets/animationGroups",
    this function will recursively search that directory and return a list
    of all animation groups (.json files) that it finds.
    )
r   listdir
startswithextsepr   r    isdirr#   get_anim_groupsr,   )anim_group_dirreturn_full_pathsdir_contentsanim_groups
anim_group	full_paths         r   rF   rF   S   s     :n--LK" 
/ 
/
  ++ 	GLL<<	7==## 	/y:KLLMMMM  /""9----"":....r   c                    t          | d          5 }|                                }t          j        dt          j        |          }t          j        dt          j        |          }t          j        |          }d d d            n# 1 swxY w Y   |t                   }d |D             }t          j	        
                    |           }t          j	                            |          d         }||fS )Nrz//.*\nz#.*\nc                 (    g | ]}|t                    S r   )ANIM_NAME_ATTRr   s     r   r   z+get_clips_in_anim_group.<locals>.<listcomp>q   s    888!N#888r   r   )r8   readresubr   linesepjsonloadsANIM_GROUP_JSON_TOP_KEYr   r+   splitext)	json_filefh	json_datarK   
anim_clipsanim_group_names         r   get_clips_in_anim_groupr^   j   s   	i		 +GGII	F9bj)<<	F8RZ;;	Z	**
	+ + + + + + + + + + + + + + +
 34J88Z888Jg&&y11Og&&77:OZ((s   A)BB
B
c                 j   g }| rt           j                            |           st          d| z            t	          | d          5 }|                                }ddd           n# 1 swxY w Y   |                                }t          j        |          }|	                    |          D ]}	|		                    |          D ]k}
|
D ]f}|
                    |          }|                                }|
                    |          }t          |          }|                    ||f           gl|S )z`
    Use this function to parse the SoundbanksInfo.xml file that contains audio event info.
    zInvalid XML file provided: %srN   N)r   r   r3   r4   r8   rQ   stripET
fromstringitergetlowerintr,   )xml_filesound_banks_attrincluded_events_attraudio_event_name_attraudio_event_id_attrall_audio_eventsrZ   xml_datarootsound_banks
all_eventsevent
event_nameevent_ids                 r   ,get_audio_events_in_soundbanks_info_xml_filert   w   s     E27>>(33 E88CDDD	h		 7799              ~~H=""Dyy!122 
@ 
@%**+?@@ 		@ 		@J# @ @ #YY'<==
'--//
 99%899x== ''X(>????@		@    A''A+.A+c           
         d |D             }g }g }| rt           j                            |           st          d| z            t	          | d          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |                                          D ]\  }}t          |          }|D ]}		 t          |	t                             }
n# t          $ r Y *w xY w|
t          k    r	 |	t                   }|	t                   }t          |||||           h# t          $ rP g }t           |	v rB|	t                    D ]4}|                    |t$                              t'          ||||           5Y w xY wƌ||fS )a  
    Given an animation JSON file and a list of all available audio
    events, this function will parse the animation file to
    determine what audio events it uses.  It will then return a
    tuple of two lists:

    (available_events, unavailable_events)

    The first list is valid audio events in the animation. The second
    list is the audio events in the animation that are unavailable.
    c                     g | ]
}|d          S )   r   r   s     r   r   z1get_audio_event_usage_in_anim.<locals>.<listcomp>   s    BBBqtBBBr   Invalid JSON file provided: %srN   N)r   r   r3   r4   r8   rU   loadlistitemsstrKEYFRAME_TYPE_ATTRKeyErrorAUDIO_KEYFRAME_TYPEAUDIO_EVENT_NAMES_ATTRAUDIO_EVENT_ID_ATTR_check_using_event_nameEVENT_GROUPS_ATTRr#   EVENT_IDS_ATTR_check_using_event_id)rY   all_available_eventsall_available_event_idsavailable_eventsunavailable_eventsrZ   contents	anim_clip	keyframeskeyframekeyframe_typeaudio_events	audio_idseventss                 r   get_audio_event_usage_in_animr      s>    CB-ABBB GBGNN955 G9IEFFF	i		 !9R==! ! ! ! ! ! ! ! ! ! ! ! ! ! ! $X^^%5%5 6 6 R R	9	NN	! 	R 	RH #H-?$@ A A   
   333R#+,B#CL !))< =I+L)EY,<>PR R R R   X X X "I(H44&./@&A X XF%,,VN-CDDD1)=T2BDVX X X XX 4	R4 011s7   A55A9<A9:C
C"!C"1DAE98E9c                     | D ]D}t          |          }||v r|                    d |f           -|                    d |f           Ed S )N)rf   r,   )r   r   r   r   audio_ids        r   r   r      sj     8 8x==...##T8$45555%%tX&677778 8r   c                    t          |           t          |          k    rt          dt          dt                    t	          t          |                     D ]s}t          | |                                                   }t          ||                   }||f|v r|                    ||f           \|                    ||f           td S )NzBad audio keyframe in z5 has mismatched number of audio event IDs and names: )	lenr4   rY   r   ranger}   re   rf   r,   )r   r   r   r   r   idxaudio_eventr   s           r   r   r      s    
<C	NN**j6?iiK L L 	LS&&'' 
? 
? ,s+,,2244y~&&"&:::##[($;<<<<%%{H&=>>>>
? 
?r   c           	         d}g }t           j                            | |          }t          |          }|sd|z  }t	          ||z            t          |          }t          |                                          D ]\  }	}
|
d         }t          |          }|D ]W}|	                    t           j        
                    t           j                            |                    d                    X	 t          t          t           j        |                     t          j        t           j                            |d                              # t           $ r}t#          d|z             Y d}~d}~ww xY wi }t           j                            | |          }t%          |          }|sd|z  }t	          ||z            |D ]}	 t'          |          \  }}n%# t          $ r d|z  }t	          ||z            w xY wg }|D ]}||vr|	                    |           |r$t           j                            |          }|||<   |rd}t)          ||           dS dS )	z
    This function will raise ValueError with relevant info if any
    animation groups use any animations that are unavailable.
    zBUnable to validate animations used in animation groups because: %sNo tar files available in %sr   6WARNING: Failed to cleanup temp files or directory: %sNz#No animation groups available in %szUnable to parse %szDFound unavailable animations used in the following animation groups:)r   r   r    r)   r4   r1   r{   r|   r@   r,   rX   r+   mapremovermdirdirnameOSErrorr-   rF   r^   report_problems)externals_diranim_assets_diranim_group_assets_dirproblem_msg	all_animstar_files_dirr(   	this_probtar_file_dictr0   
file_paths	file_pathr<   rY   eproblemsanim_groups_dirrJ   rK   r]   r\   unavailable_animsr   	msg_titles                           r   check_anims_all_anim_groupsr      s    WK IGLL@@Mm,,I 22]B	y0111"9--M!%m&9&9&;&;!<!< 	R 	R	:qM	'	22' 	O 	OIRW--bg.>.>y.I.IJJ1MNNNN	RRY//000HRW__^A%6778888 	R 	R 	RKaOQQQQQQQQ	R
 Hgll=2GHHO!/22K 29OK	y0111! : :
	6*A**M*M'OZZ 	6 	6 	6,z9I[94555	6 # 	4 	4I	))!((333 	: g..z::O(9H_% -Z	),,,,,- -s%   4AE
E9E44E9G"H c                    d}t           j                            | |          }	 t          |          }n<# t          t
          t          t          j        f$ r}t          ||z            d}~ww xY wi }t           j                            | |          }t          |          }|sd|z  }	t          ||	z            t          |          }
t          |
                                          D ]\  }}|d         }t          |          }|D ];}t          ||          \  }}|r$t           j                            |          }|||<   <	 t          t!          t           j        |                     t          j        t           j                            |d                              # t
          $ r}t)          d|z             Y d}~d}~ww xY w|rd}t+          ||           dS dS )z
    This function will raise ValueError with relevant info if any
    animations use any audio events that are unavailable.
    z>Unable to validate audio events used in animations because: %sNr   r   r   z@Found unavailable audio events used in the following animations:)r   r   r    rt   IOErrorr   r4   ra   
ParseErrorr)   r1   r{   r|   r@   r   r+   r   r   r   r   r-   r   )r   r   soundbanks_xml_filer   rl   r   r   r   r(   r   r   r0   r   r   r<   rY   r   r   	anim_namer   s                       r   check_audio_events_all_animsr     s    SK ',,}6IJJ*GH[\\Wj"-8 * * *q)))*
 HGLL@@Mm,,I 22]B	y0111"9--M!%m&9&9&;&;!<!< R R	:qM	'	22' 	9 	9I3PQZ\l3m3m00! 9G,,Y77	&8#	RRY//000HRW__^A%6778888 	R 	R 	RKaOQQQQQQQQ	R  -V	),,,,,- -s.   4 "A-A((A-AF$$
G
.GG
c                 h   g }t          |                                           D ]\  }}g }|D ]X}t          |t                    r|                    |           -|d         }|d         }|                    |d|d           Y|r2d|z  }	|	d                    |          z  }	|                    |	           |                                 t          j        dz  }	|	|z  }	|	t          j        z  }	|	t          j                            |          z  }	|	t          j        z  }	t          |	           d S )Nr   rx   z ()z	%s uses: z,    )
r{   r|   
isinstancer}   r,   r    sortr   rT   r-   )
r   r   msgs	containerunavailable_contentsformatted_contentscontentcontent_name
content_idmsgs
             r   r   r   H  sB   D+/0@0@+A+A  '	'+ 	R 	RG'3'' R"))'2222&qz$QZ
"))|||ZZZ*PQQQQ 		)C499/000CKKIIKKK
*q.C9C2:C2:??4   C2:C	#JJJJJr   c                     d}| D ]W}	 |t                    }n# t          $ r Y w xY w	 |t                   }n# t          $ r d}Y nw xY w||z   }t          ||          }X|S )Nr   )TRIGGER_TIME_ATTRr   DURATION_TIME_ATTRmax)keyframe_listanim_lengthr   trigger_time_msduration_time_mskeyframe_length_mss         r   get_anim_lengthr   b  s    K! 
; 
;	&'89OO 	 	 	H		!'(:; 	! 	! 	! 	!,/??+'9::s   
##5AAc                    i }| rt           j                            |           st          d| z            t	          | d          5 }t          j        |          }d d d            n# 1 swxY w Y   t          |                                          D ]v\  }}t          |          }t          |          }t          |t                    s9|t          |          k    rt          |          }nt          d|d|d           |||<   w|S )Nry   rN   zWARNING: The length of 'z' is not an integer (length = r   )r   r   r3   r4   r8   rU   rz   r{   r|   r}   r   r   rf   r-   )rY   anim_name_length_mappingrZ   r   r   r   r   s          r   get_anim_name_and_lengthr   r  sV   ! GBGNN955 G9IEFFF	i		 !9R==! ! ! ! ! ! ! ! ! ! ! ! ! ! ! $X^^%5%5 6 6 	: 	:	9	NN	%i00+s++ 	3c+....!+.."KKK1 3 3 3.9 ++##ru   )T),__doc__r   r   r    SOUNDBANKS_XML_FILEANIM_ASSETS_DIRANIM_GROUP_ASSETS_DIRr~   r   r   r   r   r   r   r   SOUND_BANKS_XML_ATTRINCLUDED_EVENTS_XML_ATTRAUDIO_EVENT_NAME_XML_ATTRAUDIO_EVENT_ID_XML_ATTRrW   rP   rR   r7   rU   xml.etree.ElementTreeetreeElementTreera   r5   zipfiler)   r1   r@   rF   r^   rt   r   r   r   r   r   r   r   r   r   r   r   <module>r      s#    
			 gll#8*iQeff  ',,1<@@ %79JKK   * $ $ $ & !  $ + "   '  
			   " " " " " " " " "    	 	 	
 
 
   .
) 
) 
) MaF^G`E\   >02 02 02f8 8 8? ? ?$ @O6K3- 3- 3- 3-l AP5H*- *- *- *-Z  4   $ $ $ $ $r   