public MemoryBlock(uint address, bool isSomething, LBA compressed, LBA filetable, LBA uncompressed) { this.address = address; this.isSomething = isSomething; this.compressed = compressed; this.filetable = filetable; this.uncompressed = uncompressed; cutscenes = new List <LBA>(); }
public byte[] ExtractBlock(Reader reader, LBA lba, uint baseOffset) { byte[] data; if (lba.lba < baseOffset || lba.size <= 0) { return(null); } reader.BaseStream.Seek((lba.lba - baseOffset) * 0x800, SeekOrigin.Begin); data = reader.ReadBytes((int)lba.size); return(data); }
public List <byte[]> ExtractPackedBlocks(Reader reader, LBA lba, uint baseOffset) { List <byte[]> datas = new List <byte[]>(); byte[] data; if (lba.lba < baseOffset || lba.size <= 0) { return(null); } reader.BaseStream.Seek((lba.lba - baseOffset) * 0x800, SeekOrigin.Begin); data = new byte[0]; uint end = (lba.lba + lba.size - baseOffset) * 0x800; bool previousWasZero = false; bool previousWasFF = false; while (reader.BaseStream.Position < end) { uint decompressedSize = reader.ReadUInt32(); // 0x8000 if (previousWasFF) { if (decompressedSize == 0xFFFFFFFF && reader.ReadUInt32() == 0) { reader.Align(0x800); previousWasFF = false; } else { reader.BaseStream.Position = 0x800 * (reader.BaseStream.Position / 0x800); byte[] uncompressedData = reader.ReadBytes(0x800); if (uncompressedData != null) { int originalDataLength = data.Length; Array.Resize(ref data, originalDataLength + uncompressedData.Length); Array.Copy(uncompressedData, 0, data, originalDataLength, uncompressedData.Length); } } } else { if (decompressedSize == 0) { if (previousWasZero) { reader.Align(0x800); previousWasZero = false; break; } else { previousWasZero = true; } previousWasFF = false; // If previous was zero, then padding to 0x800. If previous was not zero, then new file. print(decompressedSize + " - " + String.Format("0x{0:X8}", reader.BaseStream.Position)); datas.Add(data); data = new byte[0]; continue; } else if (decompressedSize == 0xFFFFFFFF) { if (previousWasZero) { reader.Align(0x800); previousWasFF = true; } previousWasZero = false; continue; } else { previousWasZero = false; previousWasFF = false; } uint compressedSize = reader.ReadUInt32(); print(decompressedSize + " - " + String.Format("0x{0:X8}", reader.BaseStream.Position)); byte[] uncompressedData = null; byte[] compressedData = reader.ReadBytes((int)compressedSize); using (var compressedStream = new MemoryStream(compressedData)) using (var lzo = new LzoStream(compressedStream, CompressionMode.Decompress)) using (Reader lzoReader = new Reader(lzo, Settings.s.IsLittleEndian)) { lzo.SetLength(decompressedSize); uncompressedData = lzoReader.ReadBytes((int)decompressedSize); } if (uncompressedData != null) { int originalDataLength = data.Length; Array.Resize(ref data, originalDataLength + uncompressedData.Length); Array.Copy(uncompressedData, 0, data, originalDataLength, uncompressedData.Length); } } } if (data.Length > 0) { datas.Add(data); } return(datas); }