Skip to content

Commit a4894c4

Browse files
Restore "assets" table population and documentation tweaks (#38)
The AssetBundleHandler got lost in recent code change, so restore it. Add a few cross links to the new Addressables Build Layout parsing. Fix the section "Matching content back to the source asset" because it wasn't explaining that some source asset paths actually are available in the build output (for AssetBundle explicit assets, and the scenes in a Player Build) New example showing how to find implicitly referenced assets.
1 parent 6fcdbd4 commit a4894c4

File tree

6 files changed

+132
-9
lines changed

6 files changed

+132
-9
lines changed

Analyzer/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,19 @@ important types like Meshes, Shaders, Texture2D and AnimationClips. For example
186186
File, then rows are added to both the `objects` table and the `meshes` table. The meshes table contains columns that only apply to Mesh objects, for example the number of vertices, indices, bones, and channels. The `mesh_view` is a view that joins the `objects` table with the `meshes` table, so that you can see all the properties of a Mesh object in one place.
187187

188188
Each supported Unity object type follows the same pattern:
189-
* A Handler class in the SQLite/Handlers (e.g. [MeshHandler.cs](./SQLite/Handler/MeshHandler.cs).
190-
* The registration of the handler in the m_Handlers dictionary in [SQLiteWriter.cs](./SQLite/SQLiteWriter.cs).
189+
* A Handler class in the SQLite/Handlers, e.g. [MeshHandler.cs](./SQLite/Handler/MeshHandler.cs).
190+
* The registration of the handler in the m_Handlers dictionary in [SerializedFileSQLiteWriter.cs](./SQLite/Writers/SerializedFileSQLiteWriter.cs).
191191
* SQL statements defining extra tables and views associated with the type, e.g. [Mesh.sql](./SQLite/Resources/Mesh.sql).
192-
* A Reader class that uses RandomAccessReader to read properties from the serialized object. e.g. [Mesh.cs](./SerializedObjects/Mesh.cs).
192+
* A Reader class that uses RandomAccessReader to read properties from the serialized object, e.g. [Mesh.cs](./SerializedObjects/Mesh.cs).
193193

194194
It would be possible to extend the Analyze library to add additional columns for the existing types, or by following the same pattern to add additional types. The [dump](../UnityDataTool/README.md#dump) feature of UnityDataTool is a useful way to see the property names and other details of the serialization for a type. Based on that information, code in the Reader class can use the RandomAccessReader to retrieve those properties to bring them into the SQLite database.
195+
196+
## Supporting Other File Formats
197+
198+
Another direction of possible extension is to support analyzing additional file formats, beyond Unity SerializedFiles.
199+
200+
This the approach taken to analyze Addressables Build Layout files, which are JSON files using the format defined in [BuildLayout.cs](./SQLite/Parsers/Models/BuildLayout.cs).
201+
202+
Support for another file format could be added by deriving an additional class from SQLiteWriter and implementing a class derived from ISQLiteFileParser. Then follow the existing code structure convention to add new Commands (derived from AbstractCommand) and Resource .sql files to establish additional tables in the database.
203+
204+
An example of another file format that could be useful to support, as the tool evolves, are the yaml [.manifest files](https://docs.unity3d.com/Manual/assetbundles-file-format.html), generated by BuildPipeline.BuildAssetBundles().
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
using Microsoft.Data.Sqlite;
4+
using UnityDataTools.Analyzer.SerializedObjects;
5+
using UnityDataTools.FileSystem.TypeTreeReaders;
6+
7+
namespace UnityDataTools.Analyzer.SQLite.Handlers;
8+
9+
public class AssetBundleHandler : ISQLiteHandler
10+
{
11+
SqliteCommand m_InsertCommand;
12+
private SqliteCommand m_InsertDepCommand;
13+
private Regex m_SceneNameRegex = new Regex(@"([^//]+)\.unity");
14+
15+
public void Init(SqliteConnection db)
16+
{
17+
using var command = db.CreateCommand();
18+
command.CommandText = Resources.AssetBundle;
19+
command.ExecuteNonQuery();
20+
21+
m_InsertCommand = db.CreateCommand();
22+
23+
m_InsertCommand.CommandText = "INSERT INTO assets(object, name) VALUES(@object, @name)";
24+
m_InsertCommand.Parameters.Add("@object", SqliteType.Integer);
25+
m_InsertCommand.Parameters.Add("@name", SqliteType.Text);
26+
27+
m_InsertDepCommand = db.CreateCommand();
28+
29+
m_InsertDepCommand.CommandText = "INSERT INTO asset_dependencies(object, dependency) VALUES(@object, @dependency)";
30+
m_InsertDepCommand.Parameters.Add("@object", SqliteType.Integer);
31+
m_InsertDepCommand.Parameters.Add("@dependency", SqliteType.Integer);
32+
}
33+
34+
public void Process(Context ctx, long objectId, RandomAccessReader reader, out string name, out long streamDataSize)
35+
{
36+
var assetBundle = AssetBundle.Read(reader);
37+
38+
foreach (var asset in assetBundle.Assets)
39+
{
40+
if (!assetBundle.IsSceneAssetBundle)
41+
{
42+
var fileId = ctx.LocalToDbFileId[asset.PPtr.FileId];
43+
var objId = ctx.ObjectIdProvider.GetId((fileId, asset.PPtr.PathId));
44+
m_InsertCommand.Transaction = ctx.Transaction;
45+
m_InsertCommand.Parameters["@object"].Value = objId;
46+
m_InsertCommand.Parameters["@name"].Value = asset.Name;
47+
m_InsertCommand.ExecuteNonQuery();
48+
49+
for (int i = asset.PreloadIndex; i < asset.PreloadIndex + asset.PreloadSize; ++i)
50+
{
51+
var dependency = assetBundle.PreloadTable[i];
52+
var depFileId = ctx.LocalToDbFileId[dependency.FileId];
53+
var depId = ctx.ObjectIdProvider.GetId((depFileId, dependency.PathId));
54+
m_InsertDepCommand.Transaction = ctx.Transaction;
55+
m_InsertDepCommand.Parameters["@object"].Value = objId;
56+
m_InsertDepCommand.Parameters["@dependency"].Value = depId;
57+
m_InsertDepCommand.ExecuteNonQuery();
58+
}
59+
}
60+
else
61+
{
62+
var match = m_SceneNameRegex.Match(asset.Name);
63+
64+
if (match.Success)
65+
{
66+
var sceneName = match.Groups[1].Value;
67+
var objId = ctx.ObjectIdProvider.GetId((ctx.SerializedFileIdProvider.GetId(sceneName), 0));
68+
m_InsertCommand.Transaction = ctx.Transaction;
69+
m_InsertCommand.Parameters["@object"].Value = objId;
70+
m_InsertCommand.Parameters["@name"].Value = asset.Name;
71+
m_InsertCommand.ExecuteNonQuery();
72+
}
73+
}
74+
}
75+
76+
name = assetBundle.Name;
77+
streamDataSize = 0;
78+
}
79+
80+
public void Finalize(SqliteConnection db)
81+
{
82+
using var command = new SqliteCommand();
83+
command.Connection = db;
84+
command.CommandText = "CREATE INDEX asset_dependencies_object ON asset_dependencies(object)";
85+
command.ExecuteNonQuery();
86+
87+
command.CommandText = "CREATE INDEX asset_dependencies_dependency ON asset_dependencies(dependency)";
88+
command.ExecuteNonQuery();
89+
}
90+
91+
void IDisposable.Dispose()
92+
{
93+
m_InsertCommand?.Dispose();
94+
m_InsertDepCommand?.Dispose();
95+
}
96+
}

Analyzer/SQLite/Parsers/SerializedFileParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool ShouldIgnoreFile(string file)
6464
private static readonly HashSet<string> IgnoredExtensions = new()
6565
{
6666
".txt", ".resS", ".resource", ".json", ".dll", ".pdb", ".exe", ".manifest", ".entities", ".entityheader",
67-
".ini", ".config"
67+
".ini", ".config", ".hash"
6868
};
6969

7070
bool ProcessFile(string file, string rootDirectory)

Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class SerializedFileSQLiteWriter : IDisposable
3535
{ "Shader", new ShaderHandler() },
3636
{ "AudioClip", new AudioClipHandler() },
3737
{ "AnimationClip", new AnimationClipHandler() },
38+
{ "AssetBundle", new AssetBundleHandler() },
3839
{ "PreloadData", new PreloadDataHandler() },
3940
};
4041

Documentation/addressables-build-reports.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ You can analyze a directory with both asset bundles (*.bundle) and json files (*
114114

115115
Once the data is in the database, you can run queries to analyze your Addressables build:
116116

117+
118+
#### Find all implicit assets for an explicit asset
119+
```sql
120+
-- Find implicitly included assets for a given explicit asset id
121+
SELECT a.explicit_asset_id, b.id, b.asset_path, b.asset_path
122+
FROM addressables_build_explicit_asset_internal_referenced_other_assets a,
123+
addressables_build_data_from_other_assets b
124+
WHERE a.internal_referenced_other_asset_rid = b.id
125+
AND a.build_id = b.build_id;
126+
AND a.explicit_asset_id = 5092
127+
AND a.build_id = 3;
128+
```
129+
117130
#### Find the cache name for an addressables bundle
118131
Addressables renames bundles to make it possible to do content updates. Internally bundles are still named by their internal hash and are cached based upon this name. If you want to lookup how a remote bundle will be cached in Unity's [cache](https://docs.unity3d.com/ScriptReference/Caching.html) you can use the addressables_build_cached_bundles view.
119132
```sql
@@ -165,4 +178,4 @@ group by a.id;
165178
SELECT name, start_time, duration, error
166179
FROM addressables_builds
167180
WHERE coalesce(error, '') != '';
168-
```
181+
```

Documentation/analyze-examples.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,13 @@ This is a large subject, see [Comparing Builds](comparing-builds.md).
220220

221221
## Example: Matching content back to the source asset
222222

223-
UnityDataTool works on the output of a Unity build, which, by its very nature, only contains the crucial data needed to efficiently load built content in the Player. So it does not include any information about the assets and scenes in the project that was used to create that build. However you may want to match content back to the original source asset or scene. For example if the size of an AssetBundle has unexpectedly changed between builds then you may want to track down which source assets could be responsible for that change. Or you may want to confirm that some particular image has been included in the build.
223+
UnityDataTool works on the output of a Unity build, which, by its very nature, only contains the crucial data needed to efficiently load built content in the Player. It does not include complete information about the assets and scenes in the project that was used to create that build. You may want to match content back to the original source asset or scene. For example if the size of an AssetBundle has unexpectedly changed between builds then you may want to track down which source assets could be responsible for that change. Or you may want to confirm that some particular image has been included in the build.
224224

225-
In many cases the source asset can be inferred based on your specific knowledge of your project, and how the build was configured. For example the level files in a Player build match the Scenes in the Build Profile Scene list. And the content of AssetBundles is driven from the assignment of specific assets to those AssetBundles (or Addressable groups).
225+
For AssetBundles partial asset information can be found in the m_Containers list, inside the AssetBundle object. This records assets that were explicitly added to AssetBundles. In the database this can be found in the `assets` table. However, assets that are included in the build implicitly (because they are referenced from the explicitly added assets) will not be recorded anywhere in the AssetBundle content.
226+
227+
Similarly for a player build the only paths populated in the `assets` table are the scenes from the Build Profile Scene List. The paths of the assets in the sharedAsset files is not recorded anywhere in the build output.
228+
229+
In many cases the source asset can be inferred based on your specific knowledge of your project, and how the build was configured. For example the level files in a Player build match the Scenes in the Build Profile Scene list. And the content of AssetBundles is driven from the assignment of specific assets to those AssetBundles (or Addressable groups), along with assets they depend on.
226230

227231
Also, in many cases the name of objects matches the file name of the asset. For example the Texture2D "red" object probably comes from a file named red.png somewhere in the project.
228232

@@ -235,5 +239,4 @@ Examples of alternative sources of build information:
235239
* The [BuildReport](https://docs.unity3d.com/ScriptReference/Build.Reporting.BuildReport.html) has detailed source information in the PackedAssets section. The [BuildReportInspector](https://github.com/Unity-Technologies/BuildReportInspector) is a useful way to view data from the BuildReport.
236240
* The Editor log reports a lot of information during a build.
237241
* Regular AssetBundle builds create [.manifest files](https://docs.unity3d.com/Manual/assetbundles-file-format.html), which contain information about the source assets and types.
238-
* Addressable builds do not produce BuildReport files, nor .manifest files. But there is similar reporting, for example the [Build Layout Report](https://docs.unity3d.com/Packages/[email protected]/manual/BuildLayoutReport.html).
239-
242+
* Addressable builds do not produce BuildReport files, nor .manifest files. But UnityDataTools supports analyzing the [Addressables Build Reports](addressables-build-reports.md) and will populate the `addressables_build_explicit_assets` and `addressables_build_data_from_other_assets` tables.

0 commit comments

Comments
 (0)