From 643460efa6b85c56006d500c7eac8ef587d0c6e1 Mon Sep 17 00:00:00 2001 From: MaxED Date: Mon, 13 Jul 2015 11:20:02 +0000 Subject: [PATCH] Map Analysis mode: added "Check very short linedefs" check (unchecked by default), which finds linedefs shorter than 1 m.u. Fixed: script editor must be closed during "Open map in current WAD" action. Added: PNG offsets are now taken into account when loading sprites. --- Source/Core/General/General.cs | 3 + Source/Core/IO/FileImageReader.cs | 44 ++++++++++++- .../Plugins/BuilderModes/BuilderModes.csproj | 2 + .../ErrorChecks/CheckShortLinedefs.cs | 41 ++++++++++++ .../ErrorChecks/CheckStrayVertices.cs | 2 +- .../ErrorChecks/ResultMissingTexture.cs | 1 - .../ErrorChecks/ResultShortLinedef.cs | 66 +++++++++++++++++++ 7 files changed, 155 insertions(+), 4 deletions(-) create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/CheckShortLinedefs.cs create mode 100644 Source/Plugins/BuilderModes/ErrorChecks/ResultShortLinedef.cs diff --git a/Source/Core/General/General.cs b/Source/Core/General/General.cs index 892a35e8..acc96e42 100644 --- a/Source/Core/General/General.cs +++ b/Source/Core/General/General.cs @@ -1309,6 +1309,9 @@ namespace CodeImp.DoomBuilder mainwindow.ClearDisplay(); mainwindow.RemoveHintsDocker(); //mxd + //mxd. Close the script editor + map.CloseScriptEditor(false); + // Let the plugins know plugins.OnMapOpenBegin(); diff --git a/Source/Core/IO/FileImageReader.cs b/Source/Core/IO/FileImageReader.cs index 87e32f06..01c79485 100644 --- a/Source/Core/IO/FileImageReader.cs +++ b/Source/Core/IO/FileImageReader.cs @@ -376,9 +376,43 @@ namespace CodeImp.DoomBuilder.IO { offsetx = int.MinValue; offsety = int.MinValue; - return ReadAsBitmap(stream); - } + Bitmap bmp = ReadAsBitmap(stream); + //mxd. Read PNG offsets + if(imagetype == DevilImageType.IL_PNG && bmp != null) + { + stream.Position = 8; + using(BinaryReader reader = new BinaryReader(stream)) + { + // Read chunks untill we encounter either "grAb" or "IDAT" + while(reader.BaseStream.Position < reader.BaseStream.Length) + { + // Big Endian! + int chunklength = ToInt32BigEndian(reader.ReadBytes(4)); + string chunkname = new string(reader.ReadChars(4)); + + if(chunkname == "grAb") + { + offsetx = ToInt32BigEndian(reader.ReadBytes(4)); + offsety = ToInt32BigEndian(reader.ReadBytes(4)); + break; + } + else if(chunkname == "IDAT") + { + break; + } + else + { + // Skip the rest of the chunk + reader.BaseStream.Position += chunklength + 4; + } + } + } + } + + // Return the image + return bmp; + } // This reads the image and returns a Bitmap public Bitmap ReadAsBitmap(Stream stream) @@ -478,6 +512,12 @@ namespace CodeImp.DoomBuilder.IO throw new InvalidDataException(); } } + + //mxd + private static int ToInt32BigEndian(byte[] buffer) + { + return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; + } #endregion } diff --git a/Source/Plugins/BuilderModes/BuilderModes.csproj b/Source/Plugins/BuilderModes/BuilderModes.csproj index 1307fc98..5f9e8676 100644 --- a/Source/Plugins/BuilderModes/BuilderModes.csproj +++ b/Source/Plugins/BuilderModes/BuilderModes.csproj @@ -228,6 +228,7 @@ + @@ -238,6 +239,7 @@ + diff --git a/Source/Plugins/BuilderModes/ErrorChecks/CheckShortLinedefs.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckShortLinedefs.cs new file mode 100644 index 00000000..e63eb820 --- /dev/null +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckShortLinedefs.cs @@ -0,0 +1,41 @@ +using CodeImp.DoomBuilder.Map; +using System.Threading; + +namespace CodeImp.DoomBuilder.BuilderModes +{ + [ErrorChecker("Check very short linedefs", false, 10)] + public class CheckShortLinedefs : ErrorChecker + { + private const int PROGRESS_STEP = 1000; + + // Constructor + public CheckShortLinedefs() + { + // Total progress is done when all linedefs are checked + SetTotalProgress(General.Map.Map.Linedefs.Count / PROGRESS_STEP); + } + + // This runs the check + public override void Run() + { + int progress = 0; + int stepprogress = 0; + + // Go for all linedefs + foreach(Linedef l in General.Map.Map.Linedefs) + { + if(l.Length < 1.0f) SubmitResult(new ResultShortLinedef(l)); + + // 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/CheckStrayVertices.cs b/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs index e93f7c7b..f772d623 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/CheckStrayVertices.cs @@ -21,7 +21,7 @@ namespace CodeImp.DoomBuilder.BuilderModes int progress = 0; int stepprogress = 0; - // Go for all things + // Go for all vertices foreach(Vertex v in General.Map.Map.Vertices) { if(v.Linedefs == null || v.Linedefs.Count == 0) SubmitResult(new ResultStrayVertex(v)); diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultMissingTexture.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultMissingTexture.cs index 12c25048..eee2b532 100644 --- a/Source/Plugins/BuilderModes/ErrorChecks/ResultMissingTexture.cs +++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultMissingTexture.cs @@ -17,7 +17,6 @@ #region ================== Namespaces using System; -using CodeImp.DoomBuilder.Config; using CodeImp.DoomBuilder.Map; using CodeImp.DoomBuilder.Rendering; diff --git a/Source/Plugins/BuilderModes/ErrorChecks/ResultShortLinedef.cs b/Source/Plugins/BuilderModes/ErrorChecks/ResultShortLinedef.cs new file mode 100644 index 00000000..eba0afd0 --- /dev/null +++ b/Source/Plugins/BuilderModes/ErrorChecks/ResultShortLinedef.cs @@ -0,0 +1,66 @@ +#region ================== Namespaces + +using System; +using CodeImp.DoomBuilder.Map; +using CodeImp.DoomBuilder.Rendering; + +#endregion + +namespace CodeImp.DoomBuilder.BuilderModes +{ + public class ResultShortLinedef : ErrorResult + { + #region ================== Variables + + private readonly Linedef line; + + #endregion + + #region ================== Properties + + public override int Buttons { get { return 0; } } + + #endregion + + #region ================== Constructor / Destructor + + // Constructor + public ResultShortLinedef(Linedef l) + { + // Initialize + line = l; + viewobjects.Add(l); + hidden = l.IgnoredErrorChecks.Contains(this.GetType()); //mxd + description = "This linedef is shorter than 1 map unit. This can porentially cause nodebuilding errors."; + } + + #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) line.IgnoredErrorChecks.Add(t); + else if(line.IgnoredErrorChecks.Contains(t)) line.IgnoredErrorChecks.Remove(t); + } + + // This must return the string that is displayed in the listbox + public override string ToString() + { + return "Linedef " + line.Index + " is shorter than 1 m.u."; + } + + // Rendering + public override void PlotSelection(IRenderer2D renderer) + { + renderer.PlotLinedef(line, General.Colors.Selection); + renderer.PlotVertex(line.Start, ColorCollection.VERTICES); + renderer.PlotVertex(line.End, ColorCollection.VERTICES); + } + + #endregion + } +} \ No newline at end of file