// 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) { SRTrace.WriteLine(""); SRTrace.WriteLine("V-FILE HEADER:"); signature = binaryReader.ReadUInt16(); SRTrace.WriteLine(" V-File Signature: 0x{0:X4}", signature); if (signature != 0x3854) throw new Exception("Incorrect V-file signature. Not a valid zone header file."); version = binaryReader.ReadUInt16(); SRTrace.WriteLine(" V-File Version: {0}", version); if (version != 4) throw new Exception("Incorrect V-file version."); int refDataSize = binaryReader.ReadInt32(); SRTrace.WriteLine(" Reference Data Size: {0}", refDataSize); refDataStart = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Reference Data Start: 0x{0:X8}", refDataStart); // if (refDataStart != 0) // throw new SRZoneFileException("Expected reference data start to be zero."); int refCount = binaryReader.ReadInt32(); SRTrace.WriteLine(" Reference Count: {0}", refCount); unknown = binaryReader.ReadUInt32(); SRTrace.WriteLine(" Unknown: 0x{0:X8}", unknown); binaryReader.BaseStream.Seek(12, SeekOrigin.Current); long refDataOffset = binaryReader.BaseStream.Position; SRTrace.WriteLine(""); SRTrace.WriteLine(" REFERENCE DATA:"); referenceData = new List<string>(refCount); referenceNamesByReadOffset = new Dictionary<long, string>(refCount); var positionDataStart = binaryReader.BaseStream.Position; for (int i = 1; i <= refCount; i++) { long offset = binaryReader.BaseStream.Position - positionDataStart; string name = binaryReader.ReadString(); SRTrace.WriteLine(" {0,3}. {1}", i, name); referenceData.Add(name); referenceNamesByReadOffset.Add(offset, OptionNameReferenceIdentifier ? name : i.ToString()); } var finalNull = binaryReader.ReadByte(); if (finalNull != 0) throw new Exception("Expected trailing null byte."); }
/// <summary> /// Reads a property block from a file binary stream and creates a new property object containing the data. /// The returned property will be an instance of one of the concrete derived property classes. /// </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> /// <returns>The new zone property which was read from the input stream.</returns> public static SRZoneProperty Create(SRBinaryReader binaryReader, int index) { SRZoneProperty property; try { // Read the common header binaryReader.Align(Alignment); SRTrace.WriteLine(""); SRTrace.WriteLine(" PROPERTY #{0}: [file offset 0x{1:X8}]", index + 1, binaryReader.BaseStream.Position); UInt16 type = binaryReader.ReadUInt16(); string typeName = (type < PropertyTypeNames.Length) ? PropertyTypeNames[type] : "unknown"; SRTrace.WriteLine(" Type: {0} ({1})", type, typeName); UInt16 size = binaryReader.ReadUInt16(); SRTrace.WriteLine(" Size: {0} bytes", size); Int32 nameCrc = binaryReader.ReadInt32(); SRTrace.WriteLine(" Name CRC: 0x{0:X8} ({0})", nameCrc, nameCrc); // Create the appropriate derived class based on the header information property = Create(type, nameCrc); // Read the class-specific data into the derived class property.ReadData(binaryReader, size); // WARNING: There's a bunch of cruft after the "size" length which is part of the padding to // a dword boundry, but if we don't save it then the output file won't compare to the input file. if (OptionPreservePadding) { var paddingSize = AlignPaddingSize(binaryReader.BaseStream.Position, Alignment); if (paddingSize > 0) property.paddingData = new SRRawDataBlock(binaryReader, paddingSize); else property.paddingData = null; } } catch (Exception e) { // Add context information for the error message if (index >= 0) e.Data[BlockName] = index + 1; throw; } return property; }