存档完善和修复

This commit is contained in:
2026-05-20 15:26:51 +08:00
parent ec633d9b79
commit 8ae2de5bcb
10 changed files with 106 additions and 41 deletions

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 44b3cc2acf7eadd478af66d7d4cf770a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -54,6 +54,13 @@ namespace BaseGames.Core.Save
public void Register(ISaveable s) => _saveables.Add(s);
public void Unregister(ISaveable s) => _saveables.Remove(s);
// ── 游玩时间追踪 ──────────────────────────────────────────────────────
private void Update()
{
if (_current != null)
_current.Meta.Playtime += Time.unscaledDeltaTime;
}
// ── 存档 ──────────────────────────────────────────────────────────────
public async Task SaveAsync(int slot = -1)
{

View File

@@ -34,7 +34,7 @@ namespace BaseGames.Core.Save
[Serializable]
public class SaveMeta
{
public string Version = "2.1";
public string Version = "2.2";
public int SlotIndex;
public string LastSaved; // ISO 8601
public float Playtime;
@@ -61,10 +61,6 @@ namespace BaseGames.Core.Save
public List<string> UnlockedFormIds = new();
public DeathShadeSaveData DeathShade;
// 护盾:-1 = 满护盾(默认),>= 0 = 当前耐久值
public int ShieldHP = -1;
public bool ShieldIsBroken = false;
}
[Serializable]
@@ -80,7 +76,6 @@ namespace BaseGames.Core.Save
public class EquipmentSaveData
{
public List<string> EquippedCharmIds = new();
public int NotchesUsed;
public int MaxNotches;
public List<string> OwnedCharmIds = new();
public List<string> UpgradedCharmIds = new();
@@ -252,7 +247,7 @@ namespace BaseGames.Core.Save
[Serializable]
public class SettingsSaveData
{
/// <summary>玩家选择的语言。空字符串 = 使用系统默认。</summary>
/// <summary>玩家选择的语言。空字符串 = 使用系统默认。由 LocalizationManager 读写。</summary>
public string Language = string.Empty;
}
}

View File

@@ -4,11 +4,11 @@ namespace BaseGames.Core.Save
{
/// <summary>
/// 处理存档版本迁移。
/// 迁移链:旧版本 → "2.0" → "2.1"CurrentVersion每个分支按顺序落下执行fall-through
/// 迁移链:旧版本 → "2.0" → "2.1" → "2.2"CurrentVersion每个分支按顺序落下执行fall-through
/// </summary>
public static class SaveMigrator
{
public const string CurrentVersion = "2.1";
public const string CurrentVersion = "2.2";
public static SaveData Migrate(SaveData data)
{
@@ -43,6 +43,21 @@ namespace BaseGames.Core.Save
v = "2.1";
}
// ── 2.1 → 2.2 ───────────────────────────────────────────────────────
if (v == "2.1")
{
// 2.2 删除 EquipmentSaveData.NotchesUsed冗余由 TryEquipCharm 重新计算)。
// 2.2 删除 PlayerSaveData.ShieldHP / ShieldIsBroken护盾在存档点始终全满无需持久化
// 2.2 删除 SettingsSaveData.Language全局设置由 SettingsManager 写入 settings.json
// 旧存档中这些字段由 Newtonsoft.Json 的 [JsonExtensionData] 忽略,无需额外处理。
// Equipment.MaxNotches旧存档若为 0EquipmentManager.OnLoad 回退到初始 Notch 数量。
if (data.Equipment != null && data.Equipment.MaxNotches == 0)
data.Equipment.MaxNotches = 0; // 保持 0OnLoad 回退到 config.initialNotchCount
Debug.Log("[SaveMigrator] 从 '2.1' 迁移至 '2.2'。");
v = "2.2";
}
// ── 未识别的未来版本 ─────────────────────────────────────────────────
if (v != CurrentVersion)
Debug.LogWarning($"[SaveMigrator] 未知版本 '{v}',将直接使用(可能存在兼容性问题)。");