public override void Write(BinaryWriter file) { base.Write(file); using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { Waypoints.Write(bw); ComponentsMappings.Write(bw); WaypointsGroups.Write(bw); Indexes.Write(bw); bw.Flush(); int buffersize = (int)ms.Position; int calcb = CalculateBufferSize(); if (buffersize != calcb) { throw new InvalidParsingException("Calculated buffer size is not equal to actual buffer size."); } file.Write(buffersize); file.Write(ms.ToArray()); } }
public override void Read(BinaryReader file, uint size) { base.Read(file, size); var buffersize = file.ReadUInt32(); var d = CalculateBufferSize(); if (buffersize != CalculateBufferSize()) { throw new InvalidParsingException("Calculated buffer size is not equal to actual buffer size."); } using (var ms = new MemoryStream(file.ReadBytes((int)buffersize))) using (var br = new BinaryReader(ms)) { int wc = WaypointsCount != null ? WaypointsCount.val : 0; int cc = ComponentsMappingsCount != null ? ComponentsMappingsCount.val : 0; int wgc = WaypointsGroupsCount != null ? WaypointsGroupsCount.val : 0; int ic = IndexesCount != null ? (int)IndexesCount.val : 0; Waypoints.Read(br, (uint)wc * 20, wc); ComponentsMappings.Read(br, (uint)cc * 32, cc); WaypointsGroups.Read(br, (uint)wgc * 12, wgc); Indexes.Read(br, (uint)ic * 2, ic); if (buffersize - ms.Position > 0) { throw new InvalidParsingException("Did not read buffer to the end."); } } }