From bdc6a239344a70e3db5181667163c86c943cc7c9 Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Sat, 23 May 2020 10:01:52 +0200
Subject: [PATCH] Fixed some more cases where UniValues were set to floats.
 Added throwing exceptions to the UniValue constructor (gasp!)

---
 Source/Core/Geometry/Tools.cs                        | 12 ++++++------
 Source/Core/IO/ClipboardStreamWriter.cs              |  4 ++--
 Source/Core/Map/Sector.cs                            |  4 ++--
 Source/Core/Map/UniFields.cs                         |  1 +
 Source/Core/Map/UniValue.cs                          |  8 ++++++++
 Source/Core/Rendering/Renderer3D.cs                  | 10 +++++-----
 Source/Core/Rendering/SurfaceEntry.cs                |  2 +-
 Source/Core/Rendering/SurfaceManager.cs              |  2 +-
 Source/Core/Rendering/SurfaceUpdate.cs               |  2 +-
 .../Plugins/BuilderModes/VisualModes/VisualLower.cs  |  4 ++--
 10 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/Source/Core/Geometry/Tools.cs b/Source/Core/Geometry/Tools.cs
index 4d38d46aa..edca7fded 100755
--- a/Source/Core/Geometry/Tools.cs
+++ b/Source/Core/Geometry/Tools.cs
@@ -1687,7 +1687,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Front.MiddleRequired() && l.Front.LongMiddleTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Front.LongMiddleTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Front.LongMiddleTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Front.Fields, "offsetx_mid", offset);
 					}
@@ -1695,7 +1695,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Front.HighRequired() && l.Front.LongHighTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Front.LongHighTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Front.LongHighTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Front.Fields, "offsetx_top", offset);
 					}
@@ -1703,7 +1703,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Front.LowRequired() && l.Front.LongLowTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Front.LongLowTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Front.LongLowTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Front.Fields, "offsetx_bottom", offset);
 					}
@@ -1714,7 +1714,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Back.MiddleRequired() && l.Back.LongMiddleTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Back.LongMiddleTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Back.LongMiddleTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Back.Fields, "offsetx_mid", offset);
 					}
@@ -1722,7 +1722,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Back.HighRequired() && l.Back.LongHighTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Back.LongHighTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Back.LongHighTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Back.Fields, "offsetx_top", offset);
 					}
@@ -1730,7 +1730,7 @@ namespace CodeImp.DoomBuilder.Geometry
 					if(l.Back.LowRequired() && l.Back.LongLowTexture != MapSet.EmptyLongName && General.Map.Data.GetTextureExists(l.Back.LongLowTexture)) 
 					{
 						ImageData texture = General.Map.Data.GetTextureImage(l.Back.LongLowTexture);
-						float offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
+						double offset = (int)Math.Round((reversed ? totalLength - curLength - l.Length : curLength));
 						if(texture.IsImageLoaded) offset %= texture.Width;
 						if(offset > 0) UniFields.SetFloat(l.Back.Fields, "offsetx_bottom", offset);
 					}
