Terraria ModLoader  0.11.7.5
A mod to make and play Terraria mods
BinaryIO.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 
4 namespace Terraria.ModLoader.IO
5 {
6  public static class BinaryIO
7  {
8  public static void WriteItem(this BinaryWriter writer, Item item, bool readStack = false, bool readFavorite = false) =>
9  ItemIO.Send(item, writer, readStack, readFavorite);
10 
11  public static Item ReadItem(this BinaryReader reader, bool readStack = false, bool readFavorite = false) =>
12  ItemIO.Receive(reader, readStack, readFavorite);
13 
14  //copied from BinaryWriter.Read7BitEncodedInt
15  public static void WriteVarInt(this BinaryWriter writer, int value) {
16  // Write out an int 7 bits at a time. The high bit of the byte,
17  // when on, tells reader to continue reading more bytes.
18  uint v = (uint)value; // support negative numbers
19  while (v >= 0x80) {
20  writer.Write((byte)(v | 0x80));
21  v >>= 7;
22  }
23  writer.Write((byte)v);
24  }
25 
26  //copied from BinaryReader.Read7BitEncodedInt
27  public static int ReadVarInt(this BinaryReader reader) {
28  // Read out an Int32 7 bits at a time. The high bit
29  // of the byte when on means to continue reading more bytes.
30  int count = 0;
31  int shift = 0;
32  byte b;
33  do {
34  // Check for a corrupted stream. Read a max of 5 bytes.
35  if (shift == 5 * 7) // 5 bytes max per Int32, shift += 7
36  throw new FormatException("variable length int with more than 32 bits");
37 
38  // ReadByte handles end of stream cases for us.
39  b = reader.ReadByte();
40  count |= (b & 0x7F) << shift;
41  shift += 7;
42  } while ((b & 0x80) != 0);
43  return count;
44  }
45 
46  public static void SafeWrite(this BinaryWriter writer, Action<BinaryWriter> write) {
47  var ms = new MemoryStream();//memory thrash should be fine here
48  write(new BinaryWriter(ms));
49  writer.WriteVarInt((int)ms.Length);
50  ms.Position = 0;
51  ms.CopyTo(writer.BaseStream);
52  }
53 
54  public static void SafeRead(this BinaryReader reader, Action<BinaryReader> read) {
55  int length = reader.ReadVarInt();
56  var ms = new MemoryStream(reader.ReadBytes(length));
57  read(new BinaryReader(ms));
58  if (ms.Position != length)
59  throw new IOException("Read underflow " + ms.Position + " of " + length + " bytes");
60  }
61 
62  public static void ReadBytes(this Stream stream, byte[] buf) {
63  int r, pos = 0;
64  while ((r = stream.Read(buf, pos, buf.Length - pos)) > 0)
65  pos += r;
66 
67  if (pos != buf.Length)
68  throw new IOException($"Stream did not contain enough bytes ({pos}) < ({buf.Length})");
69  }
70 
71  public static byte[] ReadBytes(this Stream stream, int len) => ReadBytes(stream, (long)len);
72 
73  public static byte[] ReadBytes(this Stream stream, long len) {
74  var buf = new byte[len];
75  stream.ReadBytes(buf);
76  return buf;
77  }
78  }
79 }
static byte[] ReadBytes(this Stream stream, long len)
Definition: BinaryIO.cs:73
static void SafeRead(this BinaryReader reader, Action< BinaryReader > read)
Definition: BinaryIO.cs:54
static int ReadVarInt(this BinaryReader reader)
Definition: BinaryIO.cs:27
static void WriteVarInt(this BinaryWriter writer, int value)
Definition: BinaryIO.cs:15
static void SafeWrite(this BinaryWriter writer, Action< BinaryWriter > write)
Definition: BinaryIO.cs:46
static void Receive(Item item, BinaryReader reader, bool readStack=false, bool readFavorite=false)
Definition: ItemIO.cs:151
static void Send(Item item, BinaryWriter writer, bool writeStack=false, bool writeFavourite=false)
Definition: ItemIO.cs:143
static void ReadBytes(this Stream stream, byte[] buf)
Definition: BinaryIO.cs:62