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

87 lines
1.7 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.

''' 用來儲存所有的SceneID的數據集
'''
@tool
extends Resource
class_name SceneIDDatabase
var next_id: int = 10000
@export var entries: Array[SceneIDEntry] = []
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
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
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]
)
# ✅ 无论有没有 warning都返回 result
return result