91 lines
1.9 KiB
GDScript
91 lines
1.9 KiB
GDScript
'''
|
||
用來儲存所有的SceneID的數據集,通常,
|
||
1. 你不應該通過面板修改這個DataBase。
|
||
2. 每個項目通常只存在一個ID Database
|
||
'''
|
||
|
||
@tool
|
||
extends Resource
|
||
class_name SceneIDDatabase
|
||
|
||
var next_id: int = 10000
|
||
|
||
@export var entries: Array[SceneIDEntry] = []
|
||
|
||
## 請求一個ID
|
||
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)
|
||
|
||
# 转成 Set(O(1) contains)
|
||
for id in used_ids:
|
||
used_set[id] = true
|
||
|
||
var candidate := prefix + 1
|
||
while used_set.has(candidate):
|
||
candidate += 1
|
||
|
||
return candidate
|
||
|
||
## 注冊SceneID
|
||
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
|
||
|
||
## 檢查SceneID 是否有效
|
||
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]
|
||
)
|
||
|
||
return result
|