Funkin/source/Paths.hx

202 lines
5.8 KiB
Haxe

package;
import flixel.FlxG;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxAtlasFrames;
import openfl.utils.AssetType;
import openfl.media.Sound;
import openfl.utils.Assets;
class Paths
{
inline public static var SOUND_EXT = #if web "mp3" #else "ogg" #end;
public static var currentTrackedAssets:Map<String, FlxGraphic> = [];
public static var currentTrackedSounds:Map<String, Sound> = [];
public static var localTrackedAssets:Array<String> = [];
static var currentLevel:String;
static public function setCurrentLevel(name:String)
{
currentLevel = name.toLowerCase();
}
public static function clearUnusedMemory()
{
for (key in currentTrackedAssets.keys())
{
if (!localTrackedAssets.contains(key) && key != null)
{
var obj = currentTrackedAssets.get(key);
@:privateAccess
if (obj != null)
{
Assets.cache.removeBitmapData(key);
Assets.cache.clearBitmapData(key);
Assets.cache.clear(key);
FlxG.bitmap._cache.remove(key);
obj.destroy();
currentTrackedAssets.remove(key);
}
}
}
for (key in currentTrackedSounds.keys())
{
if (!localTrackedAssets.contains(key) && key != null)
{
var obj = currentTrackedSounds.get(key);
if (obj != null)
{
Assets.cache.removeSound(key);
Assets.cache.clearSounds(key);
Assets.cache.clear(key);
currentTrackedSounds.remove(key);
}
}
}
}
public static function clearStoredMemory()
{
@:privateAccess
for (key in FlxG.bitmap._cache.keys())
{
var obj = FlxG.bitmap._cache.get(key);
if (obj != null && !currentTrackedAssets.exists(key))
{
Assets.cache.removeBitmapData(key);
Assets.cache.clearBitmapData(key);
Assets.cache.clear(key);
FlxG.bitmap._cache.remove(key);
obj.destroy();
}
}
@:privateAccess
for (key in Assets.cache.getSoundKeys())
{
if (key != null && !currentTrackedSounds.exists(key))
{
var obj = Assets.cache.getSound(key);
if (obj != null)
{
Assets.cache.removeSound(key);
Assets.cache.clearSounds(key);
Assets.cache.clear(key);
}
}
}
localTrackedAssets = [];
}
static function getPath(file:String, type:AssetType, library:Null<String>)
{
if (library != null)
return getLibraryPath(file, library);
if (currentLevel != null)
{
var levelPath = getLibraryPathForce(file, currentLevel);
if (Assets.exists(levelPath, type))
return levelPath;
levelPath = getLibraryPathForce(file, "shared");
if (Assets.exists(levelPath, type))
return levelPath;
}
return getPreloadPath(file);
}
static public function getLibraryPath(file:String, library = "preload")
return if (library == "preload" || library == "default") getPreloadPath(file); else getLibraryPathForce(file, library);
inline static function getLibraryPathForce(file:String, library:String)
return '$library:assets/$library/$file';
inline static function getPreloadPath(file:String)
return 'assets/$file';
inline static public function file(file:String, type:AssetType = TEXT, ?library:String)
return getPath(file, type, library);
inline static public function txt(key:String, ?library:String)
return getPath('data/$key.txt', TEXT, library);
inline static public function xml(key:String, ?library:String)
return getPath('data/$key.xml', TEXT, library);
inline static public function json(key:String, ?library:String)
return getPath('data/$key.json', TEXT, library);
static public function sound(key:String, ?library:String, ?cache:Bool = true):Sound
return returnSound('sounds/$key', SOUND, library, cache);
inline static public function soundRandom(key:String, min:Int, max:Int, ?library:String)
return sound(key + FlxG.random.int(min, max), library);
inline static public function music(key:String, ?library:String, ?cache:Bool = true):Sound
return returnSound('music/$key', MUSIC, library, cache);
inline static public function voices(song:String, ?cache:Bool = true):Sound
return returnSound('songs/' + ${song.toLowerCase()} + '/Voices', cache);
inline static public function inst(song:String, ?cache:Bool = true):Sound
return returnSound('songs/' + ${song.toLowerCase()} + '/Inst', cache);
inline static public function image(key:String, ?library:String, ?cache:Bool = true):FlxGraphic
return returnGraphic('images/$key', IMAGE, library, cache);
inline static public function font(key:String)
return 'assets/fonts/$key';
inline static public function video(key:String)
return 'assets/$key.mp4';
inline static public function getSparrowAtlas(key:String, ?library:String, ?cache:Bool = true)
return FlxAtlasFrames.fromSparrow(returnGraphic('images/$key', cache), xml('images/$key'));
inline static public function getPackerAtlas(key:String, ?library:String, ?cache:Bool = true)
return FlxAtlasFrames.fromSpriteSheetPacker(returnGraphic('images/$key', cache), txt('images/$key'));
public static function returnGraphic(key:String, ?cache:Bool = true):FlxGraphic
{
var path:String = 'assets/$key.png';
if (Assets.exists(path, IMAGE, library))
{
if (!currentTrackedAssets.exists(path))
{
var graphic:FlxGraphic = FlxGraphic.fromBitmapData(Assets.getBitmapData(path), false, path, cache);
graphic.persist = true;
currentTrackedAssets.set(path, graphic);
}
localTrackedAssets.push(path);
return currentTrackedAssets.get(path);
}
trace('$key its null');
return null;
}
public static function returnSound(key:String, ?cache:Bool = true):Sound
{
if (Assets.exists('assets/$key.$SOUND_EXT', SOUND, library))
{
var path:String = 'assets/$key.$SOUND_EXT';
if (!currentTrackedSounds.exists(path))
currentTrackedSounds.set(path, Assets.getSound(path, library, cache));
localTrackedAssets.push(path);
return currentTrackedSounds.get(path);
}
trace('$key its null');
return null;
}
}