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

91 lines
1.9 KiB
GDScript3
Raw Permalink Normal View History

2026-01-04 21:21:01 +08:00
'''
SceneID的數據集
1. DataBase
2. ID Database
2026-01-02 18:37:09 +08:00
'''
@tool
2026-01-01 16:33:32 +08:00
extends Resource
class_name SceneIDDatabase
2026-01-02 18:37:09 +08:00
var next_id: int = 10000
@export var entries: Array[SceneIDEntry] = []
2026-01-04 21:21:01 +08:00
## 請求一個ID
2026-01-02 18:37:09 +08:00
func request_new_id(prefix: int) -> int:
##合理性檢測
if prefix < 0: return -1
##獲取到已經使用的所有的ID
var used_ids : Array[int] = []
var used_set := {}
for i in entries:
used_ids.append(i.scene_id)
# 转成 SetO(1) contains
for id in used_ids:
used_set[id] = true
var candidate := prefix + 1
while used_set.has(candidate):
candidate += 1
return candidate
2026-01-04 21:21:01 +08:00
## 注冊SceneID
2026-01-02 18:37:09 +08:00
func register_scene_id(
scene_path: String,
uid: String,
scene_id: int
) -> bool:
for entry in entries:
if entry.scene_id == scene_id:
push_error("SceneIDDatabase: duplicated scene_id %d" % scene_id)
return false
var entry := SceneIDEntry.new()
entry.scene_id = scene_id
entry.scene_path = scene_path
entry.scene_uid = uid
entries.append(entry)
emit_changed()
return true
2026-01-04 21:21:01 +08:00
## 檢查SceneID 是否有效
2026-01-02 18:37:09 +08:00
func validate_scene_id(
scene_id: int,
scene_uid: String,
scene_path: String
) -> SceneIDValidationResult:
var result := SceneIDValidationResult.new()
var matched_entry : SceneIDEntry = null
for entry in entries:
if entry.scene_id == scene_id:
matched_entry = entry
break
if matched_entry == null:
result.add_warning(
"Scene ID %d is not registered in SceneIDDatabase."
% scene_id
)
return result
if matched_entry.scene_uid != scene_uid:
result.add_warning(
"Scene ID %d belongs to a different scene."
% scene_id
)
if matched_entry.scene_path != scene_path:
result.add_warning(
"Scene ID %d path mismatch.\nDB: %s\nCurrent: %s"
% [scene_id, matched_entry.scene_path, scene_path]
)
2026-01-01 16:33:32 +08:00
2026-01-02 18:37:09 +08:00
return result