refactor(save-system): 移除旧存档基础设施 DeepRepair、IData 与 LoadIndex
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Utility;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
@@ -24,15 +21,6 @@ namespace AibisDream.Framework
|
||||
public abstract void SaveLevel();
|
||||
}
|
||||
|
||||
public interface IData
|
||||
{
|
||||
IEnumerator Load();
|
||||
|
||||
void Save()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#region IOC容器
|
||||
|
||||
public class IOCContainer
|
||||
@@ -89,183 +77,6 @@ namespace AibisDream.Framework
|
||||
}
|
||||
}
|
||||
|
||||
public class DataContainer
|
||||
{
|
||||
private Dictionary<string, IData> _instances = new();
|
||||
|
||||
public int Count => _instances.Count;
|
||||
|
||||
public void Register<T>(T instance) where T : IData
|
||||
{
|
||||
var key = typeof(T).AssemblyQualifiedName;
|
||||
if (key != null) _instances[key] = instance;
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class, IData
|
||||
{
|
||||
var key = typeof(T).AssemblyQualifiedName;
|
||||
if (key != null && _instances.TryGetValue(key, out var retInstance))
|
||||
{
|
||||
return retInstance as T;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public object Get(Type type)
|
||||
{
|
||||
var key = type.AssemblyQualifiedName;
|
||||
if (key != null && _instances.TryGetValue(key, out var retInstance))
|
||||
{
|
||||
return retInstance;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public T Unregister<T>() where T : class, IData
|
||||
{
|
||||
var res = Get<T>();
|
||||
var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;
|
||||
if (res != null && assemblyQualifiedName != null)
|
||||
{
|
||||
_instances.Remove(assemblyQualifiedName);
|
||||
return res;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_instances.Clear();
|
||||
}
|
||||
|
||||
public JObject SaveAsJson()
|
||||
{
|
||||
foreach (var data in _instances.Values)
|
||||
{
|
||||
data.Save();
|
||||
}
|
||||
return JObject.FromObject(_instances);
|
||||
}
|
||||
|
||||
public IEnumerator LoadByJson(JObject dataJson)
|
||||
{
|
||||
// 从加载序号1到10加载数据
|
||||
for (var i = 1; i <= 10; i++)
|
||||
{
|
||||
// 先筛选对应序号
|
||||
var curIdx = i;
|
||||
var idxInstances = _instances.Where(item => LoadIndex.GetLoadIdx(item.Value) == curIdx).ToArray();
|
||||
// 回填数据并加载
|
||||
foreach (var instancePair in idxInstances)
|
||||
{
|
||||
if (!dataJson.TryGetValue(instancePair.Key, out var data))
|
||||
{
|
||||
Debug.Log(instancePair.Key + "未注册");
|
||||
continue;
|
||||
}
|
||||
|
||||
var fixDataType = Type.GetType(instancePair.Key);
|
||||
if (fixDataType == null)
|
||||
{
|
||||
Debug.Log(instancePair.Key + "类型不存在");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 把Json中读到的参数填回Data
|
||||
var tempObj = data.ToObject(fixDataType);
|
||||
CommonUtil.CopyFields(tempObj, instancePair.Value);
|
||||
|
||||
// 回填完成后进行加载
|
||||
yield return instancePair.Value.Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetFieldValue<T>(string variableName, out T fieldValue)
|
||||
{
|
||||
fieldValue = default;
|
||||
var parts = variableName.Split('.');
|
||||
if (parts.Length <= 3)
|
||||
{
|
||||
Debug.LogError($"{variableName}输入格式错误,应为 '$data.类名.字段名'");
|
||||
return false;
|
||||
}
|
||||
|
||||
var className = parts[1];
|
||||
var fieldName = parts[2];
|
||||
|
||||
var key = _instances.Keys.FirstOrDefault(key => key.Contains(className));
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
Debug.LogError($"未找到类名为 {className} 的实例");
|
||||
return false;
|
||||
}
|
||||
|
||||
var instance = _instances[key];
|
||||
var fieldInfo = instance.GetType().GetField(fieldName);
|
||||
if (fieldInfo == null)
|
||||
{
|
||||
Debug.LogError($"未找到字段名为 {fieldName} 的字段");
|
||||
return false;
|
||||
}
|
||||
|
||||
var value = fieldInfo.GetValue(instance);
|
||||
if (typeof(T) == typeof(string))
|
||||
{
|
||||
// 单独处理字符串类型
|
||||
fieldValue = (T)(object)(value?.ToString() ?? string.Empty);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof(T) == typeof(float))
|
||||
{
|
||||
// 处理数字类型转换为 float
|
||||
if (value is IConvertible)
|
||||
{
|
||||
try
|
||||
{
|
||||
fieldValue = (T)(object)Convert.ToSingle(value);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"无法将字段值转换为 float: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.LogError("字段值不是可转换为 float 的类型");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
// 处理布尔类型转换为 bool
|
||||
if (value is IConvertible)
|
||||
{
|
||||
fieldValue = (T)(object)Convert.ToBoolean(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
Debug.LogError("字段值不是可转换为 bool 的类型");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value is T typedValue)
|
||||
{
|
||||
// 处理其他类型
|
||||
fieldValue = typedValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
Debug.LogError($"未找到类型为 {typeof(T).Name} 的转换方法");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 事件相关
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class LoadIndex : Attribute
|
||||
{
|
||||
private int Idx { get; }
|
||||
|
||||
public LoadIndex(int idx)
|
||||
{
|
||||
Idx = idx;
|
||||
}
|
||||
|
||||
public static int GetLoadIdx<T>(T instance)
|
||||
{
|
||||
// 先通过反射拿到属性
|
||||
var attribute = instance.GetType().GetCustomAttribute<LoadIndex>();
|
||||
return attribute?.Idx ?? 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbb1d63c733941879217082bda086ade
|
||||
timeCreated: 1742905792
|
||||
@@ -1,39 +0,0 @@
|
||||
using System.Collections;
|
||||
using AibisDream.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace AibisDream.SaveSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// 深度维修子系统的旧 IData 注册表(基于 <see cref="DataContainer"/>)。
|
||||
/// <para>
|
||||
/// FixSystem / Eye / Chip 等仍通过此处 RegisterData;尚未纳入新 <see cref="SaveSnapshot.deepRepair"/>。
|
||||
/// P5 评估后迁移或废弃;与 <see cref="YarnVariableStorage"/>、快照 I/O 分离。
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class DeepRepairDataRegistry
|
||||
{
|
||||
private static readonly DataContainer Container = new();
|
||||
|
||||
public static void RegisterData<T>(T data) where T : IData
|
||||
{
|
||||
Container.Register(data);
|
||||
}
|
||||
|
||||
public static void UnregisterData<T>() where T : class, IData
|
||||
{
|
||||
Container.Unregister<T>();
|
||||
}
|
||||
|
||||
/// <summary>旧格式存档 systemData 段还原;按 LoadIndex 顺序执行各 IData.Load()。</summary>
|
||||
public static IEnumerator LoadByJson(JObject dataJson)
|
||||
{
|
||||
if (dataJson == null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
yield return Container.LoadByJson(dataJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba4cbc590c8177045b16b8a02db31121
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace AibisDream.SaveSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// 深度维修是否纳入新快照、以何种粒度纳入(P5 逐个定案)。
|
||||
/// </summary>
|
||||
public enum DeepRepairSavePolicy
|
||||
{
|
||||
/// <summary>完整阶段终点状态写入 deepRepair.sections。</summary>
|
||||
Supported,
|
||||
|
||||
/// <summary>只存简化后的关键字段。</summary>
|
||||
Simplified,
|
||||
|
||||
/// <summary>不存;读档回到进入该维修前的可存点。</summary>
|
||||
Dropped
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="SaveSnapshot.deepRepair"/> 占位段。
|
||||
/// P1 捕获时不写入;读档时缺省或 sections 为空则不报错。
|
||||
/// 正式实现后替代/收敛 <see cref="DeepRepairDataRegistry"/> 的旧 IData 路径。
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class DeepRepairSnapshot
|
||||
{
|
||||
public DeepRepairSavePolicy policy = DeepRepairSavePolicy.Dropped;
|
||||
|
||||
/// <summary>按维修模块 id 分子段,具体 key 在 P5 定义。</summary>
|
||||
public Dictionary<string, JObject> sections = new();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ee71c8af7b93d64aa87335a011eccf5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user