@@ -9,6 +9,7 @@ use std::{
9
9
path:: Path ,
10
10
} ;
11
11
12
+ use base64:: write;
12
13
use fxhash:: {
13
14
FxHashMap ,
14
15
FxHashSet ,
@@ -601,6 +602,11 @@ where T: Write + Seek
601
602
}
602
603
write ! ( self , "]" ) ?; // costumes
603
604
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
+ }
604
610
write ! ( self , "]" ) ?; // sounds
605
611
if let Some ( x_position) = & sprite. x_position {
606
612
let x_position = x_position. evaluate ( ) ;
@@ -834,6 +840,40 @@ where T: Write + Seek
834
840
write ! ( self , "}}" ) // costume
835
841
}
836
842
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
+
837
877
pub fn proc ( & mut self , s : S , d : D , proc : & Proc , definition : & [ Stmt ] ) -> io:: Result < ( ) > {
838
878
let this_id = self . id . new_id ( ) ;
839
879
let prototype_id = self . id . new_id ( ) ;
0 commit comments