Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.IO.BinaryIO Class Reference

Static Public Member Functions

static void ReadBytes (this Stream stream, byte[] buf)
 
static byte[] ReadBytes (this Stream stream, int len)
 
static Item ReadItem (this BinaryReader reader, bool readStack=false, bool readFavorite=false)
 
static int ReadVarInt (this BinaryReader reader)
 
static void SafeRead (this BinaryReader reader, Action< BinaryReader > read)
 
static void SafeWrite (this BinaryWriter writer, Action< BinaryWriter > write)
 
static void WriteItem (this BinaryWriter writer, Item item, bool readStack=false, bool readFavorite=false)
 
static void WriteVarInt (this BinaryWriter writer, int value)
 

Detailed Description

Definition at line 6 of file BinaryIO.cs.

Member Function Documentation

static void Terraria.ModLoader.IO.BinaryIO.ReadBytes ( this Stream  stream,
byte[]  buf 
)
static

Definition at line 62 of file BinaryIO.cs.

62  {
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  }
static byte [] Terraria.ModLoader.IO.BinaryIO.ReadBytes ( this Stream  stream,
int  len 
)
static

Definition at line 71 of file BinaryIO.cs.

71  {
72  var buf = new byte[len];
73  stream.ReadBytes(buf);
74  return buf;
75  }
static Item Terraria.ModLoader.IO.BinaryIO.ReadItem ( this BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static
static int Terraria.ModLoader.IO.BinaryIO.ReadVarInt ( this BinaryReader  reader)
static

Definition at line 27 of file BinaryIO.cs.

27  {
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  }
static void Terraria.ModLoader.IO.BinaryIO.SafeRead ( this BinaryReader  reader,
Action< BinaryReader read 
)
static

Definition at line 54 of file BinaryIO.cs.

54  {
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  }
static void Terraria.ModLoader.IO.BinaryIO.SafeWrite ( this BinaryWriter  writer,
Action< BinaryWriter write 
)
static

Definition at line 46 of file BinaryIO.cs.

46  {
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  }
static void Terraria.ModLoader.IO.BinaryIO.WriteItem ( this BinaryWriter  writer,
Item  item,
bool  readStack = false,
bool  readFavorite = false 
)
static
static void Terraria.ModLoader.IO.BinaryIO.WriteVarInt ( this BinaryWriter  writer,
int  value 
)
static

Definition at line 15 of file BinaryIO.cs.

15  {
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  }