feat: add versioned saves and ASCII migration

This commit is contained in:
goose
2026-10-03 22:42:00 -04:00
parent 2d7b24167a
commit cd84273565
18 changed files with 937 additions and 4 deletions
@@ -0,0 +1,89 @@
class_name SaveMigrator
extends RefCounted
# The Godot format starts at one. ASCII saves have their own version number and
# are recognized by their `levels` key, never by an optimistic version match.
const CURRENT_SCHEMA_VERSION := 1
const LEGACY_SUPPORTED_KEYS := ["version", "seed", "difficulty", "permadeath", "current_floor", "player", "levels", "flags", "quests", "achievements", "audience", "floor_deadline", "companion", "companion_achievements", "recruits", "recap", "faction_war", "options", "inventory", "playtime_seconds", "updated_at"]
static func migrate(source: Dictionary) -> Dictionary:
if source.has("run") and source.has("runtime"):
var version := int(source.get("version", 0))
if version > CURRENT_SCHEMA_VERSION:
return {"ok": false, "error": "Save schema %d is newer than this game." % version}
if version == CURRENT_SCHEMA_VERSION:
return {"ok": true, "data": source.duplicate(true), "report": _report([])}
return {"ok": false, "error": "Unsupported Godot save schema %d." % version}
if not source.has("levels"):
return {"ok": false, "error": "Save is neither a supported Godot save nor an ASCII save."}
var unsupported: Array[String] = []
for key in source:
if not String(key) in LEGACY_SUPPORTED_KEYS:
unsupported.append(String(key))
unsupported.sort()
var player: Dictionary = source.get("player", {})
var position := _legacy_position(player)
var floors: Dictionary = {}
for floor_key in (source.get("levels", {}) as Dictionary):
floors[str(floor_key)] = _legacy_floor(source.levels[floor_key])
var current_floor := int(source.get("current_floor", 0))
var quest_records: Array = _legacy_quests(source.get("quests", {}))
var runtime_player := {
"id": String(player.get("id", "player")), "cell": position,
"hit_points": int(player.get("hit_points", player.get("hp", 1))),
"max_hit_points": int(player.get("max_hit_points", player.get("max_hp", player.get("hp", 1)))),
"attack_damage": int(player.get("attack_damage", player.get("attack", 0))),
"content_id": String(player.get("content_id", "player")),
"xp": int(player.get("xp", 0)),
}
var data := {
"version": CURRENT_SCHEMA_VERSION,
"run": {
"seed": String(source.get("seed", "")),
"settings": {"seed": String(source.get("seed", "")), "difficulty_id": String(source.get("difficulty", "crawler")), "permadeath": bool(source.get("permadeath", false)), "pet_enabled": source.get("companion", null) != null},
"player_name": String(player.get("name", "Sans-Nom")), "base_archetype": String(player.get("archetype", "vanguard")),
"current_floor": current_floor, "current_turn": 0, "gold": int(player.get("gold", 0)), "floors": floors,
"floor_deadlines": _legacy_deadlines(source.get("floor_deadline", {})), "inventory": {"capacity": 20, "items": source.get("inventory", [])}, "equipment": {}, "identified_instance_ids": [],
"quest_records": quest_records, "achievement_state": source.get("achievements", {}).duplicate(true), "audience_state": source.get("audience", {}).duplicate(true), "recap_state": source.get("recap", {}).duplicate(true), "faction_war_state": source.get("faction_war", {}).duplicate(true),
"moderation_state": {}, "is_game_over": false, "death_outcome": "", "slot_policy": "active", "log": [],
"meta": {"legacy_flags": source.get("flags", {}).duplicate(true), "legacy_options": source.get("options", {}).duplicate(true), "playtime_seconds": float(source.get("playtime_seconds", 0.0))},
},
"runtime": {"player": runtime_player, "companion": source.get("companion", null), "party": source.get("recruits", []), "dialogue_player": {}},
"metadata": {"updated_at": float(source.get("updated_at", 0.0)), "source": "ascii"},
}
return {"ok": true, "data": data, "report": _report(unsupported)}
static func _legacy_position(player: Dictionary) -> Array:
if player.get("cell", null) is Array:
return player.cell
return [int(player.get("x", 0)), int(player.get("y", 0))]
static func _legacy_floor(value: Dictionary) -> Dictionary:
# Level serializers from ASCII already use the same map/room concepts. Keep
# every supported key instead of filtering it: unsupported top-level fields
# are reported above, while level content remains user progress.
return value.duplicate(true)
static func _legacy_deadlines(value: Dictionary) -> Dictionary:
var result := {}
if value.has("turns_remaining"):
result["0"] = value
else:
for key in value:
result[String(key)] = value[key]
return result
static func _legacy_quests(value: Dictionary) -> Array:
var records: Array = []
for quest_id in value:
var legacy: Dictionary = value[quest_id] if value[quest_id] is Dictionary else {}
var status_name := String(legacy.get("status", legacy.get("state", "inactive")))
var status := 0
if status_name == "active": status = 1
elif status_name == "completed": status = 2
elif status_name == "abandoned": status = 3
records.append({"quest_id": String(quest_id), "status": status, "objectives": legacy.get("objectives", {}).duplicate(true), "rewards_granted": bool(legacy.get("rewards_granted", false))})
return records
static func _report(unsupported_keys: Array) -> Dictionary:
return {"unsupported_keys": unsupported_keys, "is_clean": unsupported_keys.is_empty()}