-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathSerializedFileSQLiteWriter.cs
More file actions
365 lines (315 loc) · 14.9 KB
/
Copy pathSerializedFileSQLiteWriter.cs
File metadata and controls
365 lines (315 loc) · 14.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Data.Sqlite;
using UnityDataTools.Analyzer.SQLite.Commands.SerializedFile;
using UnityDataTools.Analyzer.SQLite.Handlers;
using UnityDataTools.Analyzer.Util;
using UnityDataTools.BinaryFormat;
using UnityDataTools.FileSystem;
using UnityDataTools.FileSystem.TypeTreeReaders;
namespace UnityDataTools.Analyzer.SQLite.Writers;
public class SerializedFileSQLiteWriter : IDisposable
{
private HashSet<int> m_TypeSet = new();
private int m_CurrentAssetBundleId = -1;
private int m_NextAssetBundleId = 0;
private bool m_SkipReferences;
private bool m_SkipCrc;
private IdProvider<string> m_SerializedFileIdProvider = new();
private ObjectIdProvider m_ObjectIdProvider = new();
// The refs table stores ids into these deduplicated string tables instead of repeating the
// property path/type strings on every row. Ids are assigned lazily and are global across all
// files; the HashSets track which ids have already had their lookup row written.
private IdProvider<string> m_PropertyPathIdProvider = new();
private IdProvider<string> m_PropertyTypeIdProvider = new();
private HashSet<int> m_PropertyPathSet = new();
private HashSet<int> m_PropertyTypeSet = new();
private Regex m_RegexSceneFile = new(@"BuildPlayer-([^\.]+)(?:\.sharedAssets)?");
// Used to map PPtr fileId to its corresponding serialized file id in the database.
Dictionary<int, int> m_LocalToDbFileId = new();
private Dictionary<string, ISQLiteHandler> m_Handlers = new()
{
{ "Mesh", new MeshHandler() },
{ "Texture2D", new Texture2DHandler() },
{ "Shader", new ShaderHandler() },
{ "AudioClip", new AudioClipHandler() },
{ "AnimationClip", new AnimationClipHandler() },
{ "AssetBundle", new AssetBundleHandler() },
{ "PreloadData", new PreloadDataHandler() },
{ "MonoScript", new MonoScriptHandler() },
{ "BuildReport", new BuildReportHandler() },
{ "PackedAssets", new PackedAssetsHandler() },
};
// serialized files
private AddReference m_AddReferenceCommand = new AddReference();
private AddPropertyName m_AddPropertyNameCommand = new AddPropertyName();
private AddPropertyType m_AddPropertyTypeCommand = new AddPropertyType();
private AddAssetBundle m_AddAssetBundleCommand = new AddAssetBundle();
private AddSerializedFile m_AddSerializedFileCommand = new AddSerializedFile();
private AddObject m_AddObjectCommand = new AddObject();
private AddType m_AddTypeCommand = new AddType();
private AddAssetDependency m_InsertDepCommand = new AddAssetDependency();
private bool m_Initialized;
private SqliteConnection m_Database;
private SqliteCommand m_LastId = new SqliteCommand();
private SqliteTransaction m_CurrentTransaction = null;
public SerializedFileSQLiteWriter(SqliteConnection database, bool skipReferences, bool skipCrc)
{
m_Initialized = false;
m_Database = database;
m_SkipReferences = skipReferences;
m_SkipCrc = skipCrc;
}
public void Init()
{
if (m_Initialized)
return;
m_Initialized = true;
foreach (var handler in m_Handlers.Values)
{
handler.Init(m_Database);
}
CreateSQLiteCommands();
}
private void CreateSQLiteCommands()
{
// build serialized file commands
m_AddReferenceCommand.CreateCommand(m_Database);
m_AddPropertyNameCommand.CreateCommand(m_Database);
m_AddPropertyTypeCommand.CreateCommand(m_Database);
m_AddAssetBundleCommand.CreateCommand(m_Database);
m_AddSerializedFileCommand.CreateCommand(m_Database);
m_AddObjectCommand.CreateCommand(m_Database);
m_AddTypeCommand.CreateCommand(m_Database);
m_InsertDepCommand.CreateCommand(m_Database);
m_LastId = m_Database.CreateCommand();
m_LastId.CommandText = "SELECT last_insert_rowid()";
}
public void BeginAssetBundle(string name, long size)
{
if (m_CurrentAssetBundleId != -1)
{
throw new InvalidOperationException("SQLWriter.BeginAssetBundle called twice");
}
m_CurrentAssetBundleId = m_NextAssetBundleId++;
m_AddAssetBundleCommand.SetValue("id", m_CurrentAssetBundleId);
m_AddAssetBundleCommand.SetValue("name", name);
m_AddAssetBundleCommand.SetValue("file_size", size);
m_AddAssetBundleCommand.ExecuteNonQuery();
}
public void EndAssetBundle()
{
if (m_CurrentAssetBundleId == -1)
{
throw new InvalidOperationException("SQLWriter.EndAssetBundle called before SQLWriter.BeginAssetBundle");
}
m_CurrentAssetBundleId = -1;
}
public void WriteSerializedFile(string relativePath, string fullPath, string containingFolder)
{
// A file without TypeTrees can only be opened when its types exactly match this build of
// UnityFileSystemApi. Handing such a file to the native loader produces misleading version
// mismatch errors and can crash the process, so detect and reject it up front. The native
// VFS path here may be a real file or an entry inside a mounted archive.
using (var detectStream = new UnityFileStream(fullPath))
{
if (SerializedFileDetector.IsMissingTypeTrees(detectStream))
throw new SerializedFileOpenException(fullPath, missingTypeTrees: true);
}
using var sf = UnityFileSystem.OpenSerializedFile(fullPath);
using var reader = new UnityFileReader(fullPath, 64 * 1024 * 1024);
using var pptrReader = new PPtrAndCrcProcessor(sf, reader, containingFolder, m_SkipCrc, AddReference);
int serializedFileId = m_SerializedFileIdProvider.GetId(Path.GetFileName(fullPath).ToLower());
int sceneId = -1;
using var transaction = m_Database.BeginTransaction();
m_CurrentTransaction = transaction;
var match = m_RegexSceneFile.Match(relativePath);
if (match.Success)
{
var sceneName = match.Groups[1].Value;
// There is no Scene object in Unity (a Scene is the full content of a
// SerializedFile). We generate an object id using the name of the Scene
// as SerializedFile name, and the object id 0.
sceneId = m_ObjectIdProvider.GetId((m_SerializedFileIdProvider.GetId(sceneName), 0));
// There are 2 SerializedFiles per Scene, one ends with .sharedAssets. This is a
// dirty trick to avoid inserting the scene object a second time.
if (relativePath.EndsWith(".sharedAssets"))
{
m_AddObjectCommand.SetTransaction(transaction);
m_AddObjectCommand.SetValue("game_object", ""); // or other value
m_AddObjectCommand.SetValue("id", sceneId);
m_AddObjectCommand.SetValue("object_id", 0);
m_AddObjectCommand.SetValue("serialized_file", serializedFileId);
// The type is set to -1 which doesn't exist in Unity, but is associated to
// "Scene" in the database.
m_AddObjectCommand.SetValue("type", -1);
m_AddObjectCommand.SetValue("name", sceneName);
m_AddObjectCommand.SetValue("size", 0);
m_AddObjectCommand.SetValue("crc32", 0);
m_AddObjectCommand.ExecuteNonQuery();
}
}
m_LocalToDbFileId.Clear();
Context ctx = new()
{
AssetBundleId = m_CurrentAssetBundleId,
SerializedFileId = serializedFileId,
SceneId = sceneId,
ObjectIdProvider = m_ObjectIdProvider,
SerializedFileIdProvider = m_SerializedFileIdProvider,
LocalToDbFileId = m_LocalToDbFileId,
Transaction = transaction,
};
ctx.Transaction = transaction;
try
{
m_AddSerializedFileCommand.SetTransaction(transaction);
m_AddSerializedFileCommand.SetValue("id", serializedFileId);
m_AddSerializedFileCommand.SetValue("asset_bundle", m_CurrentAssetBundleId == -1 ? "" : m_CurrentAssetBundleId);
m_AddSerializedFileCommand.SetValue("name", relativePath);
m_AddSerializedFileCommand.ExecuteNonQuery();
int localId = 0;
m_LocalToDbFileId.Add(localId++, serializedFileId);
foreach (var extRef in sf.ExternalReferences)
{
m_LocalToDbFileId.Add(localId++,
m_SerializedFileIdProvider.GetId(extRef.Path.Substring(extRef.Path.LastIndexOf('/') + 1).ToLower()));
}
foreach (var obj in sf.Objects)
{
var currentObjectId = m_ObjectIdProvider.GetId((serializedFileId, obj.Id));
// Console.WriteLine($"\nProcessing {currentObjectId}");
var root = sf.GetTypeTreeRoot(obj.Id);
var offset = obj.Offset;
uint crc32 = 0;
if (!m_TypeSet.Contains(obj.TypeId))
{
m_AddTypeCommand.SetTransaction(transaction);
m_AddTypeCommand.SetValue("id", obj.TypeId);
m_AddTypeCommand.SetValue("name", root.Type);
m_AddTypeCommand.ExecuteNonQuery();
m_TypeSet.Add(obj.TypeId);
}
var randomAccessReader = new RandomAccessReader(sf, root, reader, offset);
string name = string.Empty;
long streamDataSize = 0;
if (m_Handlers.TryGetValue(root.Type, out var handler))
{
handler.Process(ctx, currentObjectId, randomAccessReader,
out name, out streamDataSize);
}
else if (randomAccessReader.HasChild("m_Name"))
{
name = randomAccessReader["m_Name"].GetValue<string>();
}
if (randomAccessReader.HasChild("m_GameObject"))
{
var pptr = randomAccessReader["m_GameObject"];
var fileId = m_LocalToDbFileId[pptr["m_FileID"].GetValue<int>()];
var gameObjectID = m_ObjectIdProvider.GetId((fileId, pptr["m_PathID"].GetValue<long>()));
m_AddObjectCommand.SetValue("game_object", gameObjectID);
}
else
{
m_AddObjectCommand.SetValue("game_object", "");
}
// The walk both extracts references and accumulates the CRC, so it is needed
// unless both are disabled. When CRC is on but references are off, the walk
// still resolves referenced object ids (AddReference skips the insert).
if (!m_SkipReferences || !m_SkipCrc)
{
crc32 = pptrReader.Process(currentObjectId, offset, root);
}
// convert this to the new syntax
m_AddObjectCommand.SetTransaction(transaction);
m_AddObjectCommand.SetValue("id", currentObjectId);
m_AddObjectCommand.SetValue("object_id", obj.Id);
m_AddObjectCommand.SetValue("serialized_file", serializedFileId);
m_AddObjectCommand.SetValue("type", obj.TypeId);
m_AddObjectCommand.SetValue("name", name);
m_AddObjectCommand.SetValue("size", obj.Size + streamDataSize);
m_AddObjectCommand.SetValue("crc32", crc32);
m_AddObjectCommand.ExecuteNonQuery();
// If this is a Scene AssetBundle, add the object as a depencency of the
// current scene.
if (ctx.SceneId != -1)
{
m_InsertDepCommand.SetTransaction(transaction);
m_InsertDepCommand.SetValue("object", ctx.SceneId);
m_InsertDepCommand.SetValue("dependency", currentObjectId);
m_InsertDepCommand.ExecuteNonQuery();
}
}
transaction.Commit();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
// Callback from PPtrAndCrcProcessor for each reference discovered in the SerializedFile
private int AddReference(long objectId, int fileId, long pathId, string propertyPath, string propertyType)
{
// Always resolve the id so the CRC stays stable; only persist the row when references
// are being extracted.
var referencedObjectId = m_ObjectIdProvider.GetId((m_LocalToDbFileId[fileId], pathId));
if (!m_SkipReferences)
{
var propertyPathId = GetPropertyPathId(propertyPath);
var propertyTypeId = GetPropertyTypeId(propertyType);
m_AddReferenceCommand.SetTransaction(m_CurrentTransaction);
m_AddReferenceCommand.SetValue("object", objectId);
m_AddReferenceCommand.SetValue("referenced_object", referencedObjectId);
m_AddReferenceCommand.SetValue("property_path", propertyPathId);
m_AddReferenceCommand.SetValue("property_type", propertyTypeId);
m_AddReferenceCommand.ExecuteNonQuery();
}
return referencedObjectId;
}
// Resolve a property path/type string to its id, writing the lookup row the first time the
// string is seen. Called within the current transaction (references are being extracted).
private int GetPropertyPathId(string propertyPath)
{
var id = m_PropertyPathIdProvider.GetId(propertyPath);
if (m_PropertyPathSet.Add(id))
{
m_AddPropertyNameCommand.SetTransaction(m_CurrentTransaction);
m_AddPropertyNameCommand.SetValue("id", id);
m_AddPropertyNameCommand.SetValue("name", propertyPath);
m_AddPropertyNameCommand.ExecuteNonQuery();
}
return id;
}
private int GetPropertyTypeId(string propertyType)
{
var id = m_PropertyTypeIdProvider.GetId(propertyType);
if (m_PropertyTypeSet.Add(id))
{
m_AddPropertyTypeCommand.SetTransaction(m_CurrentTransaction);
m_AddPropertyTypeCommand.SetValue("id", id);
m_AddPropertyTypeCommand.SetValue("name", propertyType);
m_AddPropertyTypeCommand.ExecuteNonQuery();
}
return id;
}
public void Dispose()
{
foreach (var handler in m_Handlers.Values)
{
handler.Dispose();
}
// Serialized file dispose calls
m_AddAssetBundleCommand.Dispose();
m_AddSerializedFileCommand.Dispose();
m_AddReferenceCommand.Dispose();
m_AddPropertyNameCommand.Dispose();
m_AddPropertyTypeCommand.Dispose();
m_AddObjectCommand.Dispose();
m_AddTypeCommand.Dispose();
m_InsertDepCommand.Dispose();
m_LastId.Dispose();
}
}