red_dragon_pon/base/scripts/autoload/resource_handler.gd

35 lines
1.1 KiB
GDScript

extends Node
## Dictionary, that holds resource id and path to it
var resource_registry: Dictionary = {}
const DEFAULT_WEAPON_RESOURCE_ID: StringName = "weapon_base"
## Recursively add resources to registry
func add_resource_path(_path: String) -> void:
var dir = DirAccess.open(_path)
if dir == null:
return
# Used to ignore last slash if it was provided in _path
var path = dir.get_current_dir()
for subdir in dir.get_directories():
var subdir_path = "%s/%s" % [ path, subdir ]
add_resource_path(subdir_path)
for filename in dir.get_files():
if !filename.ends_with('.tres'):
continue
var filepath = "%s/%s" % [ path, filename ]
var res: IdentifiedResource = ResourceLoader.load(filepath, 'IdentifiedResource')
resource_registry[res.id] = filepath
func get_resource(id: String) -> IdentifiedResource:
var path = resource_registry.get(id)
if path == null:
return null
var res: IdentifiedResource = ResourceLoader.load(path, 'IdentifiedResource')
return res
func _init() -> void:
add_resource_path('res://base/assets/resources')
assert(resource_registry.has(DEFAULT_WEAPON_RESOURCE_ID))