Skip to content

Commit 849de7a

Browse files
Refactor order of methods
The OutputSerializedFile() method made sense earlier in the file so that the implementation is going approximately from higher level down to lower level details Sorry for the code churn but this aid readability.
1 parent f4c9d81 commit 849de7a

1 file changed

Lines changed: 72 additions & 73 deletions

File tree

TextDumper/TextDumperTool.cs

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,78 @@ int DumpArchive(DumpOptions options)
154154
return 0;
155155
}
156156

157+
void OutputSerializedFile(string path, DumpOptions options)
158+
{
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);
165+
166+
using (m_Reader = new UnityFileReader(path, 64 * 1024 * 1024))
167+
using (m_SerializedFile = UnityFileSystem.OpenSerializedFile(path))
168+
{
169+
// External references provide context for PPtrs across the whole file. Skip them when a
170+
// filter is in use - the output is about a specific object, and `sf externalrefs` is the
171+
// dedicated command for listing external refs.
172+
if (!filtered)
173+
{
174+
var i = 1;
175+
176+
m_Writer.WriteLine("External References");
177+
foreach (var extRef in m_SerializedFile.ExternalReferences)
178+
{
179+
m_Writer.WriteLine($"path({i}): \"{extRef.Path}\" GUID: {extRef.Guid} Type: {(int)extRef.Type}");
180+
++i;
181+
}
182+
m_Writer.WriteLine();
183+
}
184+
185+
bool dumpedObject = false;
186+
foreach (var obj in m_SerializedFile.Objects)
187+
{
188+
if (objectId != 0 && obj.Id != objectId)
189+
continue;
190+
191+
var root = m_SerializedFile.GetTypeTreeRoot(obj.Id);
192+
193+
if (typeFilter != null)
194+
{
195+
if (filterByTypeId)
196+
{
197+
if (obj.TypeId != filterTypeId)
198+
continue;
199+
}
200+
else
201+
{
202+
var typeName = TypeIdRegistry.GetTypeName(obj.TypeId);
203+
// GetTypeName returns the id as a string when the type is unknown;
204+
// fall back to the TypeTree root node for script types.
205+
if (typeName == obj.TypeId.ToString())
206+
typeName = root.Type;
207+
if (!string.Equals(typeName, typeFilter, StringComparison.OrdinalIgnoreCase))
208+
continue;
209+
}
210+
}
211+
212+
var offset = obj.Offset;
213+
214+
m_Writer.Write($"ID: {obj.Id} (ClassID: {obj.TypeId}) ");
215+
RecursiveDump(root, ref offset, 0);
216+
m_Writer.WriteLine();
217+
dumpedObject = true;
218+
}
219+
220+
if ((objectId != 0 || typeFilter != null) && !dumpedObject)
221+
{
222+
if (objectId != 0)
223+
m_Writer.WriteLine($"Object with ID {objectId} not found.");
224+
else
225+
m_Writer.WriteLine($"No objects found matching type \"{typeFilter}\".");
226+
}
227+
}
228+
}
157229

158230
void RecursiveDump(TypeTreeNode node, ref long offset, int level, int arrayIndex = -1)
159231
{
@@ -439,79 +511,6 @@ bool DumpManagedReferenceData(TypeTreeNode refTypeNode, TypeTreeNode referencedT
439511
return true;
440512
}
441513

442-
void OutputSerializedFile(string path, DumpOptions options)
443-
{
444-
var objectId = options.ObjectId;
445-
var typeFilter = string.IsNullOrWhiteSpace(options.TypeFilter) ? null : options.TypeFilter;
446-
bool filtered = objectId != 0 || typeFilter != null;
447-
448-
int filterTypeId = 0;
449-
bool filterByTypeId = typeFilter != null && int.TryParse(typeFilter, out filterTypeId);
450-
451-
using (m_Reader = new UnityFileReader(path, 64 * 1024 * 1024))
452-
using (m_SerializedFile = UnityFileSystem.OpenSerializedFile(path))
453-
{
454-
// External references provide context for PPtrs across the whole file. Skip them when a
455-
// filter is in use - the output is about a specific object, and `sf externalrefs` is the
456-
// dedicated command for listing external refs.
457-
if (!filtered)
458-
{
459-
var i = 1;
460-
461-
m_Writer.WriteLine("External References");
462-
foreach (var extRef in m_SerializedFile.ExternalReferences)
463-
{
464-
m_Writer.WriteLine($"path({i}): \"{extRef.Path}\" GUID: {extRef.Guid} Type: {(int)extRef.Type}");
465-
++i;
466-
}
467-
m_Writer.WriteLine();
468-
}
469-
470-
bool dumpedObject = false;
471-
foreach (var obj in m_SerializedFile.Objects)
472-
{
473-
if (objectId != 0 && obj.Id != objectId)
474-
continue;
475-
476-
var root = m_SerializedFile.GetTypeTreeRoot(obj.Id);
477-
478-
if (typeFilter != null)
479-
{
480-
if (filterByTypeId)
481-
{
482-
if (obj.TypeId != filterTypeId)
483-
continue;
484-
}
485-
else
486-
{
487-
var typeName = TypeIdRegistry.GetTypeName(obj.TypeId);
488-
// GetTypeName returns the id as a string when the type is unknown;
489-
// fall back to the TypeTree root node for script types.
490-
if (typeName == obj.TypeId.ToString())
491-
typeName = root.Type;
492-
if (!string.Equals(typeName, typeFilter, StringComparison.OrdinalIgnoreCase))
493-
continue;
494-
}
495-
}
496-
497-
var offset = obj.Offset;
498-
499-
m_Writer.Write($"ID: {obj.Id} (ClassID: {obj.TypeId}) ");
500-
RecursiveDump(root, ref offset, 0);
501-
m_Writer.WriteLine();
502-
dumpedObject = true;
503-
}
504-
505-
if ((objectId != 0 || typeFilter != null) && !dumpedObject)
506-
{
507-
if (objectId != 0)
508-
m_Writer.WriteLine($"Object with ID {objectId} not found.");
509-
else
510-
m_Writer.WriteLine($"No objects found matching type \"{typeFilter}\".");
511-
}
512-
}
513-
}
514-
515514
string ReadValue(TypeTreeNode node, long offset)
516515
{
517516
switch (Type.GetTypeCode(node.CSharpType))

0 commit comments

Comments
 (0)