@@ -32,125 +32,123 @@ public class DumpOptions
3232
3333 public int Dump ( DumpOptions options )
3434 {
35- var path = options . Path ;
36- var outputPath = options . OutputPath ;
37- var toStdout = options . ToStdout ;
38-
3935 m_SkipLargeArrays = options . SkipLargeArrays ;
4036
4137 try
4238 {
43- if ( ! File . Exists ( path ) )
39+ if ( ! File . Exists ( options . Path ) )
4440 {
45- Console . Error . WriteLine ( $ "Error: File not found: { path } ") ;
41+ Console . Error . WriteLine ( $ "Error: File not found: { options . Path } ") ;
4642 return 1 ;
4743 }
4844
49- if ( ArchiveDetector . IsUnityArchive ( path ) )
50- {
51- // The input is a Unity archive (e.g. AssetBundle); dump each serialized file inside it.
52- using var archive = UnityFileSystem . MountArchive ( path , "/" ) ;
45+ if ( ArchiveDetector . IsUnityArchive ( options . Path ) )
46+ return DumpArchive ( options ) ;
5347
54- if ( toStdout )
55- {
56- ArchiveNode ? singleSerializedFile = null ;
57- int serializedFileCount = 0 ;
58- foreach ( var node in archive . Nodes )
59- {
60- if ( node . Flags . HasFlag ( ArchiveNodeFlags . SerializedFile ) )
61- {
62- ++ serializedFileCount ;
63- singleSerializedFile ??= node ;
64- }
65- }
48+ if ( YamlSerializedFileDetector . IsYamlSerializedFile ( options . Path ) )
49+ {
50+ Console . Error . WriteLine ( "Error: The file is a YAML-format SerializedFile, which is not supported." ) ;
51+ Console . Error . WriteLine ( "UnityDataTool only supports binary-format SerializedFiles." ) ;
52+ return 1 ;
53+ }
6654
67- if ( serializedFileCount == 0 )
68- {
69- Console . Error . WriteLine ( "Error: Archive contains no SerializedFiles." ) ;
70- return 1 ;
71- }
55+ if ( SerializedFileDetector . TryDetectSerializedFile ( options . Path , out _ ) )
56+ return DumpSerializedFile ( options ) ;
7257
73- if ( serializedFileCount > 1 )
74- {
75- Console . Error . WriteLine ( $ "Error: --stdout cannot be used with an archive containing multiple SerializedFiles ({ serializedFileCount } found).") ;
76- Console . Error . WriteLine ( "Extract the archive first, or pass an individual SerializedFile as input." ) ;
77- return 1 ;
78- }
58+ Console . Error . WriteLine ( "Error: The file does not appear to be a valid Unity SerializedFile or Unity Archive." ) ;
59+ Console . Error . WriteLine ( $ "File: { options . Path } ") ;
60+ return 1 ;
61+ }
62+ catch ( Exception e )
63+ {
64+ Console . Error . WriteLine ( $ "Error: { e . GetType ( ) } : { e . Message } ") ;
65+ Console . Error . WriteLine ( e . StackTrace ) ;
66+ return 1 ;
67+ }
68+ }
7969
80- var node2 = singleSerializedFile . Value ;
81- Console . Error . WriteLine ( $ "Processing { node2 . Path } { node2 . Size } { node2 . Flags } ") ;
82- m_Writer = Console . Out ;
83- OutputSerializedFile ( "/" + node2 . Path , options ) ;
84- m_Writer . Flush ( ) ;
85- }
86- else
87- {
88- foreach ( var node in archive . Nodes )
89- {
90- Console . WriteLine ( $ "Processing { node . Path } { node . Size } { node . Flags } ") ;
91-
92- if ( node . Flags . HasFlag ( ArchiveNodeFlags . SerializedFile ) )
93- {
94- using var writer = new StreamWriter ( Path . Combine ( outputPath , Path . GetFileName ( node . Path ) + ".txt" ) , false ) ;
95- m_Writer = writer ;
96- OutputSerializedFile ( "/" + node . Path , options ) ;
97- }
98- }
99- }
70+ int DumpSerializedFile ( DumpOptions options )
71+ {
72+ try
73+ {
74+ if ( options . ToStdout )
75+ {
76+ m_Writer = Console . Out ;
77+ OutputSerializedFile ( options . Path , options ) ;
78+ m_Writer . Flush ( ) ;
10079 }
101- else if ( YamlSerializedFileDetector . IsYamlSerializedFile ( path ) )
80+ else
10281 {
103- Console . Error . WriteLine ( "Error: The file is a YAML-format SerializedFile, which is not supported." ) ;
104- Console . Error . WriteLine ( "UnityDataTool only supports binary-format SerializedFiles." ) ;
105- return 1 ;
82+ using var writer = new StreamWriter ( Path . Combine ( options . OutputPath , Path . GetFileName ( options . Path ) + ".txt" ) , false ) ;
83+ m_Writer = writer ;
84+ OutputSerializedFile ( options . Path , options ) ;
85+ }
86+ }
87+ catch ( SerializedFileOpenException )
88+ {
89+ var hint = SerializedFileDetector . GetOpenFailureHint ( options . Path ) ;
90+ if ( hint != null )
91+ {
92+ Console . Error . WriteLine ( ) ;
93+ Console . Error . WriteLine ( hint ) ;
10694 }
107- else if ( SerializedFileDetector . TryDetectSerializedFile ( path , out _ ) )
95+ return 1 ;
96+ }
97+
98+ return 0 ;
99+ }
100+
101+ // For convenience we also support directly dumping serialized files that are inside an archive,
102+ // so that its not necessary to use `archive extract` if you only want to see values from the object serialization.
103+ int DumpArchive ( DumpOptions options )
104+ {
105+ using var archive = UnityFileSystem . MountArchive ( options . Path , "/" ) ;
106+
107+ if ( options . ToStdout )
108+ {
109+ ArchiveNode ? singleSerializedFile = null ;
110+ int serializedFileCount = 0 ;
111+ foreach ( var node in archive . Nodes )
108112 {
109- // The input is a binary SerializedFile; dump it directly.
110- try
111- {
112- if ( toStdout )
113- {
114- m_Writer = Console . Out ;
115- OutputSerializedFile ( path , options ) ;
116- m_Writer . Flush ( ) ;
117- }
118- else
119- {
120- using var writer = new StreamWriter ( Path . Combine ( outputPath , Path . GetFileName ( path ) + ".txt" ) , false ) ;
121- m_Writer = writer ;
122- OutputSerializedFile ( path , options ) ;
123- }
124- }
125- catch ( SerializedFileOpenException )
113+ if ( node . Flags . HasFlag ( ArchiveNodeFlags . SerializedFile ) )
126114 {
127- var hint = SerializedFileDetector . GetOpenFailureHint ( path ) ;
128- if ( hint != null )
129- {
130- Console . Error . WriteLine ( ) ;
131- Console . Error . WriteLine ( hint ) ;
132- }
133- return 1 ;
134- }
135- catch ( Exception e )
136- {
137- Console . Error . WriteLine ( $ "Error: { e . GetType ( ) } : { e . Message } ") ;
138- Console . Error . WriteLine ( e . StackTrace ) ;
139- return 1 ;
115+ ++ serializedFileCount ;
116+ singleSerializedFile ??= node ;
140117 }
141118 }
142- else
119+
120+ if ( serializedFileCount == 0 )
121+ {
122+ Console . Error . WriteLine ( "Error: Archive contains no SerializedFiles." ) ;
123+ return 1 ;
124+ }
125+
126+ if ( serializedFileCount > 1 )
143127 {
144- Console . Error . WriteLine ( "Error: The file does not appear to be a valid Unity SerializedFile or Unity Archive ." ) ;
145- Console . Error . WriteLine ( $ "File: { path } ") ;
128+ Console . Error . WriteLine ( $ "Error: --stdout cannot be used with an archive containing multiple SerializedFiles ( { serializedFileCount } found) .") ;
129+ Console . Error . WriteLine ( "Extract the archive first, or pass an individual SerializedFile as input. ") ;
146130 return 1 ;
147131 }
132+
133+ var node2 = singleSerializedFile . Value ;
134+ Console . Error . WriteLine ( $ "Processing { node2 . Path } { node2 . Size } { node2 . Flags } ") ;
135+ m_Writer = Console . Out ;
136+ OutputSerializedFile ( "/" + node2 . Path , options ) ;
137+ m_Writer . Flush ( ) ;
148138 }
149- catch ( Exception e )
139+ else
150140 {
151- Console . Error . WriteLine ( $ "Error: { e . GetType ( ) } : { e . Message } ") ;
152- Console . Error . WriteLine ( e . StackTrace ) ;
153- return 1 ;
141+ foreach ( var node in archive . Nodes )
142+ {
143+ Console . WriteLine ( $ "Processing { node . Path } { node . Size } { node . Flags } ") ;
144+
145+ if ( node . Flags . HasFlag ( ArchiveNodeFlags . SerializedFile ) )
146+ {
147+ using var writer = new StreamWriter ( Path . Combine ( options . OutputPath , Path . GetFileName ( node . Path ) + ".txt" ) , false ) ;
148+ m_Writer = writer ;
149+ OutputSerializedFile ( "/" + node . Path , options ) ;
150+ }
151+ }
154152 }
155153
156154 return 0 ;
0 commit comments