Files
aibis-dream/Assets/Scripts/SaveSystem/SnapshotSectionDeserializer.cs
T

32 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Newtonsoft.Json.Linq;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 读盘后 <see cref="SaveSnapshot.sections"/> 中的值常为 <see cref="JObject"/>
/// 本类按 SaveId 转回各 Provider 所需的强类型 DTO。
/// </summary>
public static class SnapshotSectionDeserializer
{
/// <summary>若 dto 已是强类型则原样返回;若为 JObject 则 ToObject 到对应 DTO。</summary>
public static object Coerce(string saveId, object dto)
{
if (dto is not JObject jobj)
{
return dto;
}
return saveId switch
{
SnapshotProviderIds.Environment => jobj.ToObject<EnvironmentSnapshotDto>(),
SnapshotProviderIds.Actor => jobj.ToObject<ActorSnapshotDto>(),
SnapshotProviderIds.Audio => jobj.ToObject<AudioSnapshotDto>(),
SnapshotProviderIds.Timeline => jobj.ToObject<TimelineSnapshotDto>(),
SnapshotProviderIds.Fix => jobj.ToObject<FixSnapshotDto>(),
SnapshotProviderIds.Screen => jobj.ToObject<ScreenSnapshotDto>(),
_ => jobj
};
}
}
}