forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2Buffer.cs
More file actions
345 lines (296 loc) · 13.9 KB
/
Copy pathL2Buffer.cs
File metadata and controls
345 lines (296 loc) · 13.9 KB
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
using System;
namespace L2dotNET.Utility
{
/// <summary>
/// Provides some methods for buffers manipulation.
/// </summary>
public static class L2Buffer
{
/// <summary>
/// Copies array of <see cref="byte"/> values from source to destination buffer.
/// </summary>
/// <param name="source">Source buffer.</param>
/// <param name="srcOffset">Source buffer offset.</param>
/// <param name="destination">Destination buffer.</param>
/// <param name="destOffset">Destination buffer offset.</param>
/// <param name="size">Count of bytes to copy.</param>
/// <returns>Destination buffer.</returns>
public static unsafe byte[] Copy(byte[] source, int srcOffset, byte[] destination, int destOffset, int size)
{
fixed (byte* src = source, dst = destination)
Copy(src, srcOffset, dst, destOffset, size);
return destination;
}
/// <summary>
/// Copies array of <see cref="byte"/> values from source to destination buffer.
/// </summary>
/// <param name="src">Source buffer reference.</param>
/// <param name="srcOffset">Source buffer offset.</param>
/// <param name="dst">Destination buffer reference.</param>
/// <param name="dstOffset">Destination buffer offset.</param>
/// <param name="size">Amount of bytes to copy from source to destination buffer.</param>
public static unsafe void Copy(byte* src, int srcOffset, byte* dst, int dstOffset, int size)
{
int index = 0;
src += srcOffset;
dst += dstOffset;
while ((size - index) >= sizeof(long))
{
*(long*)(dst + index) = *(long*)(src + index);
index += sizeof(long);
}
while ((size - index) >= sizeof(int))
{
*(int*)(dst + index) = *(int*)(src + index);
index += sizeof(int);
}
while ((size - index) >= sizeof(short))
{
*(short*)(dst + index) = *(short*)(src + index);
index += sizeof(short);
}
while (index < size)
*(dst + index) = *(src + index++);
//srcOffset += size;
}
/// <summary>
/// Copies array of <see cref="byte"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="byte"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination <see cref="byte"/>s buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void Copy(byte* w, int size, byte* dst, ref int offset)
{
int index = 0;
dst += offset;
offset += size;
while ((size - index) >= sizeof(long))
{
*(long*)(dst + index) = *(long*)(w + index);
index += sizeof(long);
}
while ((size - index) >= sizeof(int))
{
*(int*)(dst + index) = *(int*)(w + index);
index += sizeof(int);
}
while ((size - index) >= sizeof(short))
{
*(short*)(dst + index) = *(short*)(w + index);
index += sizeof(short);
}
while (index < size)
*(dst + index) = *(w + index++);
}
/// <summary>
/// Copies array of <see cref="short"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="short"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination <see cref="byte"/>s buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void UnsafeCopy(short* w, int size, byte* dst, ref int offset)
{
Copy((byte*)w, size, dst, ref offset);
}
/// <summary>
/// Returns new array of <see cref="short"/> values, copied from the beginning of provided <paramref name="src"/> pointer.
/// </summary>
/// <param name="src">Source <see cref="short"/> values array pointer.</param>
/// <param name="length">Length of array to copy.</param>
/// <returns>New array of <see cref="short"/> values, copied from the beginning of provided <paramref name="src"/> pointer.</returns>
public static unsafe short[] SpecialCopy(short* src, int length)
{
short[] destiny = new short[length];
fixed (short* dst = destiny)
{
int index = 0;
while ((length - index) >= (sizeof(long) / sizeof(short)))
{
*(long*)(dst + index) = *(long*)(src + index);
index += sizeof(long) / sizeof(short);
}
while ((length - index) >= (sizeof(int) / sizeof(short)))
{
*(int*)(dst + index) = *(int*)(src + index);
index += sizeof(int) / sizeof(short);
}
while ((length - index) > 0)
*(dst + index) = *(src + index++);
}
return destiny;
}
/// <summary>
/// Copies array of <see cref="int"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="int"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination <see cref="byte"/>s buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void UnsafeCopy(int* w, int size, byte* dst, ref int offset)
{
Copy((byte*)w, size, dst, ref offset);
}
/// <summary>
/// Copies array of <see cref="double"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="double"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void UnsafeCopy(double* w, int size, byte* dst, ref int offset)
{
Copy((byte*)w, size, dst, ref offset);
}
/// <summary>
/// Copies array of <see cref="long"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="long"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void UnsafeCopy(long* w, int size, byte* dst, ref int offset)
{
Copy((byte*)w, size, dst, ref offset);
}
/// <summary>
/// Copies array of <see cref="char"/> values to destination buffer.
/// </summary>
/// <param name="w">Reference to <see cref="char"/> values array.</param>
/// <param name="size">Amount of bytes to write into destination buffer.</param>
/// <param name="dst">Destination buffer reference.</param>
/// <param name="offset">Destination buffer offset.</param>
public static unsafe void UnsafeCopy(char* w, int size, byte* dst, ref int offset)
{
Copy((byte*)w, size, dst, ref offset);
}
/// <summary>
/// Cuts some bytes from source buffer.
/// </summary>
/// <param name="source">Source buffer.</param>
/// <param name="startIndex">Source start size.</param>
/// <param name="size">Count of bytes to cut.</param>
/// <returns>Result buffer.</returns>
public static byte[] Cut(byte[] source, int startIndex, int size)
{
return Copy(source, startIndex, new byte[size], 0, size);
}
/// <summary>
/// Replaces part of buffer with replacement buffer.
/// </summary>
/// <param name="buffer">Destination buffer.</param>
/// <param name="index">Destination replacement size.</param>
/// <param name="replacement">Replacement buffer.</param>
/// <param name="size">Replacement bytes count.</param>
/// <returns>Destination buffer.</returns>
public static byte[] Replace(byte[] buffer, int index, byte[] replacement, int size)
{
return Copy(replacement, 0, buffer, index, size);
}
/// <summary>
/// Extends buffer.
/// </summary>
/// <param name="source">Source buffer.</param>
/// <param name="sourceIndex">Source buffer start-copy size.</param>
/// <param name="neededLength">Result buffer capacity.</param>
/// <returns>Extended buffer.</returns>
public static byte[] Extend(ref byte[] source, int sourceIndex, int neededLength)
{
return source = Copy(source, 0, new byte[neededLength], sourceIndex, neededLength);
}
/// <summary>
/// Extends buffer.
/// </summary>
/// <param name="source">Source buffer.</param>
/// <param name="additionalLength">Additional capacity.</param>
/// <returns>Extended buffer.</returns>
public static byte[] Extend(ref byte[] source, int additionalLength)
{
return source = Copy(source, 0, new byte[source.Length + additionalLength], 0, source.Length);
}
/// <summary>
/// Gives string representation of <see cref="byte"/> values buffer.
/// </summary>
/// <param name="buffer"><see cref="byte"/> values array.</param>
/// <returns>String representation of <see cref="byte"/> values array.</returns>
public static string ToString(byte[] buffer)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("Buffer dump, length: {0}{1}Index |---------------------------------------------| |--------------|{1}", buffer.Length, Environment.NewLine);
int index = 0;
while (index < buffer.Length)
{
string hex = string.Empty,
data = string.Empty;
int i;
for (i = 0; (i < 16) && ((index + i) < buffer.Length); i++)
{
hex += $"{buffer[index + i]:X2} ";
if ((buffer[i + index] > 31) && (buffer[i + index] < 127))
data += (char)buffer[i + index];
else
data += ".";
}
while (i < 16)
{
hex += " ";
i++;
}
sb.Append($"{index.ToString("X5")} {hex} {data}{Environment.NewLine}");
index += 16;
}
sb.Append(" |---------------------------------------------| |--------------|");
return sb.ToString();
}
/// <summary>
/// Converts <see cref="byte"/> values from one array to <see cref="char"/> values array (<see cref="string"/>).
/// </summary>
/// <param name="src"><see cref="byte"/> values array reference.</param>
/// <param name="srcOffset"><see cref="byte"/> values array offset.</param>
/// <param name="dst">Destination string.</param>
/// <param name="bytesCount">Amount of <see cref="byte"/> values to convert to <see cref="char"/> values.</param>
public static unsafe void GetTrimmedString(byte* src, int srcOffset, ref string dst, int bytesCount)
{
bytesCount = srcOffset + bytesCount;
while ((srcOffset < bytesCount) && (src[srcOffset] != 0))
dst += (char)src[srcOffset++];
}
/// <summary>
/// Converts <see cref="byte"/> values array to <see cref="char"/> values array (<see cref="string"/>).
/// </summary>
/// <param name="src">Source <see cref="byte"/> values buffer.</param>
/// <param name="srcOffset">Source buffer offset.</param>
/// <param name="maxLength">Max source buffer position, that method can reach.</param>
public static unsafe string GetTrimmedString(byte* src, ref int srcOffset, int maxLength)
{
string dst = string.Empty;
while ((src[srcOffset] != 0) && ((srcOffset + sizeof(char)) < maxLength))
{
dst += (char)src[srcOffset];
srcOffset += sizeof(char);
}
srcOffset += sizeof(char);
return dst;
}
/// <summary>
/// Copies array of generic values from one array to an other.
/// </summary>
/// <typeparam name="TU">Some generic type.</typeparam>
/// <param name="source">Source array.</param>
/// <param name="srcOffset">Source array offset.</param>
/// <param name="destination">Destination array.</param>
/// <param name="dstOffset">Destination array offset.</param>
/// <param name="length">Values to copy count.</param>
/// <returns>Copied array of generic values.</returns>
public static TU[] Copy<TU>(TU[] source, long srcOffset, TU[] destination, long dstOffset, long length)
{
if ((length > (source.Length - srcOffset)) || (length > (destination.Length - dstOffset)))
throw new InvalidOperationException();
length += srcOffset;
while (srcOffset < length)
destination[dstOffset++] = source[srcOffset++];
return destination;
}
}
}