Skip to content

Commit

Permalink
0.0.4-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AXiX-official committed Jun 29, 2024
1 parent 9227568 commit eb49686
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 19 deletions.
4 changes: 3 additions & 1 deletion UnityAsset.NET/Compression/LZ4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ public static int EncodeFast(ReadOnlySpan<byte> source, Span<byte> target)
}
int literalLen = (int) (s - anchor);
int matchLen = 4;
while (s + matchLen < sourceEnd && *(s + matchLen) == *(sourceOffset + matchLen))
while (s + matchLen < sourceEnd)
{
if (*(s + matchLen) != *(sourceOffset + matchLen))
break;
matchLen++;
}
int token = Math.Min(literalLen, 15) << 4 | Math.Min(matchLen - 4, 15);
Expand Down
49 changes: 49 additions & 0 deletions UnityAsset.NET/Hash128.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text;
using UnityAsset.NET.IO;

namespace UnityAsset.NET;

public struct Hash128
{
public byte[] data; //16 bytes

public Hash128(byte[] data)
{
this.data = data;
}
public Hash128(AssetReader reader)
{
data = reader.ReadBytes(16);
}

public bool IsZero()
{
if (data == null)
return true;

for (int i = 0; i < data.Length; i++)
{
if (data[i] != 0)
return false;
}

return true;
}

public override string ToString()
{
StringBuilder hex = new StringBuilder(data.Length * 2);

foreach (byte b in data)
{
hex.AppendFormat("{0:x2}", b);
}

return hex.ToString();
}

public static Hash128 NewBlankHash()
{
return new Hash128() { data = new byte[16] };
}
}
27 changes: 17 additions & 10 deletions UnityAsset.NET/SerializedFile/SerializedFileHeader.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using UnityAsset.NET.Enums;
using System.Text;
using UnityAsset.NET.Enums;
using UnityAsset.NET.IO;

namespace UnityAsset.NET.SerializedFile;

public sealed class SerializedFileHeader
{
public uint MetadataSize;
public long FileSize;
public ulong FileSize;
public SerializedFileFormatVersion Version;
public long DataOffset;
public byte Endianess;
public ulong DataOffset;
public bool Endianess;

public SerializedFileHeader(AssetReader reader)
{
Expand All @@ -20,25 +21,31 @@ public SerializedFileHeader(AssetReader reader)

if (Version < SerializedFileFormatVersion.Unknown_9)
{
throw new Exception("Unsupported version.");
throw new Exception($"Unsupported version: {Version}");
}

Endianess = reader.ReadByte();
Endianess = reader.ReadBoolean();
reader.ReadBytes(3);// unused bytes

if (Version >= SerializedFileFormatVersion.LargeFilesSupport)
{
MetadataSize = reader.ReadUInt32();
FileSize = reader.ReadUInt32();
DataOffset = reader.ReadUInt32();
FileSize = reader.ReadUInt64();
DataOffset = reader.ReadUInt64();
reader.ReadInt64(); // unknown
}

reader.BigEndian = Endianess == 1;
reader.BigEndian = Endianess;
}

public override string ToString()
{
return $"MetadataSize: 0x{MetadataSize:X8} | FileSize: 0x{FileSize:X8} | Version: {Version} | DataOffset: 0x{DataOffset:X8} | Endianness: {(EndianType)Endianess}";
StringBuilder sb = new StringBuilder();
sb.AppendFormat("MetadataSize: 0x{0:X8} | ", MetadataSize);
sb.AppendFormat("FileSize: 0x{0:X8} | ", FileSize);
sb.AppendFormat("Version: {0} | ", Version);
sb.AppendFormat("DataOffset: 0x{0:X8} | ", DataOffset);
sb.AppendFormat("Endianness: {0}", Endianess ? "BigEndian" : "LittleEndian");
return sb.ToString();
}
}
20 changes: 17 additions & 3 deletions UnityAsset.NET/SerializedFile/SerializedFileMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using UnityAsset.NET.Enums;
using System.Text;
using UnityAsset.NET.Enums;
using UnityAsset.NET.IO;


namespace UnityAsset.NET.SerializedFile;

