godot-plateformer/addons/reedscene/scene/ReedSceneIDEditorSystem.gd

40 lines
1.2 KiB
GDScript
Raw Permalink 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.

@tool
extends RefCounted
class_name ReedSceneIDEditorSystem
const DB_PATH := "res://addons/reedscene/demo/data_base/GLOBAL_SCENE_ID.tres"
static func _load_db_readonly() -> SceneIDDatabase:
# 忽略缓存,避免拿到同一个共享实例(更稳)
return ResourceLoader.load(DB_PATH, "", ResourceLoader.CACHE_MODE_IGNORE)
static func _clone_db_to_new(db_ro: SceneIDDatabase) -> SceneIDDatabase:
var db := SceneIDDatabase.new()
if db_ro:
db.next_id = db_ro.next_id
# 深拷贝 Dictionary彻底断开只读引用链
db.scene_map = db_ro.scene_map.duplicate(true)
return db
static func request_id_for_scene(scene_path: String) -> int:
var db_ro := _load_db_readonly()
if db_ro == null:
push_error("[ReedSceneIDEditorSystem] Database not found: " + DB_PATH)
return -1
var db : SceneIDDatabase = _clone_db_to_new(db_ro)
if db.scene_map.has(scene_path):
return int(db.scene_map[scene_path])
var new_id :int = db.next_id
db.next_id += 1
db.scene_map[scene_path] = new_id
var err := ResourceSaver.save(db, DB_PATH)
if err != OK:
push_error("[ReedSceneIDEditorSystem] Failed to save DB (%s), err=%d" % [DB_PATH, err])
return -1
return new_id