-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSaveDataSerializer.cs
53 lines (47 loc) · 2.04 KB
/
SaveDataSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Shockah.Kokoro;
using StardewModdingAPI;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Shockah.SeasonAffixes;
internal interface ISaveDataSerializer
{
SerializedSaveData Serialize(SaveData data);
SaveData Deserialize(SerializedSaveData data);
}
internal sealed class SaveDataSerializer : ISaveDataSerializer
{
public SerializedSaveData Serialize(SaveData data)
{
SerializedSaveData result = new(SeasonAffixes.Instance.ModManifest.Version);
result.ActiveAffixes.AddRange(data.ActiveAffixes.Select(a => a.UniqueID));
result.AffixChoiceHistory.AddRange(data.AffixChoiceHistory.Select(step => step.Select(a => a.UniqueID).ToList()));
result.AffixSetChoiceHistory.AddRange(data.AffixSetChoiceHistory.Select(step => step.Select(set => set.Select(a => a.UniqueID).ToList()).ToList()));
return result;
}
public SaveData Deserialize(SerializedSaveData data)
{
ISeasonAffix? GetOrLog(string id, string context, LogLevel level)
{
var affix = SeasonAffixes.Instance.GetAffix(id);
if (affix is null)
SeasonAffixes.Instance.Monitor.Log($"Tried to deserialize affix `{id}` for {context}, but no such affix is registered. Did you remove a mod?", level);
return affix;
}
bool TryGetOrLog(string id, string context, LogLevel level, [NotNullWhen(true)] out ISeasonAffix? affix)
{
var value = GetOrLog(id, context, level);
affix = value;
return value is not null;
}
SaveData result = new();
foreach (var id in data.ActiveAffixes)
if (TryGetOrLog(id, "active affixes", LogLevel.Warn, out var affix))
result.ActiveAffixes.Add(affix);
foreach (var step in data.AffixChoiceHistory)
result.AffixChoiceHistory.Add(step.Select(id => GetOrLog(id, "affix choice history", LogLevel.Info)).WhereNotNull().ToHashSet());
foreach (var step in data.AffixSetChoiceHistory)
result.AffixSetChoiceHistory.Add(step.Select(set => (ISet<ISeasonAffix>)set.Select(id => GetOrLog(id, "affix set choice history", LogLevel.Info)).WhereNotNull().ToHashSet()).ToHashSet());
return result;
}
}