-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathAssetHelper.cs
More file actions
249 lines (230 loc) · 8.92 KB
/
AssetHelper.cs
File metadata and controls
249 lines (230 loc) · 8.92 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
////////////////////////////
// ASSETSTOOLS.NET PLUGINS
// Hey, watch out! This
// library isn't done yet.
// You've been warned!
using System;
using System.Collections.Generic;
namespace AssetsTools.NET.Extra
{
public static class AssetHelper
{
public static uint FixAudioID(uint id)
{
if (id == 0xf1) //AudioMixerController
id = 0xf0; //AudioMixer
else if (id == 0xf3) //AudioMixerGroupController
id = 0x111; //AudioMixerGroup
else if (id == 0xf5) //AudioMixerSnapshotController
id = 0x110; //AudioMixerSnapshot
return id;
}
public static ClassDatabaseType FindAssetClassByID(ClassDatabaseFile cldb, uint id)
{
id = FixAudioID(id);
foreach (ClassDatabaseType type in cldb.classes)
{
if ((uint)type.classId == id)
return type;
}
return null;
}
public static ClassDatabaseType FindAssetClassByName(ClassDatabaseFile cldb, string name)
{
foreach (ClassDatabaseType type in cldb.classes)
{
if (type.name.GetString(cldb) == name)
return type;
}
return null;
}
public static Type_0D FindTypeTreeTypeByID(TypeTree typeTree, uint id)
{
foreach (Type_0D type in typeTree.unity5Types)
{
if ((uint)type.classId == id)
return type;
}
return null;
}
public static Type_0D FindTypeTreeTypeByID(TypeTree typeTree, uint id, ushort scriptIndex)
{
foreach (Type_0D type in typeTree.unity5Types)
{
//5.5+
if ((uint)type.classId == id && type.scriptIndex == scriptIndex)
return type;
//5.4-
if (type.classId < 0 && id == 0x72 && (type.scriptIndex - 0x10000 == type.classId))
return type;
}
return null;
}
public static Type_0D FindTypeTreeTypeByScriptIndex(TypeTree typeTree, ushort scriptIndex)
{
foreach (Type_0D type in typeTree.unity5Types)
{
if (type.scriptIndex == scriptIndex)
return type;
}
return null;
}
public static Type_0D FindTypeTreeTypeByName(TypeTree typeTree, string name)
{
foreach (Type_0D type in typeTree.unity5Types)
{
if (type.typeFieldsEx[0].GetTypeString(type.stringTable) == name)
return type;
}
return null;
}
public static ushort GetScriptIndex(AssetsFile file, AssetFileInfoEx info)
{
if (file.header.format < 0x10)
return info.scriptIndex;
else
return file.typeTree.unity5Types[info.curFileTypeOrIndex].scriptIndex;
}
public static string GetAssetNameFast(AssetsFile file, ClassDatabaseFile cldb, AssetFileInfoEx info)
{
ClassDatabaseType type = FindAssetClassByID(cldb, info.curFileType);
var reader = file.reader;
if (file.typeTree.hasTypeTree)
{
ushort scriptId = GetScriptIndex(file, info);
Type_0D ttType = FindTypeTreeTypeByID(file.typeTree, info.curFileType, scriptId);
string ttTypeName = ttType.typeFieldsEx[0].GetTypeString(ttType.stringTable);
if (ttType.typeFieldsEx.Length == 0) return type.name.GetString(cldb); //fallback to cldb
if (ttType.typeFieldsEx.Length > 1 && ttType.typeFieldsEx[1].GetNameString(ttType.stringTable) == "m_Name")
{
reader.Position = info.absoluteFilePos;
return reader.ReadCountStringInt32();
}
//todo, use the typetree since we have it already, there could be extra fields
else if (ttTypeName == "GameObject")
{
reader.Position = info.absoluteFilePos;
int size = reader.ReadInt32();
int componentSize = file.header.format > 0x10 ? 0x0c : 0x10;
reader.Position += size * componentSize;
reader.Position += 0x04;
return reader.ReadCountStringInt32();
}
else if (ttTypeName == "MonoBehaviour")
{
reader.Position = info.absoluteFilePos;
reader.Position += 0x1c;
string name = reader.ReadCountStringInt32();
if (name != "")
{
return name;
}
}
return ttTypeName;
}
string typeName = type.name.GetString(cldb);
if (type.fields.Count == 0) return type.name.GetString(cldb);
if (type.fields.Count > 1 && type.fields[1].fieldName.GetString(cldb) == "m_Name")
{
reader.Position = info.absoluteFilePos;
return reader.ReadCountStringInt32();
}
else if (typeName == "GameObject")
{
reader.Position = info.absoluteFilePos;
int size = reader.ReadInt32();
int componentSize = file.header.format > 0x10 ? 0x0c : 0x10;
reader.Position += size * componentSize;
reader.Position += 0x04;
return reader.ReadCountStringInt32();
}
else if (typeName == "MonoBehaviour")
{
reader.Position = info.absoluteFilePos;
reader.Position += 0x1c;
string name = reader.ReadCountStringInt32();
if (name != "")
{
return name;
}
}
return typeName;
}
//no classdatabase but may not work
public static string GetAssetNameFastNaive(AssetsFile file, AssetFileInfoEx info)
{
var reader = file.reader;
if (AssetsFileExtra.HasName(info.curFileType))
{
reader.Position = info.absoluteFilePos;
return reader.ReadCountStringInt32();
}
else if (info.curFileType == 0x01)
{
reader.Position = info.absoluteFilePos;
int size = reader.ReadInt32();
int componentSize = file.header.format > 0x10 ? 0xC : 0x10;
reader.Position += size * componentSize;
reader.Position += 4;
return reader.ReadCountStringInt32();
}
else if (info.curFileType == 0x72)
{
reader.Position = info.absoluteFilePos;
reader.Position += 28;
string name = reader.ReadCountStringInt32();
if (name != "")
{
return name;
}
}
return string.Empty;
}
public static AssetFileInfoEx GetAssetInfo(this AssetsFileTable table, string name, bool caseSensitive = true)
{
if (!caseSensitive)
name = name.ToLower();
for (int i = 0; i < table.assetFileInfo.Length; i++)
{
AssetFileInfoEx info = table.assetFileInfo[i];
string infoName = GetAssetNameFastNaive(table.file, info);
if (!caseSensitive)
infoName = infoName.ToLower();
if (infoName == name)
{
return info;
}
}
return null;
}
public static AssetFileInfoEx GetAssetInfo(this AssetsFileTable table, string name, uint typeId, bool caseSensitive = true)
{
if (!caseSensitive)
name = name.ToLower();
for (int i = 0; i < table.assetFileInfo.Length; i++)
{
AssetFileInfoEx info = table.assetFileInfo[i];
string infoName = GetAssetNameFastNaive(table.file, info);
if (!caseSensitive)
infoName = infoName.ToLower();
if (info.curFileType == typeId && infoName == name)
{
return info;
}
}
return null;
}
public static List<AssetFileInfoEx> GetAssetsOfType(this AssetsFileTable table, int typeId)
{
List<AssetFileInfoEx> infos = new List<AssetFileInfoEx>();
foreach (AssetFileInfoEx info in table.assetFileInfo)
{
if (info.curFileType == typeId)
{
infos.Add(info);
}
}
return infos;
}
}
}