From b996d8a380056ed9cb3a3f90f3c534e29e77e53c Mon Sep 17 00:00:00 2001
From: ZZYZX <zzyzx@virtual>
Date: Tue, 14 Feb 2017 11:56:38 +0200
Subject: [PATCH] Internal: not copying all lumps manually anymore on 'Save
 As'. The WAD is copied with File.Copy anyway.

---
 Source/Core/General/MapManager.cs             | 49 ++++---------------
 Source/Core/IO/WAD.cs                         | 28 ++++++++---
 Source/Core/Properties/AssemblyInfo.cs        |  4 +-
 .../BuilderModes/Properties/AssemblyInfo.cs   |  2 +-
 4 files changed, 32 insertions(+), 51 deletions(-)

diff --git a/Source/Core/General/MapManager.cs b/Source/Core/General/MapManager.cs
index bbba1e855..446b58396 100755
--- a/Source/Core/General/MapManager.cs
+++ b/Source/Core/General/MapManager.cs
@@ -937,52 +937,20 @@ namespace CodeImp.DoomBuilder
 					if(File.Exists(settingsfile)) File.Delete(settingsfile);
 				}
 
+                // [ZZ] We really want to tell apart saving into current archive from saving into a new one.
+                //      Treat "save as" into the current archive as normal save.
+                bool isSaveAs = (purpose == SavePurpose.AsNewFile) && (newfilepathname != filepathname);
 				// On Save AS we have to copy the previous file to the new file
-				if((purpose == SavePurpose.AsNewFile) && (filepathname != string.Empty)) 
+				if(isSaveAs) 
 				{
 					// Copy if original file still exists
 					if(File.Exists(filepathname)) File.Copy(filepathname, newfilepathname, true);
 				}
 
 				// If the target file exists, we need to rebuild it
-				if(File.Exists(newfilepathname)) 
-				{
-					// Move the target file aside
-					origwadfile = newfilepathname + ".temp";
-					File.Move(newfilepathname, origwadfile);
-
-					// Open original file
-					WAD origwad = new WAD(origwadfile, true);
-
-					// Create new target file
-					targetwad = new WAD(newfilepathname) { IsIWAD = origwad.IsIWAD }; //mxd. Let's preserve wad type
-
-					// Copy all lumps, except the original map
-					GameConfiguration origcfg; //mxd
-					if(origmapconfigname == configinfo.Filename) 
-					{
-						origcfg = config;
-					} 
-					else 
-					{
-						ConfigurationInfo ci = General.GetConfigurationInfo(origmapconfigname);
-						origcfg = new GameConfiguration(ci.Configuration);
-
-						// Needed only once!
-						origmapconfigname = configinfo.Filename;
-					}
-
-					mapheaderindex = CopyAllLumpsExceptMap(origwad, targetwad, origcfg, origmapname);
-
-					// Close original file and delete it
-					origwad.Dispose();
-					File.Delete(origwadfile);
-				}
-				else 
-				{
-					// Create new target file
-					targetwad = new WAD(newfilepathname);
-				}
+                // [ZZ] The original code here would do some weird trickery with tempfiles.
+                //      I'm just finding the map lumps and deleting them on later stages.
+				targetwad = new WAD(newfilepathname);
 			} 
 			catch(Exception e) 
 			{
@@ -1006,7 +974,7 @@ namespace CodeImp.DoomBuilder
 				data.Resume();
 				General.WriteLogLine("Map saving failed: " + e.Message);
 				return false;
-			} 
+			}
 
 			// Copy map lumps to target file
 			CopyLumpsByType(tempwadreader.WadFile, TEMP_MAP_HEADER, targetwad, origmapname, mapheaderindex, true, true, includenodes, true);
@@ -1582,6 +1550,7 @@ namespace CodeImp.DoomBuilder
 				}
 
 				target.WriteHeaders(); //mxd
+                target.Compress(); // [ZZ]
 			}
 		}
 
diff --git a/Source/Core/IO/WAD.cs b/Source/Core/IO/WAD.cs
index 5997e6373..e28a4b6ec 100755
--- a/Source/Core/IO/WAD.cs
+++ b/Source/Core/IO/WAD.cs
@@ -290,8 +290,11 @@ namespace CodeImp.DoomBuilder.IO
 		// This writes the WAD header and lumps table
 		public void WriteHeaders()
 		{
-			// Seek to beginning
-			file.Seek(0, SeekOrigin.Begin);
+            // [ZZ] don't allow any edit actions on readonly archive
+            if (isreadonly) return;
+
+            // Seek to beginning
+            file.Seek(0, SeekOrigin.Begin);
 
 			// Write WAD type
 			writer.Write(ENCODING.GetBytes(isiwad ? TYPE_IWAD : TYPE_PWAD));
@@ -425,8 +428,11 @@ namespace CodeImp.DoomBuilder.IO
 		public Lump Insert(string name, int position, int datalength) { return Insert(name, position, datalength, true); } //mxd
 		public Lump Insert(string name, int position, int datalength, bool writeheaders)
 		{
-			// We will be adding a lump
-			numlumps++;
+            // [ZZ] don't allow any edit actions on readonly archive
+            if (isreadonly) return null;
+
+            // We will be adding a lump
+            numlumps++;
 			
 			// Extend the file
 			file.SetLength(file.Length + datalength + 16);
@@ -448,8 +454,11 @@ namespace CodeImp.DoomBuilder.IO
 		// This removes a lump from the WAD file by index
 		public void RemoveAt(int index)
 		{
-			// Remove from list
-			Lump l = lumps[index];
+            // [ZZ] don't allow any edit actions on readonly archive
+            if (isreadonly) return;
+
+            // Remove from list
+            Lump l = lumps[index];
 			lumps.RemoveAt(index);
 			l.Dispose();
 			numlumps--;
@@ -461,8 +470,11 @@ namespace CodeImp.DoomBuilder.IO
 		// This removes a lump from the WAD file
 		public void Remove(Lump lump)
 		{
-			// Remove from list
-			lumps.Remove(lump);
+            // [ZZ] don't allow any edit actions on readonly archive
+            if (isreadonly) return;
+
+            // Remove from list
+            lumps.Remove(lump);
 			lump.Dispose();
 			numlumps--;
 			
diff --git a/Source/Core/Properties/AssemblyInfo.cs b/Source/Core/Properties/AssemblyInfo.cs
index b63dfe53a..bbb2c1c66 100755
--- a/Source/Core/Properties/AssemblyInfo.cs
+++ b/Source/Core/Properties/AssemblyInfo.cs
@@ -30,6 +30,6 @@ using CodeImp.DoomBuilder;
 //      Build Number
 //      Revision
 //
-[assembly: AssemblyVersion("2.3.0.2888")]
+[assembly: AssemblyVersion("2.3.0.2891")]
 [assembly: NeutralResourcesLanguageAttribute("en")]
-[assembly: AssemblyHash("946e805")]
+[assembly: AssemblyHash("1231450")]
diff --git a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
index 71efc34d0..8b9511fa1 100755
--- a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
+++ b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
@@ -29,5 +29,5 @@ using System.Resources;
 //      Build Number
 //      Revision
 //
-[assembly: AssemblyVersion("2.3.0.2888")]
+[assembly: AssemblyVersion("2.3.0.2891")]
 [assembly: NeutralResourcesLanguageAttribute("en")]
-- 
GitLab