@@ -9,6 +9,7 @@ use std::{
99 path:: Path ,
1010} ;
1111
12+ use base64:: write;
1213use fxhash:: {
1314 FxHashMap ,
1415 FxHashSet ,
@@ -601,6 +602,11 @@ where T: Write + Seek
601602 }
602603 write ! ( self , "]" ) ?; // costumes
603604 write ! ( self , r#","sounds":["# ) ?;
605+ let mut comma = false ;
606+ for sound in & sprite. sounds {
607+ write_comma_io ( & mut self . zip , & mut comma) ?;
608+ self . sound ( input, sound, d) ?;
609+ }
604610 write ! ( self , "]" ) ?; // sounds
605611 if let Some ( x_position) = & sprite. x_position {
606612 let x_position = x_position. evaluate ( ) ;
@@ -834,6 +840,40 @@ where T: Write + Seek
834840 write ! ( self , "}}" ) // costume
835841 }
836842
843+ pub fn sound ( & mut self , input : & Path , sound : & Sound , d : D ) -> io:: Result < ( ) > {
844+ let path = input. join ( & * sound. path ) ;
845+ let hash = self
846+ . costumes
847+ . get ( & sound. path )
848+ . cloned ( )
849+ . map ( Ok :: < _ , io:: Error > )
850+ . unwrap_or_else ( || {
851+ let mut file = match File :: open ( & path) {
852+ Ok ( file) => file,
853+ Err ( error) => {
854+ d. report ( DiagnosticKind :: IOError ( error) , & sound. span ) ;
855+ return Ok ( Default :: default ( ) ) ;
856+ }
857+ } ;
858+ let mut hasher = Md5 :: new ( ) ;
859+ io:: copy ( & mut file, & mut hasher) ?;
860+ let hash: SmolStr = format ! ( "{:x}" , hasher. finalize( ) ) . into ( ) ;
861+ self . costumes . insert ( sound. path . clone ( ) , hash. clone ( ) ) ;
862+ Ok ( hash)
863+ } ) ?;
864+ let ( _, extension) = sound. path . rsplit_once ( '.' ) . unwrap_or_default ( ) ;
865+ self . sound_entry ( & sound. name , & hash, extension)
866+ }
867+
868+ pub fn sound_entry ( & mut self , name : & str , hash : & str , extension : & str ) -> io:: Result < ( ) > {
869+ write ! ( self , "{{" ) ?;
870+ write ! ( self , r#""name":{}"# , json!( name) ) ?;
871+ write ! ( self , r#","assetId":"{hash}""# ) ?;
872+ write ! ( self , r#","dataFormat":"{extension}""# ) ?;
873+ write ! ( self , r#","md5ext":"{hash}.{extension}""# ) ?;
874+ write ! ( self , "}}" ) // costume
875+ }
876+
837877 pub fn proc ( & mut self , s : S , d : D , proc : & Proc , definition : & [ Stmt ] ) -> io:: Result < ( ) > {
838878 let this_id = self . id . new_id ( ) ;
839879 let prototype_id = self . id . new_id ( ) ;
0 commit comments