From 554f79f585b04729dcda87931ed933fd3717d711 Mon Sep 17 00:00:00 2001
From: spherallic <spherallic@gmail.com>
Date: Tue, 30 May 2023 16:49:45 +0200
Subject: [PATCH] Revert "Internal: reverted mysterious MaxED edit in map
 saving functions"

This reverts commit 4d97f41a4be85d598542eb008fd157e30ef54475.
---
 Source/Core/General/MapManager.cs | 34 ++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/Source/Core/General/MapManager.cs b/Source/Core/General/MapManager.cs
index 8ee61e82..30f56a02 100644
--- a/Source/Core/General/MapManager.cs
+++ b/Source/Core/General/MapManager.cs
@@ -401,7 +401,7 @@ namespace CodeImp.DoomBuilder
 
 			// Copy the map lumps to the temp file
 			General.WriteLogLine("Copying map lumps to temporary file...");
-			CopyLumpsByType(mapwad, options.CurrentName, tempwad, TEMP_MAP_HEADER, true, true, true, true);
+			CopyLumpsByType(mapwad, options.CurrentName, tempwad, TEMP_MAP_HEADER, REPLACE_TARGET_MAP, true, true, true, true);
 
 			// Close the map file
 			mapwad.Dispose();
@@ -508,7 +508,7 @@ namespace CodeImp.DoomBuilder
 
 			// Copy the map lumps to the temp file
 			General.WriteLogLine("Copying map lumps to temporary file...");
-			CopyLumpsByType(mapwad, options.CurrentName, tempwad, TEMP_MAP_HEADER, true, true, true, true);
+			CopyLumpsByType(mapwad, options.CurrentName, tempwad, TEMP_MAP_HEADER, REPLACE_TARGET_MAP, true, true, true, true);
 
 			// Close the map file
 			mapwad.Dispose();
@@ -902,6 +902,7 @@ namespace CodeImp.DoomBuilder
             // Determine original map name
             string origmapname = (!string.IsNullOrEmpty(options.PreviousName) && purpose != SavePurpose.IntoFile) ? options.PreviousName : options.CurrentName;
 			string origwadfile = string.Empty; //mxd
+			int mapheaderindex = REPLACE_TARGET_MAP; //mxd. Lump index of the map file header in the source WAD
 
 			try 
 			{
@@ -987,7 +988,7 @@ namespace CodeImp.DoomBuilder
 			}
 
 			// Copy map lumps to target file
-			CopyLumpsByType(tempwad, TEMP_MAP_HEADER, targetwad, origmapname, true, true, includenodes, true);
+			CopyLumpsByType(tempwad, TEMP_MAP_HEADER, targetwad, origmapname, mapheaderindex, true, true, includenodes, true);
 
 			// mxd. Was the map renamed?
 			if(options.LevelNameChanged) 
@@ -1197,7 +1198,7 @@ namespace CodeImp.DoomBuilder
 
 				// Copy lumps to buildwad
 				General.WriteLogLine("Copying map lumps to temporary build file...");
-				CopyLumpsByType(tempwad, TEMP_MAP_HEADER, buildwad, BUILD_MAP_HEADER, true, false, false, true);
+				CopyLumpsByType(tempwad, TEMP_MAP_HEADER, buildwad, BUILD_MAP_HEADER, REPLACE_TARGET_MAP, true, false, false, true);
 
 				// Close buildwad
 				buildwad.Dispose();
@@ -1242,7 +1243,7 @@ namespace CodeImp.DoomBuilder
 					{
 						// Copy nodebuilder lumps to temp file
 						General.WriteLogLine("Copying nodebuilder lumps to temporary file...");
-						CopyLumpsByType(buildwad, BUILD_MAP_HEADER, tempwad, TEMP_MAP_HEADER, false, false, true, false);
+						CopyLumpsByType(buildwad, BUILD_MAP_HEADER, tempwad, TEMP_MAP_HEADER, REPLACE_TARGET_MAP, false, false, true, false);
 					}
 					else 
 					{
@@ -1442,11 +1443,13 @@ namespace CodeImp.DoomBuilder
 		// This copies specific map lumps from one WAD to another
 		private void CopyLumpsByType(WAD source, string sourcemapname,
 									 WAD target, string targetmapname,
+									 int targetheaderinsertindex, //mxd
 									 bool copyrequired, bool copyblindcopy,
 									 bool copynodebuild, bool copyscript) 
 		{
 			// Find the map header in target (mxd. Or use the provided one)
-            int tgtheaderindex = target.FindLumpIndex(targetmapname);
+			bool replacetargetmap = (targetheaderinsertindex == REPLACE_TARGET_MAP); //mxd
+			int tgtheaderindex = (replacetargetmap ? target.FindLumpIndex(targetmapname) : targetheaderinsertindex); //mxd
 			if(tgtheaderindex == -1) 
 			{
 				// If this header doesnt exists in the target
@@ -1477,13 +1480,16 @@ namespace CodeImp.DoomBuilder
 						if(sourceindex > -1) 
 						{
 							//mxd. Don't do this when inserting a map (SaveMap() removes the old version of the map before calling CopyLumpsByType())
-							// Remove lump at target
-							int lumpindex = RemoveSpecificLump(target, tgtlumpname, tgtheaderindex, targetmapname, config.MapLumps);
+							if(replacetargetmap)
+							{
+								// Remove lump at target
+								int lumpindex = RemoveSpecificLump(target, tgtlumpname, tgtheaderindex, targetmapname, config.MapLumps);
 
-							// Determine target index
-							// When original lump was found and removed then insert at that position
-							// otherwise insert after last insertion position
-							if(lumpindex > -1) targetindex = lumpindex; else targetindex++;
+								// Determine target index
+								// When original lump was found and removed then insert at that position
+								// otherwise insert after last insertion position
+								if(lumpindex > -1) targetindex = lumpindex; else targetindex++;
+							}
 							if(targetindex > target.Lumps.Count) targetindex = target.Lumps.Count;
 
 							// Copy the lump to the target
@@ -1491,6 +1497,9 @@ namespace CodeImp.DoomBuilder
 							Lump lump = source.Lumps[sourceindex];
 							Lump newlump = target.Insert(tgtlumpname, targetindex, lump.Length, false);
 							lump.CopyTo(newlump);
+
+							//mxd. We still need to increment targetindex...
+							if(!replacetargetmap) targetindex++;
 						}
 						else 
 						{
@@ -1505,6 +1514,7 @@ namespace CodeImp.DoomBuilder
 				}
 
 				target.WriteHeaders(); //mxd
+                target.Compress(); // [ZZ]
 			}
 		}
 
-- 
GitLab