Skip to content

Commit

Permalink
Merge pull request #3 from krzychu124/2-nullreference-exception
Browse files Browse the repository at this point in the history
NullReferenceException OnDestroy bugfix
  • Loading branch information
krzychu124 committed Jul 15, 2019
2 parents 5df71a9 + 76d4c3c commit 7cabfc1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 44 deletions.
2 changes: 1 addition & 1 deletion BrokenNodeDetector/BrokenNodeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace BrokenNodeDetector
{
public class BrokenNodeDetector : IUserMod {
public static readonly string Version = "0.2";
public static readonly string Version = "0.3";

public string Name => "Broken Node Detector " + Version;

Expand Down
17 changes: 7 additions & 10 deletions BrokenNodeDetector/LoadingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ public class LoadingExtension : LoadingExtensionBase {
public bool DetourInited { get; private set; }
public static MainUI MainUi { get; private set; }
public static IDictionary<MethodInfo, RedirectCallsState> DetouredMethodStates { get; private set; } = new Dictionary<MethodInfo, RedirectCallsState>();

public override void OnCreated(ILoading loading) {
base.OnCreated(loading);
Debug.Log("OnCreated");
}

public override void OnLevelLoaded(LoadMode mode) {
Debug.Log("OnLevelLoaded Mode: " + mode);
base.OnLevelLoaded(mode);

if (mode == LoadMode.NewGame
Expand All @@ -35,22 +29,25 @@ public class LoadingExtension : LoadingExtensionBase {
}

public override void OnLevelUnloading() {
Debug.Log("OnLevelUnloading");
base.OnLevelUnloading();

RevertDetours();
if (MainUi != null) {
UnityEngine.Object.Destroy(MainUi);
MainUi = null;
}
}

private void InitDetours() {
if (DetourInited) return;

bool detourFailed = false;
try {
Debug.Log("BND: Deploying manual detours");
Debug.Log("[BND] Deploying manual detours");

DetouredMethodStates = AssemblyRedirector.Deploy();
} catch (Exception e) {
Debug.LogError("Could not deploy manual detours for Broken Nodes Detector");
Debug.LogError("[BND] Could not deploy manual detours for Broken Nodes Detector");
Debug.Log(e.ToString());
Debug.Log(e.StackTrace);
detourFailed = true;
Expand All @@ -61,7 +58,7 @@ public class LoadingExtension : LoadingExtensionBase {
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage("Broken Nodes Detector failed to load", "Broken Nodes Detector failed to load. You can continue playing but the mod may not work properly.", true);
});
} else {
Debug.Log("Detours successful");
Debug.Log("[BND] Detours successful");
}

DetourInited = true;
Expand Down
30 changes: 0 additions & 30 deletions BrokenNodeDetector/ModService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class ModService {
private readonly Dictionary<ushort, uint> _nodeCalls;

public List<ushort> Results { get; private set; }
// public List<ushort> SegmentList { get; private set; }

static ModService() {
Instance = new ModService();
Expand All @@ -19,7 +18,6 @@ public class ModService {
private ModService() {
_nodeCalls = new Dictionary<ushort, uint>();
Results = new List<ushort>();
// SegmentList = new List<ushort>();
}

public void StartDetector() {
Expand Down Expand Up @@ -54,34 +52,6 @@ public class ModService {
Results.Add(pair.Key);
}
});
// CalculateSegments();
}

// /// <summary>
// /// Calculates unique segments ids from list of Result node ids
// /// </summary>
// private void CalculateSegments() {
// SegmentList.Clear();
// int count = Results.Count;
// ushort node1 = 0;
// ushort node2 = 0;
// for (int i = 0; i < count - 1; i++) {
// for (int j = i + 1; j < count - 1; j++) {
// node1 = Results[i];
// node2 = Results[j];
// if (!NetManager.instance.m_nodes.m_buffer[node1].IsConnectedTo(node2)) continue;
// ushort currentSegment;
// for (int k = 0; k < 8; k++) {
// currentSegment = NetManager.instance.m_nodes.m_buffer[node1].GetSegment(k);
//
// if (currentSegment == 0) continue;
//
// if (node2 == NetManager.instance.m_segments.m_buffer[currentSegment].GetOtherNode(node1)) {
// SegmentList.Add(node2);
// }
// }
// }
// }
// }
}
}
11 changes: 9 additions & 2 deletions BrokenNodeDetector/UI/MainPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public class MainPanel : UIPanel {
if (ModService.Instance.Results.Count == 0) {
_brokenNodesLabel.text = "Great! Nothing found :-)";
_mainPanel.height = 160;
Debug.Log("BrokenNodeDetector - nothing found");
Debug.Log("[BND] Nothing found :-)");
} else {
_brokenNodesLabel.relativePosition = new Vector2(10, 120);
_brokenNodesLabel.text = $"Found {ModService.Instance.Results.Count} possibly broken nodes\n" +
Expand All @@ -198,11 +198,18 @@ public class MainPanel : UIPanel {
"3. Repeat 1-2 until nothing new found\n" +
"Run detector again if you want :)";
_invalidNodes = ModService.Instance.Results;
Debug.Log($"BrokenNodeDetector found {_invalidNodes.Count} nodes. ({string.Join(",", _invalidNodes.Select(i => i.ToString()).ToArray())})");
Debug.Log($"[BND] Found {_invalidNodes.Count} nodes. ({string.Join(",", _invalidNodes.Select(i => i.ToString()).ToArray())})");
_invalidNodesEnumerator = _invalidNodes.GetEnumerator();
_moveNextButton.Show();
_mainPanel.height = 300;
}
}

private new void OnDestroy() {
base.OnDestroy();
if (_mainPanel != null) {
Destroy(_mainPanel);
}
}
}
}
6 changes: 5 additions & 1 deletion BrokenNodeDetector/UI/MainUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public class MainUI : UIComponent {

public override void OnDestroy() {
base.OnDestroy();
MainPanel.OnDestroy();
if (MainPanel != null) {
Destroy(MainPanel);
MainPanel = null;
MainKey = null;
}
}
}
}

0 comments on commit 7cabfc1

Please sign in to comment.