Add resource loading

This commit is contained in:
JeremyStar™ 2024-05-14 01:30:08 +02:00
parent f90d854cea
commit dcb30cfac8
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -76,6 +76,73 @@ func remove_all_slides() -> void:
for scene in sms.get_scene_collection_list(CoreTypes.SceneType.MAIN): for scene in sms.get_scene_collection_list(CoreTypes.SceneType.MAIN):
sms.remove_scene(scene.name) sms.remove_scene(scene.name)
# +++ resource loading +++
func load_resources(list: Dictionary) -> Dictionary:
logger.verb("Loading resources: " + str(list.keys()))
var output: Dictionary = {}
for resource in list:
output.merge({ resource: await load_resource(list[resource]) })
return output
func load_resource(path: String):
logger.diag("Loading resource located at " + path)
var load_path: String
if presenloader.is_file:
# Check if file exists
if !presenloader.reader.file_exists(path):
await logger.crash(core.misc.stringify_variables("File path %path% could not be located inside presentation archive", { "path": path }))
# Write into temporary file
presenloader.write_temporary_file(presenloader.reader.read_file(path))
# Update 'load_path'
load_path = presenloader.temporary_file
else:
# Check if file exists
if !FileAccess.file_exists(presenloader.path + "/" + path):
await logger.crash(core.misc.stringify_variables("File path %path% could not be located inside presentation directory (full path is %path_full%)", { "path": path, "path_full": presenloader.path + "/" + path }))
# Update 'load_path'
load_path = presenloader.path + "/" + path
# Load resource from temporary file
var resource = _load_resource_format_loader(load_path, path)
# Check if loading failed
if resource == null:
await logger.crash("Resource could not be read")
# Return resource
return resource
func _load_resource_format_loader(load_path: String, path: String):
var resource = null
if path.ends_with(".tscn"):
logger.diag("Using format loader for PackedScene")
resource = ResourceLoader.load(load_path)
# Try to instantiate
if resource.can_instantiate(): resource = resource.instantiate()
else: resource = Control.new() # does not cause runtime errors
elif path.ends_with(".ttf") or path.ends_with(".otf") or path.ends_with(".woff") or path.ends_with(".woff2"):
logger.diag("Using format loader for FontFile")
resource = FontFile.new()
resource.load_dynamic_font(load_path)
elif path.ends_with(".png") or path.ends_with(".jpg") or path.ends_with(".svg") or path.ends_with(".ktx") or path.ends_with(".tga") or path.ends_with(".webp"):
logger.diag("Using format loader for Image")
resource = Image.load_from_file(load_path)
else:
logger.diag("Using generic format loader")
resource = ResourceLoader.load(load_path)
return resource
# +++ etc +++ # +++ etc +++
func get_source_path(path: String) -> String: func get_source_path(path: String) -> String:
return "<presentation root>/" + path return "<presentation root>/" + path