Skip to content

Commit

Permalink
Make it so we don't just rip the whole vanilla pot
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Aug 31, 2024
1 parent 1d8934d commit 1d6adc5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
38 changes: 36 additions & 2 deletions Winch/Data/ExtendedSaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
Expand Down Expand Up @@ -97,13 +98,33 @@ internal void ExtractModdedData()
saveData.grids.Add(key, grid);
}
}

var potsToRemove = new List<SerializedCrabPotPOIData>();
foreach (var crabPotPOI in baseSaveData.serializedCrabPotPOIs)
{
if (crabPotPOI.IsModded() || crabPotPOI.grid.spatialItems.Any(WinchExtensions.IsModded))
if (crabPotPOI.IsModded())
{
potsToRemove.Add(crabPotPOI);
}
else if (crabPotPOI.grid.spatialItems.Any(WinchExtensions.IsModded) || crabPotPOI.grid.spatialUnderlayItems.Any(WinchExtensions.IsModded))
{
var moddedItems = crabPotPOI.grid.spatialItems.Where(WinchExtensions.IsModded).ToList();
var moddedUnderlayItems = crabPotPOI.grid.spatialUnderlayItems.Where(WinchExtensions.IsModded).ToList();
crabPotPOI.grid.spatialItems.RemoveAll(WinchExtensions.IsModded);
crabPotPOI.grid.spatialUnderlayItems.RemoveAll(WinchExtensions.IsModded);
var partialCrabPot = (SerializedCrabPotPOIData)FormatterServices.GetUninitializedObject(typeof(SerializedCrabPotPOIData));
partialCrabPot.deployableItemId = crabPotPOI.deployableItemId;
partialCrabPot.x = crabPotPOI.x;
partialCrabPot.z = crabPotPOI.z;
partialCrabPot.lastUpdate = crabPotPOI.lastUpdate;
partialCrabPot.timeUntilNextCatchRoll = crabPotPOI.timeUntilNextCatchRoll;
partialCrabPot.durability = crabPotPOI.durability;
partialCrabPot.hadDurabilityRemaining = crabPotPOI.hadDurabilityRemaining;
partialCrabPot.grid = new SerializableGrid();
partialCrabPot.grid.spatialItems = moddedItems;
partialCrabPot.grid.spatialUnderlayItems = moddedUnderlayItems;
saveData.serializedCrabPotPOIs.Add(partialCrabPot);
}
}
foreach (var crabPotPOI in potsToRemove)
{
Expand Down Expand Up @@ -134,6 +155,7 @@ internal void InsertModdedData()
baseSaveData.grids.Add(key, grid);
}
}

var potsToRemove = new List<SerializedCrabPotPOIData>();
foreach (var crabPotPOI in saveData.serializedCrabPotPOIs)
{
Expand All @@ -142,7 +164,19 @@ internal void InsertModdedData()
foreach (var crabPotPOI in potsToRemove)
{
saveData.serializedCrabPotPOIs.Remove(crabPotPOI);
baseSaveData.serializedCrabPotPOIs.Add(crabPotPOI);
if (baseSaveData.serializedCrabPotPOIs.TryGetValue(pot =>
{
return pot.deployableItemId == crabPotPOI.deployableItemId
&& pot.x == crabPotPOI.x && pot.z == crabPotPOI.z
&& pot.lastUpdate == crabPotPOI.lastUpdate && pot.timeUntilNextCatchRoll == crabPotPOI.timeUntilNextCatchRoll
&& pot.durability == crabPotPOI.durability && pot.hadDurabilityRemaining == crabPotPOI.hadDurabilityRemaining;
}, out var baseCrabPotPOI))
{
baseCrabPotPOI.grid.spatialItems.AddRange(crabPotPOI.grid.spatialItems);
baseCrabPotPOI.grid.spatialUnderlayItems.AddRange(crabPotPOI.grid.spatialUnderlayItems);
}
else
baseSaveData.serializedCrabPotPOIs.Add(crabPotPOI);
}
}

Expand Down
14 changes: 14 additions & 0 deletions Winch/Util/WinchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,20 @@ public static void QuickRemoveAt<T>(this IList<T> list, int index)
list[index] = list[list.Count - 1];
list.RemoveAt(list.Count - 1);
}

public static bool TryGetValue<T>(this IEnumerable<T> source, Predicate<T> predicate, out T value)
{
foreach (var item in source)
{
if (predicate(item))
{
value = item;
return true;
}
}
value = default(T);
return false;
}
#endregion

#region Unity
Expand Down

0 comments on commit 1d6adc5

Please sign in to comment.