66using UnityDataTools . Analyzer . SQLite . Handlers ;
77using UnityDataTools . FileSystem ;
88using UnityDataTools . FileSystem . TypeTreeReaders ;
9+ using UnityDataTools . Analyzer . Build ;
10+ using static System . Runtime . InteropServices . JavaScript . JSType ;
11+ using System . Xml . Linq ;
12+ using Newtonsoft . Json ;
913
1014namespace UnityDataTools . Analyzer . SQLite ;
1115
@@ -45,6 +49,9 @@ public class SQLiteWriter : IWriter
4549 private SqliteCommand m_AddObjectCommand = new SqliteCommand ( ) ;
4650 private SqliteCommand m_AddTypeCommand = new SqliteCommand ( ) ;
4751 private SqliteCommand m_InsertDepCommand = new SqliteCommand ( ) ;
52+ private SqliteCommand m_InsertBuild = new SqliteCommand ( ) ;
53+ private SqliteCommand m_ExplicitAsset = new SqliteCommand ( ) ;
54+ private SqliteCommand m_LastId = new SqliteCommand ( ) ;
4855 private SqliteTransaction m_CurrentTransaction = null ;
4956 public SQLiteWriter ( string databaseName , bool skipReferences )
5057 {
@@ -146,8 +153,45 @@ private void CreateSQLiteCommands()
146153 m_InsertDepCommand . CommandText = "INSERT INTO asset_dependencies(object, dependency) VALUES(@object, @dependency)" ;
147154 m_InsertDepCommand . Parameters . Add ( "@object" , SqliteType . Integer ) ;
148155 m_InsertDepCommand . Parameters . Add ( "@dependency" , SqliteType . Integer ) ;
149- }
150156
157+ m_InsertBuild = m_Database . CreateCommand ( ) ;
158+ m_InsertBuild . CommandText = "INSERT INTO build_layouts (name, build_target, start_time, duration, error, package_version, player_version, build_script, result_hash, type, unity_version) VALUES (@name, @build_target, @start_time, @duration, @error, @package_version, @player_version, @build_script, @result_hash, @type, @unity_version)" ;
159+ m_InsertBuild . Parameters . Add ( "@name" , SqliteType . Text ) ;
160+ m_InsertBuild . Parameters . Add ( "@build_target" , SqliteType . Integer ) ;
161+ m_InsertBuild . Parameters . Add ( "@start_time" , SqliteType . Integer ) ;
162+ m_InsertBuild . Parameters . Add ( "@duration" , SqliteType . Real ) ;
163+ m_InsertBuild . Parameters . Add ( "@error" , SqliteType . Text ) ;
164+ m_InsertBuild . Parameters . Add ( "@package_version" , SqliteType . Text ) ;
165+ m_InsertBuild . Parameters . Add ( "@player_version" , SqliteType . Text ) ;
166+ m_InsertBuild . Parameters . Add ( "@build_script" , SqliteType . Text ) ;
167+ m_InsertBuild . Parameters . Add ( "@result_hash" , SqliteType . Text ) ;
168+ m_InsertBuild . Parameters . Add ( "@type" , SqliteType . Integer ) ;
169+ m_InsertBuild . Parameters . Add ( "@unity_version" , SqliteType . Text ) ;
170+
171+ m_ExplicitAsset = m_Database . CreateCommand ( ) ;
172+ m_ExplicitAsset . CommandText =
173+ "INSERT INTO build_layout_explicit_assets (id, build_id, bundle, file, asset_hash, asset_path, addressable_name, externally_referenced_assets, group_guid, guid, internal_id, internal_referenced_explicit_assets, internal_referenced_other_assets, labels, streamed_size, serialized_size, main_asset_type) VALUES (@id, @build_id, @bundle, @file, @asset_hash, @asset_path, @addressable_name, @externally_referenced_assets, @group_guid, @guid, @internal_id, @internal_referenced_explicit_assets, @internal_referenced_other_assets, @labels, @streamed_size, @serialized_size, @main_asset_type)" ;
174+ m_ExplicitAsset . Parameters . Add ( "@id" , SqliteType . Integer ) ;
175+ m_ExplicitAsset . Parameters . Add ( "@build_id" , SqliteType . Integer ) ;
176+ m_ExplicitAsset . Parameters . Add ( "@bundle" , SqliteType . Integer ) ;
177+ m_ExplicitAsset . Parameters . Add ( "@file" , SqliteType . Integer ) ;
178+ m_ExplicitAsset . Parameters . Add ( "@asset_hash" , SqliteType . Text ) ;
179+ m_ExplicitAsset . Parameters . Add ( "@asset_path" , SqliteType . Text ) ;
180+ m_ExplicitAsset . Parameters . Add ( "@addressable_name" , SqliteType . Text ) ;
181+ m_ExplicitAsset . Parameters . Add ( "@externally_referenced_assets" , SqliteType . Text ) ; // JSONB type in SQLite uses TEXT
182+ m_ExplicitAsset . Parameters . Add ( "@group_guid" , SqliteType . Text ) ;
183+ m_ExplicitAsset . Parameters . Add ( "@guid" , SqliteType . Text ) ;
184+ m_ExplicitAsset . Parameters . Add ( "@internal_id" , SqliteType . Text ) ;
185+ m_ExplicitAsset . Parameters . Add ( "@internal_referenced_explicit_assets" , SqliteType . Text ) ; // JSONB type in SQLite uses TEXT
186+ m_ExplicitAsset . Parameters . Add ( "@internal_referenced_other_assets" , SqliteType . Text ) ; // JSONB type in SQLite uses TEXT
187+ m_ExplicitAsset . Parameters . Add ( "@labels" , SqliteType . Text ) ; // JSONB type in SQLite uses TEXT
188+ m_ExplicitAsset . Parameters . Add ( "@streamed_size" , SqliteType . Integer ) ;
189+ m_ExplicitAsset . Parameters . Add ( "@serialized_size" , SqliteType . Integer ) ;
190+ m_ExplicitAsset . Parameters . Add ( "@main_asset_type" , SqliteType . Integer ) ;
191+
192+ m_LastId = m_Database . CreateCommand ( ) ;
193+ m_LastId . CommandText = "SELECT last_insert_rowid()" ;
194+ }
151195 public void BeginAssetBundle ( string name , long size )
152196 {
153197 if ( m_CurrentAssetBundleId != - 1 )
@@ -172,6 +216,70 @@ public void EndAssetBundle()
172216 m_CurrentAssetBundleId = - 1 ;
173217 }
174218
219+ public void WriteBuildLayout ( string filename , BuildLayout buildLayout )
220+ {
221+ using var transaction = m_Database . BeginTransaction ( ) ;
222+ m_CurrentTransaction = transaction ;
223+
224+ try
225+ {
226+ m_InsertBuild . Transaction = transaction ;
227+ m_InsertBuild . Parameters [ "@name" ] . Value = Path . GetFileName ( filename ) ;
228+ m_InsertBuild . Parameters [ "@build_target" ] . Value = buildLayout . BuildTarget ;
229+ m_InsertBuild . Parameters [ "@start_time" ] . Value = buildLayout . BuildStartTime ;
230+ m_InsertBuild . Parameters [ "@duration" ] . Value = buildLayout . Duration ;
231+ m_InsertBuild . Parameters [ "@error" ] . Value = buildLayout . BuildError ;
232+ m_InsertBuild . Parameters [ "@package_version" ] . Value = buildLayout . PackageVersion ;
233+ m_InsertBuild . Parameters [ "@player_version" ] . Value = buildLayout . PlayerBuildVersion ;
234+ m_InsertBuild . Parameters [ "@build_script" ] . Value = buildLayout . BuildScript ;
235+ m_InsertBuild . Parameters [ "@result_hash" ] . Value = buildLayout . BuildResultHash ;
236+ m_InsertBuild . Parameters [ "@type" ] . Value = buildLayout . BuildType ;
237+ m_InsertBuild . Parameters [ "@unity_version" ] . Value = buildLayout . UnityVersion ;
238+ m_InsertBuild . ExecuteNonQuery ( ) ;
239+
240+ m_LastId . Transaction = transaction ;
241+ long buildId = ( long ) m_LastId . ExecuteScalar ( ) ;
242+ Console . WriteLine ( $ "Build ID: { buildId } ") ;
243+
244+ foreach ( var reference in buildLayout . references . RefIds )
245+ {
246+ switch ( reference . type . Class )
247+ {
248+ case "BuildLayout/ExplicitAsset" :
249+ m_ExplicitAsset . Transaction = transaction ;
250+ m_ExplicitAsset . Parameters [ "@id" ] . Value = reference . rid ;
251+ m_ExplicitAsset . Parameters [ "@build_id" ] . Value = buildId ;
252+ m_ExplicitAsset . Parameters [ "@bundle" ] . Value = reference . data . Bundle . rid ;
253+ m_ExplicitAsset . Parameters [ "@file" ] . Value = reference . data . File . rid ;
254+ m_ExplicitAsset . Parameters [ "@asset_hash" ] . Value = reference . data . AssetHash . Hash ;
255+ m_ExplicitAsset . Parameters [ "@asset_path" ] . Value = reference . data . AssetPath ;
256+ m_ExplicitAsset . Parameters [ "@addressable_name" ] . Value = reference . data . AddressableName ;
257+ m_ExplicitAsset . Parameters [ "@externally_referenced_assets" ] . Value = JsonConvert . SerializeObject ( reference . data . ExternallyReferencedAssets ) ?? "[]" ;
258+ m_ExplicitAsset . Parameters [ "@group_guid" ] . Value = reference . data . GroupGuid ;
259+ m_ExplicitAsset . Parameters [ "@guid" ] . Value = reference . data . Guid ;
260+ m_ExplicitAsset . Parameters [ "@internal_id" ] . Value = reference . data . InternalId ;
261+ m_ExplicitAsset . Parameters [ "@internal_referenced_explicit_assets" ] . Value = JsonConvert . SerializeObject ( reference . data . InternalReferencedExplicitAssets ) ?? "[]" ;
262+ m_ExplicitAsset . Parameters [ "@internal_referenced_other_assets" ] . Value = JsonConvert . SerializeObject ( reference . data . InternalReferencedOtherAssets ) ?? "[]" ;
263+ m_ExplicitAsset . Parameters [ "@labels" ] . Value = JsonConvert . SerializeObject ( reference . data . Labels ) ?? "[]" ;
264+ m_ExplicitAsset . Parameters [ "@main_asset_type" ] . Value = reference . data . MainAssetType ;
265+ m_ExplicitAsset . Parameters [ "@serialized_size" ] . Value = reference . data . SerializedSize ;
266+ m_ExplicitAsset . Parameters [ "@streamed_size" ] . Value = reference . data . StreamedSize ;
267+ m_ExplicitAsset . ExecuteNonQuery ( ) ;
268+ break ;
269+ }
270+ }
271+
272+ // do the stuff
273+ transaction . Commit ( ) ;
274+ }
275+ catch ( Exception e )
276+ {
277+ transaction . Rollback ( ) ;
278+ throw ;
279+ }
280+ }
281+
282+
175283 public void WriteSerializedFile ( string relativePath , string fullPath , string containingFolder )
176284 {
177285 using var sf = UnityFileSystem . OpenSerializedFile ( fullPath ) ;
0 commit comments