tModLoader v0.11.8.9
A mod to make and play Terraria mods
BigEndian.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Linq;
4
6{
8 {
9 public BigEndianWriter(Stream output) : base(output) { }
10
11 private void WriteBigEndian(byte[] bytes) {
12 if (BitConverter.IsLittleEndian)
13 bytes = bytes.Reverse().ToArray();
14
15 Write(bytes);
16 }
17
18 public override void Write(short value) { WriteBigEndian(BitConverter.GetBytes(value)); }
19 public override void Write(ushort value) { WriteBigEndian(BitConverter.GetBytes(value)); }
20 public override void Write(int value) { WriteBigEndian(BitConverter.GetBytes(value)); }
21 public override void Write(uint value) { WriteBigEndian(BitConverter.GetBytes(value)); }
22 public override void Write(long value) { WriteBigEndian(BitConverter.GetBytes(value)); }
23 public override void Write(ulong value) { WriteBigEndian(BitConverter.GetBytes(value)); }
24 public override void Write(float value) { WriteBigEndian(BitConverter.GetBytes(value)); }
25 public override void Write(double value) { WriteBigEndian(BitConverter.GetBytes(value)); }
26 }
27
29 {
30 public BigEndianReader(Stream input) : base(input) { }
31
32 private byte[] ReadBigEndian(int len) =>
33 BitConverter.IsLittleEndian ? ReadBytes(len).Reverse().ToArray() : ReadBytes(len);
34
35 public override short ReadInt16() => BitConverter.ToInt16(ReadBigEndian(2), 0);
36 public override ushort ReadUInt16() => BitConverter.ToUInt16(ReadBigEndian(2), 0);
37 public override int ReadInt32() => BitConverter.ToInt32(ReadBigEndian(4), 0);
38 public override uint ReadUInt32() => BitConverter.ToUInt32(ReadBigEndian(4), 0);
39 public override long ReadInt64() => BitConverter.ToInt64(ReadBigEndian(8), 0);
40 public override ulong ReadUInt64() => BitConverter.ToUInt64(ReadBigEndian(8), 0);
41 public override float ReadSingle() => BitConverter.ToSingle(ReadBigEndian(4), 0);
42 public override double ReadDouble() => BitConverter.ToDouble(ReadBigEndian(8), 0);
43 }
44}
override void Write(ulong value)
Definition: BigEndian.cs:23
override void Write(float value)
Definition: BigEndian.cs:24
override void Write(uint value)
Definition: BigEndian.cs:21
override void Write(double value)
Definition: BigEndian.cs:25
override void Write(short value)
Definition: BigEndian.cs:18
void WriteBigEndian(byte[] bytes)
Definition: BigEndian.cs:11
override void Write(long value)
Definition: BigEndian.cs:22
override void Write(int value)
Definition: BigEndian.cs:20
override void Write(ushort value)
Definition: BigEndian.cs:19