public void TestLMPFileWrite() { string fileName = Path.GetTempPath() + Guid.NewGuid() + ".lmp"; // Read BSP and then write to a temp file var reader = FileReader.OpenStream("testdata/map.bsp"); var bsp1 = new BSP(reader); reader.Dispose(); bsp1.WriteLMP(fileName, (int)LumpType.GameLump); // Re-open the temp file and do basic consistency check using var fs = File.Open(fileName, FileMode.Open, FileAccess.Read); using var reader2 = new BinaryReader(fs); Assert.Equal(20, reader2.ReadInt32()); // Lump header size Assert.Equal((int)LumpType.GameLump, reader2.ReadInt32()); // Lump index/type Assert.Equal(0, reader2.ReadInt32()); // Lump version Assert.Equal(60, reader2.ReadInt32()); // Lump data size Assert.Equal(1, reader2.ReadInt32()); // BSP revision reader2.Dispose(); File.Delete(fileName); }
/// <summary> /// Performs the lump extraction. Assumes that the options have been validated. /// </summary> /// <param name="opts">The lump extraction options</param> /// <exception cref="ArgumentException">Thrown if opts.BSPPath is not a file path</exception> /// <exception cref="LumpEmptyException">Thrown if the specified lump is already empty</exception> public static void ExtractLump(Options opts) { var lumpIndex = opts.LumpIndex; var bspFolder = Path.GetDirectoryName(opts.BSPPath); var bspName = Path.GetFileNameWithoutExtension(opts.BSPPath); if (bspFolder == null || bspName == null || Directory.Exists(opts.BSPPath)) { throw new ArgumentException("Invalid BSP path specified"); } // Read BSP Console.WriteLine("Reading and parsing BSP"); var bsp = new BSP(opts.BSPPath); if (bsp.Lumps[lumpIndex].Data.Length <= 1) { throw new LumpEmptyException(); } // True if we will modify the original BSP var willModify = !opts.DontClear; // Backup original BSP if (opts.MakeBackup && willModify) { Console.WriteLine("Saving BSP backup"); var backupBSPPath = Path.Combine(bspFolder, $"{bspName}.bsp.orig"); File.Move(opts.BSPPath, backupBSPPath); } // Write lump if (!opts.DontExtract) { Console.WriteLine("Writing .lmp file"); var lmpPath = Path.Combine(bspFolder, $"{bspName}_l_{lumpIndex}.lmp"); bsp.WriteLMP(lmpPath, lumpIndex); } // Clear lump if (!opts.DontClear) { Console.WriteLine("Clearing lump"); bsp.Lumps[lumpIndex].Clear(); } // Clear lump and write new BSP if (willModify) { Console.WriteLine("Writing to original file"); bsp.WriteBSP(opts.BSPPath); } Console.WriteLine("Done"); }
public void TestLMPWrite() { var reader1 = FileReader.OpenStream("testdata/map.bsp"); var bsp = new BSP(reader1); using var ms = new MemoryStream(); using var writer = new BinaryWriter(ms); bsp.WriteLMP(writer, (int)LumpType.GameLump); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); using var reader2 = new BinaryReader(ms); Assert.Equal(20, reader2.ReadInt32()); // Lump header size Assert.Equal((int)LumpType.GameLump, reader2.ReadInt32()); // Lump index/type Assert.Equal(0, reader2.ReadInt32()); // Lump version Assert.Equal(60, reader2.ReadInt32()); // Lump data size Assert.Equal(1, reader2.ReadInt32()); // BSP revision Assert.Equal(60, ms.Length - ms.Position); // Data }