forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive.cs
217 lines (197 loc) · 7.18 KB
/
Archive.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using UnityDataTools.FileSystem;
namespace UnityDataTools.UnityDataTool;
public static class Archive
{
private static readonly byte[] WebBundlePrefix = Encoding.UTF8.GetBytes("UnityWebData1.0\0");
public static int HandleExtract(FileInfo filename, DirectoryInfo outputFolder)
{
try
{
if (IsWebBundle(filename))
{
ExtractWebBundle(filename, outputFolder);
}
else
{
ExtractAssetBundle(filename, outputFolder);
}
}
catch (Exception err) when (
err is NotSupportedException
|| err is FileFormatException)
{
Console.Error.WriteLine("Error opening archive");
Console.Error.WriteLine(err.Message);
return 1;
}
return 0;
}
public static int HandleList(FileInfo filename)
{
try
{
if (IsWebBundle(filename))
{
ListWebBundle(filename);
}
else
{
ListAssetBundle(filename);
}
}
catch (Exception err) when (
err is NotSupportedException
|| err is FileFormatException)
{
Console.Error.WriteLine("Error opening archive");
Console.Error.WriteLine(err.Message);
return 1;
}
return 0;
}
public static bool IsWebBundle(FileInfo filename)
{
var path = filename.ToString();
return (
path.EndsWith(".data")
|| path.EndsWith(".data.gz")
|| path.EndsWith(".data.br")
);
}
struct WebBundleFileDescription
{
public uint ByteOffset;
public uint Size;
public string Path;
}
static void ExtractWebBundle(FileInfo filename, DirectoryInfo outputFolder) {
Console.WriteLine($"Extracting web bundle: {filename}");
using var fileStream = File.Open(filename.ToString(), FileMode.Open);
using var stream = GetStream(filename, fileStream);
using var reader = new BinaryReader(stream, Encoding.UTF8);
var fileDescriptions = ParseWebBundleHeader(reader);
foreach (var description in fileDescriptions)
{
ExtractFileFromWebBundle(description, reader, outputFolder);
}
}
static Stream GetStream(FileInfo filename, FileStream fileStream) {
var fileExtension = Path.GetExtension(filename.ToString());
return fileExtension switch
{
".data" => fileStream,
".gz" => new GZipStream(fileStream, CompressionMode.Decompress),
".br" => new BrotliStream(fileStream, CompressionMode.Decompress),
_ => throw new FileFormatException("Incorrect file extension for web bundle"),
};
}
static List<WebBundleFileDescription> ParseWebBundleHeader(BinaryReader reader)
{
var result = new List<WebBundleFileDescription>();
var prefix = ReadBytes(reader, WebBundlePrefix.Length);
if (!prefix.SequenceEqual(WebBundlePrefix)) {
throw new FileFormatException("File is not a valid web bundle.");
}
uint headerSize = ReadUInt32(reader);
// Advance offset past prefix string and header size uint.
var currentByteOffset = WebBundlePrefix.Length + sizeof(uint);
while (currentByteOffset < headerSize)
{
var fileByteOffset = ReadUInt32(reader);
var fileSize = ReadUInt32(reader);
var filePathLength = ReadUInt32(reader);
var filePath = Encoding.UTF8.GetString(ReadBytes(reader, (int) filePathLength));
result.Add(new WebBundleFileDescription() {
ByteOffset = fileByteOffset,
Size = fileSize,
Path = filePath,
});
// Advance byte offset, so we keep track of the position (to know when we're done reading the header).
currentByteOffset += 3 * sizeof(uint) + filePath.Length;
}
return result;
}
static void ExtractFileFromWebBundle(WebBundleFileDescription description, BinaryReader reader, DirectoryInfo outputFolder)
{
// This function assumes `reader` is at the start of the binary data representing the file contents.
Console.WriteLine($"... Extracting {description.Path}");
var path = Path.Combine(outputFolder.ToString(), description.Path);
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, ReadBytes(reader, (int) description.Size));
}
static uint ReadUInt32(BinaryReader reader)
{
try {
return reader.ReadUInt32();
}
catch (EndOfStreamException)
{
throw new FileFormatException("File data is corrupt.");
}
}
static byte[] ReadBytes(BinaryReader reader, int count)
{
var result = reader.ReadBytes(count);
if (result.Length != count)
{
throw new FileFormatException("File data is corrupt.");
}
return result;
}
static void ExtractAssetBundle(FileInfo filename, DirectoryInfo outputFolder)
{
Console.WriteLine($"Extracting asset bundle: {filename}");
using var archive = UnityFileSystem.MountArchive(filename.FullName, "/");
foreach (var node in archive.Nodes)
{
Console.WriteLine($"... Extracting {node.Path}");
CopyFile("/" + node.Path, Path.Combine(outputFolder.FullName, node.Path));
}
}
static void ListAssetBundle(FileInfo filename)
{
using var archive = UnityFileSystem.MountArchive(filename.FullName, "/");
foreach (var node in archive.Nodes)
{
Console.WriteLine($"{node.Path}");
Console.WriteLine($" Size: {node.Size}");
Console.WriteLine($" Flags: {node.Flags}");
Console.WriteLine();
}
}
static void ListWebBundle(FileInfo filename)
{
using var fileStream = File.Open(filename.ToString(), FileMode.Open);
using var stream = GetStream(filename, fileStream);
using var reader = new BinaryReader(stream, Encoding.UTF8);
var fileDescriptions = ParseWebBundleHeader(reader);
foreach (var description in fileDescriptions)
{
Console.WriteLine($"{description.Path}");
Console.WriteLine($" Size: {description.Size}");
Console.WriteLine();
}
}
static void CopyFile(string source, string dest)
{
using var sourceFile = UnityFileSystem.OpenFile(source);
// Create the containing directory if it doesn't exist.
Directory.CreateDirectory(Path.GetDirectoryName(dest));
using var destFile = new FileStream(dest, FileMode.Create);
const int blockSize = 256 * 1024;
var buffer = new byte[blockSize];
long actualSize;
do
{
actualSize = sourceFile.Read(blockSize, buffer);
destFile.Write(buffer, 0, (int)actualSize);
}
while (actualSize == blockSize);
}
}