11using System ;
22using System . IO ;
33using System . Text . Json ;
4+ using UnityDataTools . Analyzer . Util ;
45using UnityDataTools . FileSystem ;
56
67namespace UnityDataTools . UnityDataTool ;
@@ -9,44 +10,77 @@ public static class SerializedFileCommands
910{
1011 public static int HandleExternalRefs ( FileInfo filename , OutputFormat format )
1112 {
12- try
13+ return OpenSerializedFileAndProcess ( filename , sf =>
1314 {
14- using var sf = UnityFileSystem . OpenSerializedFile ( filename . FullName ) ;
15-
1615 if ( format == OutputFormat . Json )
1716 OutputExternalRefsJson ( sf ) ;
1817 else
1918 OutputExternalRefsText ( sf ) ;
20- }
21- catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
22- {
23- Console . Error . WriteLine ( $ "Error opening serialized file: { filename . FullName } ") ;
24- Console . Error . WriteLine ( err . Message ) ;
25- return 1 ;
26- }
27-
28- return 0 ;
19+ } ) ;
2920 }
3021
3122 public static int HandleObjectList ( FileInfo filename , OutputFormat format )
3223 {
33- try
24+ return OpenSerializedFileAndProcess ( filename , sf =>
3425 {
35- using var sf = UnityFileSystem . OpenSerializedFile ( filename . FullName ) ;
36-
3726 if ( format == OutputFormat . Json )
3827 OutputObjectListJson ( sf ) ;
3928 else
4029 OutputObjectListText ( sf ) ;
30+ } ) ;
31+ }
32+
33+ /// <summary>
34+ /// Opens a SerializedFile and processes it with the given action.
35+ /// Provides helpful error messages if the file is not a valid SerializedFile.
36+ /// </summary>
37+ private static int OpenSerializedFileAndProcess ( FileInfo filename , Action < SerializedFile > processAction )
38+ {
39+ string filePath = filename . FullName ;
40+
41+ // Check if file exists (should already be validated by System.CommandLine, but be defensive)
42+ if ( ! File . Exists ( filePath ) )
43+ {
44+ Console . Error . WriteLine ( $ "Error: File not found: { filePath } ") ;
45+ return 1 ;
46+ }
47+
48+ // Common misuse would be to try to use the command with an AssetBundle
49+ // so give an instructive error message for that case.
50+ if ( ArchiveDetector . IsUnityArchive ( filePath ) )
51+ {
52+ Console . Error . WriteLine ( $ "Error: The file is an AssetBundle or other Unity Archive, not a SerializedFile.") ;
53+ Console . Error . WriteLine ( $ "File: { filePath } ") ;
54+ Console . Error . WriteLine ( ) ;
55+ Console . Error . WriteLine ( "Unity Archives can contain SerializedFiles inside them." ) ;
56+ Console . Error . WriteLine ( "To access the SerializedFiles, first extract the archive using:" ) ;
57+ Console . Error . WriteLine ( $ " UnityDataTool archive extract \" { filePath } \" -o <output-directory>") ;
58+ Console . Error . WriteLine ( ) ;
59+ Console . Error . WriteLine ( "Then you can run serialized-file commands on the extracted files." ) ;
60+ return 1 ;
61+ }
62+
63+ bool isSerializedFile = SerializedFileDetector . TryDetectSerializedFile ( filePath , out var _ ) ;
64+ if ( ! isSerializedFile )
65+ {
66+ Console . Error . WriteLine ( $ "Error: The file does not appear to be a valid Unity SerializedFile.") ;
67+ Console . Error . WriteLine ( $ "File: { filePath } ") ;
68+ return 1 ;
69+ }
70+
71+ // Try to open and process the SerializedFile
72+ try
73+ {
74+ using var sf = UnityFileSystem . OpenSerializedFile ( filePath ) ;
75+ processAction ( sf ) ;
76+ return 0 ;
4177 }
4278 catch ( Exception err ) when ( err is NotSupportedException || err is FileFormatException )
4379 {
44- Console . Error . WriteLine ( $ "Error opening serialized file : { filename . FullName } ") ;
80+ Console . Error . WriteLine ( $ "Error opening SerializedFile : { filePath } ") ;
4581 Console . Error . WriteLine ( err . Message ) ;
4682 return 1 ;
4783 }
48-
49- return 0 ;
5084 }
5185
5286 private static void OutputExternalRefsText ( SerializedFile sf )
0 commit comments