forked from WolvenKit/WolvenKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BundleItem.cs
180 lines (172 loc) · 6.96 KB
/
BundleItem.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
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using Doboz;
using Ionic.Zlib;
using LZ4;
using Snappy;
using WolvenKit.Common;
using WolvenKit.Common.Extensions;
using WolvenKit.Common.Model;
namespace WolvenKit.Bundles
{
public class BundleItem : IGameFile
{
public IGameArchive Archive { get; set; }
public string Name { get; set; }
public byte[] Hash { get; set; }
public uint Empty { get; set; }
public uint Size { get; set; }
public uint ZSize { get; set; }
/// <summary>
/// !!! Double check when writing !!! Some files use 64bit, older files may use 32bit.
/// </summary>
public long PageOffset { get; set; }
public ulong TimeStamp { get; set; }
public byte[] Zero { get; set; }
public uint CRC { get; set; }
public uint Compression { get; set; }
public string DateString { get; set; }
public string CompressionType
{
get
{
switch (Compression)
{
case 0:
return "None";
case 1:
return "Zlib";
case 2:
return "Snappy";
case 3:
return "Doboz";
case 4:
return "Lz4";
case 5:
return "Lz4";
default:
return "Unknown";
}
}
}
public void ExtractExistingMMF(Stream output)
{
// old deprecated way of loading mmfs still used in unit test
var hash = Archive.ArchiveAbsolutePath.GetHashMD5();
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(hash, MemoryMappedFileRights.Read))
//using (var viewstream = mmf.CreateViewStream(PageOffset, ZSize, MemoryMappedFileAccess.Read))
ExtractExistingMMF(output, mmf);
}
/// <summary>
/// Extract existing memory-mapped-file,
/// decompress with the proper algorithm.
/// </summary>
/// <param name="output"></param>
public void ExtractExistingMMF(Stream output, MemoryMappedFile memorymappedbundle)
{
/* var hash = *//*@"Global\" + *//*Archive.FileName.GetHashMD5();
System.Console.WriteLine(hash);
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(hash, MemoryMappedFileRights.Read, HandleInheritability.Inheritable))
*/
using (var viewstream = memorymappedbundle.CreateViewStream(PageOffset, ZSize, MemoryMappedFileAccess.Read))
{
switch (CompressionType)
{
case "None":
{
viewstream.CopyTo(output);
break;
}
case "Lz4":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = LZ4Codec.Decode(buffer, 0, c, (int)Size);
output.Write(uncompressed, 0, uncompressed.Length);
break;
}
case "Snappy":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = SnappyCodec.Uncompress(buffer);
output.Write(uncompressed, 0, uncompressed.Length);
break;
}
case "Doboz":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = DobozCodec.Decode(buffer, 0, c);
output.Write(uncompressed, 0, uncompressed.Length);
break;
}
case "Zlib":
{
var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);
zlib.CopyTo(output);
break;
}
default:
throw new MissingCompressionException("Unhandled compression algorithm.")
{
Compression = Compression
};
}
viewstream.Close();
}
}
public void Extract(Stream output)
{
using (var file = MemoryMappedFile.CreateFromFile(Archive.ArchiveAbsolutePath, FileMode.Open))
using (var viewstream = file.CreateViewStream(PageOffset, ZSize, MemoryMappedFileAccess.Read))
{
switch (CompressionType)
{
case "None":
{
viewstream.CopyTo(output);
break;
}
case "Lz4":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = LZ4Codec.Decode(buffer, 0, c, (int) Size);
output.Write(uncompressed, 0, uncompressed.Length);
break;
}
case "Snappy":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = SnappyCodec.Uncompress(buffer);
output.Write(uncompressed,0,uncompressed.Length);
break;
}
case "Doboz":
{
var buffer = new byte[ZSize];
var c = viewstream.Read(buffer, 0, buffer.Length);
var uncompressed = DobozCodec.Decode(buffer, 0, c);
output.Write(uncompressed, 0, uncompressed.Length);
break;
}
case "Zlib":
{
var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);
zlib.CopyTo(output);
break;
}
default:
throw new MissingCompressionException("Unhandled compression algorithm.")
{
Compression = Compression
};
}
viewstream.Close();
}
}
}
}