Skip to content

Commit eb49686

Browse files
committed
0.0.4-beta.1
1 parent 9227568 commit eb49686

7 files changed

Lines changed: 165 additions & 19 deletions

File tree

UnityAsset.NET/Compression/LZ4.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ public static int EncodeFast(ReadOnlySpan<byte> source, Span<byte> target)
106106
}
107107
int literalLen = (int) (s - anchor);
108108
int matchLen = 4;
109-
while (s + matchLen < sourceEnd && *(s + matchLen) == *(sourceOffset + matchLen))
109+
while (s + matchLen < sourceEnd)
110110
{
111+
if (*(s + matchLen) != *(sourceOffset + matchLen))
112+
break;
111113
matchLen++;
112114
}
113115
int token = Math.Min(literalLen, 15) << 4 | Math.Min(matchLen - 4, 15);

UnityAsset.NET/Hash128.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Text;
2+
using UnityAsset.NET.IO;
3+
4+
namespace UnityAsset.NET;
5+
6+
public struct Hash128
7+
{
8+
public byte[] data; //16 bytes
9+
10+
public Hash128(byte[] data)
11+
{
12+
this.data = data;
13+
}
14+
public Hash128(AssetReader reader)
15+
{
16+
data = reader.ReadBytes(16);
17+
}
18+
19+
public bool IsZero()
20+
{
21+
if (data == null)
22+
return true;
23+
24+
for (int i = 0; i < data.Length; i++)
25+
{
26+
if (data[i] != 0)
27+
return false;
28+
}
29+
30+
return true;
31+
}
32+
33+
public override string ToString()
34+
{
35+
StringBuilder hex = new StringBuilder(data.Length * 2);
36+
37+
foreach (byte b in data)
38+
{
39+
hex.AppendFormat("{0:x2}", b);
40+
}
41+
42+
return hex.ToString();
43+
}
44+
45+
public static Hash128 NewBlankHash()
46+
{
47+
return new Hash128() { data = new byte[16] };
48+
}
49+
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
using UnityAsset.NET.Enums;
1+
using System.Text;
2+
using UnityAsset.NET.Enums;
23
using UnityAsset.NET.IO;
34

45
namespace UnityAsset.NET.SerializedFile;
56

67
public sealed class SerializedFileHeader
78
{
89
public uint MetadataSize;
9-
public long FileSize;
10+
public ulong FileSize;
1011
public SerializedFileFormatVersion Version;
11-
public long DataOffset;
12-
public byte Endianess;
12+
public ulong DataOffset;
13+
public bool Endianess;
1314

1415
public SerializedFileHeader(AssetReader reader)
1516
{
@@ -20,25 +21,31 @@ public SerializedFileHeader(AssetReader reader)
2021

2122
if (Version < SerializedFileFormatVersion.Unknown_9)
2223
{
23-
throw new Exception("Unsupported version.");
24+
throw new Exception($"Unsupported version: {Version}");
2425
}
2526

26-
Endianess = reader.ReadByte();
27+
Endianess = reader.ReadBoolean();
2728
reader.ReadBytes(3);// unused bytes
2829

2930
if (Version >= SerializedFileFormatVersion.LargeFilesSupport)
3031
{
3132
MetadataSize = reader.ReadUInt32();
32-
FileSize = reader.ReadUInt32();
33-
DataOffset = reader.ReadUInt32();
33+
FileSize = reader.ReadUInt64();
34+
DataOffset = reader.ReadUInt64();
3435
reader.ReadInt64(); // unknown
3536
}
3637

37-
reader.BigEndian = Endianess == 1;
38+
reader.BigEndian = Endianess;
3839
}
3940

4041
public override string ToString()
4142
{
42-
return $"MetadataSize: 0x{MetadataSize:X8} | FileSize: 0x{FileSize:X8} | Version: {Version} | DataOffset: 0x{DataOffset:X8} | Endianness: {(EndianType)Endianess}";
43+
StringBuilder sb = new StringBuilder();
44+
sb.AppendFormat("MetadataSize: 0x{0:X8} | ", MetadataSize);
45+
sb.AppendFormat("FileSize: 0x{0:X8} | ", FileSize);
46+
sb.AppendFormat("Version: {0} | ", Version);
47+
sb.AppendFormat("DataOffset: 0x{0:X8} | ", DataOffset);
48+
sb.AppendFormat("Endianness: {0}", Endianess ? "BigEndian" : "LittleEndian");
49+
return sb.ToString();
4350
}
4451
}

UnityAsset.NET/SerializedFile/SerializedFileMetadata.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using UnityAsset.NET.Enums;
1+
using System.Text;
2+
using UnityAsset.NET.Enums;
23
using UnityAsset.NET.IO;
34

45

56
namespace UnityAsset.NET.SerializedFile;
67

7-
public class SerializedFileMetadata
8+
public sealed class SerializedFileMetadata
89
{
910
public string UnityVersion;
1011

@@ -16,7 +17,7 @@ public class SerializedFileMetadata
1617

1718
public SerializedFileMetadata(AssetReader reader, SerializedFileFormatVersion version)
1819
{
19-
UnityVersion = reader.ReadStringToNull();
20+
UnityVersion = reader.ReadNullTerminated();
2021
Platform = (BuildTarget)reader.ReadUInt32();
2122
if (version >= SerializedFileFormatVersion.HasTypeTreeHashes)
2223
{
@@ -29,5 +30,18 @@ public SerializedFileMetadata(AssetReader reader, SerializedFileFormatVersion ve
2930
{
3031
Types.Add(new SerializedType(reader, version, TypeTreeEnabled, false));
3132
}
33+
34+
int assetCount = reader.ReadInt32();
35+
reader.AlignStream(4);
36+
}
37+
38+
public override string ToString()
39+
{
40+
StringBuilder sb = new StringBuilder();
41+
sb.AppendFormat("UnityVersion: {0} | ", UnityVersion);
42+
sb.AppendFormat("Platform: {0} | ", Platform);
43+
sb.AppendFormat("TypeTreeEnabled: {0} | ", TypeTreeEnabled);
44+
sb.AppendFormat("Types: {0}", Types.Count);
45+
return sb.ToString();
3246
}
3347
}

UnityAsset.NET/SerializedFile/SerializedType.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44

55
namespace UnityAsset.NET.SerializedFile;
66

7-
public class SerializedType
7+
public sealed class SerializedType
88
{
99
public int ClassID;
1010

1111
public bool IsStrippedType;
1212

1313
public short ScriptTypeIndex;
1414

15+
public Hash128 ScriptIdHash;
16+
17+
public Hash128 TypeHash;
18+
19+
public bool IsRefType;
20+
21+
public List<SerializedType> Nodes;
22+
23+
public byte[] StringBufferBytes;
24+
25+
public int[] TypeDependencies;
26+
27+
public SerializedTypeReference TypeReference;
28+
1529
public SerializedType(AssetReader reader, SerializedFileFormatVersion version, bool typeTreeEnabled, bool isRefType)
1630
{
1731
ClassID = reader.ReadInt32();
@@ -34,7 +48,39 @@ public SerializedType(AssetReader reader, SerializedFileFormatVersion version, b
3448
(version >= RefactorTypeData && ClassID == (int)AssetClassID.MonoBehaviour) ||
3549
(isRefType && ScriptTypeIndex > 0))
3650
{
37-
//ScriptIdHash = new Hash128(reader);
51+
ScriptIdHash = new Hash128(reader);
52+
}
53+
54+
TypeHash = new Hash128(reader);
55+
IsRefType = isRefType;
56+
57+
if (typeTreeEnabled)
58+
{
59+
int typeTreeNodeCount = reader.ReadInt32();
60+
int stringBufferLen = reader.ReadInt32();
61+
Nodes = new List<SerializedType>(typeTreeNodeCount);
62+
for (int i = 0; i < typeTreeNodeCount; i++)
63+
{
64+
Nodes.Add(new SerializedType(reader, version, typeTreeEnabled, false));
65+
}
66+
StringBufferBytes = reader.ReadBytes(stringBufferLen);
67+
if (version >= StoresTypeDependencies)
68+
{
69+
if (!isRefType)
70+
{
71+
int dependenciesCount = reader.ReadInt32();
72+
TypeDependencies = new int[dependenciesCount];
73+
for (int i = 0; i < dependenciesCount; i++)
74+
{
75+
TypeDependencies[i] = reader.ReadInt32();
76+
}
77+
}
78+
else
79+
{
80+
TypeReference = new SerializedTypeReference();
81+
TypeReference.ReadMetadata(reader);
82+
}
83+
}
3884
}
3985
}
4086
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using UnityAsset.NET.IO;
2+
3+
namespace UnityAsset.NET.SerializedFile;
4+
5+
public class SerializedTypeReference
6+
{
7+
public string ClassName;
8+
9+
public string Namespace;
10+
11+
public string AsmName;
12+
13+
public SerializedTypeReference() { }
14+
15+
public SerializedTypeReference(string className, string nameSpace, string asmName)
16+
{
17+
ClassName = className;
18+
Namespace = nameSpace;
19+
AsmName = asmName;
20+
}
21+
22+
public void ReadMetadata(AssetReader reader)
23+
{
24+
ClassName = reader.ReadNullTerminated();
25+
Namespace = reader.ReadNullTerminated();
26+
AsmName = reader.ReadNullTerminated();
27+
}
28+
}

UnityAsset.NET/UnityAsset.NET.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<Nullable>enable</Nullable>
77
<Authors>AXiX</Authors>
88
<Description>A .NET library for reading and modifying Unity assets and bundles.</Description>
9-
<Version>0.0.3</Version>
9+
<Version>0.0.4-beta.1</Version>
1010
<Title>UnityAsset.NET</Title>
1111
<Copyright>AXiX</Copyright>
1212
<PackageProjectUrl>https://github.com/AXiX-official/UnityAsset.NET</PackageProjectUrl>
1313
<RepositoryUrl>https://github.com/AXiX-official/UnityAsset.NET</RepositoryUrl>
1414
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
15-
<AssemblyVersion>0.0.3</AssemblyVersion>
16-
<FileVersion>0.0.3</FileVersion>
15+
<AssemblyVersion>0.0.4.1</AssemblyVersion>
16+
<FileVersion>0.0.4.1</FileVersion>
1717
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<PackageTags>Unity</PackageTags>

0 commit comments

Comments
 (0)