@@ -11,39 +11,43 @@ public static class SerializedFileCommands
1111{
1212 public static int HandleExternalRefs ( FileInfo filename , OutputFormat format )
1313 {
14- if ( ! ValidateSerializedFile ( filename . FullName , out _ ) )
14+ // External references are read directly from the parsed metadata rather than via UnityFileSystemApi.
15+ //
16+ // Advantages: works for any modern SerializedFile (version >= 19), including Player builds
17+ // that were compiled without TypeTrees — files that UnityFileSystemApi cannot open at all.
18+ //
19+ // Trade-offs: Files older than version 19 (Unity 2019.1) are not supported by the metadata parser.
20+ //
21+ // These trade-offs are minor compared to the benefit of handling the common no-TypeTree case,
22+ // so there is no need to keep the UnityFileSystemApi code path.
23+ if ( ! ValidateSerializedFile ( filename . FullName , out var fileInfo ) )
1524 return 1 ;
1625
17- try
26+ if ( ! SerializedFileDetector . TryParseMetadata ( filename . FullName , fileInfo , out var metadata , out var errorMessage ) )
1827 {
19- using var sf = UnityFileSystem . OpenSerializedFile ( filename . FullName ) ;
20- if ( format == OutputFormat . Json )
21- OutputExternalRefsJson ( sf ) ;
22- else
23- OutputExternalRefsText ( sf ) ;
24- return 0 ;
28+ Console . Error . WriteLine ( $ "Error: Failed to parse external references for: { filename . FullName } ") ;
29+ Console . Error . WriteLine ( errorMessage ) ;
30+ return 1 ;
2531 }
26- catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
32+
33+ if ( metadata . ExternalReferences == null )
2734 {
28- Console . Error . WriteLine ( $ "Error opening SerializedFile: { filename . FullName } ") ;
29- Console . Error . WriteLine ( err . Message ) ;
35+ Console . Error . WriteLine ( $ "Error: External references could not be parsed for: { filename . FullName } ") ;
3036 return 1 ;
3137 }
38+
39+ if ( format == OutputFormat . Json )
40+ OutputExternalRefsJson ( metadata . ExternalReferences ) ;
41+ else
42+ OutputExternalRefsText ( metadata . ExternalReferences ) ;
43+
44+ return 0 ;
3245 }
3346
3447 public static int HandleObjectList ( FileInfo filename , OutputFormat format )
3548 {
3649 // The object list is read directly from the parsed metadata rather than via UnityFileSystemApi.
37- //
38- // Advantages: works for any modern SerializedFile (version >= 19), including Player builds
39- // that were compiled without TypeTrees — files that UnityFileSystemApi cannot open at all.
40- //
41- // Trade-offs: type names come from TypeIdRegistry rather than the file's embedded TypeTree,
42- // so uncommon types not covered by the registry are displayed as a numeric TypeId. Files
43- // older than version 19 (Unity 2019.1) are not supported by the metadata parser.
44- //
45- // These trade-offs are minor compared to the benefit of handling the common no-TypeTree case,
46- // so there is no need to keep the UnityFileSystemApi code path.
50+ // (See comment in HandleExternalRefs() for the reasons for doing it that way)
4751 if ( ! ValidateSerializedFile ( filename . FullName , out var fileInfo ) )
4852 return 1 ;
4953
@@ -149,24 +153,21 @@ private static bool ValidateSerializedFile(string filePath, out SerializedFileIn
149153 return true ;
150154 }
151155
152- private static void OutputExternalRefsText ( SerializedFile sf )
156+ private static void OutputExternalRefsText ( ExternalReference [ ] refs )
153157 {
154- var refs = sf . ExternalReferences ;
155-
156- for ( int i = 0 ; i < refs . Count ; i ++ )
158+ for ( int i = 0 ; i < refs . Length ; i ++ )
157159 {
158160 var extRef = refs [ i ] ;
159161 var displayValue = ! string . IsNullOrEmpty ( extRef . Path ) ? extRef . Path : extRef . Guid ;
160162 Console . WriteLine ( $ "Index: { i + 1 } , Path: { displayValue } ") ;
161163 }
162164 }
163165
164- private static void OutputExternalRefsJson ( SerializedFile sf )
166+ private static void OutputExternalRefsJson ( ExternalReference [ ] refs )
165167 {
166- var refs = sf . ExternalReferences ;
167- var jsonArray = new object [ refs . Count ] ;
168+ var jsonArray = new object [ refs . Length ] ;
168169
169- for ( int i = 0 ; i < refs . Count ; i ++ )
170+ for ( int i = 0 ; i < refs . Length ; i ++ )
170171 {
171172 var extRef = refs [ i ] ;
172173 jsonArray [ i ] = new
0 commit comments