tModLoader v0.11.8.9
A mod to make and play 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 byte[] ReadBytes (this Stream stream, long 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

◆ ReadBytes() [1/3]

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 }

◆ ReadBytes() [2/3]

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

◆ ReadBytes() [3/3]

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

Definition at line 73 of file BinaryIO.cs.

73 {
74 var buf = new byte[len];
75 stream.ReadBytes(buf);
76 return buf;
77 }

◆ ReadItem()

static Item Terraria.ModLoader.IO.BinaryIO.ReadItem ( this BinaryReader  reader,
bool  readStack = false,
bool  readFavorite = false 
)
static

◆ ReadVarInt()

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 }

◆ SafeRead()

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 }

◆ SafeWrite()

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 }

◆ WriteItem()

static void Terraria.ModLoader.IO.BinaryIO.WriteItem ( this BinaryWriter  writer,
Item  item,
bool  readStack = false,
bool  readFavorite = false 
)
static

◆ WriteVarInt()

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 }