-
Notifications
You must be signed in to change notification settings - Fork 172
/
MemoryStream.cs
375 lines (308 loc) · 7.37 KB
/
MemoryStream.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
namespace KBEngine
{
using UnityEngine;
using System;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
/*
二进制数据流模块
能够将一些基本类型序列化(writeXXX)成二进制流同时也提供了反序列化(readXXX)等操作
*/
public class MemoryStream : ObjectPool<MemoryStream>
{
public const int BUFFER_MAX = 1460 * 4;
public int rpos = 0;
public int wpos = 0;
private byte[] datas_ = new byte[BUFFER_MAX];
private static System.Text.ASCIIEncoding _converter = new System.Text.ASCIIEncoding();
[StructLayout(LayoutKind.Explicit, Size = 4)]
struct PackFloatXType
{
[FieldOffset(0)]
public float fv;
[FieldOffset(0)]
public UInt32 uv;
[FieldOffset(0)]
public Int32 iv;
}
/// <summary>
/// 把自己放回缓冲池
/// </summary>
public void reclaimObject()
{
clear();
reclaimObject(this);
}
public byte[] data()
{
return datas_;
}
public void setData(byte[] data)
{
datas_ = data;
}
//---------------------------------------------------------------------------------
public SByte readInt8()
{
return (SByte)datas_[rpos++];
}
public Int16 readInt16()
{
rpos += 2;
return BitConverter.ToInt16(datas_, rpos - 2);
}
public Int32 readInt32()
{
rpos += 4;
return BitConverter.ToInt32(datas_, rpos - 4);
}
public Int64 readInt64()
{
rpos += 8;
return BitConverter.ToInt64(datas_, rpos - 8);
}
public Byte readUint8()
{
return datas_[rpos++];
}
public UInt16 readUint16()
{
rpos += 2;
return BitConverter.ToUInt16(datas_, rpos - 2);
}
public UInt32 readUint32()
{
rpos += 4;
return BitConverter.ToUInt32(datas_, rpos - 4);
}
public UInt64 readUint64()
{
rpos += 8;
return BitConverter.ToUInt64(datas_, rpos - 8);
}
public float readFloat()
{
rpos += 4;
return BitConverter.ToSingle(datas_, rpos - 4);
}
public double readDouble()
{
rpos += 8;
return BitConverter.ToDouble(datas_, rpos - 8);
}
public string readString()
{
int offset = rpos;
while(datas_[rpos++] != 0)
{
}
return _converter.GetString(datas_, offset, rpos - offset - 1);
}
public byte[] readBlob()
{
UInt32 size = readUint32();
byte[] buf = new byte[size];
Array.Copy(datas_, rpos, buf, 0, size);
rpos += (int)size;
return buf;
}
public Vector2 readPackXZ()
{
PackFloatXType xPackData;
PackFloatXType zPackData;
xPackData.fv = 0f;
zPackData.fv = 0f;
xPackData.uv = 0x40000000;
zPackData.uv = 0x40000000;
Byte v1 = readUint8();
Byte v2 = readUint8();
Byte v3 = readUint8();
UInt32 data = 0;
data |= ((UInt32)v1 << 16);
data |= ((UInt32)v2 << 8);
data |= (UInt32)v3;
xPackData.uv |= (data & 0x7ff000) << 3;
zPackData.uv |= (data & 0x0007ff) << 15;
xPackData.fv -= 2.0f;
zPackData.fv -= 2.0f;
xPackData.uv |= (data & 0x800000) << 8;
zPackData.uv |= (data & 0x000800) << 20;
Vector2 vec = new Vector2(xPackData.fv, zPackData.fv);
return vec;
}
public float readPackY()
{
PackFloatXType yPackData;
yPackData.fv = 0f;
yPackData.uv = 0x40000000;
UInt16 data = readUint16();
yPackData.uv |= ((UInt32)data & 0x7fff) << 12;
yPackData.fv -= 2f;
yPackData.uv |= ((UInt32)data & 0x8000) << 16;
return yPackData.fv;
}
//---------------------------------------------------------------------------------
public void writeInt8(SByte v)
{
datas_[wpos++] = (Byte)v;
}
public void writeInt16(Int16 v)
{
writeInt8((SByte)(v & 0xff));
writeInt8((SByte)(v >> 8 & 0xff));
}
public void writeInt32(Int32 v)
{
for(int i=0; i<4; i++)
writeInt8((SByte)(v >> i * 8 & 0xff));
}
public void writeInt64(Int64 v)
{
byte[] getdata = BitConverter.GetBytes(v);
for(int i=0; i<getdata.Length; i++)
{
datas_[wpos++] = getdata[i];
}
}
public void writeUint8(Byte v)
{
datas_[wpos++] = v;
}
public void writeUint16(UInt16 v)
{
writeUint8((Byte)(v & 0xff));
writeUint8((Byte)(v >> 8 & 0xff));
}
public void writeUint32(UInt32 v)
{
for(int i=0; i<4; i++)
writeUint8((Byte)(v >> i * 8 & 0xff));
}
public void writeUint64(UInt64 v)
{
byte[] getdata = BitConverter.GetBytes(v);
for(int i=0; i<getdata.Length; i++)
{
datas_[wpos++] = getdata[i];
}
}
public void writeFloat(float v)
{
byte[] getdata = BitConverter.GetBytes(v);
for(int i=0; i<getdata.Length; i++)
{
datas_[wpos++] = getdata[i];
}
}
public void writeDouble(double v)
{
byte[] getdata = BitConverter.GetBytes(v);
for(int i=0; i<getdata.Length; i++)
{
datas_[wpos++] = getdata[i];
}
}
public void writeBlob(byte[] v)
{
UInt32 size = (UInt32)v.Length;
if(size + 4 > space())
{
Dbg.ERROR_MSG("memorystream::writeBlob: no free!");
return;
}
writeUint32(size);
for(UInt32 i=0; i<size; i++)
{
datas_[wpos++] = v[i];
}
}
public void writeString(string v)
{
if(v.Length > space())
{
Dbg.ERROR_MSG("memorystream::writeString: no free!");
return;
}
byte[] getdata = System.Text.Encoding.ASCII.GetBytes(v);
for(int i=0; i<getdata.Length; i++)
{
datas_[wpos++] = getdata[i];
}
datas_[wpos++] = 0;
}
//---------------------------------------------------------------------------------
public void append(byte[] datas, UInt32 offset, UInt32 size)
{
UInt32 free = space();
if (free < size) {
byte[] newdatas = new byte[datas_.Length + size * 2];
Array.Copy(datas_, 0, newdatas, 0, wpos);
datas_ = newdatas;
}
Array.Copy(datas, offset, datas_, wpos, size);
wpos += (int)size;
}
//---------------------------------------------------------------------------------
public void readSkip(UInt32 v)
{
rpos += (int)v;
}
//---------------------------------------------------------------------------------
public UInt32 space()
{
return (UInt32)(data().Length - wpos);
}
//---------------------------------------------------------------------------------
public UInt32 length()
{
return (UInt32)(wpos - rpos);
}
//---------------------------------------------------------------------------------
public bool readEOF()
{
return (BUFFER_MAX - rpos) <= 0;
}
//---------------------------------------------------------------------------------
public void done()
{
rpos = wpos;
}
//---------------------------------------------------------------------------------
public void clear()
{
rpos = wpos = 0;
if(datas_.Length > BUFFER_MAX)
datas_ = new byte[BUFFER_MAX];
}
//---------------------------------------------------------------------------------
public byte[] getbuffer()
{
byte[] buf = new byte[length()];
Array.Copy(data(), rpos, buf, 0, length());
return buf;
}
//---------------------------------------------------------------------------------
public string toString()
{
string s = "";
int ii = 0;
byte[] buf = getbuffer();
for(int i=0; i<buf.Length; i++)
{
ii += 1;
if(ii >= 200)
{
s = "";
ii = 0;
}
s += buf[i];
s += " ";
}
return s;
}
}
}