Skip to content

Commit

Permalink
Automatically append file extension to sound asset ids (#3314)
Browse files Browse the repository at this point in the history
* auto add sound extensions

* allow FLX_SOUND_ADD_EXT to specify the extension

* doc

* d'oh

* D'oh!

* allow any file extension

* rename to FLX_DEFAULT_SOUND_EXT
  • Loading branch information
Geokureli authored Dec 12, 2024
1 parent 0fad3b0 commit e4a994d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ class FlxSound extends FlxBasic
/**
* One of the main setup functions for sounds, this function loads a sound from an embedded MP3.
*
* **Note:** If the `FLX_SOUND_ADD_EXT` flag is enabled, you may omit the file extension
*
* @param EmbeddedSound An embedded Class object representing an MP3 file.
* @param Looped Whether or not this sound should loop endlessly.
* @param AutoDestroy Whether or not this FlxSound instance should be destroyed when the sound finishes playing.
Expand Down
44 changes: 39 additions & 5 deletions flixel/system/frontEnds/AssetFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class AssetFrontEnd
public function new () {}
#end

#if (FLX_DEFAULT_SOUND_EXT == "1" || FLX_NO_DEFAULT_SOUND_EXT)
public final defaultSoundExtension:String = #if flash ".mp3" #else ".ogg" #end;
#else
public final defaultSoundExtension:String = '.${haxe.macro.Compiler.getDefine("FLX_DEFAULT_SOUND_EXT")}';
#end

/**
* Used by methods like `getAsset`, `getBitmapData`, `getText`, their "unsafe" counterparts and
* the like to get assets synchronously. Can be set to a custom function to avoid the existing
Expand Down Expand Up @@ -231,6 +237,12 @@ class AssetFrontEnd
*/
public dynamic function exists(id:String, ?type:FlxAssetType)
{
#if FLX_DEFAULT_SOUND_EXT
// add file extension
if (type == SOUND)
id = addSoundExt(id);
#end

#if FLX_STANDARD_ASSETS_DIRECTORY
return Assets.exists(id, type.toOpenFlType());
#else
Expand All @@ -253,6 +265,12 @@ class AssetFrontEnd
*/
public dynamic function isLocal(id:String, ?type:FlxAssetType, useCache = true)
{
#if FLX_DEFAULT_SOUND_EXT
// add file extension
if (type == SOUND)
id = addSoundExt(id);
#end

#if FLX_STANDARD_ASSETS_DIRECTORY
return Assets.isLocal(id, type.toOpenFlType(), useCache);
#else
Expand Down Expand Up @@ -327,11 +345,13 @@ class AssetFrontEnd
*/
public inline function getSoundUnsafe(id:String, useCache = true):Sound
{
return cast getAssetUnsafe(id, SOUND, useCache);
return cast getAssetUnsafe(addSoundExtIf(id), SOUND, useCache);
}

/**
* Gets an instance of a sound, logs when the asset is not found
* Gets an instance of a sound, logs when the asset is not found.
*
* **Note:** If the `FLX_DEFAULT_SOUND_EXT` flag is enabled, you may omit the file extension
*
* @param id The ID or asset path for the sound
* @param useCache Whether to allow use of the asset cache (if one exists)
Expand All @@ -340,7 +360,7 @@ class AssetFrontEnd
*/
public inline function getSound(id:String, useCache = true, ?logStyle:LogStyle):Sound
{
return cast getAsset(id, SOUND, useCache, logStyle);
return cast getAsset(addSoundExtIf(id), SOUND, useCache, logStyle);
}

/**
Expand All @@ -352,11 +372,25 @@ class AssetFrontEnd
* @return A new `Sound` object Note: Dos not return a `FlxSound`
*/
public inline function getSoundAddExt(id:String, useCache = true, ?logStyle:LogStyle):Sound
{
return getSound(addSoundExt(id), useCache, logStyle);
}

inline function addSoundExtIf(id:String)
{
#if FLX_DEFAULT_SOUND_EXT
return addSoundExt(id);
#else
return id;
#end
}

inline function addSoundExt(id:String)
{
if (!id.endsWith(".mp3") && !id.endsWith(".ogg") && !id.endsWith(".wav"))
id += "." + #if flash "mp3" #else "ogg" #end;
return id + defaultSoundExtension;

return getSound(id, useCache, logStyle);
return id;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions flixel/system/frontEnds/SoundFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class SoundFrontEnd
/**
* Set up and play a looping background soundtrack.
*
* **Note:** If the `FLX_SOUND_ADD_EXT` flag is enabled, you may omit the file extension
*
* @param embeddedMusic The sound file you want to loop in the background.
* @param volume How loud the sound should be, from 0 to 1.
* @param looped Whether to loop this music.
Expand Down Expand Up @@ -132,6 +134,8 @@ class SoundFrontEnd
/**
* Creates a new FlxSound object.
*
* **Note:** If the `FLX_SOUND_ADD_EXT` flag is enabled, you may omit the file extension
*
* @param embeddedSound The embedded sound resource you want to play. To stream, use the optional URL parameter instead.
* @param volume How loud to play it (0 to 1).
* @param looped Whether to loop this sound.
Expand Down Expand Up @@ -230,6 +234,8 @@ class SoundFrontEnd
/**
* Plays a sound from an embedded sound. Tries to recycle a cached sound first.
*
* **Note:** If the `FLX_SOUND_ADD_EXT` flag is enabled, you may omit the file extension
*
* @param embeddedSound The embedded sound resource you want to play.
* @param volume How loud to play it (0 to 1).
* @param looped Whether to loop this sound.
Expand Down
8 changes: 8 additions & 0 deletions flixel/system/macros/FlxDefines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ private enum UserDefines
* any `</asset>` tags in your project.xml, to reduce your total memory
*/
FLX_CUSTOM_ASSETS_DIRECTORY;
/**
* Allows you to use sound paths with no extension, and the default sound type for that
* target will be used. If enabled it will use ogg on all targets except flash, which uses mp3.
* If this flag is set to any string, that is used for the file extension
*/
FLX_DEFAULT_SOUND_EXT;
}

/**
Expand Down Expand Up @@ -100,6 +106,7 @@ private enum HelperDefines
FLX_STANDARD_ASSETS_DIRECTORY;
/** The normalized, absolute path of `FLX_CUSTOM_ASSETS_DIRECTORY`, used internally */
FLX_CUSTOM_ASSETS_DIRECTORY_ABS;
FLX_NO_DEFAULT_SOUND_EXT;
}

class FlxDefines
Expand Down Expand Up @@ -200,6 +207,7 @@ class FlxDefines
defineInversion(FLX_SWF_VERSION_TEST, FLX_NO_SWF_VERSION_TEST);
defineInversion(FLX_NO_HEALTH, FLX_HEALTH);
defineInversion(FLX_TRACK_POOLS, FLX_NO_TRACK_POOLS);
defineInversion(FLX_DEFAULT_SOUND_EXT, FLX_NO_DEFAULT_SOUND_EXT);
// defineInversion(FLX_TRACK_GRAPHICS, FLX_NO_TRACK_GRAPHICS); // special case
}

Expand Down
2 changes: 2 additions & 0 deletions tests/coverage/Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<haxedef name="FLX_NO_SAVE" />
<haxedef name="FLX_NO_HEALTH" />
<haxedef name="FLX_4_LEGACY_COLLISION" />
<haxedef name="FLX_SOUND_ADD_EXT" />
</section>
<section if="coverage2">
<haxedef name="debug" />
Expand All @@ -77,5 +78,6 @@
<haxedef name="FLX_NO_SAVE" />
<haxedef name="FLX_NO_HEALTH" />
<haxedef name="FLX_4_LEGACY_COLLISION" />
<haxedef name="FLX_SOUND_ADD_EXT" />
</section>
</project>

0 comments on commit e4a994d

Please sign in to comment.