Skip to content

Commit 41aa3ab

Browse files
committed
0.1.1
1 parent 9ce618c commit 41aa3ab

8 files changed

Lines changed: 157 additions & 123 deletions

File tree

UnityAsset.NET/Asset.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,6 @@ public Asset(AssetFileInfo info, DataBuffer db)
1414
{
1515
Info = info;
1616
RawData = db;
17-
var nodeDataList = new List<NodeData>();
18-
for (int i = 0; i < info.Type.Nodes.Count; i++)
19-
{
20-
var typeTreeNode = info.Type.Nodes[i];
21-
var node = new NodeData(typeTreeNode);
22-
nodeDataList.Add(node);
23-
}
24-
var parent = nodeDataList[0];
25-
for (int i = 1; i < nodeDataList.Count; i++)
26-
{
27-
while (nodeDataList[i].Level <= parent.Level)
28-
{
29-
parent = parent.Parent;
30-
}
31-
nodeDataList[i].Parent = parent;
32-
parent.Children ??= new ();
33-
parent.Children.Add(nodeDataList[i]);
34-
parent = nodeDataList[i];
35-
}
36-
for (int i = 0; i < nodeDataList.Count; i++)
37-
{
38-
var node = nodeDataList[i];
39-
node.Value = NodeData.ReadValue(nodeDataList, db, ref i);
40-
}
41-
NodeData = nodeDataList[0];
17+
NodeData = new NodeData(db, info.Type.Nodes, info.Type.Nodes[0]);
4218
}
4319
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using UnityAsset.NET.Files.SerializedFiles;
2+
3+
namespace UnityAsset.NET.Extensions;
4+
5+
public static class ListTypeTreeNodeExtensions
6+
{
7+
public static TypeTreeNode? Parent(this List<TypeTreeNode> nodes, TypeTreeNode current)
8+
{
9+
if (current.Index == 0)
10+
return null;
11+
var nodesSpan = nodes.AsReadOnlySpan();
12+
for (int i = (int)current.Index - 1; i >= 0; i--)
13+
{
14+
if (nodesSpan[i].Level < current.Level)
15+
return nodesSpan[i];
16+
}
17+
return null;
18+
}
19+
20+
public static List<TypeTreeNode>? Children(this List<TypeTreeNode> nodes, TypeTreeNode current)
21+
{
22+
var nodesSpan = nodes.AsReadOnlySpan();
23+
if ((int)current.Index + 1 >= nodesSpan.Length || nodesSpan[(int)current.Index + 1].Level <= current.Level)
24+
return null;
25+
var children = new List<TypeTreeNode>();
26+
for (int i = (int)current.Index + 1; i < nodesSpan.Length; i++)
27+
{
28+
if (nodesSpan[i].Level <= current.Level)
29+
break;
30+
if (nodesSpan[i].Level == current.Level + 1)
31+
children.Add(nodesSpan[i]);
32+
}
33+
return children;
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using UnityAsset.NET.Files.SerializedFiles;
2+
3+
namespace UnityAsset.NET.Extensions;
4+
5+
public static class TypeTreeNodeExtensions
6+
{
7+
public static bool RequiresAlign(this TypeTreeNode node) => (node.MetaFlags & 0x4000) != 0;
8+
9+
public static TypeTreeNode? Parent(this TypeTreeNode current, List<TypeTreeNode> nodes)
10+
{
11+
if (current.Index == 0)
12+
return null;
13+
var nodesSpan = nodes.AsReadOnlySpan();
14+
for (int i = (int)current.Index - 1; i >= 0; i--)
15+
{
16+
if (nodesSpan[i].Level < current.Level)
17+
return nodesSpan[i];
18+
}
19+
return null;
20+
}
21+
22+
public static List<TypeTreeNode>? Children(this TypeTreeNode current, List<TypeTreeNode> nodes)
23+
{
24+
var nodesSpan = nodes.AsReadOnlySpan();
25+
if ((int)current.Index + 1 >= nodesSpan.Length || nodesSpan[(int)current.Index + 1].Level <= current.Level)
26+
return null;
27+
var children = new List<TypeTreeNode>();
28+
for (int i = (int)current.Index + 1; i < nodesSpan.Length; i++)
29+
{
30+
if (nodesSpan[i].Level <= current.Level)
31+
break;
32+
if (nodesSpan[i].Level == current.Level + 1)
33+
children.Add(nodesSpan[i]);
34+
}
35+
return children;
36+
}
37+
}

UnityAsset.NET/Files/SerializedFiles/SerializedType.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ public sealed class SerializedType
1616
public Hash128 TypeHash;
1717
public bool IsRefType;
1818
public List<TypeTreeNode>? Nodes;
19-
public TypeTreeNode? TypeTree;
2019
public byte[]? StringBufferBytes;
2120
public int[]? TypeDependencies;
2221
public SerializedTypeReference? TypeReference;
2322

24-
public SerializedType(Int32 typeId, bool isStrippedType, Int16 scriptTypeIndex, Hash128? scriptIdHash, Hash128 typeHash, bool isRefType, List<TypeTreeNode>? nodes, byte[]? stringBufferBytes, TypeTreeNode? typeTree, int[]? typeDependencies, SerializedTypeReference? typeReference)
23+
public SerializedType(Int32 typeId, bool isStrippedType, Int16 scriptTypeIndex, Hash128? scriptIdHash, Hash128 typeHash, bool isRefType, List<TypeTreeNode>? nodes, byte[]? stringBufferBytes, int[]? typeDependencies, SerializedTypeReference? typeReference)
2524
{
2625
TypeID = typeId;
2726
IsStrippedType = isStrippedType;
@@ -30,7 +29,6 @@ public SerializedType(Int32 typeId, bool isStrippedType, Int16 scriptTypeIndex,
3029
TypeHash = typeHash;
3130
IsRefType = isRefType;
3231
Nodes = nodes;
33-
TypeTree = typeTree;
3432
StringBufferBytes = stringBufferBytes;
3533
TypeDependencies = typeDependencies;
3634
TypeReference = typeReference;
@@ -67,17 +65,6 @@ public static SerializedType Parse(DataBuffer db, SerializedFileFormatVersion ve
6765
if (nodes[0].Level != 0)
6866
throw new Exception(
6967
$"The first node of TypeTreeNodes should have a level of 0 but gets {nodes[0].Level}");
70-
var parent = nodes[0];
71-
for (int i = 1; i < typeTreeNodeCount; i++)
72-
{
73-
while (nodes[i].Level <= parent.Level)
74-
parent = parent.Parent;
75-
nodes[i].Parent = parent;
76-
parent.Children ??= new ();
77-
parent.Children.Add(nodes[i]);
78-
parent = nodes[i];
79-
}
80-
typeTree = nodes[0];
8168
if (version >= StoresTypeDependencies)
8269
{
8370
if (isRefType)
@@ -86,7 +73,7 @@ public static SerializedType Parse(DataBuffer db, SerializedFileFormatVersion ve
8673
typeDependencies = db.ReadIntArray(db.ReadInt32());
8774
}
8875
}
89-
return new SerializedType(typeID, isStrippedType, scriptTypeIndex, scriptIdHash, typeHash, isRefType, nodes, stringBufferBytes, typeTree, typeDependencies, typeReference);
76+
return new SerializedType(typeID, isStrippedType, scriptTypeIndex, scriptIdHash, typeHash, isRefType, nodes, stringBufferBytes, typeDependencies, typeReference);
9077
}
9178

9279
private static string ReadString(DataBuffer db, uint value)

UnityAsset.NET/Files/SerializedFiles/TypeTreeNode.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public class TypeTreeNode
1717
public UInt64 RefTypeHash;
1818
public string Type;
1919
public string Name;
20-
public TypeTreeNode? Parent;
21-
public List<TypeTreeNode>? Children;
2220

2321
public TypeTreeNode(UInt16 vesion, byte level, TypeTreeNodeFlags typeFlags,
2422
UInt32 typeStringOffset, UInt32 nameStringOffset, Int32 byteSize, UInt32 index, UInt32 metaFlags,

UnityAsset.NET/IO/DataBuffer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public int WriteNullTerminatedString(string value)
363363
return byteCount + 1;
364364
}
365365

366-
public string ReadAlignedString()
366+
public string ReadSizedString()
367367
{
368368
var result = "";
369369
var length = ReadInt32();
@@ -372,11 +372,10 @@ public string ReadAlignedString()
372372
var stringData = ReadBytes(length);
373373
result = Encoding.UTF8.GetString(stringData);
374374
}
375-
Align(4);
376375
return result;
377376
}
378377

379-
public int WriteAlignedString(string value)
378+
public int WriteSizedString(string value)
380379
{
381380
var byteCount = Encoding.UTF8.GetByteCount(value);
382381
EnsureCapacity(byteCount + 8);

0 commit comments

Comments
 (0)