Files
spellforge/scripts/autoloads/config_mgr.gd
T
2026-07-20 10:56:52 +08:00

40 lines
1.2 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## ConfigMgr — JSON 配置加载器(Autoload: ConfigMgr
## 权威来源:architecture_design.md §2 数据层
extends Node
var _cache: Dictionary = {} # { file_path: String → Dictionary }
## 加载 JSON 配置文件(带缓存)
func load_json(path: String) -> Dictionary:
if _cache.has(path):
return _cache[path]
if not FileAccess.file_exists(path):
push_warning("ConfigMgr: 文件不存在 %s" % path)
return {}
var text: String = FileAccess.get_file_as_string(path)
var parsed = JSON.parse_string(text)
if parsed == null:
push_error("ConfigMgr: JSON 解析失败 %s" % path)
return {}
if parsed is Dictionary:
_cache[path] = parsed
return parsed
push_error("ConfigMgr: JSON 根节点不是 Dictionary %s" % path)
return {}
## 查询键值(支持点号路径,如 "enemies.basic.hp"
func get_value(path: String, key: String, default_val = null):
var data: Dictionary = load_json(path)
var keys: PackedStringArray = key.split(".")
var node = data
for k in keys:
if node is Dictionary and node.has(k):
node = node[k]
else:
return default_val
return node
## 清除缓存(热重载时调用)
func clear_cache() -> void:
_cache.clear()