Funkin/source/funkin/Paths.hx

179 lines
4.9 KiB
Haxe
Raw Normal View History

package funkin;
2021-02-08 16:34:48 -05:00
import flixel.graphics.frames.FlxAtlasFrames;
2021-03-01 23:00:02 -05:00
import openfl.utils.AssetType;
import openfl.utils.Assets as OpenFlAssets;
2021-02-08 16:34:48 -05:00
/**
* A core class which handles determining asset paths.
*/
2021-02-08 16:34:48 -05:00
class Paths
{
static var currentLevel:Null<String> = null;
2024-06-20 16:17:53 -04:00
public static function setCurrentLevel(name:Null<String>):Void
{
2024-06-20 16:17:53 -04:00
if (name == null)
{
currentLevel = null;
}
else
{
currentLevel = name.toLowerCase();
}
}
2024-02-22 20:56:41 -05:00
public static function stripLibrary(path:String):String
{
2024-03-16 22:20:22 -04:00
var parts:Array<String> = path.split(':');
2024-02-22 20:56:41 -05:00
if (parts.length < 2) return path;
return parts[1];
}
public static function getLibrary(path:String):String
{
2024-03-16 22:20:22 -04:00
var parts:Array<String> = path.split(':');
if (parts.length < 2) return 'preload';
2024-02-22 20:56:41 -05:00
return parts[0];
}
2024-03-16 22:20:22 -04:00
static function getPath(file:String, type:AssetType, library:Null<String>):String
{
if (library != null) return getLibraryPath(file, library);
if (currentLevel != null)
{
2024-03-16 22:20:22 -04:00
var levelPath:String = getLibraryPathForce(file, currentLevel);
if (OpenFlAssets.exists(levelPath, type)) return levelPath;
}
2024-03-16 22:20:22 -04:00
var levelPath:String = getLibraryPathForce(file, 'shared');
if (OpenFlAssets.exists(levelPath, type)) return levelPath;
return getPreloadPath(file);
}
2024-03-16 22:20:22 -04:00
public static function getLibraryPath(file:String, library = 'preload'):String
{
2024-03-16 22:20:22 -04:00
return if (library == 'preload' || library == 'default') getPreloadPath(file); else getLibraryPathForce(file, library);
}
2024-03-16 22:20:22 -04:00
static inline function getLibraryPathForce(file:String, library:String):String
{
return '$library:assets/$library/$file';
}
2024-03-16 22:20:22 -04:00
static inline function getPreloadPath(file:String):String
{
return 'assets/$file';
}
2024-03-16 22:20:22 -04:00
public static function file(file:String, type:AssetType = TEXT, ?library:String):String
{
return getPath(file, type, library);
}
2024-03-16 22:20:22 -04:00
public static function animateAtlas(path:String, ?library:String):String
{
2023-08-06 22:20:18 -04:00
return getLibraryPath('images/$path', library);
}
2024-03-16 22:20:22 -04:00
public static function txt(key:String, ?library:String):String
{
return getPath('data/$key.txt', TEXT, library);
}
2024-03-16 22:20:22 -04:00
public static function frag(key:String, ?library:String):String
{
return getPath('shaders/$key.frag', TEXT, library);
}
2024-03-16 22:20:22 -04:00
public static function vert(key:String, ?library:String):String
{
return getPath('shaders/$key.vert', TEXT, library);
}
2024-03-16 22:20:22 -04:00
public static function xml(key:String, ?library:String):String
{
return getPath('data/$key.xml', TEXT, library);
}
2024-03-16 22:20:22 -04:00
public static function json(key:String, ?library:String):String
{
return getPath('data/$key.json', TEXT, library);
}
2024-03-16 22:20:22 -04:00
public static function sound(key:String, ?library:String):String
{
return getPath('sounds/$key.${Constants.EXT_SOUND}', SOUND, library);
}
2024-03-16 22:20:22 -04:00
public static function soundRandom(key:String, min:Int, max:Int, ?library:String):String
{
return sound(key + FlxG.random.int(min, max), library);
}
2024-03-16 22:20:22 -04:00
public static function music(key:String, ?library:String):String
{
return getPath('music/$key.${Constants.EXT_SOUND}', MUSIC, library);
}
2024-03-16 22:20:22 -04:00
public static function videos(key:String, ?library:String):String
{
return getPath('videos/$key.${Constants.EXT_VIDEO}', BINARY, library ?? 'videos');
}
2024-03-16 22:20:22 -04:00
public static function voices(song:String, ?suffix:String = ''):String
{
2024-03-16 22:20:22 -04:00
if (suffix == null) suffix = ''; // no suffix, for a sorta backwards compatibility with older-ish voice files
return 'songs:assets/songs/${song.toLowerCase()}/Voices$suffix.${Constants.EXT_SOUND}';
}
2024-04-18 20:23:03 -04:00
/**
* Gets the path to an `Inst.mp3/ogg` song instrumental from songs:assets/songs/`song`/
* @param song name of the song to get instrumental for
* @param suffix any suffix to add to end of song name, used for `-erect` variants usually
* @param withExtension if it should return with the audio file extension `.mp3` or `.ogg`.
* @return String
*/
public static function inst(song:String, ?suffix:String = '', ?withExtension:Bool = true):String
{
2024-04-18 20:23:03 -04:00
var ext:String = withExtension ? '.${Constants.EXT_SOUND}' : '';
return 'songs:assets/songs/${song.toLowerCase()}/Inst$suffix$ext';
}
2024-03-16 22:20:22 -04:00
public static function image(key:String, ?library:String):String
{
return getPath('images/$key.png', IMAGE, library);
}
2024-03-16 22:20:22 -04:00
public static function font(key:String):String
{
return 'assets/fonts/$key';
}
2024-03-16 22:20:22 -04:00
public static function ui(key:String, ?library:String):String
{
return xml('ui/$key', library);
}
2024-03-16 22:20:22 -04:00
public static function getSparrowAtlas(key:String, ?library:String):FlxAtlasFrames
{
return FlxAtlasFrames.fromSparrow(image(key, library), file('images/$key.xml', library));
}
2024-03-16 22:20:22 -04:00
public static function getPackerAtlas(key:String, ?library:String):FlxAtlasFrames
{
return FlxAtlasFrames.fromSpriteSheetPacker(image(key, library), file('images/$key.txt', library));
}
2021-03-01 23:00:02 -05:00
}
2024-04-18 20:23:03 -04:00
enum abstract PathsFunction(String)
{
var MUSIC;
var INST;
var VOICES;
var SOUND;
}