Skip to content

Commit 1016390

Browse files
committed
Auto Generate UnityCN Info
1 parent 3a4a307 commit 1016390

4 files changed

Lines changed: 63 additions & 35 deletions

File tree

UnityAsset.NET/BundleFile/BundleFile.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public BundleFile(string path, string? key = null)
4747
{
4848
}
4949

50+
public BundleFile(byte[] data, string? key = null)
51+
{
52+
using AssetReader reader = new AssetReader(data);
53+
54+
UnityCNKey = key;
55+
56+
Header = new Header(reader);
57+
ReadBundleWithHeader(reader, Header, key);
58+
}
59+
5060
public BundleFile(Stream input, string? key = null)
5161
{
5262
using AssetReader reader = new AssetReader(input);
@@ -266,13 +276,13 @@ private void ReadBlocks(AssetReader reader)
266276
}
267277
}
268278

269-
public void WriteToFile(string path, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
279+
public void WriteToFile(string path, string infoPacker = "none", string dataPacker = "none", bool unityCN = false, string key = "")
270280
{
271281
using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
272-
Write(fs, infoPacker, dataPacker, unityCN);
282+
Write(fs, infoPacker, dataPacker, unityCN, key);
273283
}
274284

275-
public void Write(Stream output, string infoPacker = "none", string dataPacker = "none", bool unityCN = false)
285+
public void Write(Stream output, string infoPacker = "none", string dataPacker = "none", bool unityCN = false, string key = "")
276286
{
277287
MemoryStream compressedStream = new MemoryStream();
278288

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

313323
if (unityCN)
314324
{
315-
if (UnityCNKey == null)
316-
{
317-
throw new Exception("UnityCN key is required for encryption");
318-
}
319325
if (UnityCNInfo == null)
320326
{
321-
throw new Exception("TODO: UnityCNInfo is null");
327+
if (key != "")
328+
{
329+
UnityCNKey = key;
330+
UnityCNInfo = new UnityCN(UnityCNKey);
331+
}
332+
else
333+
{
334+
throw new Exception("UnityCN key is required for encryption");
335+
}
322336
}
323337
UnityCNInfo.reset();
324338
compressedStream.Position = 0;
@@ -424,18 +438,6 @@ private void calculateSize(long BlocksStreamLength, bool unityCN)
424438
Header.size = size;
425439
}
426440

427-
public void Bumbo()
428-
{
429-
var blockFlag = DataInfo.BlocksInfo[^1].flags;
430-
var blockSize = int.MaxValue / 2;
431-
DataInfo.BlocksInfo.Add(new StorageBlockInfo((uint)blockSize, (uint)blockSize, blockFlag));
432-
DataInfo.DirectoryInfo[^1].size += blockSize;
433-
cabStreams[^1].SetLength(cabStreams[^1].Length + blockSize);
434-
cabStreams[^1].Position = cabStreams[^1].Length - blockSize;
435-
cabStreams[^1].Write(new byte[blockSize]);
436-
cabStreams[^1].Position = 0;
437-
}
438-
439441
private int[] ParseVersion()
440442
{
441443
var versionSplit = Regex.Replace(Header.unityRevision, @"\D", ".").Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);

UnityAsset.NET/BundleFile/UnityCN.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,29 @@ public UnityCN(AssetReader reader, string key)
4040
reset();
4141
}
4242

43+
public UnityCN(string key)
44+
{
45+
SetKey(key);
46+
47+
value = 0;
48+
49+
InfoBytes = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xA6, 0xB1, 0xDE, 0x48, 0x9E, 0x2B, 0x53, 0x5C];
50+
InfoKey = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10];
51+
EncryptKey(InfoKey, InfoBytes);
52+
53+
SignatureKey = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10];
54+
SignatureBytes = Encoding.UTF8.GetBytes(Signature);
55+
EncryptKey(SignatureKey, SignatureBytes);
56+
57+
reset();
58+
}
59+
4360
public void reset()
4461
{
45-
var infoBytes = InfoBytes.ToArray();
46-
var infoKey = InfoKey.ToArray();
47-
var signatureBytes = SignatureBytes.ToArray();
48-
var signatureKey = SignatureKey.ToArray();
62+
var infoBytes = (byte[])InfoBytes.Clone();
63+
var infoKey = (byte[])InfoKey.Clone();
64+
var signatureBytes = (byte[])SignatureBytes.Clone();
65+
var signatureKey = (byte[])SignatureKey.Clone();
4966

5067
DecryptKey(signatureKey, signatureBytes);
5168

@@ -123,6 +140,16 @@ private void DecryptKey(byte[] key, byte[] data)
123140
data[i] ^= key[i];
124141
}
125142
}
143+
144+
private void EncryptKey(byte[] key, byte[] data)
145+
{
146+
if (Encryptor != null)
147+
{
148+
key = Encryptor.TransformFinalBlock(key, 0, key.Length);
149+
for (int i = 0; i < 0x10; i++)
150+
data[i] ^= key[i];
151+
}
152+
}
126153

