// READERS / WRITERS /// <summary> /// Reads a data block from a file binary stream. /// </summary> /// <param name="binaryReader">Binary reader to read the block from. Must point to the beginning of the block.</param> /// <param name="index">Index within a sequence (starts at 0).</param> public void Read(SRBinaryReader binaryReader, int index) { try { binaryReader.Align(Alignment); SRTrace.WriteLine(""); SRTrace.WriteLine(" OBJECT #{0}: [file offset 0x{1:X8}]", index + 1, binaryReader.BaseStream.Position); handleOffset = binaryReader.ReadUInt64(); SRTrace.WriteLine(" Handle Offset: 0x{0:X16}", handleOffset); parentHandleOffset = binaryReader.ReadUInt64(); SRTrace.WriteLine(" Parent Handle Offset: 0x{0:X16}", parentHandleOffset); objectTypeHash = binaryReader.ReadInt32(); SRTrace.WriteLine(" Object Type Hash: 0x{0:X8}", objectTypeHash); var propertyCount = binaryReader.ReadUInt16(); SRTrace.WriteLine(" Number of Properties: {0}", propertyCount); var bufferSize = binaryReader.ReadUInt16(); SRTrace.WriteLine(" Buffer Size: {0}", bufferSize); var nameOffset = binaryReader.ReadUInt16(); SRTrace.WriteLine(" Name Offset: {0}", nameOffset); padding = binaryReader.ReadUInt16(); SRTrace.WriteLine(" Padding: {0}", padding); if (propertyCount == 0) throw new SRZoneFileException("Object has no properties."); propertyList = new List<SRZoneProperty>(propertyCount); var namePosition = binaryReader.BaseStream.Position + nameOffset - SRZoneProperty.DataOffset; name = null; for (int i = 0; i < propertyCount; i++) { long position = AlignUp(binaryReader.BaseStream.Position, SRZoneProperty.Alignment); SRZoneProperty property = SRZoneProperty.Create(binaryReader, i); propertyList.Add(property); if (position == namePosition) { if (property is SRZoneStringProperty) name = property.ToString(); else if (property.Type == SRZoneProperty.StringType) name = (i + 1).ToString(); else throw new SRZoneFileException("Name Offset does not point to a string property."); } } if (nameOffset != 0 && name == null) throw new SRZoneFileException("Name Offset does not point to a valid property."); } catch (Exception e) { // Add context information for the error message if (index >= 0) e.Data[BlockName] = index + 1; throw; } }
// READERS / WRITERS /// <summary> /// Reads a data block from a file binary stream. /// </summary> /// <param name="binaryReader">Binary reader to read the block from. Must point to the beginning of the block.</param> /// <param name="size">Maximum number of bytes to read.</param> public void Read(SRBinaryReader binaryReader, int size) { SRTrace.WriteLine(""); SRTrace.WriteLine(" OBJECT SECTION HEADER:"); signature = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Header Signature: 0x{0:X8}", signature); if (signature != 0x574F4246) throw new SRZoneFileException("Invalid section ID"); version = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Version: {0}", version); if (version != 5) throw new SRZoneFileException("Invalid version number"); var numObjects = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Number of Objects: {0}", numObjects); var numHandles = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Number of Handles: {0}", numHandles); flags = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Flags: 0x{0:X8}", flags); handleListPointer = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Handle List Pointer: 0x{0:X8} (run-time)", handleListPointer); objectDataPointer = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Object Data Pointer: 0x{0:X8} (run-time)", objectDataPointer); objectDataSize = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Object Data Size: {0,-10} (run-time)", objectDataSize); SRTrace.WriteLine(""); SRTrace.WriteLine(" HANDLE LIST:"); handleList = new List<UInt64>((int)numHandles); for (int i = 0; i < numHandles; i++) { handleList.Add(binaryReader.ReadUInt64()); SRTrace.WriteLine(" {0,3}. 0x{1:X16}", i + 1, handleList[i]); } objectList = new List<SRZoneObject>((int)numObjects); for (int i = 0; i < numObjects; i++) objectList.Add(new SRZoneObject(binaryReader, i)); }