优化 archive 命名结构

This commit is contained in:
cakipaul 2025-07-15 16:14:30 +08:00
parent c75f1efb5d
commit 3ca0437651
2 changed files with 21 additions and 12 deletions

View File

@ -12,12 +12,13 @@ var archive: AssembledArchive:
# current archive
static var user_root_dir := "user://data/" # must end with "/"
static var archive_dir := "user://data/archives/"
static var archive_dir := "user://data/archives_dict/"
static var archive_prefix := "save"
const CURRENT_VERSION = 6
var archives := {}
var archives_dict: Dictionary[int, AssembledArchive] = {}
var archives_notes_dict: Dictionary[int, String] = {}
var autosave_timer := Timer.new()
@ -35,7 +36,7 @@ func _ready() -> void:
load_config()
# 在 debug or editor 模式下,直接保证有 archive
if GlobalConfig.DEBUG or Engine.is_editor_hint():
if archives.size() == 0:
if archives_dict.size() == 0:
create_and_use_new_archive(0)
else:
# debug 模式下默认使用 0 号存档
@ -60,7 +61,7 @@ func _on_archive_id_changed():
# print("_on_archive_id_changed same id=", selected_id)
# return
print("_on_archive_id_changed id=", selected_id)
if not archives.has(selected_id):
if not archives_dict.has(selected_id):
print("新建存档 ", selected_id)
create_and_use_new_archive(selected_id)
# 已创建新存档 [ID:ui_new_archive]
@ -119,7 +120,10 @@ func _check_dirs_and_archives() -> bool:
# get archive number
for file in files:
if file.begins_with(archive_prefix) and file.ends_with(GlobalConfig.RES_FILE_FORMAT):
var id_str = file.get_basename().substr(archive_prefix.length()).strip_escapes()
# format: save012_xxxxx; save000
var id_and_note = file.get_basename().substr(archive_prefix.length()).strip_escapes().split("_", true, 1)
var id_str = id_and_note[0]
var note_str = id_and_note[1] if id_and_note.size() >= 2 else (archive_prefix + id_str)
# 非三位数的 id 会被忽略
if id_str.length() != 3:
continue
@ -127,13 +131,14 @@ func _check_dirs_and_archives() -> bool:
# 读取范围是 0-99
if id < 0 or id > 99:
continue
archives_notes_dict[id] = note_str
var path = archive_dir + file
if not archives.has(id):
if not archives_dict.has(id):
var res = ResourceLoader.load(
path, "AssembledArchive", ResourceLoader.CACHE_MODE_REPLACE_DEEP
)
if is_instance_valid(res) and res.version >= CURRENT_VERSION:
archives[id] = res
archives_dict[id] = res
else:
printerr("SKIP INVALID ARCHIVE! file=", file)
return true
@ -173,7 +178,7 @@ func _create_and_save_new_archive_resoure(id, take_over_path = false) -> void:
archive.archive_id = id
archive.created_time = Time.get_datetime_string_from_system(false, true)
ResourceSaver.save(archive, archive_path)
archives[id] = archive
archives_dict[id] = archive
# 超过 999 个存档会出问题;不过这个游戏不会有这么多存档
@ -188,6 +193,10 @@ func _get_archive_path(id: int) -> String:
return archive_dir + archive_prefix + id_str + GlobalConfig.RES_FILE_FORMAT
func allow_resume(id := 1) -> bool:
return archives_dict.has(id)
func save_all() -> void:
# save config
var config = GlobalConfigManager.config
@ -238,10 +247,10 @@ func load_archive() -> void:
# if archive and selected_id == archive.archive_id:
# return
print("load_archive ", selected_id)
if not archives.has(selected_id):
if not archives_dict.has(selected_id):
_handle_load_error(str(selected_id) + " 号存档", "查找")
return
archive = archives[selected_id]
archive = archives_dict[selected_id]
# emit signal
archive_loaded.emit()
check_autosave_options()

View File

@ -22,7 +22,7 @@ func _ready():
func _check_resume_btn():
if not ArchiveManager.archives.has(1):
if not ArchiveManager.allow_resume(1):
resume_btn.queue_free()
@ -37,7 +37,7 @@ func _on_resume_pressed():
sfx_click.global_play()
# 继续一号存档
print("Resume Game")
if ArchiveManager.archives.has(1):
if ArchiveManager.allow_resume(1):
# 设置 current_selected_archive_id 后,存档会自动加载
GlobalConfigManager.config.current_selected_archive_id = 1
else: