private void UncompressStream()
{
#if DEBUG_GR2_SERIALIZATION
System.Console.WriteLine(String.Format(" ===== Repacking sections ===== "));
#endif
uint totalSize = 0;
foreach (var section in Sections)
{
totalSize += section.Header.uncompressedSize;
}
// Copy the whole file, as we'll update its contents because of relocations and marshalling fixups
byte[] uncompressedStream = new byte[totalSize];
this.Stream = new MemoryStream(uncompressedStream);
this.Reader = new BinaryReader(this.Stream);
for (int i = 0; i < Sections.Count; i++)
{
var section = Sections[i];
var hdr = section.Header;
byte[] sectionContents = new byte[hdr.compressedSize];
InputStream.Position = hdr.offsetInFile;
InputStream.Read(sectionContents, 0, (int)hdr.compressedSize);
var originalOffset = hdr.offsetInFile;
hdr.offsetInFile = (uint)Stream.Position;
if (section.Header.compression == 0)
{
Stream.Write(sectionContents, 0, sectionContents.Length);
}
else if (section.Header.uncompressedSize > 0)
{
var uncompressed = Granny2Compressor.Decompress(
(int)hdr.compression,
sectionContents, (int)hdr.uncompressedSize,
(int)hdr.first16bit, (int)hdr.first8bit, (int)hdr.uncompressedSize);
Stream.Write(uncompressed, 0, uncompressed.Length);
}
#if DEBUG_GR2_SERIALIZATION
System.Console.WriteLine(String.Format(" {0}: {1:X8} ({2}) --> {3:X8} ({4})", i, originalOffset, hdr.compressedSize, hdr.offsetInFile, hdr.uncompressedSize));
#endif
}
}