-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathSerializedFileSQLiteWriter.cs
More file actions
401 lines (352 loc) · 17.9 KB
/
Copy pathSerializedFileSQLiteWriter.cs
File metadata and controls
401 lines (352 loc) · 17.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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_CurrentArchiveId = -1;
private int m_NextArchiveId = 0;
private bool m_SkipReferences;
private bool m_SkipCrc;
// Global id assignment shared across every serialized file in the database.
// m_SerializedFileIdProvider maps a serialized file (by lowercased file name) to its
// serialized_files row id; m_ObjectIdProvider maps a (serialized file id, pathId) pair to
// its objects row id. See ObjectIdProvider for how cross-file references are resolved.
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();
// Detects the SerializedFiles of a BuildPipeline.BuildAssetBundles scene bundle
// ("BuildPlayer-<SceneName>") and captures the scene name. Scene bundles from the Scriptable
// Build Pipeline / Addressables instead use "CAB-<hash>" / "CAB-<hash>.sharedAssets" and are
// handled by the .sharedAssets branch in WriteSerializedFile plus AssetBundleHandler.
// Player-build scenes ("level0", ...) have no scene object; their PreloadData dependencies are
// attributed to the PreloadData object itself (PreloadDataHandler).
private Regex m_RegexSceneFile = new(@"BuildPlayer-([^\.]+)(?:\.sharedAssets)?");
// Rebuilt for each serialized file: maps a PPtr's local m_FileID (0 = this file, 1..N = an
// entry in this file's external reference table) to the global serialized file id. A PPtr's
// (m_FileID, m_PathID) is only meaningful within its own file, so it must be translated
// through this map before being handed to m_ObjectIdProvider.
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 AddArchive m_AddArchiveCommand = new AddArchive();
private AddSerializedFile m_AddSerializedFileCommand = new AddSerializedFile();
private AddObject m_AddObjectCommand = new AddObject();
private AddType m_AddTypeCommand = new AddType();
private AddPreloadDependency m_InsertDepCommand = new AddPreloadDependency();
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_AddArchiveCommand.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 BeginArchive(string name, long size)
{
if (m_CurrentArchiveId != -1)
{
throw new InvalidOperationException("SQLWriter.BeginArchive called twice");
}
m_CurrentArchiveId = m_NextArchiveId++;
m_AddArchiveCommand.SetValue("id", m_CurrentArchiveId);
m_AddArchiveCommand.SetValue("name", name);
m_AddArchiveCommand.SetValue("file_size", size);
m_AddArchiveCommand.ExecuteNonQuery();
}
public void EndArchive()
{
if (m_CurrentArchiveId == -1)
{
throw new InvalidOperationException("SQLWriter.EndArchive called before SQLWriter.BeginArchive");
}
m_CurrentArchiveId = -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).ToLowerInvariant());
int sceneId = -1;
using var transaction = m_Database.BeginTransaction();
m_CurrentTransaction = transaction;
// A scene has no single Unity object to represent it, yet a scene bundle lists the scene
// (by its .unity path) as the bundle's asset and its preloads need something to hang off
// of. So for scene bundles we synthesize one "Scene" object per scene, keyed on the
// scene's SerializedFile so every place that references it computes the same id. It is the
// target of the assetbundle_assets row (AssetBundleHandler) and of the scene's preload
// dependencies (the content loop below and PreloadDataHandler).
var match = m_RegexSceneFile.Match(relativePath);
if (match.Success)
{
// BuildPipeline.BuildAssetBundles scene bundle. The scene name comes from the file name
// itself, and the scene object is keyed on that name (AssetBundleHandler matches it
// from the "<sceneName>.unity" container entry).
var sceneName = match.Groups[1].Value;
var sceneFileId = m_SerializedFileIdProvider.GetId(sceneName);
sceneId = m_ObjectIdProvider.GetId((sceneFileId, 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();
}
}
else if (Path.GetFileName(fullPath).EndsWith(".sharedAssets", StringComparison.OrdinalIgnoreCase))
{
// Scriptable Build Pipeline / Addressables scene bundle: a scene's shared-assets file is
// "<sceneFile>.sharedAssets" (e.g. "CAB-<hash>.sharedAssets"). Resolve the scene object
// id from the scene file name so the content loop below and PreloadDataHandler attach
// the scene's dependencies to it. Unlike the BuildPipeline case above, the Scene object
// itself (and its readable name) is created by AssetBundleHandler, which gets the scene
// path -> file mapping from the AssetBundle object's m_SceneHashes.
var fileName = Path.GetFileName(fullPath);
var sceneFileName = fileName.Substring(0, fileName.Length - ".sharedAssets".Length);
var sceneFileId = m_SerializedFileIdProvider.GetId(sceneFileName.ToLowerInvariant());
sceneId = m_ObjectIdProvider.GetId((sceneFileId, 0));
}
m_LocalToDbFileId.Clear();
Context ctx = new()
{
ArchiveId = m_CurrentArchiveId,
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("archive", m_CurrentArchiveId == -1 ? "" : m_CurrentArchiveId);
m_AddSerializedFileCommand.SetValue("name", relativePath);
m_AddSerializedFileCommand.ExecuteNonQuery();
// Local file id 0 is always this file itself; ids 1..N follow the order of the
// external reference table. Resolve each external reference to its global file id
// by (lowercased) file name.
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).ToLowerInvariant()));
}
foreach (var obj in sf.Objects)
{
// serializedFileId is already this file's global id, so no LocalToDbFileId
// translation is needed for the file's own objects; obj.Id is the pathId.
var currentObjectId = m_ObjectIdProvider.GetId((serializedFileId, obj.Id));
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_AddArchiveCommand.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();
}
}