-
Notifications
You must be signed in to change notification settings - Fork 0
/
RawDataBuilder.cs
130 lines (112 loc) · 3.25 KB
/
RawDataBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
using Terraria;
namespace PowerfulSign
{
class RawDataBuilder
{
public RawDataBuilder()
{
this.memoryStream = new MemoryStream();
this.writer = new BinaryWriter(this.memoryStream);
this.writer.BaseStream.Position = 3L;
}
public RawDataBuilder(PacketTypes type)
{
this.memoryStream = new MemoryStream();
this.writer = new BinaryWriter(this.memoryStream);
this.writer.BaseStream.Position = 3L;
SetType(type);
}
public RawDataBuilder SetType(PacketTypes type)
{
this.type = (int)type;
long position = this.writer.BaseStream.Position;
this.writer.BaseStream.Position = 2L;
this.writer.Write((short)type);
this.writer.BaseStream.Position = position;
return this;
}
public RawDataBuilder PackSByte(sbyte num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackByte(byte num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackInt16(short num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackUInt16(ushort num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackInt32(int num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackUInt32(uint num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackUInt64(ulong num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackSingle(float num)
{
this.writer.Write(num);
return this;
}
public RawDataBuilder PackString(string str)
{
this.writer.Write(str);
return this;
}
public RawDataBuilder PackRGB(Color? color)
{
this.writer.WriteRGB((Color)color);
return this;
}
public RawDataBuilder PackVector2(Vector2 v)
{
this.writer.Write(v.X);
this.writer.Write(v.Y);
return this;
}
private void UpdateLength()
{
long position = this.writer.BaseStream.Position;
this.writer.BaseStream.Position = 0L;
this.writer.Write((short)position);
this.writer.BaseStream.Position = position;
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder stringBuilder = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
{
stringBuilder.AppendFormat("{0:x2}", b);
}
return stringBuilder.ToString();
}
public byte[] GetByteData()
{
this.UpdateLength();
return this.memoryStream.ToArray();
}
int type = -1;
public MemoryStream memoryStream;
public BinaryWriter writer;
}
}