-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTDynArray.cs
92 lines (84 loc) · 2.53 KB
/
TDynArray.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WolvenKit.W3Strings;
namespace WolvenKit.Bundles
{
class TDynArray<T> : List<T>, ISerializable where T : ISerializable, new()
{
public void Deserialize(BinaryReader reader)
{
this.Clear();
Int32 count = reader.ReadVLQInt32();
if (count == 0)
return;
for (int i = 0; i < count; i++)
{
var item = new T();
item.Deserialize(reader);
this.Add(item);
}
Console.WriteLine(new T().GetType().Name + " - Reader is at: " + reader.BaseStream.Position + "[0x"+ reader.BaseStream.Position.ToString("X") + "] left: " + ((int)reader.BaseStream.Length-reader.BaseStream.Position) + "[0x" + ((int)reader.BaseStream.Length-reader.BaseStream.Position).ToString("X") + "]");
}
public void Serialize(BinaryWriter writer)
{
writer.WriteVLQInt32(this.Count);
if (this.Count == 0)
return;
foreach(var item in this)
{
item.Serialize(writer);
}
}
}
public static class brext
{
public static int ReadVLQInt32(this BinaryReader br)
{
var b1 = br.ReadByte();
var sign = (b1 & 128) == 128;
var next = (b1 & 64) == 64;
var size = b1 % 128 % 64;
var offset = 6;
while (next)
{
var b = br.ReadByte();
size = (b % 128) << offset | size;
next = (b & 128) == 128;
offset += 7;
}
return sign ? size * -1 : size;
}
public static void WriteVLQInt32(this BinaryWriter bw, int value)
{
bool negative = value < 0;
value = Math.Abs(value);
byte b = (byte)(value & 0x3F);
value >>= 6;
if (negative)
{
b |= 0x80;
}
bool cont = value != 0;
if (cont)
{
b |= 0x40;
}
bw.Write(b);
while (cont)
{
b = (byte)(value & 0x7F);
value >>= 7;
cont = value != 0;
if (cont)
{
b |= 0x80;
}
bw.Write(b);
}
}
}
}