43 lines
1.4 KiB
GDScript
43 lines
1.4 KiB
GDScript
class_name FallbackLibrary
|
|
extends RefCounted
|
|
## Authored degraded-DM fallback text (charter §13). On ANY failure the client
|
|
## shows one of these lines in-voice — never an error. Missing/malformed content
|
|
## yields a hardcoded constant, so the fallback itself cannot fail.
|
|
|
|
const PATH := "res://content/fallback/narrator.json"
|
|
const NPC_PATH := "res://content/fallback/npc.json"
|
|
const LAST_RESORT := "The chamber is cold. Something waits in the dark."
|
|
const NPC_LAST_RESORT := "The figure regards you in silence, and the moment passes."
|
|
|
|
var _lines: Array = []
|
|
var _npc_lines: Array = []
|
|
|
|
|
|
func _init(path := PATH, npc_path := NPC_PATH) -> void:
|
|
_lines = _load(path, "narrator")
|
|
_npc_lines = _load(npc_path, "npc")
|
|
|
|
|
|
func narrator_line() -> String:
|
|
return str(_lines[0]) if not _lines.is_empty() else LAST_RESORT
|
|
|
|
|
|
func npc_line() -> String:
|
|
return str(_npc_lines[0]) if not _npc_lines.is_empty() else NPC_LAST_RESORT
|
|
|
|
|
|
static func _load(path: String, key: String) -> Array:
|
|
if not FileAccess.file_exists(path):
|
|
return []
|
|
var text := FileAccess.get_file_as_string(path)
|
|
# Instance parse() (not JSON.parse_string) so malformed input returns an
|
|
# Error code instead of pushing an engine error the test config promotes.
|
|
var json := JSON.new()
|
|
if json.parse(text) != OK:
|
|
return []
|
|
var data = json.get_data()
|
|
if typeof(data) != TYPE_DICTIONARY:
|
|
return []
|
|
var lines = data.get(key, [])
|
|
return lines if typeof(lines) == TYPE_ARRAY else []
|