Skip to content

Commit 3f0e3e8

Browse files
authored
Update README.md
1 parent 9b2c0eb commit 3f0e3e8

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,32 @@ Type -> Class
1515
Field -> Attribute -> Class Variable
1616
Method -> Class Function
1717
Object -> Class Instance
18+
19+
Serialization -> Writing an object bytes on disk in a way that's reversable.
20+
Deserialization -> Reading back a serialized object from disk.
1821
```
19-
Now with that in mind, assemblies are simply a collections of types, every type have fields, every field have a spacific data type, to deserialize objects that inherite from MonoBehavior type in unity game's asset files, we need info about the fields of those objects to be able parser them, normally but not always, when you try to read MonoBehavior objects from asset file, what you get only is the location and bounds of a portion of bytes in that asset file where this MonoBehavior object exists, this portion is a mix of all of field values, but you cannot make sense of those bytes unless you know what kind of fields exists in this object? what's their order? how much bytes you should read for every field? this is where type trees comes into play!
22+
Now with that in mind, assemblies are simply a collections of types, every type have fields, every field have a spacific data type, to deserialize objects that inherites from MonoBehavior type in unity game's asset files, we need info about the fields of those objects to be able parse them, normally but not always, when you try to read MonoBehavior objects from an asset file, what you get only is the location and bounds of a portion of bytes in that asset file where this MonoBehavior object exists, this portion is a mix of all of field values, but you cannot make sense of those bytes unless you know what kind of fields exists in this object? what's the order to read them in? how much bytes you should read for every field? here is where type trees comes into play.
2023

21-
For example, here a class want to serialize:-
24+
For example, here's a type, called "B", that we want to serialize an object based on it:-
2225
```
26+
class A
27+
{
28+
public int Number;
29+
public decimal Number2;
30+
}
2331
32+
class B : A
33+
{
34+
public bool Flag;
35+
}
2436
```
2537

38+
Type "B" itself have a single boolian field (1 byte), also it inherits from type "A" which have two fields, an integer field (8 bytes), and a decimal field (16 bytes), assuming I will take them in this order, we can serialize an object based on type "B" into a single byte stream where the first byte is the boolian, the 8 bytes after it is the integer and the next 16 bytes are the decimal.
39+
40+
Now imagine I have given you this stream of bytes, without any info on what type "B" fields are and what it inherites from, can you parse the bytes? abvoiusly not, you will need a type tree that tells you what type "B" fields are and what their datatype is to be able to parse those bytes back to the object.
41+
2642
# How Type Tree Looks Like?
27-
Now let's start, a type tree is a three layer neasted-map-array data structure (map of arrays of maps to be spacific), there's a type tree per assembly, a single type tree starts with a root map, that map have pairs of type names as keys, and node arrays as values, every node is a map of four pairs, here's an example of a single type tree root map pair:-
43+
A type tree is a three layer neasted-map-array data structure (map of arrays of maps to be spacific), there's a type tree per assembly, a single type tree starts with a root map, that map have pairs of type names as keys, and node arrays as values, every node is a map of four pairs, here's an example of a single type tree root map pair:-
2844
```
2945
"CustomSignalExtendReceiver": [
3046
{
@@ -109,3 +125,6 @@ Now let's start, a type tree is a three layer neasted-map-array data structure (
109125
```
110126

111127
# What's a Node?
128+
A node is a map of four pairs, it starts with "m_Type" for the name of the type the node represents, "m_Name" for the name of variable assigned with that type, "m_Metaflag" which is a flag used by Unity editor to assign spacific properties, there's only one important flag that changes how the bytes being parsed, lastly "m_Level" which spacifies the neasting level of the node relative to the nodes behind it, when you align all of those nodes based on their levels, you will see that the tree shape of that type is:-
129+
130+
![image](https://github.com/AhmedAhmedEG/UnityTypeTreeGenerator/assets/16827679/8d0b65e4-dc24-4023-9c22-9cc3f2b2a067)

0 commit comments

Comments
 (0)