feat: import ASCII content catalog
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
class_name ContentCatalog
|
||||
extends RefCounted
|
||||
|
||||
const ContentLoader = preload("res://scripts/infrastructure/content/content_loader.gd")
|
||||
|
||||
var _entries := {
|
||||
"item": {},
|
||||
"enemy": {},
|
||||
"spell": {},
|
||||
"loot_table": {},
|
||||
"npc": {},
|
||||
"quest": {},
|
||||
}
|
||||
var _diagnostics := PackedStringArray()
|
||||
|
||||
static func load_from_directory(path: String) -> ContentCatalog:
|
||||
var catalog := ContentCatalog.new()
|
||||
var loaded := ContentLoader.load_directory(path)
|
||||
catalog._diagnostics = loaded.diagnostics
|
||||
for kind in catalog._entries:
|
||||
for definition in loaded.entries.get(kind, []):
|
||||
var id := StringName(definition.id)
|
||||
if catalog._entries[kind].has(id):
|
||||
catalog._diagnostics.append("Duplicate %s id: %s" % [kind, id])
|
||||
continue
|
||||
catalog._entries[kind][id] = definition
|
||||
return catalog
|
||||
|
||||
func get_item(id: StringName) -> Dictionary:
|
||||
return _definition_for("item", id)
|
||||
|
||||
func get_enemy(id: StringName) -> Dictionary:
|
||||
return _definition_for("enemy", id)
|
||||
|
||||
func get_spell(id: StringName) -> Dictionary:
|
||||
return _definition_for("spell", id)
|
||||
|
||||
func get_loot_table(id: StringName) -> Dictionary:
|
||||
return _definition_for("loot_table", id)
|
||||
|
||||
func get_npc(id: StringName) -> Dictionary:
|
||||
return _definition_for("npc", id)
|
||||
|
||||
func get_quest(id: StringName) -> Dictionary:
|
||||
return _definition_for("quest", id)
|
||||
|
||||
func get_diagnostics() -> PackedStringArray:
|
||||
return _diagnostics
|
||||
|
||||
func get_ids(kind: StringName) -> Array[StringName]:
|
||||
var ids: Array[StringName] = []
|
||||
for id in _entries.get(String(kind), {}):
|
||||
ids.append(id)
|
||||
ids.sort()
|
||||
return ids
|
||||
|
||||
func _definition_for(kind: String, id: StringName) -> Dictionary:
|
||||
return _entries[kind].get(id, {})
|
||||
@@ -0,0 +1 @@
|
||||
uid://ci0ri8e5031bp
|
||||
@@ -0,0 +1,78 @@
|
||||
class_name ContentLoader
|
||||
extends RefCounted
|
||||
|
||||
const SUPPORTED_CATALOGS := {
|
||||
"items.json": "item",
|
||||
"enemies.json": "enemy",
|
||||
"spells.json": "spell",
|
||||
"loot_tables.json": "loot_table",
|
||||
"npcs.json": "npc",
|
||||
}
|
||||
const SUPPORTED_DIRECTORY_CATALOGS := {
|
||||
"quests": "quest",
|
||||
}
|
||||
|
||||
static func load_directory(path: String) -> Dictionary:
|
||||
var result := {
|
||||
"entries": {},
|
||||
"diagnostics": PackedStringArray(),
|
||||
}
|
||||
for catalog_name in SUPPORTED_CATALOGS.values():
|
||||
result.entries[catalog_name] = []
|
||||
for catalog_name in SUPPORTED_DIRECTORY_CATALOGS.values():
|
||||
result.entries[catalog_name] = []
|
||||
|
||||
var directory := DirAccess.open(path)
|
||||
if directory == null:
|
||||
result.diagnostics.append("Unable to open content directory: %s" % path)
|
||||
return result
|
||||
_load_directory(directory, "", result)
|
||||
return result
|
||||
|
||||
static func _load_directory(directory: DirAccess, relative_directory: String, result: Dictionary) -> void:
|
||||
directory.list_dir_begin()
|
||||
var entry_name := directory.get_next()
|
||||
while not entry_name.is_empty():
|
||||
if entry_name != "." and entry_name != "..":
|
||||
var relative_path := entry_name if relative_directory.is_empty() else relative_directory.path_join(entry_name)
|
||||
if directory.current_is_dir():
|
||||
var child_directory := DirAccess.open(directory.get_current_dir().path_join(entry_name))
|
||||
if child_directory != null:
|
||||
_load_directory(child_directory, relative_path, result)
|
||||
else:
|
||||
_load_file(directory.get_current_dir().path_join(entry_name), relative_path, result)
|
||||
entry_name = directory.get_next()
|
||||
directory.list_dir_end()
|
||||
|
||||
static func _load_file(path: String, relative_path: String, result: Dictionary) -> void:
|
||||
if path.get_extension().to_lower() != "json":
|
||||
return
|
||||
var file := FileAccess.open(path, FileAccess.READ)
|
||||
if file == null:
|
||||
result.diagnostics.append("Unable to read content file: %s" % relative_path)
|
||||
return
|
||||
var parsed := JSON.parse_string(file.get_as_text())
|
||||
if parsed == null:
|
||||
result.diagnostics.append("Invalid JSON content file: %s" % relative_path)
|
||||
return
|
||||
var catalog_name := _catalog_for(relative_path)
|
||||
if catalog_name.is_empty():
|
||||
return
|
||||
var definitions: Array = parsed if parsed is Array else [parsed]
|
||||
for index in definitions.size():
|
||||
var definition = definitions[index]
|
||||
if not definition is Dictionary:
|
||||
result.diagnostics.append("Invalid content entry in %s entry %d" % [relative_path, index])
|
||||
continue
|
||||
if not definition.has("id") or not definition.id is String or definition.id.is_empty():
|
||||
result.diagnostics.append("Missing required id in %s entry %d" % [relative_path, index])
|
||||
continue
|
||||
result.entries[catalog_name].append(definition)
|
||||
|
||||
static func _catalog_for(relative_path: String) -> String:
|
||||
if SUPPORTED_CATALOGS.has(relative_path):
|
||||
return SUPPORTED_CATALOGS[relative_path]
|
||||
var path_parts := relative_path.split("/", false)
|
||||
if path_parts.size() == 2 and SUPPORTED_DIRECTORY_CATALOGS.has(path_parts[0]):
|
||||
return SUPPORTED_DIRECTORY_CATALOGS[path_parts[0]]
|
||||
return ""
|
||||
@@ -0,0 +1 @@
|
||||
uid://cbj6epwwnbqa3
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
"$script_dir/godot-import.sh"
|
||||
|
||||
GODOT_BIN="${GODOT_BIN:-/snap/bin/godot4}"
|
||||
if [[ ! -x "$GODOT_BIN" ]]; then
|
||||
GODOT_BIN="$(command -v godot4 || command -v godot || true)"
|
||||
fi
|
||||
: "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}"
|
||||
|
||||
log_path="$(mktemp)"
|
||||
trap 'rm -f "$log_path"' EXIT
|
||||
set +e
|
||||
"$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/integration -gexit 2>&1 | tee "$log_path"
|
||||
godot_status=${PIPESTATUS[0]}
|
||||
set -e
|
||||
if grep -Eq 'SCRIPT ERROR|Parse Error' "$log_path"; then
|
||||
echo "integration tests failed: Godot reported a script or parse error" >&2
|
||||
exit 1
|
||||
fi
|
||||
exit "$godot_status"
|
||||
+4
-3
@@ -3,6 +3,7 @@ set -euo pipefail
|
||||
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
"$script_dir/test-unit.sh"
|
||||
"$script_dir/test-integration.sh"
|
||||
"$script_dir/test-e2e.sh"
|
||||
|
||||
set +e
|
||||
@@ -11,13 +12,13 @@ visual_status=$?
|
||||
set -e
|
||||
case "$visual_status" in
|
||||
0)
|
||||
echo "verify: unit, E2E, and visual checks passed"
|
||||
echo "verify: unit, integration, E2E, and visual checks passed"
|
||||
;;
|
||||
2)
|
||||
echo "verify: unit and E2E checks passed; visual check unavailable" >&2
|
||||
echo "verify: unit, integration, and E2E checks passed; visual check unavailable" >&2
|
||||
;;
|
||||
*)
|
||||
echo "verify: visual check failed after unit and E2E checks passed" >&2
|
||||
echo "verify: visual check failed after unit, integration, and E2E checks passed" >&2
|
||||
exit "$visual_status"
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user