11using System ;
22using System . IO ;
33using System . Text ;
4+ using UnityDataTools . Analyzer . Util ;
45using UnityDataTools . FileSystem ;
56
67namespace UnityDataTools . TextDumper ;
@@ -19,10 +20,15 @@ public int Dump(string path, string outputPath, bool skipLargeArrays, long objec
1920
2021 try
2122 {
22- try
23+ if ( ! File . Exists ( path ) )
2324 {
24- // Try the input as an unity archive, e.g. an AssetBundle
25- // In that case we dump each serialized file contained inside it.
25+ Console . WriteLine ( $ "Error: File not found: { path } ") ;
26+ return 1 ;
27+ }
28+
29+ if ( ArchiveDetector . IsUnityArchive ( path ) )
30+ {
31+ // The input is a Unity archive (e.g. AssetBundle); dump each serialized file inside it.
2632 using var archive = UnityFileSystem . MountArchive ( path , "/" ) ;
2733 foreach ( var node in archive . Nodes )
2834 {
@@ -37,27 +43,70 @@ public int Dump(string path, string outputPath, bool skipLargeArrays, long objec
3743 }
3844 }
3945 }
40- catch ( NotSupportedException )
46+ else if ( YamlSerializedFileDetector . IsYamlSerializedFile ( path ) )
4147 {
42- // Try as SerializedFile
43- using ( m_Writer = new StreamWriter ( Path . Combine ( outputPath , Path . GetFileName ( path ) + ".txt" ) , false ) )
48+ Console . WriteLine ( "Error: The file is a YAML-format SerializedFile, which is not supported." ) ;
49+ Console . WriteLine ( "UnityDataTool only supports binary-format SerializedFiles." ) ;
50+ return 1 ;
51+ }
52+ else if ( SerializedFileDetector . TryDetectSerializedFile ( path , out var fileInfo ) )
53+ {
54+ // The input is a binary SerializedFile; dump it directly.
55+ try
4456 {
45- OutputSerializedFile ( path , objectId ) ;
57+ using ( m_Writer = new StreamWriter ( Path . Combine ( outputPath , Path . GetFileName ( path ) + ".txt" ) , false ) )
58+ {
59+ OutputSerializedFile ( path , objectId ) ;
60+ }
61+ }
62+ catch ( Exception e )
63+ {
64+ PrintExceptionIfUseful ( e ) ;
65+
66+ // Post-analysis: check if the failure is due to missing TypeTrees.
67+ if ( SerializedFileDetector . TryParseMetadata ( path , fileInfo , out var metadata , out _ ) )
68+ {
69+ if ( ! metadata . EnableTypeTree )
70+ {
71+ Console . WriteLine ( ) ;
72+ Console . WriteLine ( "Note: This file does not have TypeTrees and can only be opened if all the types it" ) ;
73+ Console . WriteLine ( "uses exactly match the types available inside the build of UnityFileSystemApi being used." ) ;
74+ }
75+ }
76+
77+ return 1 ;
4678 }
4779 }
80+ else
81+ {
82+ Console . WriteLine ( "Error: The file does not appear to be a valid Unity SerializedFile or Unity Archive." ) ;
83+ Console . WriteLine ( $ "File: { path } ") ;
84+ return 1 ;
85+ }
4886 }
4987 catch ( Exception e )
5088 {
51- Console . WriteLine ( "Error!" ) ;
52- Console . Write ( $ "{ e . GetType ( ) } : ") ;
53- Console . WriteLine ( e . Message ) ;
54- Console . WriteLine ( e . StackTrace ) ;
89+ PrintExceptionIfUseful ( e ) ;
5590 return 1 ;
5691 }
5792
5893 return 0 ;
5994 }
6095
96+ static void PrintExceptionIfUseful ( Exception e )
97+ {
98+ // Avoid printing noisy messages if it is just the generic "we don't know why it failed"
99+ // fallback in UnityFileSystem.HandleErrors
100+ if ( e . GetType ( ) . ToString ( ) != "System.Exception" )
101+ Console . Write ( $ "Error: { e . GetType ( ) } : ") ;
102+ if ( ! e . Message . Contains ( "Unknown error" ) )
103+ Console . WriteLine ( e . Message ) ;
104+ var stackTrace = e . StackTrace ;
105+ if ( ! stackTrace . Contains ( "UnityFileSystem.HandleErrors" ) )
106+ Console . WriteLine ( e . StackTrace ) ;
107+ }
108+
109+
61110 void RecursiveDump ( TypeTreeNode node , ref long offset , int level , int arrayIndex = - 1 )
62111 {
63112 bool skipChildren = false ;
0 commit comments