public class SerializedFileMetadata
public sealed class SerializedFileMetadata
{
public string UnityVersion;

Expand All @@ -16,7 +17,7 @@ public class SerializedFileMetadata

public SerializedFileMetadata(AssetReader reader, SerializedFileFormatVersion version)
{
UnityVersion = reader.ReadStringToNull();
UnityVersion = reader.ReadNullTerminated();
Platform = (BuildTarget)reader.ReadUInt32();
if (version >= SerializedFileFormatVersion.HasTypeTreeHashes)
{
Expand All @@ -29,5 +30,18 @@ public SerializedFileMetadata(AssetReader reader, SerializedFileFormatVersion ve
{
Types.Add(new SerializedType(reader, version, TypeTreeEnabled, false));
}

int assetCount = reader.ReadInt32();
reader.AlignStream(4);
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("UnityVersion: {0} | ", UnityVersion);
sb.AppendFormat("Platform: {0} | ", Platform);
sb.AppendFormat("TypeTreeEnabled: {0} | ", TypeTreeEnabled);
sb.AppendFormat("Types: {0}", Types.Count);
return sb.ToString();
}
}
50 changes: 48 additions & 2 deletions UnityAsset.NET/SerializedFile/SerializedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@

namespace UnityAsset.NET.SerializedFile;

public class SerializedType
public sealed class SerializedType
{
public int ClassID;

public bool IsStrippedType;

public short ScriptTypeIndex;

public Hash128 ScriptIdHash;

public Hash128 TypeHash;

public bool IsRefType;

public List<SerializedType> Nodes;

public byte[] StringBufferBytes;

public int[] TypeDependencies;

public SerializedTypeReference TypeReference;

public SerializedType(AssetReader reader, SerializedFileFormatVersion version, bool typeTreeEnabled, bool isRefType)
{
ClassID = reader.ReadInt32();
Expand All @@ -34,7 +48,39 @@ public SerializedType(AssetReader reader, SerializedFileFormatVersion version, b
(version >= RefactorTypeData && ClassID == (int)AssetClassID.MonoBehaviour) ||
(isRefType && ScriptTypeIndex > 0))
{
//ScriptIdHash = new Hash128(reader);
ScriptIdHash = new Hash128(reader);
}

TypeHash = new Hash128(reader);
IsRefType = isRefType;

if (typeTreeEnabled)
{
int typeTreeNodeCount = reader.ReadInt32();
int stringBufferLen = reader.ReadInt32();
Nodes = new List<SerializedType>(typeTreeNodeCount);
for (int i = 0; i < typeTreeNodeCount; i++)
{
Nodes.Add(new SerializedType(reader, version, typeTreeEnabled, false));
}
StringBufferBytes = reader.ReadBytes(stringBufferLen);
if (version >= StoresTypeDependencies)
{
if (!isRefType)
{
int dependenciesCount = reader.ReadInt32();
TypeDependencies = new int[dependenciesCount];
for (int i = 0; i < dependenciesCount; i++)
{
TypeDependencies[i] = reader.ReadInt32();
}
}
else
{
TypeReference = new SerializedTypeReference();
TypeReference.ReadMetadata(reader);
}
}
}
}
}
28 changes: 28 additions & 0 deletions UnityAsset.NET/SerializedFile/SerializedTypeReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityAsset.NET.IO;

namespace UnityAsset.NET.SerializedFile;

public class SerializedTypeReference
{
public string ClassName;

public string Namespace;

public string AsmName;

public SerializedTypeReference() { }

public SerializedTypeReference(string className, string nameSpace, string asmName)
{
ClassName = className;
Namespace = nameSpace;
AsmName = asmName;
}

public void ReadMetadata(AssetReader reader)
{
ClassName = reader.ReadNullTerminated();
Namespace = reader.ReadNullTerminated();
AsmName = reader.ReadNullTerminated();
}
}
6 changes: 3 additions & 3 deletions UnityAsset.NET/UnityAsset.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<Nullable>enable</Nullable>
<Authors>AXiX</Authors>
<Description>A .NET library for reading and modifying Unity assets and bundles.</Description>
<Version>0.0.3</Version>
<Version>0.0.4-beta.1</Version>
<Title>UnityAsset.NET</Title>
<Copyright>AXiX</Copyright>
<PackageProjectUrl>https://github.com/AXiX-official/UnityAsset.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/AXiX-official/UnityAsset.NET</RepositoryUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyVersion>0.0.3</AssemblyVersion>
<FileVersion>0.0.3</FileVersion>
<AssemblyVersion>0.0.4.1</AssemblyVersion>
<FileVersion>0.0.4.1</FileVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Unity</PackageTags>
Expand Down

0 comments on commit eb49686

Please sign in to comment.