Skip to content

Commit 0ac49b4

Browse files
Better Error reporting. Rename Hash field
1 parent 9c86668 commit 0ac49b4

5 files changed

Lines changed: 82 additions & 20 deletions

File tree

TextDumper/TextDumper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20+
<ProjectReference Include="..\Analyzer\Analyzer.csproj" />
2021
<ProjectReference Include="..\UnityFileSystem\UnityFileSystem.csproj" />
2122
</ItemGroup>
2223

TextDumper/TextDumperTool.cs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4+
using UnityDataTools.Analyzer.Util;
45
using UnityDataTools.FileSystem;
56

67
namespace 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;

UnityDataTool.Tests/WebBundleSupportTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public void Teardown()
4040
public void IsWebBundle_True()
4141
{
4242
var webBundlePath = Path.Combine(m_TestDataFolder, "WebBundles", "HelloWorld.data");
43-
Assert.IsTrue(Archive.IsWebBundle(new FileInfo(webBundlePath)));
43+
Assert.IsTrue(Archive.IsWebBundle(webBundlePath));
4444
}
4545

4646
[Test]
4747
public void IsWebBundle_False()
4848
{
4949
var nonWebBundlePath = Path.Combine(m_TestDataFolder, "WebBundles", "NotAWebBundle.txt");
50-
Assert.IsFalse(Archive.IsWebBundle(new FileInfo(nonWebBundlePath)));
50+
Assert.IsFalse(Archive.IsWebBundle(nonWebBundlePath));
5151
}
5252

5353
[Test]

UnityDataTool/Archive.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO.Compression;
55
using System.Linq;
66
using System.Text;
7+
using UnityDataTools.Analyzer.Util;
78
using UnityDataTools.FileSystem;
89

910
namespace UnityDataTools.UnityDataTool;
@@ -16,14 +17,20 @@ public static int HandleExtract(FileInfo filename, DirectoryInfo outputFolder)
1617
{
1718
try
1819
{
19-
if (IsWebBundle(filename))
20+
var path = filename.ToString();
21+
if (IsWebBundle(path))
2022
{
2123
ExtractWebBundle(filename, outputFolder);
2224
}
23-
else
25+
else if (ArchiveDetector.IsUnityArchive(path))
2426
{
2527
ExtractAssetBundle(filename, outputFolder);
2628
}
29+
else
30+
{
31+
Console.Error.WriteLine("File is not a supported archive type.");
32+
return 1;
33+
}
2734
}
2835
catch (Exception err) when (
2936
err is NotSupportedException
@@ -40,14 +47,20 @@ public static int HandleList(FileInfo filename)
4047
{
4148
try
4249
{
43-
if (IsWebBundle(filename))
50+
var path = filename.ToString();
51+
if (IsWebBundle(path))
4452
{
4553
ListWebBundle(filename);
4654
}
47-
else
55+
else if (ArchiveDetector.IsUnityArchive(path))
4856
{
4957
ListAssetBundle(filename);
5058
}
59+
else
60+
{
61+
Console.Error.WriteLine("File is not a supported archive type.");
62+
return 1;
63+
}
5164
}
5265
catch (Exception err) when (
5366
err is NotSupportedException
@@ -62,9 +75,8 @@ err is NotSupportedException
6275
}
6376

6477

65-
public static bool IsWebBundle(FileInfo filename)
78+
public static bool IsWebBundle(string path)
6679
{
67-
var path = filename.ToString();
6880
return (
6981
path.EndsWith(".data")
7082
|| path.EndsWith(".data.gz")

UnityDataTool/SerializedFileCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private static object TypeTreeInfoToJson(TypeTreeInfo info)
286286
isStrippedType = info.IsStrippedType,
287287
scriptTypeIndex = info.ScriptTypeIndex,
288288
scriptID = info.ScriptID.ToString(),
289-
oldTypeHash = info.OldTypeHash.ToString(),
289+
typeTreeStructureHash = info.TypeTreeStructureHash.ToString(),
290290
typeTreeContentHash = info.TypeTreeContentHash.ToString(),
291291
typeTreeSerializedSize = info.TypeTreeSerializedSize,
292292
inlineTypeTree = info.InlineTypeTree,

0 commit comments

Comments
 (0)