Skip to content

Commit b9757fe

Browse files
Move dump options into member variables
1 parent 7336d5e commit b9757fe

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

TextDumper/TextDumperTool.cs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ namespace UnityDataTools.TextDumper;
99
public class TextDumperTool
1010
{
1111
StringBuilder m_StringBuilder = new StringBuilder(1024);
12-
bool m_SkipLargeArrays;
12+
DumpOptions m_Options;
13+
string m_TypeFilter; // m_Options.TypeFilter normalized: null when blank/unset, otherwise the user-provided string
14+
bool m_FilterByTypeId; // true when m_TypeFilter parses as a ClassID (numeric)
15+
int m_FilterTypeId; // valid only when m_FilterByTypeId is true
1316
UnityFileReader m_Reader;
1417
SerializedFile m_SerializedFile;
1518
TextWriter m_Writer;
@@ -32,31 +35,33 @@ public class DumpOptions
3235

3336
public int Dump(DumpOptions options)
3437
{
35-
m_SkipLargeArrays = options.SkipLargeArrays;
38+
m_Options = options;
39+
m_TypeFilter = string.IsNullOrWhiteSpace(m_Options.TypeFilter) ? null : m_Options.TypeFilter;
40+
m_FilterByTypeId = m_TypeFilter != null && int.TryParse(m_TypeFilter, out m_FilterTypeId);
3641

3742
try
3843
{
39-
if (!File.Exists(options.Path))
44+
if (!File.Exists(m_Options.Path))
4045
{
41-
Console.Error.WriteLine($"Error: File not found: {options.Path}");
46+
Console.Error.WriteLine($"Error: File not found: {m_Options.Path}");
4247
return 1;
4348
}
4449

45-
if (ArchiveDetector.IsUnityArchive(options.Path))
46-
return DumpArchive(options);
50+
if (ArchiveDetector.IsUnityArchive(m_Options.Path))
51+
return DumpArchive();
4752

48-
if (YamlSerializedFileDetector.IsYamlSerializedFile(options.Path))
53+
if (YamlSerializedFileDetector.IsYamlSerializedFile(m_Options.Path))
4954
{
5055
Console.Error.WriteLine("Error: The file is a YAML-format SerializedFile, which is not supported.");
5156
Console.Error.WriteLine("UnityDataTool only supports binary-format SerializedFiles.");
5257
return 1;
5358
}
5459

55-
if (SerializedFileDetector.TryDetectSerializedFile(options.Path, out _))
56-
return DumpSerializedFile(options);
60+
if (SerializedFileDetector.TryDetectSerializedFile(m_Options.Path, out _))
61+
return DumpSerializedFile();
5762

5863
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}");
64+
Console.Error.WriteLine($"File: {m_Options.Path}");
6065
return 1;
6166
}
6267
catch (Exception e)
@@ -67,26 +72,26 @@ public int Dump(DumpOptions options)
6772
}
6873
}
6974

