Skip to content

Commit

Permalink
Actually remove the dock id from the vanilla save file if modded
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 14, 2024
1 parent 2be145c commit 41b8853
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
18 changes: 18 additions & 0 deletions Winch/Data/ExtendedSaveData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal ExtendedSaveData(int slot)
internal class ModdedSaveData
{
public List<string> mods = new List<string>();
public string dockId = string.Empty;
public int dockSlotIndex = 0;
public Dictionary<string, SerializableGrid> grids = new Dictionary<string, SerializableGrid>();
public List<SerializedCrabPotPOIData> serializedCrabPotPOIs = new List<SerializedCrabPotPOIData>();
public Dictionary<string, Dictionary<string, JToken>> modData = new Dictionary<string, Dictionary<string, JToken>>();
Expand Down Expand Up @@ -70,6 +72,11 @@ internal void Create()

internal void ExtractModdedData()
{
saveData.dockId = baseSaveData.dockId;
saveData.dockSlotIndex = baseSaveData.dockSlotIndex;
if (baseSaveData.dockId.IsDockModded())
DockUtil.ResetSaveData(baseSaveData);

var gridsToRemove = new List<GridKey>();
foreach (var gridByKey in baseSaveData.grids)
{
Expand Down Expand Up @@ -127,6 +134,17 @@ internal void ExtractModdedData()

internal void InsertModdedData()
{
if (!string.IsNullOrWhiteSpace(saveData.dockId))
{
if (saveData.dockId.DoesDockExist())
{
baseSaveData.dockId = saveData.dockId;
baseSaveData.dockSlotIndex = saveData.dockSlotIndex;
}
else
WinchCore.Log.Error($"Failed to find dock with id \"{saveData.dockId}\". Resetting to Blackstone Isle.");
}

var gridsToRemove = new List<string>();
foreach (var gridByKey in saveData.grids)
{
Expand Down
1 change: 0 additions & 1 deletion Winch/Patches/API/GameSceneInitializerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static void Prefix(GameSceneInitializer __instance)
HarvestZoneUtil.CreateModdedHarvestZones();
WorldEventUtil.CreateModdedStaticWorldEvents();
ItemUtil.Encyclopedia();
DockUtil.Fix();
DredgeEvent.TriggerGameLoading(__instance);
}
catch (Exception ex)
Expand Down
39 changes: 32 additions & 7 deletions Winch/Util/DockUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,34 @@ internal static bool PopulateDestinationFromMetaWithConverters<T>(T item, Dictio
return UtilHelpers.PopulateObjectFromMeta<T>(item, meta, DestinationConverters);
}

internal static List<string> VanillaDockIDList = new List<string> {
"dock.pontoon-tpr",
"dock.pontoon-ts",
"dock.pontoon-sb",
"dock.pontoon-ds",
"dock.old-mayor-sb",
"dock.old-mayor-gc",
"dock.old-fort",
"dock.photographer-camp",
"dock.pontoon-gc",
"dock.tpr-middle",
"dock.tpr-south",
"dock.tpr-west",
"dock.tpr-east",
"dock.steel-point",
"dock.research-pontoon",
"dock.greater-marrow",
"dock.ingfell",
"dock.old-mayor-ds",
"dock.old-mayor-ts",
"dock.ancient-ruins",
"dock.gale-cliffs",
"dock.soldier-camp",
"dock.outcast-isle",
"dock.little-marrow",
"dock.ds-temple",
"dock.the-iron-rig"
};

internal static Dictionary<string, DeferredDockData> ModdedDockDataDict = new();
internal static Dictionary<string, DockData> AllDockDataDict = new();
Expand Down Expand Up @@ -322,6 +350,7 @@ internal static void Populate()

foreach (var dock in Resources.FindObjectsOfTypeAll<Dock>())
{
if (!VanillaDockIDList.Contains(dock.Data.Id)) WinchCore.Log.Warn($"Possible missing dock ID \"{dock.Data.Id}\" from the vanilla list!");
PopulateDock(dock);
}

Expand All @@ -331,14 +360,10 @@ internal static void Populate()
ResearchBottomlessLines = hceds.FirstOrDefault(hced => hced.name == "ResearchBottomlessLines");
}

internal static void Fix()
internal static void ResetSaveData(SaveData saveData)
{
var id = GameManager.Instance.SaveData.dockId;
if (!AllDockDict.ContainsKey(id) && !ModdedDockDict.Values.Any(dock => dock.Data.Id == id))
{
GameManager.Instance.SaveData.dockId = "dock.outcast-isle";
GameManager.Instance.SaveData.dockSlotIndex = 0;
}
saveData.dockId = "dock.outcast-isle";
saveData.dockSlotIndex = 0;
}

internal static void Clear()
Expand Down
14 changes: 13 additions & 1 deletion Winch/Util/WinchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static class WinchExtensions
/// <inheritdoc cref="IsModded(ItemData)"/>
public static bool IsModded(this WorldEventData worldEvent) => worldEvent is ModdedWorldEventData || !WorldEventUtil.VanillaWorldEventIDList.Contains(worldEvent.name);
/// <inheritdoc cref="IsModded(ItemData)"/>
public static bool IsModded(this DockData dockData) => dockData is DeferredDockData || DockUtil.ModdedDockDataDict.ContainsKey(dockData.id);
public static bool IsModded(this DockData dockData) => dockData is DeferredDockData || DockUtil.ModdedDockDataDict.ContainsKey(dockData.id) || !DockUtil.VanillaDockIDList.Contains(dockData.id);
/// <inheritdoc cref="IsModded(ItemData)"/>
public static bool IsModded(this Dock dock) => dock is ModdedDock || dock.dockData.IsModded();
/// <inheritdoc cref="IsModded(ItemData)"/>
Expand Down Expand Up @@ -154,6 +154,18 @@ public static class WinchExtensions
/// </summary>
public static bool DoesItemDataNotExist(this SerializedCrabPotPOIData crabPotPOI) => !crabPotPOI.DoesItemDataExist();

/// <inheritdoc cref="IsModded(ItemData)"/>
internal static bool IsDockModded(this string dockID) => DockUtil.ModdedDockDataDict.ContainsKey(dockID) || !DockUtil.VanillaDockIDList.Contains(dockID);
/// <summary>
/// Check if the associated dock data exists
/// </summary>
/// <returns>Whether the associated dock data exists or not</returns>
internal static bool DoesDockExist(this string dockID) => DockUtil.ModdedDockDataDict.ContainsKey(dockID) || DockUtil.VanillaDockIDList.Contains(dockID);
/// <summary>
/// Opposite of <see cref="DoesDockNotExist(string)"/>s
/// </summary>
internal static bool DoesDockNotExist(this string dockID) => !dockID.DoesDockExist();

public static void Reinit(this SerializableGrid grid) => grid.Init(grid.gridConfiguration, false);

/// <summary>
Expand Down

0 comments on commit 41b8853

Please sign in to comment.