Skip to content

Commit

Permalink
Auto Generate UnityCN Info
Browse files Browse the repository at this point in the history
  • Loading branch information
AXiX-official committed Aug 26, 2024
1 parent 3a4a307 commit 1016390
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
42 changes: 22 additions & 20 deletions UnityAsset.NET/BundleFile/BundleFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public BundleFile(string path, string? key = null)
{
}

public BundleFile(byte[] data, string? key = null)
{
using AssetReader reader = new AssetReader(data);

UnityCNKey = key;

Header = new Header(reader);
ReadBundleWithHeader(reader, Header, key);
}

public BundleFile(Stream input, string? key = null)
{
using AssetReader reader = new AssetReader(input);
Expand Down Expand Up @@ -266,13 +276,13 @@ private void ReadBlocks(AssetReader reader)
}
}

public void WriteToFile(string path, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
public void WriteToFile(string path, string infoPacker = "none", string dataPacker = "none", bool unityCN = false, string key = "")
{
using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
Write(fs, infoPacker, dataPacker, unityCN);
Write(fs, infoPacker, dataPacker, unityCN, key);
}

public void Write(Stream output, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
public void Write(Stream output, string infoPacker = "none", string dataPacker = "none", bool unityCN = false, string key = "")
{
MemoryStream compressedStream = new MemoryStream();

Expand Down Expand Up @@ -312,13 +322,17 @@ public void Write(Stream output, string infoPacker = "none", string dataPacker =

if (unityCN)
{
if (UnityCNKey == null)
{
throw new Exception("UnityCN key is required for encryption");
}
if (UnityCNInfo == null)
{
throw new Exception("TODO: UnityCNInfo is null");
if (key != "")
{
UnityCNKey = key;
UnityCNInfo = new UnityCN(UnityCNKey);
}
else
{
throw new Exception("UnityCN key is required for encryption");
}
}
UnityCNInfo.reset();
compressedStream.Position = 0;
Expand Down Expand Up @@ -424,18 +438,6 @@ private void calculateSize(long BlocksStreamLength, bool unityCN)
Header.size = size;
}

public void Bumbo()
{
var blockFlag = DataInfo.BlocksInfo[^1].flags;
var blockSize = int.MaxValue / 2;
DataInfo.BlocksInfo.Add(new StorageBlockInfo((uint)blockSize, (uint)blockSize, blockFlag));
DataInfo.DirectoryInfo[^1].size += blockSize;
cabStreams[^1].SetLength(cabStreams[^1].Length + blockSize);
cabStreams[^1].Position = cabStreams[^1].Length - blockSize;
cabStreams[^1].Write(new byte[blockSize]);
cabStreams[^1].Position = 0;
}

private int[] ParseVersion()
{
var versionSplit = Regex.Replace(Header.unityRevision, @"\D", ".").Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
Expand Down
35 changes: 31 additions & 4 deletions UnityAsset.NET/BundleFile/UnityCN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@ public UnityCN(AssetReader reader, string key)
reset();
}

public UnityCN(string key)
{
SetKey(key);

value = 0;

InfoBytes = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xA6, 0xB1, 0xDE, 0x48, 0x9E, 0x2B, 0x53, 0x5C];
InfoKey = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10];
EncryptKey(InfoKey, InfoBytes);

SignatureKey = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10];
SignatureBytes = Encoding.UTF8.GetBytes(Signature);
EncryptKey(SignatureKey, SignatureBytes);

reset();
}

public void reset()
{
var infoBytes = InfoBytes.ToArray();
var infoKey = InfoKey.ToArray();
var signatureBytes = SignatureBytes.ToArray();
var signatureKey = SignatureKey.ToArray();
var infoBytes = (byte[])InfoBytes.Clone();
var infoKey = (byte[])InfoKey.Clone();
var signatureBytes = (byte[])SignatureBytes.Clone();
var signatureKey = (byte[])SignatureKey.Clone();

DecryptKey(signatureKey, signatureBytes);

Expand Down Expand Up @@ -123,6 +140,16 @@ private void DecryptKey(byte[] key, byte[] data)
data[i] ^= key[i];
}
}

private void EncryptKey(byte[] key, byte[] data)
{
if (Encryptor != null)
{
key = Encryptor.TransformFinalBlock(key, 0, key.Length);
for (int i = 0; i < 0x10; i++)
data[i] ^= key[i];
}
}

private int DecryptByte(Span<byte> bytes, ref int offset, ref int index)
{
Expand Down
14 changes: 6 additions & 8 deletions UnityAsset.NET/Compression/Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ public static void DecompressToStream(ReadOnlySpan<byte> compressedData, Stream
{
case "lz4":
byte[] decompressedData = new byte[decompressedSize];
int size = LZ4.Decode(compressedData, new Span<byte>(decompressedData));
var size = LZ4Codec.Decode(compressedData, new Span<byte>(decompressedData));
if (size != decompressedSize)
{
throw new Exception($"LZ4 decompression failed, expected {decompressedSize} bytes, got {size} bytes");
}
throw new Exception($"Decompressed size mismatch, expected {decompressedSize}, got {size}");
decompressedStream.Write(decompressedData, 0, decompressedData.Length);
break;
case "lzma":
Expand All @@ -42,12 +40,12 @@ public static List<byte> CompressStream(MemoryStream uncompressedStream, string
case "none":
return uncompressedData.ToList();
case "lz4":
byte[] compressedData = new byte[LZ4.MaximumOutputSize(uncompressedData.Length)];
int compressedSize = LZ4.EncodeFast(uncompressedData, compressedData);
byte[] compressedData = new byte[LZ4Codec.MaximumOutputSize(uncompressedData.Length)];
int compressedSize = LZ4Codec.Encode(uncompressedData, compressedData);
return compressedData.Take(compressedSize).ToList();
case "lz4hc":
byte[] compressedDataHC = new byte[LZ4Codec.MaximumOutputSize(uncompressedData.Length)];
int compressedSizeHC = LZ4Codec.Encode(uncompressedData, compressedDataHC, LZ4Level.L09_HC);
int compressedSizeHC = LZ4Codec.Encode(uncompressedData, compressedDataHC, LZ4Level.L12_MAX);
return compressedDataHC.Take(compressedSizeHC).ToList();
case "lzma":
var encoder = new Encoder();
Expand All @@ -56,7 +54,7 @@ public static List<byte> CompressStream(MemoryStream uncompressedStream, string
long fileSize = uncompressedStream.Length;
uncompressedStream.Position = 0;
encoder.Code(uncompressedStream, compressedStream, -1, -1, null);
Console.WriteLine($"Compressed {fileSize} bytes to {compressedStream.Length} bytes");
//Console.WriteLine($"Compressed {fileSize} bytes to {compressedStream.Length} bytes");
return compressedStream.ToArray().ToList();
default:
throw new ArgumentException($"Unsupported compression type {compressionType}");
Expand Down
7 changes: 4 additions & 3 deletions UnityAsset.NET/UnityAsset.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<Nullable>enable</Nullable>
<Authors>AXiX</Authors>
<Description>A .NET library for reading and modifying Unity assets and bundles.</Description>
<Version>0.0.4-beta.1</Version>
<Version>0.0.4-beta.2</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.4.1</AssemblyVersion>
<FileVersion>0.0.4.1</FileVersion>
<AssemblyVersion>0.0.4.2</AssemblyVersion>
<FileVersion>0.0.4.2</FileVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Unity</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1016390

Please sign in to comment.