Skip to content

Commit

Permalink
[Sideloader] Improve card saving speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Sep 2, 2023
1 parent 879ffdd commit 0a82a19
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Core_Sideloader/UniversalAutoResolver/Core.UAR.Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,23 @@ private static void ChaFileSaveFilePostHook(ChaFile __instance)
Sideloader.Logger.LogDebug($"External info: {info.GUID} : {info.Property} : {info.Slot}");
}

// Create a property name lookup to speed up saving (on the order of 4 minutes -> 15 seconds when saving fully populated KK game)
var extInfoLookup = new Dictionary<string, ResolveInfo>();
// To keep backwards compatibility, only the first entry with a given property in the extInfo list should be used if there are multiple.
// Achieve this by adding items in reverse so the first instance overwrites the second instance that was already set.
// There should be no duplicates most of the time so it's faster to do it this way than check if item exists on each iteration.
for (var index = extInfo.Count - 1; index >= 0; index--)
{
var info = extInfo[index];
extInfoLookup[info.Property] = info;
}

void ResetStructResolveStructure(Dictionary<CategoryProperty, StructValue<int>> propertyDict, object structure, IEnumerable<ResolveInfo> extInfo2, string propertyPrefix = "")
{
foreach (var kv in propertyDict)
{
var extResolve = extInfo.FirstOrDefault(x => x.Property == $"{propertyPrefix}{kv.Key}");
// Old and slow: var extResolve = extInfo.FirstOrDefault(x => x.Property == $"{propertyPrefix}{kv.Key}");
extInfoLookup.TryGetValue($"{propertyPrefix}{kv.Key}", out var extResolve);

if (extResolve != null)
kv.Value.SetMethod(structure, extResolve.LocalSlot);
Expand Down

0 comments on commit 0a82a19

Please sign in to comment.