Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Analyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,18 @@ File, then rows are added to both the `objects` table and the `meshes` table. T

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

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.

## Supporting Other File Formats

Another direction of possible extension is to support analyzing additional file formats, beyond Unity SerializedFiles.

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).

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.

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().
96 changes: 96 additions & 0 deletions Analyzer/SQLite/Handlers/AssetBundleHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Text.RegularExpressions;
using Microsoft.Data.Sqlite;
using UnityDataTools.Analyzer.SerializedObjects;
using UnityDataTools.FileSystem.TypeTreeReaders;

namespace UnityDataTools.Analyzer.SQLite.Handlers;

public class AssetBundleHandler : ISQLiteHandler
{
SqliteCommand m_InsertCommand;
private SqliteCommand m_InsertDepCommand;
private Regex m_SceneNameRegex = new Regex(@"([^//]+)\.unity");

public void Init(SqliteConnection db)
{
using var command = db.CreateCommand();
command.CommandText = Resources.AssetBundle;
command.ExecuteNonQuery();

m_InsertCommand = db.CreateCommand();

m_InsertCommand.CommandText = "INSERT INTO assets(object, name) VALUES(@object, @name)";
m_InsertCommand.Parameters.Add("@object", SqliteType.Integer);
m_InsertCommand.Parameters.Add("@name", SqliteType.Text);

m_InsertDepCommand = db.CreateCommand();

m_InsertDepCommand.CommandText = "INSERT INTO asset_dependencies(object, dependency) VALUES(@object, @dependency)";
m_InsertDepCommand.Parameters.Add("@object", SqliteType.Integer);
m_InsertDepCommand.Parameters.Add("@dependency", SqliteType.Integer);
}

public void Process(Context ctx, long objectId, RandomAccessReader reader, out string name, out long streamDataSize)
{
var assetBundle = AssetBundle.Read(reader);

foreach (var asset in assetBundle.Assets)
{
if (!assetBundle.IsSceneAssetBundle)
{
var fileId = ctx.LocalToDbFileId[asset.PPtr.FileId];
var objId = ctx.ObjectIdProvider.GetId((fileId, asset.PPtr.PathId));
m_InsertCommand.Transaction = ctx.Transaction;
m_InsertCommand.Parameters["@object"].Value = objId;
m_InsertCommand.Parameters["@name"].Value = asset.Name;
m_InsertCommand.ExecuteNonQuery();

for (int i = asset.PreloadIndex; i < asset.PreloadIndex + asset.PreloadSize; ++i)
{
var dependency = assetBundle.PreloadTable[i];
var depFileId = ctx.LocalToDbFileId[dependency.FileId];
var depId = ctx.ObjectIdProvider.GetId((depFileId, dependency.PathId));
m_InsertDepCommand.Transaction = ctx.Transaction;
m_InsertDepCommand.Parameters["@object"].Value = objId;
m_InsertDepCommand.Parameters["@dependency"].Value = depId;
m_InsertDepCommand.ExecuteNonQuery();
}
}
else
{
var match = m_SceneNameRegex.Match(asset.Name);

if (match.Success)
{
var sceneName = match.Groups[1].Value;
var objId = ctx.ObjectIdProvider.GetId((ctx.SerializedFileIdProvider.GetId(sceneName), 0));
m_InsertCommand.Transaction = ctx.Transaction;
m_InsertCommand.Parameters["@object"].Value = objId;
m_InsertCommand.Parameters["@name"].Value = asset.Name;
m_InsertCommand.ExecuteNonQuery();
}
}
}

name = assetBundle.Name;
streamDataSize = 0;
}

public void Finalize(SqliteConnection db)
{
using var command = new SqliteCommand();
command.Connection = db;
command.CommandText = "CREATE INDEX asset_dependencies_object ON asset_dependencies(object)";
command.ExecuteNonQuery();

command.CommandText = "CREATE INDEX asset_dependencies_dependency ON asset_dependencies(dependency)";
command.ExecuteNonQuery();
}

void IDisposable.Dispose()
{
m_InsertCommand?.Dispose();
m_InsertDepCommand?.Dispose();
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored file, not new code

2 changes: 1 addition & 1 deletion Analyzer/SQLite/Parsers/SerializedFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool ShouldIgnoreFile(string file)
private static readonly HashSet<string> IgnoredExtensions = new()
{
".txt", ".resS", ".resource", ".json", ".dll", ".pdb", ".exe", ".manifest", ".entities", ".entityheader",
".ini", ".config"
".ini", ".config", ".hash"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore .hash files - just to reduce the false alarm errors when parsing a "Library\com.unity.addressables" folder

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

};

bool ProcessFile(string file, string rootDirectory)
Expand Down
1 change: 1 addition & 0 deletions Analyzer/SQLite/Writers/SerializedFileSQLiteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SerializedFileSQLiteWriter : IDisposable
{ "Shader", new ShaderHandler() },
{ "AudioClip", new AudioClipHandler() },
{ "AnimationClip", new AnimationClipHandler() },
{ "AssetBundle", new AssetBundleHandler() },
{ "PreloadData", new PreloadDataHandler() },
};

Expand Down
13 changes: 8 additions & 5 deletions Documentation/analyze-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This topic gives some examples of using the SQLite output of the UnityDataTools

The command line arguments to invoke Analyze are documented [here](../UnityDataTool/README.md#analyzeanalyse).

The definition of the views, and some internal details about how Analyze is implemented, can be found [here](../Analyzer/README.md).
The definition of the views, and some internal details about how Analyze is implemented, can be found [here](../Analyzer/README.md).

## Running Queries from the Command line

Expand Down Expand Up @@ -220,9 +220,13 @@ This is a large subject, see [Comparing Builds](comparing-builds.md).

## Example: Matching content back to the source asset

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.
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.

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).
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, asset 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.

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.

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.

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.

Expand All @@ -235,5 +239,4 @@ Examples of alternative sources of build information:
* 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.
* The Editor log reports a lot of information during a build.
* Regular AssetBundle builds create [.manifest files](https://docs.unity3d.com/Manual/assetbundles-file-format.html), which contain information about the source assets and types.
* 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/com.unity.addressables@2.4/manual/BuildLayoutReport.html).

* 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_implicit_assets` tables.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tim - in the test project i was looking at there weren't any implicit assets, so i wasn't able to confirm that that addressables_build_implicit_assets table gets populated. Hopefully my guess based on the name is correct.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually addressables_build_data_from_other_assets. Not my favorite name, but that's coming from the name of the object so keeping it consistent with the data.

Given an explicit asset id and build number this will get you a list of implicit dependencies:

SELECT a.explicit_asset_id, b.id, b.asset_path, b.asset_path 
  FROM addressables_build_explicit_asset_internal_referenced_other_assets a, 
       addressables_build_data_from_other_assets b
 WHERE a.internal_referenced_other_asset_rid = b.id 
   AND a.build_id = b.build_id;
   AND a.explicit_asset_id = 5092 
   AND a.build_id = 3 

In my case my Pillar_Stone_Corner.prefab has an fbc, a material, and a texture in the other assets table.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed up this SQL in the Addressables documentation since it seemed useful.