-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathAssetsFile.cs
More file actions
318 lines (270 loc) · 12.9 KB
/
AssetsFile.cs
File metadata and controls
318 lines (270 loc) · 12.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
using AssetsTools.NET.Extra;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace AssetsTools.NET
{
public class AssetsFile
{
public AssetsFileHeader header;
public TypeTree typeTree;
public PreloadList preloadTable;
public AssetsFileDependencyList dependencies;
public uint assetTablePos;
public uint assetCount;
public AssetsFileStatefulReader reader { get { return _reader.derive(); } }
public AssetsFileStatefulReader _reader;
public AssetsFile(AssetsFileStatefulReader reader)
{
this._reader = reader;
header = new AssetsFileHeader();
header.Read(reader);
typeTree = new TypeTree();
typeTree.Read(reader, header.format);
assetCount = reader.ReadUInt32();
reader.Align();
assetTablePos = (uint)reader.Position;
int assetInfoSize = AssetFileInfo.GetSize(header.format);
if (0x0F <= header.format && header.format <= 0x10)
{
//for these two versions, the asset info is not aligned
//for the last entry, so we have to do some weird stuff
reader.Position += ((assetInfoSize + 3) >> 2 << 2) * (assetCount - 1) + assetInfoSize;
}
else
{
reader.Position += AssetFileInfo.GetSize(header.format) * assetCount;
}
if (header.format > 0x0B)
{
preloadTable = new PreloadList();
preloadTable.Read(reader);
}
dependencies = new AssetsFileDependencyList();
dependencies.Read(reader);
}
public void Close()
{
_reader.impl.Dispose();
}
public void Write(AssetsFileWriter writer, long filePos, List<AssetsReplacer> replacers, uint fileID = 0, ClassDatabaseFile typeMeta = null)
{
if (filePos == -1)
filePos = writer.Position;
else
writer.Position = filePos;
header.Write(writer);
foreach (AssetsReplacer replacer in replacers)
{
int replacerClassId = replacer.GetClassID();
ushort replacerScriptIndex = replacer.GetMonoScriptID();
if (!typeTree.unity5Types.Any(t => t.classId == replacerClassId && t.scriptIndex == replacerScriptIndex))
{
Type_0D type = null;
if (typeMeta != null)
{
ClassDatabaseType cldbType = AssetHelper.FindAssetClassByID(typeMeta, (uint)replacerClassId);
if (cldbType != null)
{
type = C2T5.Cldb2TypeTree(typeMeta, cldbType);
//in original AssetsTools, if you tried to use a new monoId it would just try to use
//the highest existing scriptIndex that existed without making a new one (unless there
//were no monobehavours ofc) this isn't any better as we just assign a plain monobehaviour
//typetree to a type that probably has more fields. I don't really know of a better way to
//handle this at the moment as cldbs cannot differentiate monoids.
type.scriptIndex = replacerScriptIndex;
}
}
if (type == null)
{
type = new Type_0D
{
classId = replacerClassId,
unknown16_1 = 0,
scriptIndex = replacerScriptIndex,
typeHash1 = 0,
typeHash2 = 0,
typeHash3 = 0,
typeHash4 = 0,
typeFieldsExCount = 0,
stringTableLen = 0,
stringTable = ""
};
}
typeTree.unity5Types.Add(type);
}
}
typeTree.Write(writer, header.format);
Dictionary<long, AssetFileInfo> oldAssetInfosByPathId = new Dictionary<long, AssetFileInfo>();
Dictionary<long, AssetsReplacer> replacersByPathId = replacers.ToDictionary(r => r.GetPathID());
List<AssetFileInfo> newAssetInfos = new List<AssetFileInfo>();
// Collect unchanged assets (that aren't getting removed)
_reader.Position = assetTablePos;
for (int i = 0; i < assetCount; i++)
{
AssetFileInfo oldAssetInfo = new AssetFileInfo();
oldAssetInfo.Read(header.format, _reader);
oldAssetInfosByPathId.Add(oldAssetInfo.index, oldAssetInfo);
if (replacersByPathId.ContainsKey(oldAssetInfo.index))
continue;
AssetFileInfo newAssetInfo = new AssetFileInfo
{
index = oldAssetInfo.index,
curFileTypeOrIndex = oldAssetInfo.curFileTypeOrIndex,
inheritedUnityClass = oldAssetInfo.inheritedUnityClass,
scriptIndex = oldAssetInfo.scriptIndex,
unknown1 = oldAssetInfo.unknown1
};
newAssetInfos.Add(newAssetInfo);
}
// Collect modified and new assets
foreach (AssetsReplacer replacer in replacers.Where(r => r.GetReplacementType() == AssetsReplacementType.AddOrModify))
{
AssetFileInfo newAssetInfo = new AssetFileInfo
{
index = replacer.GetPathID(),
inheritedUnityClass = (ushort)replacer.GetClassID(), //for older unity versions
scriptIndex = replacer.GetMonoScriptID(),
unknown1 = 0
};
if (header.format < 0x10)
{
newAssetInfo.curFileTypeOrIndex = replacer.GetClassID();
}
else
{
if (replacer.GetMonoScriptID() == 0xFFFF)
newAssetInfo.curFileTypeOrIndex = typeTree.unity5Types.FindIndex(t => t.classId == replacer.GetClassID());
else
newAssetInfo.curFileTypeOrIndex = typeTree.unity5Types.FindIndex(t => t.classId == replacer.GetClassID() && t.scriptIndex == replacer.GetMonoScriptID());
}
newAssetInfos.Add(newAssetInfo);
}
newAssetInfos.Sort((i1, i2) => i1.index.CompareTo(i2.index));
// Write asset infos (will write again later on to update the offsets and sizes)
writer.Write(newAssetInfos.Count);
writer.Align();
long newAssetTablePos = writer.Position;
foreach (AssetFileInfo newAssetInfo in newAssetInfos)
{
newAssetInfo.Write(header.format, writer);
}
preloadTable.Write(writer);
dependencies.Write(writer);
// Temporary fix for secondaryTypeCount and friends
if (header.format >= 0x14)
{
writer.Write(0); //secondaryTypeCount
}
uint newMetadataSize = (uint)(writer.Position - filePos - 0x13); //0x13 is header - "endianness byte"? (if that's what it even is)
if (header.format >= 0x16)
{
// Remove larger variation fields as well
newMetadataSize -= 0x1c;
}
// For padding only. if all initial data before assetData is more than 0x1000, this is skipped
if (writer.Position < 0x1000)
{
while (writer.Position < 0x1000)
{
writer.Write((byte)0x00);
}
}
else
{
if (writer.Position % 16 == 0)
writer.Position += 16;
else
writer.Align16();
}
long newFirstFileOffset = writer.Position;
// Write all asset data
for (int i = 0; i < newAssetInfos.Count; i++)
{
AssetFileInfo newAssetInfo = newAssetInfos[i];
newAssetInfo.curFileOffset = writer.Position - newFirstFileOffset;
if (replacersByPathId.TryGetValue(newAssetInfo.index, out AssetsReplacer replacer))
{
replacer.Write(writer);
}
else
{
AssetFileInfo oldAssetInfo = oldAssetInfosByPathId[newAssetInfo.index];
_reader.Position = header.firstFileOffset + oldAssetInfo.curFileOffset;
_reader.CopyToCompat(writer.BaseStream, oldAssetInfo.curFileSize);
}
newAssetInfo.curFileSize = (uint)(writer.Position - (newFirstFileOffset + newAssetInfo.curFileOffset));
if (i != newAssetInfos.Count - 1)
writer.Align8();
}
long newFileSize = writer.Position - filePos;
// Write new header
AssetsFileHeader newHeader = new AssetsFileHeader
{
metadataSize = newMetadataSize,
fileSize = newFileSize,
format = header.format,
firstFileOffset = newFirstFileOffset,
endianness = header.endianness,
unknown = header.unknown,
unknown1 = header.unknown1,
unknown2 = header.unknown2
};
writer.Position = filePos;
newHeader.Write(writer);
// Write new asset infos again (this time with offsets and sizes filled in)
writer.Position = newAssetTablePos;
foreach (AssetFileInfo newAssetInfo in newAssetInfos)
{
newAssetInfo.Write(header.format, writer);
}
// Set writer position back to end of file
writer.Position = filePos + newFileSize;
}
public static bool IsAssetsFile(string filePath)
{
using var reader = AssetsFileReaderHelper.createStreamReader(filePath, false);
return IsAssetsFile(reader, 0, reader.streamImpl.Length);
}
// Temporary polyfill.
public static bool IsAssetsFile(AssetsFileReader legacyReader, long offset, long length)
{
var readerImpl = AssetsFileReaderHelper.createStreamReaderImpl(legacyReader.BaseStream);
var reader = new AssetsFileStatefulReader(readerImpl, true);
return IsAssetsFile(reader, offset, length);
}
public static bool IsAssetsFile(AssetsFileStatefulReader reader, long offset, long length)
{
//todo - not fully implemented
if (length < 0x30)
return false;
reader.Position = offset;
string possibleBundleHeader = reader.ReadStringLength(5);
if (possibleBundleHeader == "Unity")
return false;
reader.Position = offset + 0x08;
int possibleFormat = reader.ReadInt32();
if (possibleFormat > 99)
return false;
reader.Position = offset + 0x14;
if (possibleFormat >= 0x16)
{
reader.Position += 0x1c;
}
string possibleVersion = "";
char curChar;
while (reader.Position < reader.streamImpl.Length && (curChar = (char)reader.ReadByte()) != 0x00)
{
possibleVersion += curChar;
if (possibleVersion.Length > 0xFF)
{
return false;
}
}
string emptyVersion = Regex.Replace(possibleVersion, "[a-zA-Z0-9\\.\\n]", "");
string fullVersion = Regex.Replace(possibleVersion, "[^a-zA-Z0-9\\.\\n]", "");
return emptyVersion == "" && fullVersion.Length > 0;
}
}
}