From 7772ebbb5255a4ac5926f72ba53efe1a8f89e3dc Mon Sep 17 00:00:00 2001
From: MaxED <j.maxed@gmail.com>
Date: Sun, 17 Jul 2016 21:38:29 +0000
Subject: [PATCH] Fixed: voxels were incorrectly loaded from WADs. Fixed, Draw
 Geometry modes: fixed a crash when moving the mouse cursor over a vertex
 while holding Alt-Shift keys when there were no new points drawn in current
 mode.

---
 Source/Core/Data/WADReader.cs                 |  5 ++-
 .../ClassicModes/DrawGeometryMode.cs          | 32 +++++++++----------
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/Source/Core/Data/WADReader.cs b/Source/Core/Data/WADReader.cs
index 18af40cdf..d2a2b0a4b 100644
--- a/Source/Core/Data/WADReader.cs
+++ b/Source/Core/Data/WADReader.cs
@@ -988,7 +988,10 @@ namespace CodeImp.DoomBuilder.Data
 				if(lump != null)
 				{
 					voxellocation = location.GetDisplayName();
-					return lump.Stream;
+					
+					// Copy stream, because model/voxel streams are expected to be disposed
+					lump.Stream.Seek(0, SeekOrigin.Begin); // Rewind before use
+					return new MemoryStream(lump.Stream.ReadAllBytes());
 				}
 			}
 
diff --git a/Source/Plugins/BuilderModes/ClassicModes/DrawGeometryMode.cs b/Source/Plugins/BuilderModes/ClassicModes/DrawGeometryMode.cs
index 21ace362c..e33acbea9 100644
--- a/Source/Plugins/BuilderModes/ClassicModes/DrawGeometryMode.cs
+++ b/Source/Plugins/BuilderModes/ClassicModes/DrawGeometryMode.cs
@@ -308,10 +308,11 @@ namespace CodeImp.DoomBuilder.BuilderModes
 			DrawnVertex p = new DrawnVertex();
 			p.stitch = true; //mxd. Setting these to false seems to be a good way to create invalid geometry...
 			p.stitchline = true; //mxd
+			snaptocardinal = (snaptocardinal && points.Count > 0); //mxd. Don't snap to cardinal when there are no points
 
 			//mxd. If snap to cardinal directions is enabled and we have points, modify mouse position
 			Vector2D vm, gridoffset;
-			if(snaptocardinal && points.Count > 0)
+			if(snaptocardinal)
 			{
 				Vector2D offset = mousemappos - points[points.Count - 1].pos;
 				
@@ -354,7 +355,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
 				if(nv != null)
 				{
 					//mxd. Line angle must stay the same
-					if(snaptocardinal) //mxd
+					if(snaptocardinal)
 					{
 						Line2D ourline = new Line2D(points[points.Count - 1].pos, vm);
 						if(Math.Round(ourline.GetSideOfLine(nv.Position), 1) == 0)
@@ -377,23 +378,20 @@ namespace CodeImp.DoomBuilder.BuilderModes
 					//mxd. Line angle must stay the same
 					if(snaptocardinal)
 					{
-						if(points.Count > 0)
+						Line2D ourline = new Line2D(points[points.Count - 1].pos, vm);
+						Line2D nearestline = new Line2D(nl.Start.Position, nl.End.Position);
+						Vector2D intersection = Line2D.GetIntersectionPoint(nearestline, ourline, false);
+						if(!float.IsNaN(intersection.x))
 						{
-							Line2D ourline = new Line2D(points[points.Count - 1].pos, vm);
-							Line2D nearestline = new Line2D(nl.Start.Position, nl.End.Position);
-							Vector2D intersection = Line2D.GetIntersectionPoint(nearestline, ourline, false);
-							if(!float.IsNaN(intersection.x))
+							// Intersection is on nearestline?
+							float u = Line2D.GetNearestOnLine(nearestline.v1, nearestline.v2, intersection);
+
+							if(u < 0f || u > 1f) { }
+							else
 							{
-								// Intersection is on nearestline?
-								float u = Line2D.GetNearestOnLine(nearestline.v1, nearestline.v2, intersection);
-
-								if(u < 0f || u > 1f) { }
-								else
-								{
-									p.pos = new Vector2D((float)Math.Round(intersection.x, General.Map.FormatInterface.VertexDecimals),
-														 (float)Math.Round(intersection.y, General.Map.FormatInterface.VertexDecimals));
-									return p;
-								}
+								p.pos = new Vector2D((float)Math.Round(intersection.x, General.Map.FormatInterface.VertexDecimals),
+													 (float)Math.Round(intersection.y, General.Map.FormatInterface.VertexDecimals));
+								return p;
 							}
 						}
 					}
-- 
GitLab