mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-05 22:01:33 +00:00
This change looks dramatic, but it's just separating out the already-split namespaces into separate top-level folders. In theory, every single one could be built into their own Nuget package. `SabreTools.Serialization` still builds the normal Nuget package that is used by all other projects and includes all namespaces.
83 lines
2.9 KiB
C#
83 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace SabreTools.Wrappers
|
|
{
|
|
public partial class WAD3 : IExtractable
|
|
{
|
|
/// <inheritdoc/>
|
|
public bool Extract(string outputDirectory, bool includeDebug)
|
|
{
|
|
// If we have no lumps
|
|
if (DirEntries.Length == 0)
|
|
return false;
|
|
|
|
// Loop through and extract all lumps to the output
|
|
bool allExtracted = true;
|
|
for (int i = 0; i < DirEntries.Length; i++)
|
|
{
|
|
allExtracted &= ExtractLump(i, outputDirectory, includeDebug);
|
|
}
|
|
|
|
return allExtracted;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract a lump from the WAD3 to an output directory by index
|
|
/// </summary>
|
|
/// <param name="index">Lump index to extract</param>
|
|
/// <param name="outputDirectory">Output directory to write to</param>
|
|
/// <param name="includeDebug">True to include debug data, false otherwise</param>
|
|
/// <returns>True if the lump extracted, false otherwise</returns>
|
|
public bool ExtractLump(int index, string outputDirectory, bool includeDebug)
|
|
{
|
|
// If we have no lumps
|
|
if (DirEntries.Length == 0)
|
|
return false;
|
|
|
|
// If the lumps index is invalid
|
|
if (index < 0 || index >= DirEntries.Length)
|
|
return false;
|
|
|
|
// Read the data -- TODO: Handle uncompressed lumps (see BSP.ExtractTexture)
|
|
var lump = DirEntries[index];
|
|
var data = ReadRangeFromSource((int)lump.Offset, (int)lump.Length);
|
|
if (data.Length == 0)
|
|
return false;
|
|
|
|
// If we have an invalid output directory
|
|
if (string.IsNullOrEmpty(outputDirectory))
|
|
return false;
|
|
|
|
// Ensure directory separators are consistent
|
|
string filename = $"{lump.Name}.lmp";
|
|
if (Path.DirectorySeparatorChar == '\\')
|
|
filename = filename.Replace('/', '\\');
|
|
else if (Path.DirectorySeparatorChar == '/')
|
|
filename = filename.Replace('\\', '/');
|
|
|
|
// Ensure the full output directory exists
|
|
filename = Path.Combine(outputDirectory, filename);
|
|
var directoryName = Path.GetDirectoryName(filename);
|
|
if (directoryName is not null && !Directory.Exists(directoryName))
|
|
Directory.CreateDirectory(directoryName);
|
|
|
|
// Try to write the data
|
|
try
|
|
{
|
|
// Open the output file for writing
|
|
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
fs.Write(data, 0, data.Length);
|
|
fs.Flush();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.Error.WriteLine(ex);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|