Skip to content

Commit ba6190f

Browse files
Add more tests for dump
Also exclude external references when using dump with filtering (by type or object)
1 parent 6788ecd commit ba6190f

3 files changed

Lines changed: 191 additions & 17 deletions

File tree

TextDumper/TextDumperTool.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@ public int Dump(DumpOptions options)
3434
{
3535
var path = options.Path;
3636
var outputPath = options.OutputPath;
37-
var objectId = options.ObjectId;
38-
var typeFilter = options.TypeFilter;
3937
var toStdout = options.ToStdout;
4038

41-
if (string.IsNullOrWhiteSpace(typeFilter))
42-
typeFilter = null;
43-
4439
m_SkipLargeArrays = options.SkipLargeArrays;
4540

4641
try
@@ -85,7 +80,7 @@ public int Dump(DumpOptions options)
8580
var node2 = singleSerializedFile.Value;
8681
Console.Error.WriteLine($"Processing {node2.Path} {node2.Size} {node2.Flags}");
8782
m_Writer = Console.Out;
88-
OutputSerializedFile("/" + node2.Path, objectId, typeFilter);
83+
OutputSerializedFile("/" + node2.Path, options);
8984
m_Writer.Flush();
9085
}
9186
else
@@ -98,7 +93,7 @@ public int Dump(DumpOptions options)
9893
{
9994
using var writer = new StreamWriter(Path.Combine(outputPath, Path.GetFileName(node.Path) + ".txt"), false);
10095
m_Writer = writer;
101-
OutputSerializedFile("/" + node.Path, objectId, typeFilter);
96+
OutputSerializedFile("/" + node.Path, options);
10297
}
10398
}
10499
}
@@ -117,14 +112,14 @@ public int Dump(DumpOptions options)
117112
if (toStdout)
118113
{
119114
m_Writer = Console.Out;
120-
OutputSerializedFile(path, objectId, typeFilter);
115+
OutputSerializedFile(path, options);
121116
m_Writer.Flush();
122117
}
123118
else
124119
{
125120
using var writer = new StreamWriter(Path.Combine(outputPath, Path.GetFileName(path) + ".txt"), false);
126121
m_Writer = writer;
127-
OutputSerializedFile(path, objectId, typeFilter);
122+
OutputSerializedFile(path, options);
128123
}
129124
}
130125
catch (SerializedFileOpenException)
@@ -446,23 +441,33 @@ bool DumpManagedReferenceData(TypeTreeNode refTypeNode, TypeTreeNode referencedT
446441
return true;
447442
}
448443

