From bc1c32d52e7e9d96af2d62ce4039d8d6af498bfe Mon Sep 17 00:00:00 2001 From: MaxED Date: Tue, 30 Sep 2014 12:02:41 +0000 Subject: [PATCH] Map Analysis: added "Check unused things" check. Map Analysis, internal: some ErrorCheckers and ErrorResult had inconsistent namespaces. Thing Edit forms, internal: optimized missing flags check a bit. --- Source/Core/Windows/ThingEditForm.cs | 7 +- Source/Core/Windows/ThingEditFormUDMF.cs | 7 +- .../Plugins/BuilderModes/BuilderModes.csproj | 2 + .../ErrorChecks/CheckStrayVertices.cs | 2 +- .../ErrorChecks/CheckTextureAlignment.cs | 2 +- .../ErrorChecks/CheckUnknownThings.cs | 4 +- .../ErrorChecks/CheckUnusedThings.cs | 93 +++++++++++++++++++ .../ErrorChecks/ResultStrayVertex.cs | 2 +- .../ErrorChecks/ResultUnknownThing.cs | 3 +- .../ErrorChecks/ResultUnusedThing.cs | 87 +++++++++++++++++ 10 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/CheckUnusedThings.cs create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/ResultUnusedThing.cs diff --git a/Source/Core/Windows/ThingEditForm.cs b/Source/Core/Windows/ThingEditForm.cs index d69e629a..f318fb41 100644 --- a/Source/Core/Windows/ThingEditForm.cs +++ b/Source/Core/Windows/ThingEditForm.cs @@ -635,17 +635,18 @@ namespace CodeImp.DoomBuilder.Windows foreach(KeyValuePair> group in General.Map.Config.ThingFlagsCompare) { if(group.Value.Count < 2) continue; - int enabledcount = 0; + bool haveflags = false; foreach(CheckBox cb in flags.Checkboxes) { if (group.Value.ContainsKey(cb.Tag.ToString()) && cb.CheckState != CheckState.Unchecked) { - enabledcount++; + haveflags = true; + break; } } - if (enabledcount == 0) + if (!haveflags) { switch(group.Key) { diff --git a/Source/Core/Windows/ThingEditFormUDMF.cs b/Source/Core/Windows/ThingEditFormUDMF.cs index e395922a..560e3285 100644 --- a/Source/Core/Windows/ThingEditFormUDMF.cs +++ b/Source/Core/Windows/ThingEditFormUDMF.cs @@ -864,16 +864,17 @@ namespace CodeImp.DoomBuilder.Windows foreach(KeyValuePair> group in General.Map.Config.ThingFlagsCompare) { if(group.Value.Count < 2) continue; - int enabledcount = 0; + bool haveflags = false; foreach(CheckBox cb in flags.Checkboxes) { if(group.Value.ContainsKey(cb.Tag.ToString()) && cb.CheckState != CheckState.Unchecked) { - enabledcount++; + haveflags = true; + break; } } - if(enabledcount == 0) + if(!haveflags) { switch(group.Key) { diff --git a/Source/Plugins/BuilderModes/BuilderModes.csproj b/Source/Plugins/BuilderModes/BuilderModes.csproj index 9acb5007..a3ffaa65 100644 --- a/Source/Plugins/BuilderModes/BuilderModes.csproj +++ b/Source/Plugins/BuilderModes/BuilderModes.csproj @@ -237,6 +237,7 @@ + @@ -248,6 +249,7 @@ + diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs index d500b17f..8fbddc8b 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs @@ -1,7 +1,7 @@ using CodeImp.DoomBuilder.Map; using System.Threading; -namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks +namespace CodeImp.DoomBuilder.BuilderModes { [ErrorChecker("Check unconnected vertices", true, 50)] public class CheckStrayVertices : ErrorChecker diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckTextureAlignment.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckTextureAlignment.cs index 71a85d9e..775a5697 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/CheckTextureAlignment.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckTextureAlignment.cs @@ -12,7 +12,7 @@ using CodeImp.DoomBuilder.VisualModes; #endregion -namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks +namespace CodeImp.DoomBuilder.BuilderModes { [ErrorChecker("Check texture alignment", true, 1000)] public class CheckTextureAlignment : ErrorChecker diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs index ac51c656..fddc85d4 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnknownThings.cs @@ -1,8 +1,8 @@ using System.Threading; using CodeImp.DoomBuilder.Map; -namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks { - +namespace CodeImp.DoomBuilder.BuilderModes +{ [ErrorChecker("Check unknown things", true, 50)] public class CheckUnknownThings : ErrorChecker { diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckUnusedThings.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnusedThings.cs new file mode 100644 index 00000000..42094951 --- /dev/null +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckUnusedThings.cs @@ -0,0 +1,93 @@ +#region ================== Namespaces + +using System.Collections.Generic; +using System.Threading; +using CodeImp.DoomBuilder.Config; +using CodeImp.DoomBuilder.Map; + +#endregion + +namespace CodeImp.DoomBuilder.BuilderModes +{ + [ErrorChecker("Check unused things", true, 50)] + public class CheckUnusedThings : ErrorChecker + { + #region ================== Constants + + private const int PROGRESS_STEP = 10; + + #endregion + + #region ================== Constructor / Destructor + + public CheckUnusedThings() + { + // Total progress is done when all things are checked + SetTotalProgress(General.Map.Map.Things.Count / PROGRESS_STEP); + } + + #endregion + + #region ================== Methods + + // This runs the check + public override void Run() + { + int progress = 0; + int stepprogress = 0; + bool udmf = General.Map.UDMF; + + // Go for all things + foreach(Thing t in General.Map.Map.Things) + { + List messages = new List(); + foreach(KeyValuePair> group in General.Map.Config.ThingFlagsCompare) + { + if(group.Value.Count < 2) continue; + bool haveflags = false; + + foreach(KeyValuePair flags in group.Value) + { + if((udmf && t.Fields.ContainsKey(flags.Key) && (bool)t.Fields[flags.Key].Value) + || t.IsFlagSet(flags.Key)) + { + haveflags = true; + break; + } + } + + if(!haveflags) messages.Add(GetDescription(group.Key)); + } + + if(messages.Count > 0) + { + string msg = " is not used " + string.Join(", ", messages.ToArray()); + SubmitResult(new ResultUnusedThing(t, msg)); + } + + // 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); + } + } + } + + private string GetDescription(string group) + { + switch(group) + { + case "skills": return "in any skill level"; + case "gamemodes": return "in any game mode"; + case "classes": return "by any class"; + default: return "by any class, skill or game mode"; + } + } + + #endregion + } +} diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultStrayVertex.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultStrayVertex.cs index d0723dac..1961766a 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/ResultStrayVertex.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultStrayVertex.cs @@ -7,7 +7,7 @@ using System.Drawing; #endregion -namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks +namespace CodeImp.DoomBuilder.BuilderModes { public class ResultStrayVertex : ErrorResult { diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs index 19a1286b..0afafc73 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnknownThing.cs @@ -6,7 +6,8 @@ using CodeImp.DoomBuilder.Rendering; #endregion -namespace CodeImp.DoomBuilder.BuilderModes.ErrorChecks { +namespace CodeImp.DoomBuilder.BuilderModes +{ public class ResultUnknownThing : ErrorResult { diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultUnusedThing.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnusedThing.cs new file mode 100644 index 00000000..e9f26c0e --- /dev/null +++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultUnusedThing.cs @@ -0,0 +1,87 @@ +#region ================== Namespaces + +using System; +using CodeImp.DoomBuilder.Map; +using CodeImp.DoomBuilder.Rendering; + +#endregion + +namespace CodeImp.DoomBuilder.BuilderModes +{ + public class ResultUnusedThing : ErrorResult + { + #region ================== Variables + + private readonly Thing thing; + private readonly string details; + + #endregion + + #region ================== Properties + + public override int Buttons { get { return 2; } } + public override string Button1Text { get { return "Delete Thing"; } } + public override string Button2Text { get { return "Apply default flags"; } } + + #endregion + + #region ================== Constructor / Destructor + + public ResultUnusedThing(Thing t, string details) + { + // Initialize + this.thing = t; + this.details = details; + this.viewobjects.Add(t); + this.hidden = t.IgnoredErrorChecks.Contains(this.GetType()); + this.description = "This thing is not used by any class, skill or game mode."; + } + + #endregion + + #region ================== Methods + + // This sets if this result is displayed in ErrorCheckForm (mxd) + internal override void Hide(bool hide) + { + hidden = hide; + Type t = this.GetType(); + if(hide) thing.IgnoredErrorChecks.Add(t); + else if(thing.IgnoredErrorChecks.Contains(t)) thing.IgnoredErrorChecks.Remove(t); + } + + // This must return the string that is displayed in the listbox + public override string ToString() + { + return "Thing " + thing.Index + " (" + General.Map.Data.GetThingInfo(thing.Type).Title + ") at " + thing.Position.x + ", " + thing.Position.y + details; + } + + // Rendering + public override void RenderOverlaySelection(IRenderer2D renderer) + { + renderer.RenderThing(thing, renderer.DetermineThingColor(thing), 1.0f); + } + + // This removes the thing + public override bool Button1Click(bool batchMode) + { + if(!batchMode) General.Map.UndoRedo.CreateUndo("Delete thing"); + thing.Dispose(); + General.Map.IsChanged = true; + General.Map.ThingsFilter.Update(); + return true; + } + + // This sets default flags + public override bool Button2Click(bool batchMode) + { + if(!batchMode) General.Map.UndoRedo.CreateUndo("Set default thing flags"); + foreach(string f in General.Map.Config.DefaultThingFlags) thing.SetFlag(f, true); + General.Map.IsChanged = true; + General.Map.ThingsFilter.Update(); + return true; + } + + #endregion + } +}