GD-VirtualFileAccess/vfs_loaders.gd

51 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-12-03 11:39:32 -05:00
static func validate_loader(loader:Callable)->bool:
if loader.get_unbound_arguments_count() != 1:
push_error("Cannot add loader for %s as only one String arg can be taken.")
return false
elif loader.get_bound_arguments()[0] is not String \
or loader.get_bound_arguments()[0] is not StringName:
push_error("Cannot add loader for %s as only one String arg can be taken.")
return false
return true
static func load_bin(buffer:PackedByteArray)->PackedByteArray: return buffer
static func load_png(buffer:PackedByteArray)->Image:
var img:Image
img.load_png_from_buffer(buffer)
return img
static func load_mp3(buffer:PackedByteArray)->AudioStreamMP3:
var sfx := AudioStreamMP3.new()
sfx.data = buffer
return sfx
static func load_ogg(buffer:PackedByteArray)->AudioStreamOggVorbis:
var sfx := AudioStreamOggVorbis.new()
sfx.data = buffer
return sfx
static func load_txt(buffer:PackedByteArray)->String:
var txt:String = buffer.get_string_from_utf8()
return txt
# can't use CONST since Callables are technically instanced dynamically
static var DEFAULT_SUPPORTED_FILES:Dictionary[String,Callable] = {
"bin": load_bin,
"png": load_png,
"mp3": load_mp3,
"ogg": load_ogg,
"txt": load_txt,
}
static var AUDIO_FILES:Dictionary[String,Callable] = {
"mp3": load_mp3,
"ogg": load_ogg,
}