449-
void OutputSerializedFile(string path, long objectId, string typeFilter)
444+
void OutputSerializedFile(string path, DumpOptions options)
450445
{
446+
var objectId = options.ObjectId;
447+
var typeFilter = string.IsNullOrWhiteSpace(options.TypeFilter) ? null : options.TypeFilter;
448+
bool filtered = objectId != 0 || typeFilter != null;
449+
451450
int filterTypeId = 0;
452451
bool filterByTypeId = typeFilter != null && int.TryParse(typeFilter, out filterTypeId);
453452

454453
using (m_Reader = new UnityFileReader(path, 64 * 1024 * 1024))
455454
using (m_SerializedFile = UnityFileSystem.OpenSerializedFile(path))
456455
{
457-
var i = 1;
458-
459-
m_Writer.WriteLine("External References");
460-
foreach (var extRef in m_SerializedFile.ExternalReferences)
456+
// External references provide context for PPtrs across the whole file. Skip them when a
457+
// filter is in use - the output is about a specific object, and `sf externalrefs` is the
458+
// dedicated command for listing external refs.
459+
if (!filtered)
461460
{
462-
m_Writer.WriteLine($"path({i}): \"{extRef.Path}\" GUID: {extRef.Guid} Type: {(int)extRef.Type}");
463-
++i;
461+
var i = 1;
462+
463+
m_Writer.WriteLine("External References");
464+
foreach (var extRef in m_SerializedFile.ExternalReferences)
465+
{
466+
m_Writer.WriteLine($"path({i}): \"{extRef.Path}\" GUID: {extRef.Guid} Type: {(int)extRef.Type}");
467+
++i;
468+
}
469+
m_Writer.WriteLine();
464470
}
465-
m_Writer.WriteLine();
466471

467472
bool dumpedObject = false;
468473
foreach (var obj in m_SerializedFile.Objects)

UnityDataTool.Tests/DumpTests.cs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace UnityDataTools.UnityDataTool.Tests;
7+
8+
#pragma warning disable NUnit2005, NUnit2006
9+
10+
public class DumpTests
11+
{
12+
private string m_TestDataFolder;
13+
private string m_SerializedFilePath;
14+
private string m_ResourceFilePath;
15+
16+
[OneTimeSetUp]
17+
public void OneTimeSetup()
18+
{
19+
m_TestDataFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data");
20+
m_SerializedFilePath = Path.Combine(m_TestDataFolder, "PlayerWithTypeTrees", "level0");
21+
m_ResourceFilePath = Path.Combine(m_TestDataFolder, "PlayerWithTypeTrees", "sharedassets0.assets.resS");
22+
}
23+
24+
[Test]
25+
public async Task Dump_Stdout_DefaultArgs_ContainsExternalReferences()
26+
{
27+
using var sw = new StringWriter();
28+
var currentOut = Console.Out;
29+
try
30+
{
31+
Console.SetOut(sw);
32+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", m_SerializedFilePath, "--stdout" }));
33+
}
34+
finally
35+
{
36+
Console.SetOut(currentOut);
37+
}
38+
39+
var output = sw.ToString();
40+
Assert.That(output, Does.Contain("External References"));
41+
}
42+
43+
[Test]
44+
public async Task Dump_Stdout_FilterByObjectId_DumpsGameObject()
45+
{
46+
using var sw = new StringWriter();
47+
var currentOut = Console.Out;
48+
try
49+
{
50+
Console.SetOut(sw);
51+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", m_SerializedFilePath, "--stdout", "-i", "1" }));
52+
}
53+
finally
54+
{
55+
Console.SetOut(currentOut);
56+
}
57+
58+
var output = sw.ToString();
59+
Assert.That(output, Does.Contain("ID: 1 (ClassID: 1) GameObject"));
60+
Assert.That(output, Does.Contain("m_Name (string) RefHolder"));
61+
}
62+
63+
[Test]
64+
public async Task Dump_Stdout_FilterByObjectId_DumpsRenderSettings()
65+
{
66+
using var sw = new StringWriter();
67+
var currentOut = Console.Out;
68+
try
69+
{
70+
Console.SetOut(sw);
71+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", m_SerializedFilePath, "--stdout", "-i", "3" }));
72+
}
73+
finally
74+
{
75+
Console.SetOut(currentOut);
76+
}
77+
78+
var output = sw.ToString();
79+
Assert.That(output, Does.Contain("(ClassID: 104)"));
80+
Assert.That(output, Does.Contain("m_FogColor (ColorRGBA)"));
81+
}
82+
83+
[Test]
84+
public async Task Dump_Stdout_FilterByObjectId_DoesNotIncludeExternalReferences()
85+
{
86+
using var sw = new StringWriter();
87+
var currentOut = Console.Out;
88+
try
89+
{
90+
Console.SetOut(sw);
91+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", m_SerializedFilePath, "--stdout", "-i", "1" }));
92+
}
93+
finally
94+
{
95+
Console.SetOut(currentOut);
96+
}
97+
98+
var output = sw.ToString();
99+
Assert.That(output, Does.Not.Contain("External References"));
100+
}
101+
102+
[Test]
103+
public async Task Dump_Stdout_FilterByObjectId_NotFound_PrintsNotFoundMessage()
104+
{
105+
using var sw = new StringWriter();
106+
var currentOut = Console.Out;
107+
try
108+
{
109+
Console.SetOut(sw);
110+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", m_SerializedFilePath, "--stdout", "-i", "99999999" }));
111+
}
112+
finally
113+
{
114+
Console.SetOut(currentOut);
115+
}
116+
117+
var output = sw.ToString();
118+
Assert.That(output, Does.Contain("Object with ID 99999999 not found."));
119+
}
120+
121+
[Test]
122+
public async Task Dump_Stdout_InvalidFileType_Fails()
123+
{
124+
using var swOut = new StringWriter();
125+
using var swErr = new StringWriter();
126+
var currentOut = Console.Out;
127+
var currentErr = Console.Error;
128+
try
129+
{
130+
Console.SetOut(swOut);
131+
Console.SetError(swErr);
132+
Assert.AreNotEqual(0, await Program.Main(new string[] { "dump", m_ResourceFilePath, "--stdout" }));
133+
}
134+
finally
135+
{
136+
Console.SetOut(currentOut);
137+
Console.SetError(currentErr);
138+
}
139+
140+
Assert.That(swErr.ToString(), Does.Contain("does not appear to be a valid Unity SerializedFile or Unity Archive"));
141+
}
142+
}

UnityDataTool.Tests/UnityDataToolAssetBundleTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,33 @@ public async Task DumpText_SkipLargeArrays_TextFileCreatedCorrectly(
148148
Assert.AreEqual(expected, content);
149149
}
150150

151+
[Test]
152+
public async Task DumpText_Stdout_WritesDumpToStdout()
153+
{
154+
var path = Path.Combine(Context.UnityDataFolder, "assetbundle");
155+
156+
using var sw = new StringWriter();
157+
var currentOut = Console.Out;
158+
try
159+
{
160+
Console.SetOut(sw);
161+
Assert.AreEqual(0, await Program.Main(new string[] { "dump", path, "--stdout" }));
162+
}
163+
finally
164+
{
165+
Console.SetOut(currentOut);
166+
}
167+
168+
var content = sw.ToString();
169+
var expected = File.ReadAllText(Path.Combine(Context.ExpectedDataFolder, "dump", "CAB-5d40f7cad7c871cf2ad2af19ac542994.txt"));
170+
171+
// Normalize line endings.
172+
content = Regex.Replace(content, @"\r\n|\n\r|\r", "\n");
173+
expected = Regex.Replace(expected, @"\r\n|\n\r|\r", "\n");
174+
175+
Assert.AreEqual(expected, content);
176+
}
177+
151178
[Test]
152179
public async Task DumpText_TypeFilterByName_OnlyMatchingObjectsDumped()
153180
{

0 commit comments

Comments
 (0)