GD-VirtualFileAccess/vfs_loaders.gd

74 lines
2.0 KiB
GDScript3
Raw Permalink 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:
2024-12-03 13:43:48 -05:00
var img := Image.new()
2024-12-03 11:39:32 -05:00
img.load_png_from_buffer(buffer)
return img
2024-12-03 13:43:48 -05:00
static func load_jpg(buffer:PackedByteArray)->Image:
var img := Image.new()
img.load_jpg_from_buffer(buffer)
return img
2024-12-03 11:39:32 -05:00
static func load_mp3(buffer:PackedByteArray)->AudioStreamMP3:
var sfx := AudioStreamMP3.new()
sfx.data = buffer
return sfx
static func load_ogg(buffer:PackedByteArray)->AudioStreamOggVorbis:
2024-12-06 18:25:06 -05:00
return AudioStreamOggVorbis.load_from_buffer(buffer)
2024-12-03 11:39:32 -05:00
static func load_txt(buffer:PackedByteArray)->String:
var txt:String = buffer.get_string_from_utf8()
return txt
2024-12-03 13:43:48 -05:00
static func load_wav(buffer:PackedByteArray)->AudioStreamWAV:
var sfx := AudioStreamWAV.new()
push_warning("""
Cannot automatically parse wav because dynamic Godot parsing wav is currently manual and awful.
Despite this, somehow Godot's editor importer seems to handle parsing wav just fine.""")
return sfx
2024-12-03 11:39:32 -05:00
# can't use CONST since Callables are technically instanced dynamically
static var DEFAULT_SUPPORTED_FILES:Dictionary[String,Callable] = {
"bin": load_bin,
2024-12-03 13:43:48 -05:00
"txt": load_txt,
2024-12-03 11:39:32 -05:00
"png": load_png,
2024-12-03 13:43:48 -05:00
"jpg": load_jpg,
"jpeg": load_jpg,
2024-12-03 11:39:32 -05:00
"mp3": load_mp3,
"ogg": load_ogg,
2024-12-03 13:43:48 -05:00
"wav": load_wav,
2024-12-03 11:39:32 -05:00
}
2024-12-03 13:43:48 -05:00
static var DATA_FILES:Dictionary[String,Callable] = {
"bin": load_bin,
"txt": load_txt,
}
2024-12-03 11:39:32 -05:00
2024-12-03 13:43:48 -05:00
static var IMAGE_FILES:Dictionary[String,Callable] = {
"png": load_png,
"jpg": load_jpg,
"jpeg": load_jpg,
}
2024-12-03 11:39:32 -05:00
static var AUDIO_FILES:Dictionary[String,Callable] = {
"mp3": load_mp3,
"ogg": load_ogg,
2024-12-03 13:43:48 -05:00
"wav": load_wav,
2024-12-03 11:39:32 -05:00
}