127154
private int DecryptByte(Span<byte> bytes, ref int offset, ref int index)
128155
{

UnityAsset.NET/Compression/Compression.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ public static void DecompressToStream(ReadOnlySpan<byte> compressedData, Stream
1212
{
1313
case "lz4":
1414
byte[] decompressedData = new byte[decompressedSize];
15-
int size = LZ4.Decode(compressedData, new Span<byte>(decompressedData));
15+
var size = LZ4Codec.Decode(compressedData, new Span<byte>(decompressedData));
1616
if (size != decompressedSize)
17-
{
18-
throw new Exception($"LZ4 decompression failed, expected {decompressedSize} bytes, got {size} bytes");
19-
}
17+
throw new Exception($"Decompressed size mismatch, expected {decompressedSize}, got {size}");
2018
decompressedStream.Write(decompressedData, 0, decompressedData.Length);
2119
break;
2220
case "lzma":
@@ -42,12 +40,12 @@ public static List<byte> CompressStream(MemoryStream uncompressedStream, string
4240
case "none":
4341
return uncompressedData.ToList();
4442
case "lz4":
45-
byte[] compressedData = new byte[LZ4.MaximumOutputSize(uncompressedData.Length)];
46-
int compressedSize = LZ4.EncodeFast(uncompressedData, compressedData);
43+
byte[] compressedData = new byte[LZ4Codec.MaximumOutputSize(uncompressedData.Length)];
44+
int compressedSize = LZ4Codec.Encode(uncompressedData, compressedData);
4745
return compressedData.Take(compressedSize).ToList();
4846
case "lz4hc":
4947
byte[] compressedDataHC = new byte[LZ4Codec.MaximumOutputSize(uncompressedData.Length)];
50-
int compressedSizeHC = LZ4Codec.Encode(uncompressedData, compressedDataHC, LZ4Level.L09_HC);
48+
int compressedSizeHC = LZ4Codec.Encode(uncompressedData, compressedDataHC, LZ4Level.L12_MAX);
5149
return compressedDataHC.Take(compressedSizeHC).ToList();
5250
case "lzma":
5351
var encoder = new Encoder();
@@ -56,7 +54,7 @@ public static List<byte> CompressStream(MemoryStream uncompressedStream, string
5654
long fileSize = uncompressedStream.Length;
5755
uncompressedStream.Position = 0;
5856
encoder.Code(uncompressedStream, compressedStream, -1, -1, null);
59-
Console.WriteLine($"Compressed {fileSize} bytes to {compressedStream.Length} bytes");
57+
//Console.WriteLine($"Compressed {fileSize} bytes to {compressedStream.Length} bytes");
6058
return compressedStream.ToArray().ToList();
6159
default:
6260
throw new ArgumentException($"Unsupported compression type {compressionType}");

UnityAsset.NET/UnityAsset.NET.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
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.4-beta.1</Version>
9+
<Version>0.0.4-beta.2</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.4.1</AssemblyVersion>
16-
<FileVersion>0.0.4.1</FileVersion>
15+
<AssemblyVersion>0.0.4.2</AssemblyVersion>
16+
<FileVersion>0.0.4.2</FileVersion>
1717
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<PackageTags>Unity</PackageTags>
2020
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2121
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
22+
<PublishAot>true</PublishAot>
2223
</PropertyGroup>
2324

2425
<ItemGroup>

0 commit comments

Comments
 (0)