From 6c53594ec8c839b091671ed76bb3fb3b1202951c Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Fri, 3 Mar 2023 10:49:40 +0100
Subject: [PATCH] Nodes Viewer Mode: fixed a problem where trying to engage the
 mode with an empty map could crash. Fixes #857

---
 Source/Plugins/NodesViewer/BuilderPlug.cs     |  8 ++++++++
 Source/Plugins/NodesViewer/NodesViewerMode.cs | 14 +++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/Source/Plugins/NodesViewer/BuilderPlug.cs b/Source/Plugins/NodesViewer/BuilderPlug.cs
index 568722a36..dba01ab72 100755
--- a/Source/Plugins/NodesViewer/BuilderPlug.cs
+++ b/Source/Plugins/NodesViewer/BuilderPlug.cs
@@ -20,6 +20,11 @@
 
 namespace CodeImp.DoomBuilder.Plugins.NodesViewer
 {
+	internal class ToastMessages
+	{
+		public static readonly string NODESVIEWER = "nodesviewer";
+	}
+
 	public class BuilderPlug : Plug
 	{
 		#region ================== Variables
@@ -49,6 +54,9 @@ namespace CodeImp.DoomBuilder.Plugins.NodesViewer
 
 			// Keep a static reference
 			me = this;
+
+			// Register toasts
+			General.ToastManager.RegisterToast(ToastMessages.NODESVIEWER, "Nodes Viewer Mode", "Toasts related to Nodes Viewer Mode");
 		}
 
 		// This is called when the plugin is terminated
diff --git a/Source/Plugins/NodesViewer/NodesViewerMode.cs b/Source/Plugins/NodesViewer/NodesViewerMode.cs
index b8f2c7a32..b96087bb9 100755
--- a/Source/Plugins/NodesViewer/NodesViewerMode.cs
+++ b/Source/Plugins/NodesViewer/NodesViewerMode.cs
@@ -843,6 +843,13 @@ namespace CodeImp.DoomBuilder.Plugins.NodesViewer
 			Cursor.Current = Cursors.WaitCursor;
 			base.OnEngage();
 
+			if(General.Map.Map.Vertices.Count == 0)
+			{
+				General.ToastManager.ShowToast(ToastMessages.NODESVIEWER, ToastType.ERROR, "Failed to engage Nodes Viewer Mode", "The map is empty.", "Failed to engage Nodes Viewer Mode: the map is empty");
+				General.Editing.CancelMode();
+				return;
+			}
+
 			//mxd
 			bool haveNodes = General.Map.LumpExists("NODES");
 			bool haveZnodes = General.Map.LumpExists("ZNODES");
@@ -853,7 +860,12 @@ namespace CodeImp.DoomBuilder.Plugins.NodesViewer
 			if(General.Map.IsChanged || !(haveZnodes || (haveNodes || haveSectors || haveSegs || haveVerts)))
 			{
 				// We need to build the nodes!
-				if(!General.Map.RebuildNodes(General.Map.ConfigSettings.NodebuilderSave, true)) return;
+				if (!General.Map.RebuildNodes(General.Map.ConfigSettings.NodebuilderSave, true))
+				{
+					General.ToastManager.ShowToast(ToastMessages.NODESVIEWER, ToastType.ERROR, "Failed to engage Nodes Viewer Mode", "Failed to rebuild the nodes.", "Failed to engage Nodes Viewer Mode: failed to rebuild the nodes");
+					General.Editing.CancelMode();
+					return;
+				}
 
 				//mxd. Update nodes availability
 				haveNodes = General.Map.LumpExists("NODES");
-- 
GitLab