70-
int DumpSerializedFile(DumpOptions options)
75+
int DumpSerializedFile()
7176
{
7277
try
7378
{
74-
if (options.ToStdout)
79+
if (m_Options.ToStdout)
7580
{
7681
m_Writer = Console.Out;
77-
OutputSerializedFile(options.Path, options);
82+
OutputSerializedFile(m_Options.Path);
7883
m_Writer.Flush();
7984
}
8085
else
8186
{
82-
using var writer = new StreamWriter(Path.Combine(options.OutputPath, Path.GetFileName(options.Path) + ".txt"), false);
87+
using var writer = new StreamWriter(Path.Combine(m_Options.OutputPath, Path.GetFileName(m_Options.Path) + ".txt"), false);
8388
m_Writer = writer;
84-
OutputSerializedFile(options.Path, options);
89+
OutputSerializedFile(m_Options.Path);
8590
}
8691
}
8792
catch (SerializedFileOpenException)
8893
{
89-
var hint = SerializedFileDetector.GetOpenFailureHint(options.Path);
94+
var hint = SerializedFileDetector.GetOpenFailureHint(m_Options.Path);
9095
if (hint != null)
9196
{
9297
Console.Error.WriteLine();
@@ -100,11 +105,11 @@ int DumpSerializedFile(DumpOptions options)
100105

101106
// For convenience we also support directly dumping serialized files that are inside an archive,
102107
// so that it's not necessary to use `archive extract` if you only want to see values from the object serialization.
103-
int DumpArchive(DumpOptions options)
108+
int DumpArchive()
104109
{
105-
using var archive = UnityFileSystem.MountArchive(options.Path, "/");
110+
using var archive = UnityFileSystem.MountArchive(m_Options.Path, "/");
106111

107-
if (options.ToStdout)
112+
if (m_Options.ToStdout)
108113
{
109114
ArchiveNode? singleSerializedFile = null;
110115
int serializedFileCount = 0;
@@ -133,7 +138,7 @@ int DumpArchive(DumpOptions options)
133138
var node2 = singleSerializedFile.Value;
134139
Console.Error.WriteLine($"Processing {node2.Path} {node2.Size} {node2.Flags}");
135140
m_Writer = Console.Out;
136-
OutputSerializedFile("/" + node2.Path, options);
141+
OutputSerializedFile("/" + node2.Path);
137142
m_Writer.Flush();
138143
}
139144
else
@@ -144,24 +149,20 @@ int DumpArchive(DumpOptions options)
144149

145150
if (node.Flags.HasFlag(ArchiveNodeFlags.SerializedFile))
146151
{
147-
using var writer = new StreamWriter(Path.Combine(options.OutputPath, Path.GetFileName(node.Path) + ".txt"), false);
152+
using var writer = new StreamWriter(Path.Combine(m_Options.OutputPath, Path.GetFileName(node.Path) + ".txt"), false);
148153
m_Writer = writer;
149-
OutputSerializedFile("/" + node.Path, options);
154+
OutputSerializedFile("/" + node.Path);
150155
}
151156
}
152157
}
153158

154159
return 0;
155160
}
156161

157-
void OutputSerializedFile(string path, DumpOptions options)
162+
void OutputSerializedFile(string path)
158163
{
159-
var objectId = options.ObjectId;
160-
var typeFilter = string.IsNullOrWhiteSpace(options.TypeFilter) ? null : options.TypeFilter;
161-
bool filtered = objectId != 0 || typeFilter != null;
162-
163-
int filterTypeId = 0;
164-
bool filterByTypeId = typeFilter != null && int.TryParse(typeFilter, out filterTypeId);
164+
var objectId = m_Options.ObjectId;
165+
bool filtered = objectId != 0 || m_TypeFilter != null;
165166

166167
using (m_Reader = new UnityFileReader(path, 64 * 1024 * 1024))
167168
using (m_SerializedFile = UnityFileSystem.OpenSerializedFile(path))
@@ -190,11 +191,11 @@ void OutputSerializedFile(string path, DumpOptions options)
190191

191192
var root = m_SerializedFile.GetTypeTreeRoot(obj.Id);
192193

193-
if (typeFilter != null)
194+
if (m_TypeFilter != null)
194195
{
195-
if (filterByTypeId)
196+
if (m_FilterByTypeId)
196197
{
197-
if (obj.TypeId != filterTypeId)
198+
if (obj.TypeId != m_FilterTypeId)
198199
continue;
199200
}
200201
else
@@ -204,7 +205,7 @@ void OutputSerializedFile(string path, DumpOptions options)
204205
// fall back to the TypeTree root node for script types.
205206
if (typeName == obj.TypeId.ToString())
206207
typeName = root.Type;
207-
if (!string.Equals(typeName, typeFilter, StringComparison.OrdinalIgnoreCase))
208+
if (!string.Equals(typeName, m_TypeFilter, StringComparison.OrdinalIgnoreCase))
208209
continue;
209210
}
210211
}
@@ -217,12 +218,12 @@ void OutputSerializedFile(string path, DumpOptions options)
217218
dumpedObject = true;
218219
}
219220

220-
if ((objectId != 0 || typeFilter != null) && !dumpedObject)
221+
if (filtered && !dumpedObject)
221222
{
222223
if (objectId != 0)
223224
m_Writer.WriteLine($"Object with ID {objectId} not found.");
224225
else
225-
m_Writer.WriteLine($"No objects found matching type \"{typeFilter}\".");
226+
m_Writer.WriteLine($"No objects found matching type \"{m_TypeFilter}\".");
226227
}
227228
}
228229
}
@@ -347,7 +348,7 @@ void DumpArray(TypeTreeNode node, ref long offset, int level)
347348
{
348349
m_StringBuilder.Append(' ', (level + 1) * 2);
349350

350-
if (arraySize > 256 && m_SkipLargeArrays)
351+
if (arraySize > 256 && m_Options.SkipLargeArrays)
351352
{
352353
m_StringBuilder.Append("<Skipped>");
353354
offset += dataNode.Size * arraySize;

0 commit comments

Comments
 (0)