Add load_any_supported for arbitrary file extensions

This commit is contained in:
Pyral 2024-12-04 08:51:02 -05:00
parent 02f5c8fa55
commit 54ed370d87

18
vfs.gd
View File

@ -101,6 +101,24 @@ func load_supported(path:String, ext_override:String = "")->Variant:
return result
## Like load_supported, but expects [param path] to not have an extension.
## Instead, [param extensions] can contain multiple extensions, which will be
## appended and checked to exist. This allows for prioritizing one format
## over another or loosely defining a file id and allowing the engine to
## find a first match.
func load_any_supported(base_path:String, extensions:Array[String] = [])->Variant:
var _append_ext := func _append_ext(ext:String)->String:
return (base_path + ext) \
if ext.begins_with(".") \
else (base_path + "." + ext)
var try_paths:Array = extensions.map(_append_ext)
for path:String in try_paths:
if not file_exists(path): continue
return load_supported(path)
push_error("Could not find any file for '%s'.%s!" % [base_path, extensions])
return null
## [param parse] Callable(buffer:PackedByteArray)->Variant
func load_and_parse(path:String, parse:Callable)->Variant:
var abs_path:String = get_absolute_path(path)