@@ -10,59 +10,53 @@ public static class SerializedFileCommands
1010{
1111 public static int HandleExternalRefs ( FileInfo filename , OutputFormat format )
1212 {
13- return OpenSerializedFileAndProcess ( filename , sf =>
13+ if ( ! ValidateSerializedFile ( filename . FullName , out _ ) )
14+ return 1 ;
15+
16+ try
1417 {
18+ using var sf = UnityFileSystem . OpenSerializedFile ( filename . FullName ) ;
1519 if ( format == OutputFormat . Json )
1620 OutputExternalRefsJson ( sf ) ;
1721 else
1822 OutputExternalRefsText ( sf ) ;
19- } ) ;
23+ return 0 ;
24+ }
25+ catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
26+ {
27+ Console . Error . WriteLine ( $ "Error opening SerializedFile: { filename . FullName } ") ;
28+ Console . Error . WriteLine ( err . Message ) ;
29+ return 1 ;
30+ }
2031 }
2132
2233 public static int HandleObjectList ( FileInfo filename , OutputFormat format )
2334 {
24- return OpenSerializedFileAndProcess ( filename , sf =>
35+ if ( ! ValidateSerializedFile ( filename . FullName , out _ ) )
36+ return 1 ;
37+
38+ try
2539 {
40+ using var sf = UnityFileSystem . OpenSerializedFile ( filename . FullName ) ;
2641 if ( format == OutputFormat . Json )
2742 OutputObjectListJson ( sf ) ;
2843 else
2944 OutputObjectListText ( sf ) ;
30- } ) ;
31- }
32-
33- public static int HandleHeader ( FileInfo filename , OutputFormat format )
34- {
35- string filePath = filename . FullName ;
36-
37- // Check if file exists
38- if ( ! File . Exists ( filePath ) )
39- {
40- Console . Error . WriteLine ( $ "Error: File not found: { filePath } ") ;
41- return 1 ;
45+ return 0 ;
4246 }
43-
44- // Detect if it's an archive (not supported for header command)
45- if ( ArchiveDetector . IsUnityArchive ( filePath ) )
47+ catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
4648 {
47- Console . Error . WriteLine ( $ "Error: The file is an AssetBundle or other Unity Archive, not a SerializedFile.") ;
48- Console . Error . WriteLine ( $ "File: { filePath } ") ;
49- Console . Error . WriteLine ( ) ;
50- Console . Error . WriteLine ( "Unity Archives do not have a SerializedFile header." ) ;
51- Console . Error . WriteLine ( "Extract the archive first to access SerializedFiles inside:" ) ;
52- Console . Error . WriteLine ( $ " UnityDataTool archive extract \" { filePath } \" -o <output-directory>") ;
49+ Console . Error . WriteLine ( $ "Error opening SerializedFile: { filename . FullName } ") ;
50+ Console . Error . WriteLine ( err . Message ) ;
5351 return 1 ;
5452 }
53+ }
5554
56- // Try to detect the SerializedFile header
57- bool isSerializedFile = SerializedFileDetector . TryDetectSerializedFile ( filePath , out var fileInfo ) ;
58- if ( ! isSerializedFile || fileInfo == null )
59- {
60- Console . Error . WriteLine ( $ "Error: The file does not appear to be a valid Unity SerializedFile.") ;
61- Console . Error . WriteLine ( $ "File: { filePath } ") ;
55+ public static int HandleHeader ( FileInfo filename , OutputFormat format )
56+ {
57+ if ( ! ValidateSerializedFile ( filename . FullName , out var fileInfo ) )
6258 return 1 ;
63- }
6459
65- // Output the header information
6660 if ( format == OutputFormat . Json )
6761 OutputHeaderJson ( fileInfo ) ;
6862 else
@@ -72,56 +66,42 @@ public static int HandleHeader(FileInfo filename, OutputFormat format)
7266 }
7367
7468 /// <summary>
75- /// Opens a SerializedFile and processes it with the given action.
76- /// Provides helpful error messages if the file is not a valid SerializedFile.
69+ /// Validates that a file is a SerializedFile and provides helpful error messages if not.
7770 /// </summary>
78- private static int OpenSerializedFileAndProcess ( FileInfo filename , Action < SerializedFile > processAction )
71+ /// <param name="filePath">Path to the file to validate</param>
72+ /// <param name="fileInfo">SerializedFile header information if valid, null otherwise</param>
73+ /// <returns>True if valid SerializedFile, false otherwise</returns>
74+ private static bool ValidateSerializedFile ( string filePath , out SerializedFileInfo fileInfo )
7975 {
80- string filePath = filename . FullName ;
76+ fileInfo = null ;
8177
82- // Check if file exists (should already be validated by System.CommandLine, but be defensive)
8378 if ( ! File . Exists ( filePath ) )
8479 {
8580 Console . Error . WriteLine ( $ "Error: File not found: { filePath } ") ;
86- return 1 ;
81+ return false ;
8782 }
8883
89- // Common misuse would be to try to use the command with an AssetBundle
90- // so give an instructive error message for that case.
9184 if ( ArchiveDetector . IsUnityArchive ( filePath ) )
9285 {
9386 Console . Error . WriteLine ( $ "Error: The file is an AssetBundle or other Unity Archive, not a SerializedFile.") ;
9487 Console . Error . WriteLine ( $ "File: { filePath } ") ;
9588 Console . Error . WriteLine ( ) ;
96- Console . Error . WriteLine ( "Unity Archives can contain SerializedFiles inside them." ) ;
89+ Console . Error . WriteLine ( "Unity Archives contain SerializedFiles inside them." ) ;
9790 Console . Error . WriteLine ( "To access the SerializedFiles, first extract the archive using:" ) ;
9891 Console . Error . WriteLine ( $ " UnityDataTool archive extract \" { filePath } \" -o <output-directory>") ;
9992 Console . Error . WriteLine ( ) ;
10093 Console . Error . WriteLine ( "Then you can run serialized-file commands on the extracted files." ) ;
101- return 1 ;
94+ return false ;
10295 }
10396
104- bool isSerializedFile = SerializedFileDetector . TryDetectSerializedFile ( filePath , out var _ ) ;
105- if ( ! isSerializedFile )
97+ if ( ! SerializedFileDetector . TryDetectSerializedFile ( filePath , out fileInfo ) )
10698 {
10799 Console . Error . WriteLine ( $ "Error: The file does not appear to be a valid Unity SerializedFile.") ;
108100 Console . Error . WriteLine ( $ "File: { filePath } ") ;
109- return 1 ;
101+ return false ;
110102 }
111103
112- // Try to open and process the SerializedFile
113- try
114- {
115- using var sf = UnityFileSystem . OpenSerializedFile ( filePath ) ;
116- processAction ( sf ) ;
117- return 0 ;
118- }
119- catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
120- {
121- Console . Error . WriteLine ( $ "Error opening SerializedFile: { filePath } ") ;
122- Console . Error . WriteLine ( err . Message ) ;
123- return 1 ;
124- }
104+ return true ;
125105 }
126106
127107 private static void OutputExternalRefsText ( SerializedFile sf )
@@ -237,4 +217,3 @@ private static void OutputHeaderJson(SerializedFileInfo info)
237217 Console . WriteLine ( json ) ;
238218 }
239219}
240-
0 commit comments