From ab4126a39f62e9fdcb86341188d23642812439a3 Mon Sep 17 00:00:00 2001 From: spherallic Date: Tue, 30 May 2023 16:49:57 +0200 Subject: [PATCH] Revert "Internal: not copying all lumps manually anymore on 'Save As'. The WAD is copied with File.Copy anyway." This reverts commit ec90554cf4d68f45b7a52b3fb6e200654272b1de. --- Source/Core/General/MapManager.cs | 49 +++++++++++++++++++++++++------ Source/Core/IO/WAD.cs | 28 +++++------------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/Source/Core/General/MapManager.cs b/Source/Core/General/MapManager.cs index 5d77584..ce0770d 100644 --- a/Source/Core/General/MapManager.cs +++ b/Source/Core/General/MapManager.cs @@ -948,20 +948,52 @@ 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(isSaveAs) + if((purpose == SavePurpose.AsNewFile) && (!String.IsNullOrEmpty(filepathname))) { // 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 - // [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); + 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); + } } catch(Exception e) { @@ -985,7 +1017,7 @@ namespace CodeImp.DoomBuilder data.Resume(); General.WriteLogLine("Map saving failed: " + e.Message); return false; - } + } // Copy map lumps to target file CopyLumpsByType(tempwad, TEMP_MAP_HEADER, targetwad, origmapname, mapheaderindex, true, true, includenodes, true); @@ -1561,7 +1593,6 @@ namespace CodeImp.DoomBuilder } target.WriteHeaders(); //mxd - target.Compress(); // [ZZ] } } diff --git a/Source/Core/IO/WAD.cs b/Source/Core/IO/WAD.cs index 2efa6c2..2818a5b 100644 --- a/Source/Core/IO/WAD.cs +++ b/Source/Core/IO/WAD.cs @@ -280,11 +280,8 @@ namespace CodeImp.DoomBuilder.IO // This writes the WAD header and lumps table public void WriteHeaders() { - // [ZZ] don't allow any edit actions on readonly archive - if (isreadonly) return; - - // Seek to beginning - file.Seek(0, SeekOrigin.Begin); + // Seek to beginning + file.Seek(0, SeekOrigin.Begin); // Write WAD type writer.Write(ENCODING.GetBytes(isiwad ? TYPE_IWAD : TYPE_PWAD)); @@ -372,11 +369,8 @@ 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) { - // [ZZ] don't allow any edit actions on readonly archive - if (isreadonly) return null; - - // We will be adding a lump - numlumps++; + // We will be adding a lump + numlumps++; // Extend the file file.SetLength(file.Length + datalength + 16); @@ -398,11 +392,8 @@ namespace CodeImp.DoomBuilder.IO // This removes a lump from the WAD file by index public void RemoveAt(int index) { - // [ZZ] don't allow any edit actions on readonly archive - if (isreadonly) return; - - // Remove from list - Lump l = lumps[index]; + // Remove from list + Lump l = lumps[index]; lumps.RemoveAt(index); l.Dispose(); numlumps--; @@ -414,11 +405,8 @@ namespace CodeImp.DoomBuilder.IO // This removes a lump from the WAD file public void Remove(Lump lump) { - // [ZZ] don't allow any edit actions on readonly archive - if (isreadonly) return; - - // Remove from list - lumps.Remove(lump); + // Remove from list + lumps.Remove(lump); lump.Dispose(); numlumps--;