From 879cbacd934aaf3ad0f35b1d9959457ad1b2b0e5 Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Wed, 29 Aug 2012 10:09:02 +0000
Subject: [PATCH] Added unknown things check to Error Checker

---
 .../Plugins/BuilderModes/BuilderModes.csproj  |  3 ++
 .../ErrorChecks/CheckUnknownThings.cs         | 42 +++++++++++++++++++
 .../ErrorChecks/ResultUnknownThing.cs         | 41 ++++++++++++++++++
 3 files changed, 86 insertions(+)
 create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs
 create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs

diff --git a/Source/Plugins/BuilderModes/BuilderModes.csproj b/Source/Plugins/BuilderModes/BuilderModes.csproj
index b00205215..f6debf935 100644
--- a/Source/Plugins/BuilderModes/BuilderModes.csproj
+++ b/Source/Plugins/BuilderModes/BuilderModes.csproj
@@ -39,6 +39,7 @@
     <Reference Include="System.Core">
       <RequiredTargetFramework>3.5</RequiredTargetFramework>
     </Reference>
+    <Reference Include="System.Data" />
     <Reference Include="System.Drawing" />
     <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
@@ -244,11 +245,13 @@
     <Compile Include="ErrorChecks\CheckMissingTextures.cs" />
     <Compile Include="ErrorChecks\CheckUnknownFlats.cs" />
     <Compile Include="ErrorChecks\CheckUnknownTextures.cs" />
+    <Compile Include="ErrorChecks\CheckUnknownThings.cs" />
     <Compile Include="ErrorChecks\ResultNoErrors.cs" />
     <Compile Include="ErrorChecks\ResultStuckThingInThing.cs" />
     <Compile Include="ErrorChecks\ResultTextureMissing.cs" />
     <Compile Include="ErrorChecks\ResultUnknownFlat.cs" />
     <Compile Include="ErrorChecks\ResultUnknownTexture.cs" />
+    <Compile Include="ErrorChecks\ResultUnknownThing.cs" />
     <Compile Include="FindReplace\FindLinedefFlags.cs" />
     <Compile Include="FindReplace\FindThingAngle.cs" />
     <Compile Include="FindReplace\FindAnyTextureFlat.cs" />
diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs
new file mode 100644
index 000000000..f520b3a8d
--- /dev/null
+++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading;
+
+using CodeImp.DoomBuilder.Map;
+using CodeImp.DoomBuilder.Config;
+
+namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks {
+
+    [ErrorChecker("Check unknown things", true, 50)]
+    public class CheckUnknownThings : ErrorChecker {
+        private int PROGRESS_STEP = 1000;
+
+        // Constructor
+        public CheckUnknownThings()	{
+			// Total progress is done when all things are checked
+			SetTotalProgress(General.Map.Map.Things.Count / PROGRESS_STEP);
+		}
+
+        // This runs the check
+        public override void Run() {
+            int progress = 0;
+            int stepprogress = 0;
+
+            // Go for all things
+            foreach (Thing t in General.Map.Map.Things) {
+                if (General.Map.Data.GetThingInfoEx(t.Type) == null) 
+                    SubmitResult(new ResultUnknownThing(t));
+
+                // Handle thread interruption
+                try { Thread.Sleep(0); } catch (ThreadInterruptedException) { return; }
+
+                // We are making progress!
+                if ((++progress / PROGRESS_STEP) > stepprogress) {
+                    stepprogress = (progress / PROGRESS_STEP);
+                    AddProgress(1);
+                }
+            }
+        }
+    }
+}
diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs
new file mode 100644
index 000000000..191b198fd
--- /dev/null
+++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs
@@ -0,0 +1,41 @@
+using System;
+//using System.Collections.Generic;
+//using System.Text;
+using CodeImp.DoomBuilder.Map;
+using CodeImp.DoomBuilder.Rendering;
+
+namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks {
+    public class ResultUnknownThing : ErrorResult {
+        public override int Buttons { get { return 1; } }
+        public override string Button1Text { get { return "Delete Thing"; } }
+
+        private Thing thing;
+
+        // Constructor
+        public ResultUnknownThing(Thing t) {
+			// Initialize
+			this.thing = t;
+			this.viewobjects.Add(t);
+			this.description = "This thing has unknown type (eg. it's not defined in DECORATE or current game configuration).";
+		}
+
+        // This must return the string that is displayed in the listbox
+		public override string ToString() {
+            return "Thing " + General.Map.Data.GetThingInfo(thing.Type).Index + " at " + thing.Position.x + ", " + thing.Position.y + " has unknown type.";
+		}
+
+		// Rendering
+		public override void  RenderOverlaySelection(IRenderer2D renderer) {
+			renderer.RenderThing(thing, renderer.DetermineThingColor(thing), 1.0f);
+		}
+		
+		// This removes the thing
+		public override bool Button1Click()	{
+			General.Map.UndoRedo.CreateUndo("Delete thing");
+			thing.Dispose();
+			General.Map.IsChanged = true;
+			General.Map.ThingsFilter.Update();
+			return true;
+		}
+    }
+}
-- 
GitLab