public void Write(EndianWriter writer, double?version) { foreach (var partition in partitions) { writer.WriteObject(partition); } }
public void Write(EndianWriter writer, double?version) { foreach (var section in sections) { writer.WriteObject(section); } }
public void WriteStrings02(ByteOrder order) { var obj = new DataClass06 { Property1 = "Length_Prefixed_String_#01!", Property2 = "Length_Prefixed_String_#02!", Property3 = "Length_Prefixed_String_#03!" }; using (var stream = new MemoryStream()) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); reader.Seek(0x00, SeekOrigin.Begin); Assert.AreEqual(obj.Property1, reader.ReadString(ByteOrder.BigEndian)); reader.Seek(0x20, SeekOrigin.Begin); Assert.AreEqual(obj.Property2, reader.ReadString(ByteOrder.LittleEndian)); reader.Seek(0x40, SeekOrigin.Begin); Assert.AreEqual(obj.Property3, reader.ReadString(ByteOrder.BigEndian)); } }
public void Basic01(ByteOrder order) { var rng = new Random(); var obj = new DataClass01 { Property1 = (sbyte)rng.Next(sbyte.MinValue, sbyte.MaxValue), Property2 = (short)rng.Next(short.MinValue, short.MaxValue), Property3 = (int)rng.Next(int.MinValue, int.MaxValue), Property4 = (long)rng.Next(int.MinValue, int.MaxValue), Property5 = (byte)rng.Next(byte.MinValue, byte.MaxValue), Property6 = (ushort)rng.Next(ushort.MinValue, ushort.MaxValue), Property7 = unchecked ((uint)rng.Next(int.MinValue, int.MaxValue)), Property8 = (ulong)unchecked ((uint)rng.Next(int.MinValue, int.MaxValue)), Property9 = (float)rng.NextDouble(), Property10 = (double)rng.NextDouble(), Property11 = Guid.NewGuid() }; using (var stream = new MemoryStream(new byte[500])) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); Assert.AreEqual(0xFF, stream.Position); reader.Seek(0x00, SeekOrigin.Begin); Assert.AreEqual(obj.Property1, reader.ReadSByte()); reader.Seek(0x10, SeekOrigin.Begin); Assert.AreEqual(obj.Property2, reader.ReadInt16()); reader.Seek(0x20, SeekOrigin.Begin); Assert.AreEqual(obj.Property3, reader.ReadInt32()); reader.Seek(0x30, SeekOrigin.Begin); Assert.AreEqual(obj.Property4, reader.ReadInt64()); reader.Seek(0x40, SeekOrigin.Begin); Assert.AreEqual(obj.Property5, reader.ReadByte()); reader.Seek(0x50, SeekOrigin.Begin); Assert.AreEqual(obj.Property6, reader.ReadUInt16()); reader.Seek(0x60, SeekOrigin.Begin); Assert.AreEqual(obj.Property7, reader.ReadUInt32()); reader.Seek(0x70, SeekOrigin.Begin); Assert.AreEqual(obj.Property8, reader.ReadUInt64()); reader.Seek(0x80, SeekOrigin.Begin); Assert.AreEqual(obj.Property9, reader.ReadSingle()); reader.Seek(0x90, SeekOrigin.Begin); Assert.AreEqual(obj.Property10, reader.ReadDouble()); reader.Seek(0xA0, SeekOrigin.Begin); Assert.AreEqual(obj.Property11, reader.ReadGuid()); } }
public void ByteOrder02(ByteOrder order) { var rng = new Random(); var obj = new DataClass04 { Property1 = (sbyte)rng.Next(sbyte.MinValue, sbyte.MaxValue), Property2 = (short)rng.Next(short.MinValue, short.MaxValue), Property3 = (int)rng.Next(int.MinValue, int.MaxValue), Property4 = (long)rng.Next(int.MinValue, int.MaxValue), Property5 = (byte)rng.Next(byte.MinValue, byte.MaxValue), Property6 = (ushort)rng.Next(ushort.MinValue, ushort.MaxValue), Property7 = unchecked ((uint)rng.Next(int.MinValue, int.MaxValue)), Property8 = (ulong)unchecked ((uint)rng.Next(int.MinValue, int.MaxValue)), Property9 = (float)rng.NextDouble(), Property10 = (double)rng.NextDouble(), }; using (var stream = new MemoryStream(new byte[500])) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); //the highest offset should always be read last //so if no size is specified the position should end //up at the highest offset + the size of the property Assert.AreEqual(0x91, stream.Position); reader.Seek(0x70, SeekOrigin.Begin); Assert.AreEqual(obj.Property1, reader.ReadSByte()); reader.Seek(0x40, SeekOrigin.Begin); Assert.AreEqual(obj.Property2, reader.ReadInt16()); reader.Seek(0x30, SeekOrigin.Begin); Assert.AreEqual(obj.Property3, reader.ReadInt32()); reader.Seek(0x10, SeekOrigin.Begin); Assert.AreEqual(obj.Property4, reader.ReadInt64(ByteOrder.LittleEndian)); reader.Seek(0x90, SeekOrigin.Begin); Assert.AreEqual(obj.Property5, reader.ReadByte()); reader.Seek(0x60, SeekOrigin.Begin); Assert.AreEqual(obj.Property6, reader.ReadUInt16()); reader.Seek(0x00, SeekOrigin.Begin); Assert.AreEqual(obj.Property7, reader.ReadUInt32()); reader.Seek(0x80, SeekOrigin.Begin); Assert.AreEqual(obj.Property8, reader.ReadUInt64(ByteOrder.BigEndian)); reader.Seek(0x20, SeekOrigin.Begin); Assert.AreEqual(obj.Property9, reader.ReadSingle()); reader.Seek(0x50, SeekOrigin.Begin); Assert.AreEqual(obj.Property10, reader.ReadDouble()); } }
public override void WriteValue(EndianWriter writer) { writer.Seek(ValueAddress, SeekOrigin.Begin); writer.WriteObject(Value1); writer.WriteObject(Value2); if (FieldDefinition.Components > 2) { writer.WriteObject(Value3); } if (FieldDefinition.Components > 3) { writer.WriteObject(Value4); } IsDirty = false; }
void IWriteable.Write(EndianWriter writer, double?version) { var origin = writer.BaseStream.Position; for (int i = 0; i < definitions.Count; i++) { writer.Seek(origin + offset + i * size, SeekOrigin.Begin); writer.WriteObject(definitions[i]); } }
public void DataLength01(ByteOrder order) { var rng = new Random(); using (var stream = new MemoryStream(new byte[500])) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { var obj = new DataClass14 { Property1 = 5, Property2 = 100 }; writer.WriteObject(obj); Assert.AreEqual(100, stream.Position); stream.Position = 0; Assert.AreEqual(5, reader.ReadInt32()); Assert.AreEqual(100, reader.ReadInt32()); stream.Position = 0; obj = new DataClass14 { Property1 = 7, Property2 = 45 }; writer.WriteObject(obj); Assert.AreEqual(45, stream.Position); stream.Position = 0; Assert.AreEqual(7, reader.ReadInt32()); Assert.AreEqual(45, reader.ReadInt32()); } }
public void StoreType01(ByteOrder order) { var rng = new Random(); var obj = new DataClass11 { Property1 = rng.Next(short.MinValue, short.MaxValue), Property2 = rng.Next(byte.MinValue, byte.MaxValue), Property3 = rng.NextDouble() }; using (var stream = new MemoryStream()) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); stream.Position = 0; Assert.AreEqual((short)obj.Property1, reader.ReadInt16()); Assert.AreEqual((byte)obj.Property2, reader.ReadByte()); Assert.AreEqual((float)obj.Property3, reader.ReadSingle()); } }
public void WriteStrings01(ByteOrder order) { var obj = new DataClass05(); obj.Property1 = obj.Property6 = obj.Property7 = "Length_Prefixed_String_#01!"; obj.Property2 = obj.Property3 = "Fixed_Length_String_#01!"; obj.Property4 = obj.Property5 = "Null_Terminated_String_#01!"; using (var stream = new MemoryStream(new byte[500])) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); reader.Seek(0x00, SeekOrigin.Begin); Assert.AreEqual(obj.Property1, reader.ReadString()); reader.Seek(0x20, SeekOrigin.Begin); Assert.AreEqual(obj.Property2, reader.ReadString(32, true)); reader.Seek(0x40, SeekOrigin.Begin); Assert.AreEqual(obj.Property3, reader.ReadString(32, false).TrimEnd('*')); reader.Seek(0x60, SeekOrigin.Begin); Assert.AreEqual(obj.Property4, reader.ReadNullTerminatedString()); reader.Seek(0x80, SeekOrigin.Begin); Assert.AreEqual(obj.Property5, reader.ReadString(64, false).TrimEnd('\0')); reader.Seek(0xC0, SeekOrigin.Begin); Assert.AreEqual(obj.Property6, reader.ReadString(ByteOrder.LittleEndian)); reader.Seek(0xE0, SeekOrigin.Begin); Assert.AreEqual(obj.Property7, reader.ReadString(ByteOrder.BigEndian)); } }
public void Enums01(ByteOrder order) { var rng = new Random(); var obj = new DataClass12 { Property1 = (Enum01)rng.Next(1, 4), Property2 = (Enum02)rng.Next(4, 7), Property3 = (Enum03)rng.Next(7, 10), Property4 = (Enum04)rng.Next(10, 13), }; using (var stream = new MemoryStream()) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj); stream.Position = 0; Assert.AreEqual((byte)obj.Property1, reader.ReadByte()); Assert.AreEqual((short)obj.Property2, reader.ReadInt16()); Assert.AreEqual((int)obj.Property3, reader.ReadInt32()); Assert.AreEqual((long)obj.Property4, reader.ReadInt64()); } }
public void Versions02(ByteOrder order) { var rng = new Random(); var obj = new DataClass10 { Version = 0, Property1 = rng.Next(int.MinValue, int.MaxValue), Property2 = (float)rng.NextDouble(), Property3 = (float)rng.NextDouble(), Property4 = rng.NextDouble() }; using (var stream = new MemoryStream()) using (var reader = new EndianReader(stream, order)) using (var writer = new EndianWriter(stream, order)) { writer.WriteObject(obj, 1); stream.Position = 0; Assert.AreEqual(obj.Property1, reader.ReadInt32()); Assert.AreEqual(1, reader.ReadInt32()); Assert.AreEqual(obj.Property2, reader.ReadSingle()); Assert.IsTrue(reader.ReadBytes(64).All(b => b == 0)); stream.Position = 0; writer.Write(new byte[64]); //set to zeros stream.Position = 0; writer.WriteObject(obj, 2); stream.Position = 0; Assert.AreEqual(obj.Property1, reader.ReadInt32()); Assert.AreEqual(2, reader.ReadInt32()); Assert.AreEqual(0, reader.ReadInt32()); Assert.AreEqual(obj.Property2, reader.ReadSingle()); Assert.AreEqual(obj.Property3, reader.ReadSingle()); Assert.IsTrue(reader.ReadBytes(64).All(b => b == 0)); stream.Position = 0; writer.Write(new byte[64]); //set to zeros stream.Position = 0; writer.WriteObject(obj, 3); stream.Position = 0; Assert.AreEqual(obj.Property1, reader.ReadInt32()); Assert.AreEqual(3, reader.ReadInt32()); Assert.AreEqual(0, reader.ReadInt32()); Assert.AreEqual(obj.Property2, reader.ReadSingle()); Assert.AreEqual(obj.Property3, reader.ReadSingle()); Assert.IsTrue(reader.ReadBytes(64).All(b => b == 0)); stream.Position = 0; writer.Write(new byte[64]); //set to zeros stream.Position = 0; writer.WriteObject(obj, 4); stream.Position = 0; Assert.AreEqual(obj.Property1, reader.ReadInt32()); Assert.AreEqual(4, reader.ReadInt32()); Assert.AreEqual(0, reader.ReadInt32()); Assert.AreEqual(obj.Property2, reader.ReadSingle()); Assert.AreEqual(0, reader.ReadInt32()); Assert.AreEqual(obj.Property4, reader.ReadDouble()); } }