-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathBundleHelper.cs
More file actions
202 lines (178 loc) · 6.83 KB
/
BundleHelper.cs
File metadata and controls
202 lines (178 loc) · 6.83 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
using System;
using System.Collections.Generic;
using System.IO;
namespace AssetsTools.NET.Extra
{
public static class BundleHelper
{
public static byte[] LoadAssetDataFromBundle(AssetBundleFile bundle, int index)
{
bundle.GetFileRange(index, out long offset, out long length);
AssetsFileReader reader = bundle.DataReader;
reader.Position = offset;
return reader.ReadBytes((int)length);
}
public static byte[] LoadAssetDataFromBundle(AssetBundleFile bundle, string name)
{
int index = bundle.GetFileIndex(name);
if (index < 0)
return null;
return LoadAssetDataFromBundle(bundle, index);
}
public static AssetsFile LoadAssetFromBundle(AssetBundleFile bundle, int index)
{
bundle.GetFileRange(index, out long offset, out long length);
Stream stream = new SegmentStream(bundle.DataReader.BaseStream, offset, length);
AssetsFileReader reader = new AssetsFileReader(stream);
AssetsFile file = new AssetsFile();
file.Read(reader);
return file;
}
public static AssetsFile LoadAssetFromBundle(AssetBundleFile bundle, string name)
{
int index = bundle.GetFileIndex(name);
if (index < 0)
return null;
return LoadAssetFromBundle(bundle, index);
}
public static List<byte[]> LoadAllAssetsDataFromBundle(AssetBundleFile bundle)
{
List<byte[]> files = new List<byte[]>();
int numFiles = bundle.BlockAndDirInfo.DirectoryInfos.Count;
for (int i = 0; i < numFiles; i++)
{
if (bundle.IsAssetsFile(i))
{
files.Add(LoadAssetDataFromBundle(bundle, i));
}
}
return files;
}
public static List<AssetsFile> LoadAllAssetsFromBundle(AssetBundleFile bundle)
{
List<AssetsFile> files = new List<AssetsFile>();
int numFiles = bundle.BlockAndDirInfo.DirectoryInfos.Count;
for (int i = 0; i < numFiles; i++)
{
if (bundle.IsAssetsFile(i))
{
files.Add(LoadAssetFromBundle(bundle, i));
}
}
return files;
}
public static AssetBundleFile UnpackBundle(AssetBundleFile file, bool freeOriginalStream = true)
{
MemoryStream ms = new MemoryStream();
file.Unpack(new AssetsFileWriter(ms));
ms.Position = 0;
AssetBundleFile newFile = new AssetBundleFile();
newFile.Read(new AssetsFileReader(ms));
if (freeOriginalStream)
{
file.Reader.Close();
file.DataReader.Close();
}
return newFile;
}
public static AssetBundleFile UnpackBundleToStream(AssetBundleFile file, Stream stream, bool freeOriginalStream = true)
{
file.Unpack(new AssetsFileWriter(stream));
stream.Position = 0;
AssetBundleFile newFile = new AssetBundleFile();
newFile.Read(new AssetsFileReader(stream));
if (freeOriginalStream)
{
file.Reader.Close();
file.DataReader.Close();
}
return newFile;
}
public static AssetBundleDirectoryInfo GetDirInfo(AssetBundleFile bundle, int index)
{
List<AssetBundleDirectoryInfo> dirInf = bundle.BlockAndDirInfo.DirectoryInfos;
return dirInf[index];
}
public static AssetBundleDirectoryInfo GetDirInfo(AssetBundleFile bundle, string name)
{
List<AssetBundleDirectoryInfo> dirInf = bundle.BlockAndDirInfo.DirectoryInfos;
for (int i = 0; i < dirInf.Count; i++)
{
AssetBundleDirectoryInfo info = dirInf[i];
if (info.Name == name)
{
return info;
}
}
return null;
}
/// <summary>
/// Calculates the CRC32 (IEEE) of the bundle data stream.
/// This is the same CRC verified by Unity when loading an AssetBundle. It is independent of the
/// compression method and depends only on the actual content; for compressed bundles,
/// the data is unpacked before the CRC is calculated.
/// </summary>
/// <param name="bundle">Loaded bundle to calculate CRC for.</param>
/// <returns>CRC32 value.</returns>
public static uint CalculateBundleCrc32(AssetBundleFile bundle)
{
AssetBundleFile crcBundle = bundle;
bool closeCrcBundle = false;
if (bundle.DataIsCompressed)
{
crcBundle = UnpackBundle(bundle, false);
closeCrcBundle = true;
}
try
{
long totalDataLen = 0;
AssetBundleBlockInfo[] blocks = crcBundle.BlockAndDirInfo.BlockInfos;
for (int i = 0; i < blocks.Length; i++)
{
totalDataLen += blocks[i].DecompressedSize;
}
AssetsFileReader dataReader = crcBundle.DataReader;
long oldPos = dataReader.Position;
try
{
dataReader.Position = 0;
uint crc = Crc32Helper.InitialValue;
long consumed = 0;
while (consumed < totalDataLen)
{
int toRead = (int)Math.Min(Crc32Helper.ChunkSize, totalDataLen - consumed);
byte[] chunk = dataReader.ReadBytes(toRead);
if (chunk.Length != toRead)
{
throw new EndOfStreamException(
$"Unexpected end of bundle data while calculating CRC at 0x{consumed:X}."
);
}
crc = Crc32Helper.Feed(crc, chunk);
consumed += toRead;
}
return Crc32Helper.Finalize(crc);
}
finally
{
dataReader.Position = oldPos;
}
}
finally
{
if (closeCrcBundle)
{
try
{
crcBundle.Close();
}
catch
{
// some streams may have been closed during unpack operations
// so we will ignore this
}
}
}
}
}
}