diff --git a/Source/Core/IO/ClipboardStreamWriter.cs b/Source/Core/IO/ClipboardStreamWriter.cs
index 93c4b0654..2ac34c7e4 100755
--- a/Source/Core/IO/ClipboardStreamWriter.cs
+++ b/Source/Core/IO/ClipboardStreamWriter.cs
@@ -279,12 +279,12 @@ namespace CodeImp.DoomBuilder.IO
 				{
 					writer.Write((int)UniversalType.Boolean);
 					writer.Write((bool)f.Value.Value);
-				} 
+				}/*
 				else if(f.Value.Value is float) 
 				{
 					writer.Write((int)UniversalType.Float);
 					writer.Write((float)f.Value.Value);
-				}
+				}*/
 				else if(f.Value.Value is double)
 				{
 					writer.Write((int)UniversalType.Float);
diff --git a/Source/Core/Map/Sector.cs b/Source/Core/Map/Sector.cs
index 87a18c8ce..8047f5d64 100755
--- a/Source/Core/Map/Sector.cs
+++ b/Source/Core/Map/Sector.cs
@@ -128,12 +128,12 @@ namespace CodeImp.DoomBuilder.Map
 		//mxd. Rednering
 		public Color4 FogColor { get { return fogcolor; } }
 		public SectorFogMode FogMode { get { return fogmode; } }
-        public float Desaturation
+        public double Desaturation
         {
             get
             {
                 if (General.Map.UDMF && Fields.ContainsKey("desaturation"))
-                    return (float)Fields["desaturation"].Value;
+                    return (double)Fields["desaturation"].Value;
                 return 0f;
             }
         }
diff --git a/Source/Core/Map/UniFields.cs b/Source/Core/Map/UniFields.cs
index b57daed0b..2790e9358 100755
--- a/Source/Core/Map/UniFields.cs
+++ b/Source/Core/Map/UniFields.cs
@@ -226,6 +226,7 @@ namespace CodeImp.DoomBuilder.Map
 			if(val1.Type != val2.Type) return false;
 			if(val1.Value is int) return (int)val1.Value == (int)val2.Value;
 			if(val1.Value is float) return (float)val1.Value == (float)val2.Value;
+			if(val1.Value is double) return (double)val1.Value == (double)val2.Value;
 			if(val1.Value is bool) return (bool)val1.Value == (bool)val2.Value;
 			if(val1.Value is string) return (string)val1.Value == (string)val2.Value;
 			throw new NotImplementedException("Unknown Custom Field type: " + val1.Value.GetType());
diff --git a/Source/Core/Map/UniValue.cs b/Source/Core/Map/UniValue.cs
index d1b81b4f9..134e57d07 100755
--- a/Source/Core/Map/UniValue.cs
+++ b/Source/Core/Map/UniValue.cs
@@ -68,6 +68,10 @@ namespace CodeImp.DoomBuilder.Map
 		// Constructor
 		public UniValue(int type, object value)
 		{
+			// Value may only be a primitive type. Throwing exceptions in the constructor! Civil war!
+			if ((!(value is int) && !(value is double) && !(value is string) && !(value is bool)) || (value == null))
+				throw new ArgumentException("Universal field values can only be of type int, double, string or bool.");
+
 			this.type = type;
 			this.value = value;
 			
@@ -78,6 +82,10 @@ namespace CodeImp.DoomBuilder.Map
 		// Constructor
 		public UniValue(UniversalType type, object value)
 		{
+			// Value may only be a primitive type. Throwing exceptions in the constructor! Civil war!
+			if ((!(value is int) && !(value is double) && !(value is string) && !(value is bool)) || (value == null))
+				throw new ArgumentException("Universal field values can only be of type int, double, string or bool.");
+
 			this.type = (int)type;
 			this.value = value;
 
diff --git a/Source/Core/Rendering/Renderer3D.cs b/Source/Core/Rendering/Renderer3D.cs
index 37abebe1a..c54c28c11 100755
--- a/Source/Core/Rendering/Renderer3D.cs
+++ b/Source/Core/Rendering/Renderer3D.cs
@@ -897,7 +897,7 @@ namespace CodeImp.DoomBuilder.Rendering
 						graphics.SetUniform(UniformName.highlightcolor, CalculateHighlightColor((g == highlighted) && showhighlight, (g.Selected && showselection)));
 
                         // [ZZ] include desaturation factor
-                        graphics.SetUniform(UniformName.desaturation, sector.Sector.Desaturation);
+                        graphics.SetUniform(UniformName.desaturation, (float)sector.Sector.Desaturation);
 
 						// Render!
 						graphics.Draw(PrimitiveType.TriangleList, g.VertexOffset, g.Triangles);
@@ -994,7 +994,7 @@ namespace CodeImp.DoomBuilder.Rendering
 
                             // [ZZ] apply desaturation
                             if (t.Thing.Sector != null)
-                                graphics.SetUniform(UniformName.desaturation, t.Thing.Sector.Desaturation);
+                                graphics.SetUniform(UniformName.desaturation, (float)t.Thing.Sector.Desaturation);
                             else graphics.SetUniform(UniformName.desaturation, 0.0f);
 
                             // Apply changes
@@ -1204,7 +1204,7 @@ namespace CodeImp.DoomBuilder.Rendering
                     }
 
                     //
-                    graphics.SetUniform(UniformName.desaturation, sector.Sector.Desaturation);
+                    graphics.SetUniform(UniformName.desaturation, (float)sector.Sector.Desaturation);
 
                     // Set the colors to use
                     graphics.SetUniform(UniformName.sectorfogcolor, sector.Sector.FogColor);
@@ -1340,7 +1340,7 @@ namespace CodeImp.DoomBuilder.Rendering
                         graphics.SetUniform(UniformName.stencilColor, t.StencilColor.ToColorValue());
 
                         //
-                        graphics.SetUniform(UniformName.desaturation, t.Thing.Sector.Desaturation);
+                        graphics.SetUniform(UniformName.desaturation, (float)t.Thing.Sector.Desaturation);
 
                         // Apply changes
                         graphics.SetUniform(UniformName.world, world);
@@ -1514,7 +1514,7 @@ namespace CodeImp.DoomBuilder.Rendering
 				}
 
                 if (t.Thing.Sector != null)
-                    graphics.SetUniform(UniformName.desaturation, t.Thing.Sector.Desaturation);
+                    graphics.SetUniform(UniformName.desaturation, (float)t.Thing.Sector.Desaturation);
                 else graphics.SetUniform(UniformName.desaturation, 0.0f);
 
                 int lightIndex = 0;
diff --git a/Source/Core/Rendering/SurfaceEntry.cs b/Source/Core/Rendering/SurfaceEntry.cs
index 3b58acd4e..add84fe28 100755
--- a/Source/Core/Rendering/SurfaceEntry.cs
+++ b/Source/Core/Rendering/SurfaceEntry.cs
@@ -50,7 +50,7 @@ namespace CodeImp.DoomBuilder.Rendering
 		public long ceiltexture;
 
         //
-        public float desaturation;
+        public double desaturation;
 		
 		// Constructor
 		internal SurfaceEntry(int numvertices, int bufferindex, int vertexoffset)
diff --git a/Source/Core/Rendering/SurfaceManager.cs b/Source/Core/Rendering/SurfaceManager.cs
index 7c5a3b7ed..5fbec73b2 100755
--- a/Source/Core/Rendering/SurfaceManager.cs
+++ b/Source/Core/Rendering/SurfaceManager.cs
@@ -616,7 +616,7 @@ namespace CodeImp.DoomBuilder.Rendering
 					VertexBuffer lastbuffer = null;
 					foreach(SurfaceEntry entry in imgsurfaces.Value)
 					{
-                        graphics.SetUniform(UniformName.desaturation, entry.desaturation);
+                        graphics.SetUniform(UniformName.desaturation, (float)entry.desaturation);
                         
 						// Set the vertex buffer
 						SurfaceBufferSet set = sets[entry.numvertices];
diff --git a/Source/Core/Rendering/SurfaceUpdate.cs b/Source/Core/Rendering/SurfaceUpdate.cs
index 30a52a188..969f8bc89 100755
--- a/Source/Core/Rendering/SurfaceUpdate.cs
+++ b/Source/Core/Rendering/SurfaceUpdate.cs
@@ -37,7 +37,7 @@ namespace CodeImp.DoomBuilder.Rendering
 		public long ceiltexture;
 
         //
-        public float desaturation;
+        public double desaturation;
 		
 		// Constructor
 		internal SurfaceUpdate(int numvertices, bool updatefloor, bool updateceiling)
diff --git a/Source/Plugins/BuilderModes/VisualModes/VisualLower.cs b/Source/Plugins/BuilderModes/VisualModes/VisualLower.cs
index ebfa8a324..9bb865902 100755
--- a/Source/Plugins/BuilderModes/VisualModes/VisualLower.cs
+++ b/Source/Plugins/BuilderModes/VisualModes/VisualLower.cs
@@ -258,13 +258,13 @@ namespace CodeImp.DoomBuilder.BuilderModes
 		protected override void SetTextureOffsetX(int x)
 		{
 			Sidedef.Fields.BeforeFieldsChange();
-			Sidedef.Fields["offsetx_bottom"] = new UniValue(UniversalType.Float, (float)x);
+			Sidedef.Fields["offsetx_bottom"] = new UniValue(UniversalType.Float, (double)x);
 		}
 
 		protected override void SetTextureOffsetY(int y)
 		{
 			Sidedef.Fields.BeforeFieldsChange();
-			Sidedef.Fields["offsety_bottom"] = new UniValue(UniversalType.Float, (float)y);
+			Sidedef.Fields["offsety_bottom"] = new UniValue(UniversalType.Float, (double)y);
 		}
 
 		protected override void MoveTextureOffset(int offsetx, int offsety)
-- 
GitLab