77
88namespace UnityDataTools . Analyzer ;
99
10- // This class is used to extract all the PPtrs in a serialized object. It executes a callback whenever a PPtr is found.
11- // It provides a string representing the property path of the property (e.g. "m_MyObject.m_MyArray[2].m_PPtrProperty").
10+ // Walks the TypeTree of a serialized object to do two things in a single pass:
11+ // 1. Extract every PPtr (object reference). A callback is executed for each one, receiving the
12+ // property path that leads to it (e.g. "m_MyObject.m_MyArray[2].m_PPtrProperty").
13+ // 2. Accumulate a CRC32 over the object's serialized bytes, including the content of external
14+ // streams (texture/mesh/audio data stored in companion .resS/.resource files). This CRC is a
15+ // content fingerprint used to detect whether two objects are identical.
16+ // CRC computation can be disabled (skipCrc) while still extracting references.
1217public class PPtrAndCrcProcessor : IDisposable
1318{
1419 public delegate int CallbackDelegate ( long objectId , int fileId , long pathId , string propertyPath , string propertyType ) ;
@@ -19,20 +24,33 @@ public class PPtrAndCrcProcessor : IDisposable
1924 // Matched case-insensitively since the scheme casing is not guaranteed.
2025 private const string ContentAddressedPrefix = "cah:/" ;
2126
22- private SerializedFile m_SerializedFile ;
23- private UnityFileReader m_Reader ;
24- private long m_Offset ;
25- private long m_ObjectId ;
26- private uint m_Crc32 ;
27- private string m_Folder ;
28- private bool m_SkipCrc ;
29- private StringBuilder m_StringBuilder = new ( ) ;
30- private byte [ ] m_pptrBytes = new byte [ 4 ] ;
31-
32- private CallbackDelegate m_Callback ;
27+ // Configuration shared across all objects, set once in the constructor.
28+ private SerializedFile m_SerializedFile ; // file being analyzed; used to resolve referenced managed type trees
29+ private UnityFileReader m_Reader ; // reader over the serialized file holding the object data
30+ private string m_Folder ; // directory of the serialized file; used to find companion resource files
31+ private bool m_SkipCrc ; // when true, skip CRC computation (references are still extracted)
32+ private CallbackDelegate m_Callback ; // invoked for each PPtr; returns the referenced object's id
3333
34+ // Readers for external resource (.resS/.resource) files, opened on demand, reused across
35+ // objects, and disposed in Dispose().
3436 private Dictionary < string , UnityFileReader > m_resourceReaders = new ( ) ;
3537
38+ // Reusable scratch buffers, kept as fields to avoid allocating per object/property.
39+ private StringBuilder m_StringBuilder = new ( ) ; // builds the current property path during the walk
40+ private byte [ ] m_pptrBytes = new byte [ 4 ] ; // holds a referenced object id while feeding it to the CRC
41+
42+ // State for the object currently being processed, (re)initialized by each Process() call.
43+ private long m_Offset ; // current read position within m_Reader
44+ private long m_ObjectId ; // analyzer id of the object being processed, passed to the callback
45+ private uint m_Crc32 ; // CRC accumulated so far for this object
46+
47+ // serializedFile: the file whose objects are analyzed (used to resolve referenced managed types).
48+ // reader: reader over that file's bytes; Process() walks each object through it.
49+ // folder: directory containing the serialized file; companion .resS/.resource files are
50+ // looked up here when a non-content-addressed external stream contributes to the CRC.
51+ // skipCrc: when true, the tree is still walked to emit references but no CRC is computed.
52+ // callback: called for every PPtr found; its return value (the referenced object's id) is
53+ // folded into the CRC.
3654 public PPtrAndCrcProcessor ( SerializedFile serializedFile , UnityFileReader reader , string folder ,
3755 bool skipCrc , CallbackDelegate callback )
3856 {
@@ -119,6 +137,9 @@ private void AppendStreamCrc(long offset, int size, string path)
119137 m_Crc32 = resourceFile . ComputeCRC ( offset , size , m_Crc32 ) ;
120138 }
121139
140+ // Walks the serialized object rooted at `node`, whose data starts at `offset` in the reader,
141+ // emitting every PPtr through the callback. Returns a CRC32 fingerprint of the object's content
142+ // (0 when CRC is disabled). `objectId` is the analyzer id of this object, forwarded to the callback.
122143 public uint Process ( long objectId , long offset , TypeTreeNode node )
123144 {
124145 m_Offset = offset ;
0 commit comments