85 lines
2.3 KiB
GDScript
85 lines
2.3 KiB
GDScript
class_name PresentationCatalog
|
|
extends RefCounted
|
|
|
|
const CORE_CATALOG_PATH := "res://world/presentation/catalogs/core_presentation.tres"
|
|
|
|
var _valid := false
|
|
var _errors: Array[String] = []
|
|
var _definitions: Array[PresentationCueDefinition] = []
|
|
var _definitions_by_id: Dictionary = {}
|
|
|
|
|
|
static func create_core(validate_assets := true) -> PresentationCatalog:
|
|
var catalog := PresentationCatalog.new()
|
|
var resource := load(CORE_CATALOG_PATH) as PresentationCatalogResource
|
|
if resource == null:
|
|
catalog._errors = ["Core presentation catalog failed to load"]
|
|
return catalog
|
|
catalog.rebuild(resource.cues, validate_assets)
|
|
return catalog
|
|
|
|
|
|
func rebuild(
|
|
definitions: Array[PresentationCueDefinition], validate_assets := true
|
|
) -> Array[String]:
|
|
_valid = false
|
|
_errors.clear()
|
|
_definitions.clear()
|
|
_definitions_by_id.clear()
|
|
var ordered := definitions.duplicate()
|
|
ordered.sort_custom(_definition_before)
|
|
for definition in ordered:
|
|
if definition == null:
|
|
_errors.append("Presentation catalog contains a null cue")
|
|
continue
|
|
for error in (
|
|
definition.validate_assets() if validate_assets else definition.validate_metadata()
|
|
):
|
|
_errors.append("Cue '%s': %s" % [definition.cue_id, error])
|
|
if _definitions_by_id.has(definition.cue_id):
|
|
_errors.append("Duplicate presentation cue '%s'" % definition.cue_id)
|
|
else:
|
|
_definitions_by_id[definition.cue_id] = definition
|
|
_definitions.append(definition)
|
|
_errors.sort()
|
|
if not _errors.is_empty():
|
|
_definitions.clear()
|
|
_definitions_by_id.clear()
|
|
return get_errors()
|
|
_valid = true
|
|
return []
|
|
|
|
|
|
func is_valid() -> bool:
|
|
return _valid
|
|
|
|
|
|
func get_errors() -> Array[String]:
|
|
return _errors.duplicate()
|
|
|
|
|
|
func get_definition(cue_id: StringName) -> PresentationCueDefinition:
|
|
return _definitions_by_id.get(cue_id) as PresentationCueDefinition
|
|
|
|
|
|
func get_cue_ids() -> Array[StringName]:
|
|
var result: Array[StringName] = []
|
|
for definition in _definitions:
|
|
result.append(definition.cue_id)
|
|
return result
|
|
|
|
|
|
func instantiate_scene(cue_id: StringName) -> Node:
|
|
var definition := get_definition(cue_id)
|
|
return definition.instantiate_scene() if definition != null else null
|
|
|
|
|
|
func _definition_before(
|
|
first: PresentationCueDefinition, second: PresentationCueDefinition
|
|
) -> bool:
|
|
if first == null:
|
|
return second != null
|
|
if second == null:
|
|
return false
|
|
return String(first.cue_id) < String(second.cue_id)
|