-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReader.cs
141 lines (124 loc) · 4.19 KB
/
Reader.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
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
namespace coc_messages_csharp
{
public class Reader : IDisposable
{
private int messageId;
public int MessageId
{
get
{
return this.messageId;
}
}
private int unknown;
public int Unknown
{
get
{
return this.unknown;
}
}
private BinaryReader binReader;
public Reader(int messageId, int unknown, byte[] initial_bytes)
{
this.messageId = messageId;
this.unknown = unknown;
MemoryStream memStream = new MemoryStream(initial_bytes);
this.binReader = new BinaryReader(memStream);
}
public Reader(byte[] initial_bytes)
{
MemoryStream memStream = new MemoryStream(initial_bytes);
this.binReader = new BinaryReader(memStream);
this.messageId = BitConverter.ToInt32(new byte[2].Concat(this.binReader.ReadBytes(2)).Reverse().ToArray(), 0);
this.binReader.BaseStream.Position += 3;
this.unknown = BitConverter.ToInt32(new byte[2].Concat(this.binReader.ReadBytes(2)).Reverse().ToArray(), 0);
}
public byte readByte()
{
return this.binReader.ReadByte();
}
public byte[] readBytes(int bytes)
{
return this.binReader.ReadBytes(bytes);
}
public int readInt()
{
return BitConverter.ToInt32(this.readBytes(4).Reverse().ToArray(), 0);
}
public int readInt(int bytes)
{
if (bytes > 4)
throw new System.ArgumentOutOfRangeException("Int cannot be larger than 4 bytes");
return BitConverter.ToInt32(new byte[4 - bytes].Concat(this.readBytes(bytes)).Reverse().ToArray(), 0);
}
public long readLong()
{
return BitConverter.ToInt64(this.readBytes(8).Reverse().ToArray(), 0);
}
public string readString()
{
int length = this.readInt();
if (length <= 0)
return null;
return Encoding.UTF8.GetString(this.readBytes(length));
}
public string readZString()
{
int length = this.readInt();
if (length <= 0)
return null;
int zlength = this.binReader.ReadInt32();
length -= 4;
this.skipBytes(2);
length -= 2;
using (MemoryStream compressedMemoryStream = new MemoryStream(this.readBytes(length)))
{
using (MemoryStream decompressedMemoryStream = new MemoryStream(zlength))
{
using (DeflateStream decompressionStream = new DeflateStream(compressedMemoryStream, CompressionMode.Decompress, true))
{
decompressionStream.CopyTo(decompressedMemoryStream);
}
return Encoding.UTF8.GetString(decompressedMemoryStream.GetBuffer());
}
}
}
public byte[] read()
{
return this.readBytes(this.availableBytes());
}
public int peekInt()
{
int peek = this.readInt();
this.binReader.BaseStream.Position -= 4;
return peek;
}
public int peekInt(int bytes)
{
if (bytes > 4)
throw new System.ArgumentOutOfRangeException("Int cannot be larger than 4 bytes");
int peek = BitConverter.ToInt32(new byte[4 - bytes].Concat(this.readBytes(bytes)).Reverse().ToArray(), 0);
this.binReader.BaseStream.Position -= bytes;
return peek;
}
public byte[] skipBytes(int bytes)
{
return this.readBytes(bytes);
}
public int availableBytes()
{
return (int)(this.binReader.BaseStream.Length - this.binReader.BaseStream.Position);
}
public void Dispose()
{
if (this.binReader != null)
this.binReader.Dispose();
}
}
}