初次提交
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
## 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()
|
||||
Reference in New Issue
Block a user