@@ -41,13 +41,23 @@ class Paths implements ConsoleClass
4141 return parts [0 ];
4242 }
4343
44- static function getPath (file : String , type : AssetType , library : Null <String >): String
44+ public static function fixPathExtension (path : String , defaultExtension : String ): String
45+ {
46+ return if (path .lastIndexOf (" ." ) == - 1 ) ' ${path }. $defaultExtension ' ; else path ;
47+ }
48+
49+ public static function normalizePath (path : String , ? defaultExtension : String ): String
50+ {
51+ return if (defaultExtension == null ) Path .normalize (path ); else fixPathExtension (Path .normalize (path ), defaultExtension );
52+ }
53+
54+ public static function getPath (file : String , type : AssetType , ? library : String ): String
4555 {
4656 if (library != null ) return getLibraryPath (file , library );
4757
4858 if (currentLevel != null )
4959 {
50- var levelPath : String = getLibraryPathForce (file , currentLevel );
60+ var levelPath : String = getLibraryPath (file , currentLevel );
5161 if (Assets .exists (levelPath , type )) return levelPath ;
5262 }
5363
@@ -84,47 +94,74 @@ class Paths implements ConsoleClass
8494
8595 public static function txt (key : String , ? library : String ): String
8696 {
87- return getPath (' data/ $key . txt' , TEXT , library );
97+ return getPath (normalizePath ( ' data/ $key ' , ' txt' ) , TEXT , library );
8898 }
8999
90100 public static function frag (key : String , ? library : String ): String
91101 {
92- return getPath (' shaders/ $key . frag' , TEXT , library );
102+ return getPath (normalizePath ( ' shaders/ $key ' , ' frag' ) , TEXT , library );
93103 }
94104
95105 public static function vert (key : String , ? library : String ): String
96106 {
97- return getPath (' shaders/ $key . vert' , TEXT , library );
107+ return getPath (normalizePath ( ' shaders/ $key ' , ' vert' ) , TEXT , library );
98108 }
99109
100110 public static function xml (key : String , ? library : String ): String
101111 {
102- return getPath (' data/ $key . xml' , TEXT , library );
112+ return getPath (normalizePath ( ' data/ $key ' , ' xml' ) , TEXT , library );
103113 }
104114
105115 public static function json (key : String , ? library : String ): String
106116 {
107- return getPath (' data/ $key . json' , TEXT , library );
117+ return getPath (normalizePath ( ' data/ $key ' , ' json' ) , TEXT , library );
108118 }
109119
110- public static function srt (key : String , ? library : String , ? directory : String = " data/ " ): String
120+ public static function srt (key : String , ? library : String , ? directory : String = " data" ): String
111121 {
112- return getPath (' $ directory $key . srt' , TEXT , library );
122+ return getPath (normalizePath ( ' ${ directory } / $key ' , ' srt' ) , TEXT , library );
113123 }
114124
115- public static function sound (key : String , ? library : String ): String
125+ public static function sound (key : String , ? library : String , ? directory : String = ' sounds ' , ? extension : String ): String
116126 {
117- return getPath (' sounds/ $key . ${Constants .EXT_SOUND }' , SOUND , library );
127+ var normalizedPath = Path .normalize ((directory == ' ' ? ' ' : directory + ' /' ) + key );
128+ if (extension != null ) return getPath (fixPathExtension (normalizedPath , extension ), SOUND , library );
129+
130+ // Attempt to find the sound by looping through the supported file formats.
131+ var path : String ;
132+ for (extension in Constants .EXT_SOUNDS )
133+ {
134+ // no need to check if its exists in MUSIC type, as Openfl/Lime AssetLibrary have the same returns for SOUND and MUSIC internally.
135+ if (library != null )
136+ {
137+ path = getLibraryPath (fixPathExtension (normalizedPath , extension ), library );
138+ if (Assets .exists (path , SOUND )/* || Assets.exists(path, MUSIC)*/ ) return path ;
139+ }
140+ else
141+ {
142+ if (currentLevel != null )
143+ {
144+ path = getLibraryPath (fixPathExtension (normalizedPath , extension ), currentLevel );
145+ if (Assets .exists (path , SOUND )/* || Assets.exists(path, MUSIC)*/ ) return path ;
146+ }
147+
148+ path = getLibraryPathForce (fixPathExtension (normalizedPath , extension ), ' shared' );
149+ if (Assets .exists (path , SOUND )/* || Assets.exists(path, MUSIC)*/ ) return path ;
150+ }
151+ }
152+
153+ if (library != null ) return getLibraryPath (fixPathExtension (normalizedPath , Constants .EXT_SOUND ), library );
154+ else return getPreloadPath (fixPathExtension (normalizedPath , Constants .EXT_SOUND ));
118155 }
119156
120- public static function soundRandom (key : String , min : Int , max : Int , ? library : String ): String
157+ public static function soundRandom (key : String , min : Int , max : Int , ? library : String , ? extension : String ): String
121158 {
122- return sound (key + FlxG .random .int (min , max ), library );
159+ return sound (key + FlxG .random .int (min , max ), library , null , extension );
123160 }
124161
125- public static function music (key : String , ? library : String ): String
162+ public static function music (key : String , ? library : String , ? extension : String ): String
126163 {
127- return getPath ( ' music/ $ key . ${ Constants . EXT_SOUND } ' , MUSIC , library );
164+ return sound ( key , library , ' music ' , extension );
128165 }
129166
130167 public static function videos (key : String , ? library : String ): String
@@ -139,24 +176,29 @@ class Paths implements ConsoleClass
139176 return getPath (' videos/ $key . ${Constants .EXT_VIDEO }' , BINARY , library ?? ' videos' );
140177 }
141178
142- public static function voices ( song : String , ? suffix : String = ' ' ): String
179+ public static function song ( key : String , ? extension : String ): String
143180 {
144- if (suffix == null ) suffix = ' ' ; // no suffix, for a sorta backwards compatibility with older-ish voice files
181+ // For web platform that haven't loaded the library "songs" yet.
182+ if (Assets .getLibrary (" songs" ) != null ) return sound (key , ' songs' , ' ' , extension );
183+ else return getLibraryPathForce (normalizePath (key , extension ?? Constants .EXT_SOUND ), ' songs' );
184+ }
145185
146- return ' songs:assets/songs/ ${song .toLowerCase ()}/Voices $suffix . ${Constants .EXT_SOUND }' ;
186+ public static function voices (song : String , ? suffix : String = ' ' , ? extension : String ): String
187+ {
188+ if (suffix == null ) suffix = ' ' ; // no suffix, for a sorta backwards compatibility with older-ish voice files
189+ return Paths .song (' ${song .toLowerCase ()}/Voices $suffix ' , extension );
147190 }
148191
149192 /**
150193 * Gets the path to an `Inst.mp3/ogg` song instrumental from songs:assets/songs/`song`/
151194 * @param song name of the song to get instrumental for
152195 * @param suffix any suffix to add to end of song name, used for `-erect` variants usually
153- * @param withExtension if it should return with the audio file extension `.mp3` or `.ogg` .
196+ * @param extension The audio file extension of the track. If empty, the default extension is passed .
154197 * @return String
155198 */
156- public static function inst (song : String , ? suffix : String = ' ' , withExtension : Bool = true ): String
199+ public static function inst (song : String , ? suffix : String = ' ' , ? extension : String ): String
157200 {
158- var ext : String = withExtension ? ' . ${Constants .EXT_SOUND }' : ' ' ;
159- return ' songs:assets/songs/ ${song .toLowerCase ()}/Inst $suffix $ext ' ;
201+ return Paths .song (' ${song .toLowerCase ()}/Inst $suffix ' , extension );
160202 }
161203
162204 public static function image (key : String , ? library : String ): String
0 commit comments