From 54ed370d87223eb88e500a78f071fc35f9a42304 Mon Sep 17 00:00:00 2001 From: Pyral Date: Wed, 4 Dec 2024 08:51:02 -0500 Subject: [PATCH] Add load_any_supported for arbitrary file extensions --- vfs.gd | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/vfs.gd b/vfs.gd index 571b66c..f0b8175 100644 --- a/vfs.gd +++ b/vfs.gd @@ -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)