Skip to content

Commit da20ed8

Browse files
author
Lexi
committed
Cache patch manager configs
1 parent 10a1a7e commit da20ed8

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

Runtime/Core/Assets/PatchingManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,17 @@ bool killLoadingBarTips
357357
}
358358
resolve();
359359
}
360+
361+
public static void ImportConfigurations(Action resolve, Action<string> reject)
362+
{
363+
Universe.ImportConfigs(CacheManager.Inventory.SerializedConfigs);
364+
resolve();
365+
}
366+
367+
public static void ExportConfigurations(Action resolve, Action<string> reject)
368+
{
369+
CacheManager.Inventory.SerializedConfigs = Universe.ExportConfigs();
370+
resolve();
371+
}
360372
}
361373
}

Runtime/Core/Cache/Json/Inventory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Linq;
56
using PatchManager.Shared;
67
using UniLinq;
78

@@ -34,6 +35,9 @@ public sealed class Inventory
3435
[JsonProperty("patch_count")] public int PatchCount { get; internal set; } = 0;
3536
[JsonProperty("error_count")] public int ErrorCount { get; internal set; } = 0;
3637

38+
[JsonProperty("serialized_configs")]
39+
public Dictionary<string, Dictionary<string, JToken>> SerializedConfigs { get; internal set; } = new();
40+
3741
/// <summary>
3842
/// Get a <see cref="CacheEntry"/> by its label.
3943
/// </summary>

Runtime/Core/CoreModule.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ public override void Init()
8484
() => new FlowAction("Patch Manager: Creating New Assets", PatchingManager.CreateNewAssets));
8585
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(3,
8686
() => new FlowAction("Patch Manager: Rebuilding Cache", PatchingManager.RebuildAllCache));
87-
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(4,
87+
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(4, () => new FlowAction("Patch Manager: Exporting configurations", PatchingManager.ExportConfigurations));
88+
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(5,
8889
() => new FlowAction("Patch Manager: Registering Resource Locator", RegisterResourceLocator));
8990
}
9091
else
9192
{
92-
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(0,
93+
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(0, () => new FlowAction("Patch Manager: Importing previous configurations", PatchingManager.ImportConfigurations));
94+
SpaceWarp2.API.Loading.Loading.GeneralLoadingActions.Insert(1,
9395
() => new FlowAction("Patch Manager: Registering Resource Locator", RegisterResourceLocator));
9496
}
9597
}

Runtime/SassyPatching/Execution/Universe.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using SassyPatchGrammar;
99
using System.Reflection;
1010
using JetBrains.Annotations;
11+
using Newtonsoft.Json.Linq;
1112
using PatchManager.Generic.SassyPatching.Rulesets;
1213
using PatchManager.SassyPatching.Exceptions;
1314
using PatchManager.SassyPatching.NewAssets;
@@ -17,6 +18,7 @@
1718
using UniLinq;
1819
using Unity.VisualScripting;
1920
using UnityEngine;
21+
using VSwift.Modules.Extensions;
2022

2123
namespace PatchManager.SassyPatching.Execution
2224
{
@@ -97,6 +99,41 @@ public void AddConfigUpdater(long priority, string label, string? name, Expressi
9799
ConfigUpdates.Add((priority, label, name, updateExpression,snapshot));
98100
}
99101
#nullable disable
102+
103+
/// <summary>
104+
/// Exports all the config values in this universe into a dictionary of dictionaries
105+
/// </summary>
106+
/// <returns>The exported configuration values</returns>
107+
public Dictionary<string, Dictionary<string, JToken>> ExportConfigs()
108+
{
109+
Dictionary<string, Dictionary<string, JToken>> export = new();
110+
foreach (var kvp in Configs)
111+
{
112+
export[kvp.Key] = new Dictionary<string, JToken>();
113+
foreach (var kvp2 in kvp.Value)
114+
{
115+
export[kvp.Key][kvp2.Key] = kvp2.ToJToken();
116+
}
117+
}
118+
return export;
119+
}
120+
121+
/// <summary>
122+
/// Updates the configuration values from a dictionary of dictionaries
123+
/// </summary>
124+
/// <param name="import">The previously exported configuration values</param>
125+
public void ImportConfigs(Dictionary<string, Dictionary<string, JToken>> import)
126+
{
127+
foreach (var kvp in import)
128+
{
129+
if (!Configs.ContainsKey(kvp.Key)) Configs[kvp.Key] = new Dictionary<string, DataValue>();
130+
foreach (var kvp2 in kvp.Value)
131+
{
132+
Configs[kvp.Key][kvp2.Key] = DataValue.FromJToken(kvp2.Value);
133+
}
134+
}
135+
}
136+
100137
/// <summary>
101138
/// All stages defined by every mod
102139
/// </summary>

0 commit comments

Comments
 (0)