Skip to content

Commit

Permalink
Fixed storage container null pointer with pre-existing storage contai…
Browse files Browse the repository at this point in the history
…ners on a game load
  • Loading branch information
brett-taylor committed Nov 10, 2019
1 parent b6becad commit 36b0ae1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
31 changes: 24 additions & 7 deletions ResourceMonitor/Components/ResourceMonitorLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ public class TrackedResource
public class ResourceMonitorLogic : MonoBehaviour, IConstructable
{
public static List<string> DONT_TRACK_GAMEOBJECTS { get; private set; } = new List<string>();
private static readonly float COOLDOWN_TIME_BETWEEN_PICKING_UP_LAST_ITEM_TYPE = 1f;
private static readonly float COOLDOWN_TIME_BETWEEN_PICKING_UP_LAST_ITEM_TYPE = 0.7f;

public SortedDictionary<TechType, TrackedResource> TrackedResources { private set; get; } = new SortedDictionary<TechType, TrackedResource>();
public bool IsBeingDeleted { get; private set; } = false;
private ResourceMonitorDisplay rmd;
private GameObject seaBase;
private bool isEnabled = false;
private float timerTillNextPickup = .0f;
private bool isEnabled = false;
private bool runStartUpOnEnable = false;

private IEnumerator Startup()
{
Expand All @@ -49,21 +50,37 @@ private IEnumerator Startup()
}

TrackExistingStorageContainers();
Patchers.StorageContainerAwakePatcher.AddEventHandlerIfMissing(AlertedNewStorageContainerPlaced);
Patchers.StorageContainerAwakePatcher.RegisterForNewStorageContainerUpdates(this);
Patchers.InGameMenuQuitPatcher.AddEventHandlerIfMissing(CleanUp);
TurnDisplayOn();
}

public void OnConstructedChanged(bool constructed)
public void OnEnable()
{
if (IsBeingDeleted == true) return;
if (runStartUpOnEnable)
{
StartCoroutine(Startup());
runStartUpOnEnable = false;
}
}

public void OnConstructedChanged(bool constructed)
{
if (constructed)
{
if (isEnabled == false)
{
isEnabled = true;
StartCoroutine(Startup());

// Big Little Update Subnautica has caused this to be called when isActiveAndEnabled is false when loading a saved game.
if (isActiveAndEnabled)
{
StartCoroutine(Startup());
}
else
{
runStartUpOnEnable = true;
}
}
else
{
Expand Down Expand Up @@ -119,7 +136,7 @@ private void TrackExistingStorageContainers()
}
}

private void AlertedNewStorageContainerPlaced(StorageContainer sc)
public void AlertNewStorageContainerPlaced(StorageContainer sc)
{
StartCoroutine("TrackNewStorageContainerCoroutine", sc);
}
Expand Down
15 changes: 15 additions & 0 deletions ResourceMonitor/Patchers/QuitGamePatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Harmony;

namespace ResourceMonitor.Patchers
{
[HarmonyPatch(typeof(IngameMenu))]
[HarmonyPatch("QuitGameAsync")]
public class QuitGamePatcher
{
[HarmonyPostfix]
public static void Postfix()
{
StorageContainerAwakePatcher.ClearRegisteredResourceMonitors();
}
}
}
37 changes: 16 additions & 21 deletions ResourceMonitor/Patchers/StorageContainerPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Harmony;
using ResourceMonitor.Components;

namespace ResourceMonitor.Patchers
{
Expand All @@ -10,37 +12,30 @@ namespace ResourceMonitor.Patchers
[HarmonyPatch("Awake")]
public class StorageContainerAwakePatcher
{
private static Action<StorageContainer> onStorageContainerAdded;
private static readonly List<ResourceMonitorLogic> registeredResourceMonitors = new List<ResourceMonitorLogic>();

[HarmonyPostfix]
public static void Postfix(StorageContainer __instance)
{
if (onStorageContainerAdded != null)
if (__instance == null)
{
onStorageContainerAdded.Invoke(__instance);
return;
}
}

public static bool AddEventHandlerIfMissing(Action<StorageContainer> newHandler)
{
if (onStorageContainerAdded == null)
foreach(ResourceMonitorLogic resourceMonitor in registeredResourceMonitors)
{
onStorageContainerAdded += newHandler;
return true;
resourceMonitor.AlertNewStorageContainerPlaced(__instance);
}
else
{
foreach (Action<StorageContainer> action in onStorageContainerAdded.GetInvocationList())
{
if (action == newHandler)
{
return false;
}
}
}

onStorageContainerAdded += newHandler;
return true;
}
public static void RegisterForNewStorageContainerUpdates(ResourceMonitorLogic resourceMonitor)
{
registeredResourceMonitors.Add(resourceMonitor);
}

public static void ClearRegisteredResourceMonitors()
{
registeredResourceMonitors.Clear();
}
}
}
1 change: 1 addition & 0 deletions ResourceMonitor/ResourceMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="Components\PaginatorButton.cs" />
<Compile Include="Game Items\ResourceMonitorScreenGeneric.cs" />
<Compile Include="Patchers\InGameMenuQuitPatcher.cs" />
<Compile Include="Patchers\QuitGamePatcher.cs" />
<Compile Include="Patchers\StorageContainerPatcher.cs" />
<Compile Include="Game Items\ResourceMonitorScreenSmall.cs" />
<Compile Include="Game Items\ResourceMonitorScreenLarge.cs" />
Expand Down

0 comments on commit 36b0ae1

Please sign in to comment.