public static byte[] Write(WaypointsDifficulty waypointsDifficulty) { using (BitWriter writer = new BitWriter()) { writer.WriteUInt16(waypointsDifficulty.Header ?? (UInt16)0x102); var skippedProperties = new string[] { "Header" }; Type waypointsDifficultyType = typeof(WaypointsDifficulty); foreach (var waypointsDifficultyProperty in waypointsDifficultyType.GetProperties()) { if (skippedProperties.Contains(waypointsDifficultyProperty.Name)) { continue; } Type type = waypointsDifficultyProperty.PropertyType; var waypoints = waypointsDifficultyProperty.GetValue(waypointsDifficulty); foreach (var property in type.GetProperties()) { writer.WriteBit((bool)property.GetValue(waypoints)); } } writer.Align(); writer.WriteBytes(new byte[17]); return(writer.ToArray()); } }
public static WaypointsDifficulty Read(byte[] bytes) { WaypointsDifficulty waypointsDifficulty = new WaypointsDifficulty(); using (BitReader reader = new BitReader(bytes)) { waypointsDifficulty.Header = reader.ReadUInt16(); BitArray bits = new BitArray(reader.ReadBytes(17)); int i = 0; var skippedProperties = new string[] { "Header" }; foreach (var waypointsDifficultyProperty in typeof(WaypointsDifficulty).GetProperties()) { if (skippedProperties.Contains(waypointsDifficultyProperty.Name)) { continue; } Type type = waypointsDifficultyProperty.PropertyType; var waypoints = Activator.CreateInstance(type); foreach (var property in type.GetProperties()) { property.SetValue(waypoints, bits[i++]); } waypointsDifficultyProperty.SetValue(waypointsDifficulty, waypoints); } return(waypointsDifficulty); } }
public static byte[] Write(WaypointsSection waypointsSection) { using (BitWriter writer = new BitWriter()) { writer.WriteUInt16(waypointsSection.Header ?? (UInt16)0x5357); writer.WriteUInt32(waypointsSection.Version ?? 0x1); writer.WriteUInt16(waypointsSection.Length ?? (UInt16)0x50); var skippedProperties = new string[] { "Header", "Version", "Length" }; foreach (var property in typeof(WaypointsSection).GetProperties()) { if (skippedProperties.Contains(property.Name)) { continue; } WaypointsDifficulty waypointsDifficulty = (WaypointsDifficulty)property.GetValue(waypointsSection); writer.WriteBytes(WaypointsDifficulty.Write(waypointsDifficulty)); } return(writer.ToArray()); } }
public static WaypointsSection Read(byte[] bytes) { WaypointsSection waypointsSection = new WaypointsSection(); using (BitReader reader = new BitReader(bytes)) { waypointsSection.Header = reader.ReadUInt16(); waypointsSection.Version = reader.ReadUInt32(); waypointsSection.Length = reader.ReadUInt16(); var skippedProperties = new string[] { "Magic", "Header", "Version", "Length" }; foreach (var property in typeof(WaypointsSection).GetProperties()) { if (skippedProperties.Contains(property.Name)) { continue; } property.SetValue(waypointsSection, WaypointsDifficulty.Read(reader.ReadBytes(24))); } return(waypointsSection); } }