Skip to content

Commit 3a2f4d2

Browse files
committed
refactor
1 parent 5a5d06f commit 3a2f4d2

29 files changed

Lines changed: 194 additions & 289 deletions

README.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33
[![MIT](https://img.shields.io/github/license/AXiX-official/UnityAsset.NET)](https://github.com/AXiX-official/UnityAsset.NET/master/LICENSE)
44
[![NuGet Stats](https://img.shields.io/nuget/v/UnityAsset.NET.svg)](https://www.nuget.org/packages/UnityAsset.NET)
55

6-
A .NET library for reading and modifying Unity assets and bundles.
6+
A work-in-progress .NET library for parsing/serializing/patching Unity Engine asset files.
7+
8+
Only support Unity 2017.x or later.
79

810
## Features
911

10-
For now, it can only do a few simple things in the outer layer of the bundlefile
12+
### BundleFile
13+
14+
- [x] Parse/Serialize
15+
- [ ] Patch
16+
- [ ] Calculate/Patch crc32
17+
18+
### SerializedFile
1119

12-
- Read and write uncompressed/lz4-compressed/lzma-compressed Unity bundlefile
13-
- Handling UnityCN encryption
14-
- Calculate and fix the CRC32 value of bundlefile
20+
- [x] Parse/Serialize
21+
- [ ] Patch
22+
23+
### Asset
24+
25+
- [x] Parse/Serialize
26+
- [ ] Patch
1527

1628
## Acknowledgements
1729

18-
This project uses code from the following open source projects:
30+
This project stands on the shoulders of these amazing open-source projects:
1931

2032
- [Studio](https://github.com/RazTools/Studio) by [Razmoth](https://github.com/RazTools)
2133
- [AssetStudio](https://github.com/aelurum/AssetStudio) by [aelurum](https://github.com/aelurum)
2234
- [AssetsTools.NET](https://github.com/nesrak1/AssetsTools.NET) by [nesrak1](https://github.com/nesrak1)
2335
- [UnityPy](https://github.com/K0lb3/UnityPy) by [K0lb3](https://github.com/K0lb3)
24-
25-
We are grateful to the developers of these projects for their work.
36+
- [RustyAssetBundleEXtractor](https://github.com/UniversalGameExtraction/RustyAssetBundleEXtractor) by [UniversalGameExtraction](https://github.com/UniversalGameExtraction)

UnityAsset.NET/Asset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Specialized;
2+
using UnityAsset.NET.Files.SerializedFiles;
23
using UnityAsset.NET.IO;
3-
using UnityAsset.NET.SerializedFiles;
44

55
namespace UnityAsset.NET;
66

UnityAsset.NET/CRC32.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace UnityAsset.NET;
1+
using System.Runtime.CompilerServices;
2+
namespace UnityAsset.NET;
23

34
public static class CRC32
45
{
@@ -491,17 +492,17 @@ public static class CRC32
491492
71007118, 2857626143, 1470763626, 4190274555, 3490330377, 2120394392, 4035494306, 1591758899, 1999168705,
492493
3644880208, 616140069, 2328960180, 2736367686, 225524183
493494
];
494-
495+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
495496
private static byte HIBYTE(uint X) => (byte)((X >> 8) & 0xFF);
496-
497+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
497498
private static byte LOBYTE(uint X) => (byte)(X & 0xFF);
498-
499+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
499500
private static uint HIWORD(uint X) => (X >> 16) & 0xFFFF;
500-
501+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
501502
private static uint LOWORD(uint X) => X & 0xFFFF;
502-
503+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
503504
private static uint MAKEWORD(uint X, uint Y) => (Y << 8) | X;
504-
505+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
505506
private static uint MAKELONG(uint X, uint Y) => (Y << 16) | X;
506507

507508
private static byte RF(uint X)
@@ -515,13 +516,13 @@ private static byte RF(uint X)
515516
}
516517
return 0;
517518
}
518-
519+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
519520
private static byte F(uint X) => HIBYTE(HIWORD(Crc32Table[X]));
520-
521+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
521522
private static byte G(uint X) => LOBYTE(HIWORD(Crc32Table[X]));
522-
523+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
523524
private static byte H(uint X) => HIBYTE(LOWORD(Crc32Table[X]));
524-
525+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
525526
private static byte I(uint X) => LOBYTE(LOWORD(Crc32Table[X]));
526527

527528
public static byte[] rCRC(uint targetCrc, uint originalCrc)
@@ -552,7 +553,7 @@ public static byte[] rCRC(uint targetCrc, uint originalCrc)
552553
return new byte[] { d, c, b, a };
553554
}
554555

555-
public static uint CalculateCRC32(byte[] data, uint initialCrc = 0)
556+
public static uint CalculateCRC32(ReadOnlySpan<byte> data, uint initialCrc = 0)
556557
{
557558
uint crc = ~initialCrc;
558559
var len = data.Length;
@@ -576,10 +577,4 @@ public static uint CalculateCRC32(byte[] data, uint initialCrc = 0)
576577
}
577578
return ~crc;
578579
}
579-
580-
public static uint CalculateCRC32(Stream data, uint initialCrc = 0)
581-
{
582-
data.Position = 0;
583-
return CalculateCRC32(new BinaryReader(data).ReadBytes((int)data.Length), initialCrc);
584-
}
585580
}

UnityAsset.NET/CabFileWrapper.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityAsset.NET.Files.SerializedFiles;
2+
3+
namespace UnityAsset.NET.Files;
4+
5+
public static class BundleFileExtension
6+
{
7+
public static List<Asset> Assets(this BundleFile bf) =>
8+
bf.Files
9+
.Where(file => file.File is SerializedFile)
10+
.SelectMany(file => ((SerializedFile)file.File).Assets)
11+
.ToList();
12+
}

UnityAsset.NET/BundleFiles/BlocksAndCabsInfo.cs renamed to UnityAsset.NET/Files/BundleFiles/BlocksAndDirectoryInfo.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
using System.Text;
22
using UnityAsset.NET.IO;
33

4-
namespace UnityAsset.NET.BundleFiles;
4+
namespace UnityAsset.NET.Files.BundleFiles;
55

6-
public sealed class BlocksAndCabsInfo
6+
public sealed class BlocksAndDirectoryInfo
77
{
88
public byte[] UncompressedDataHash;
99
public List<StorageBlockInfo> BlocksInfo;
10-
public List<CabInfo> DirectoryInfo;
10+
public List<FileEntry> DirectoryInfo;
1111

12-
public BlocksAndCabsInfo(byte[] uncompressedDataHash,
12+
public BlocksAndDirectoryInfo(byte[] uncompressedDataHash,
1313
List<StorageBlockInfo> blocksInfo,
14-
List<CabInfo> directoryInfo)
14+
List<FileEntry> directoryInfo)
1515
{
1616
UncompressedDataHash = uncompressedDataHash;
1717
BlocksInfo = blocksInfo;
1818
DirectoryInfo = directoryInfo;
1919
}
2020

21-
public static BlocksAndCabsInfo Parse(ref DataBuffer db) => new (
21+
public static BlocksAndDirectoryInfo Parse(ref DataBuffer db) => new (
2222
db.ReadBytes(16),
2323
db.ReadList(db.ReadInt32(), StorageBlockInfo.Parse),
24-
db.ReadList(db.ReadInt32(), CabInfo.Parse)
24+
db.ReadList(db.ReadInt32(), FileEntry.Parse)
2525
);
2626

2727
public void Serialize(ref DataBuffer db) {
2828
db.WriteBytes(UncompressedDataHash);
2929
db.WriteListWithCount(BlocksInfo, (ref DataBuffer d, StorageBlockInfo info) => info.Serialize(ref d));
30-
db.WriteListWithCount(DirectoryInfo, (ref DataBuffer d, CabInfo info) => info.Serialize(ref d));
30+
db.WriteListWithCount(DirectoryInfo, (ref DataBuffer d, FileEntry info) => info.Serialize(ref d));
3131
}
3232

3333
public long SerializeSize => UncompressedDataHash.Length + 8 +
@@ -39,14 +39,10 @@ public override string ToString()
3939
StringBuilder sb = new StringBuilder();
4040
sb.AppendLine($"UncompressedDataHash: {BitConverter.ToString(UncompressedDataHash)}");
4141
for (int i = 0; i < BlocksInfo.Count; ++i)
42-
{
4342
sb.Append($"Block {i}: {BlocksInfo[i]}");
44-
}
4543
sb.AppendLine();
4644
for (int i = 0; i < DirectoryInfo.Count; ++i)
47-
{
4845
sb.Append($"Directory {i}: {DirectoryInfo[i]}");
49-
}
5046
sb.AppendLine();
5147
return sb.ToString();
5248
}

0 commit comments

Comments
 (0)