Skip to content

Commit

Permalink
merge identicals and better equality checker
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 14, 2024
1 parent 41b8853 commit b103d3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Winch/Data/ExtendedSaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ internal void ExtractModdedData()
}
}

baseSaveData.serializedCrabPotPOIs = baseSaveData.serializedCrabPotPOIs.MergeIdenticals();

var potsToRemove = new List<SerializedCrabPotPOIData>();
foreach (var crabPotPOI in baseSaveData.serializedCrabPotPOIs)
{
Expand Down Expand Up @@ -185,6 +187,8 @@ internal void InsertModdedData()
}
}

saveData.serializedCrabPotPOIs = saveData.serializedCrabPotPOIs.MergeIdenticals();

var potsToRemove = new List<SerializedCrabPotPOIData>();
foreach (var crabPotPOI in saveData.serializedCrabPotPOIs)
{
Expand Down Expand Up @@ -213,7 +217,7 @@ internal void InsertModdedData()
crabPotPOI.grid.spatialUnderlayItems = existingUnderlayItems;
}

if (baseSaveData.serializedCrabPotPOIs.TryGetValue(pot => pot.Identical(crabPotPOI), out var baseCrabPotPOI))
if (baseSaveData.serializedCrabPotPOIs.TryGetValue(pot => pot.Identical(crabPotPOI) && pot != crabPotPOI, out var baseCrabPotPOI))
{
baseCrabPotPOI.grid.spatialItems.AddRange(existingItems);
baseCrabPotPOI.grid.spatialUnderlayItems.AddRange(existingUnderlayItems);
Expand Down
39 changes: 36 additions & 3 deletions Winch/Util/WinchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,14 +1053,17 @@ public static string ToPrintedString(this AchievementData achievement)
return printStr;
}

public static string ToIdentity(this SerializedCrabPotPOIData crabPotPOI)
{
return $"{crabPotPOI.deployableItemId}: ({crabPotPOI.x}, {crabPotPOI.z}), {crabPotPOI.lastUpdate}, {crabPotPOI.timeUntilNextCatchRoll}, {crabPotPOI.durability}, {crabPotPOI.hadDurabilityRemaining}";
}

/// <summary>
/// Checks if both crab pot POI datas have identical serializable data excluding <see cref="SerializedCrabPotPOIData.grid"/>
/// </summary>
public static bool Identical(this SerializedCrabPotPOIData a, SerializedCrabPotPOIData b)
{
return a.deployableItemId == b.deployableItemId && a.x == b.x && a.z == b.z
&& a.lastUpdate == b.lastUpdate && a.timeUntilNextCatchRoll == b.timeUntilNextCatchRoll
&& a.durability == b.durability && a.hadDurabilityRemaining == b.hadDurabilityRemaining;
return a.ToIdentity() == b.ToIdentity();
}

/// <summary>
Expand All @@ -1080,6 +1083,36 @@ public static SerializedCrabPotPOIData MakeIdentical(this SerializedCrabPotPOIDa
return partialCrabPot;
}

public static List<SerializedCrabPotPOIData> MergeIdenticals(this List<SerializedCrabPotPOIData> list)
{
var rootDict = new Dictionary<string, SerializedCrabPotPOIData>();
var duplicatesDict = new Dictionary<string, List<SerializedCrabPotPOIData>>();
foreach (var crabPotPOI in list)
{
var identity = crabPotPOI.ToIdentity();
if (!rootDict.ContainsKey(identity))
{
rootDict.Add(identity, crabPotPOI);
duplicatesDict.Add(identity, new List<SerializedCrabPotPOIData>());
}
else
duplicatesDict[identity].Add(crabPotPOI);
}

foreach (var identity in duplicatesDict.Keys)
{
var root = rootDict[identity];
var duplicates = duplicatesDict[identity];
foreach (var duplicate in duplicates)
{
root.grid.spatialItems.AddRange(duplicate.grid.spatialItems);
root.grid.spatialUnderlayItems.AddRange(duplicate.grid.spatialUnderlayItems);
}
}

return rootDict.Values.ToList();
}

public static IEnumerator CheckIsSaveAllowedToBeLoaded(this TitleController titleController, int slotNum, SaveData s, Selectable selectable, Action<bool> callback)
{
WinchCore.Log.Debug("[TitleController] CheckIsSaveAllowedToBeLoaded()");
Expand Down

0 comments on commit b103d3c

Please sign in to comment.