-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 58e0e9a
Showing
25 changed files
with
1,558 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
bin/ | ||
obj/ | ||
/packages/ | ||
riderModule.iml | ||
/_ReSharper.Caches/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 AXiX | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# UnityAsset.NET | ||
|
||
A .NET library for reading and modifying Unity assets and bundles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityAsset.NET", "UnityAsset.NET\UnityAsset.NET.csproj", "{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{3B0C7B83-05AC-41A2-BFD2-9B7CDD4547EA}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
using UnityAsset.NET.Enums; | ||
using UnityAsset.NET.IO; | ||
|
||
namespace UnityAsset.NET.BundleFile; | ||
|
||
public class BlocksAndDirectoryInfo | ||
{ | ||
public byte[] UncompressedDataHash; | ||
|
||
public List<StorageBlockInfo> BlocksInfo; | ||
|
||
public List<CABInfo> DirectoryInfo; | ||
|
||
private uint uncompressedSize; | ||
|
||
public List<byte> BlocksInfoBytes; | ||
|
||
public BlocksAndDirectoryInfo(AssetReader reader, Header header, ref bool blocksInfoAtTheEnd) | ||
{ | ||
if ((header.flags & ArchiveFlags.BlocksInfoAtTheEnd) != 0) //kArchiveBlocksInfoAtTheEnd | ||
{ | ||
blocksInfoAtTheEnd = true; | ||
long position = reader.Position; | ||
reader.Position = reader.BaseStream.Length - header.compressedBlocksInfoSize; | ||
BlocksInfoBytes = reader.ReadBytes((int)header.compressedBlocksInfoSize).ToList(); | ||
reader.Position = position; | ||
} | ||
else //0x40 BlocksAndDirectoryInfoCombined | ||
{ | ||
BlocksInfoBytes = reader.ReadBytes((int)header.compressedBlocksInfoSize).ToList(); | ||
} | ||
|
||
ReadOnlySpan<byte> blocksInfoCompressedData = BlocksInfoBytes.ToArray(); | ||
var compressionType = (CompressionType)(header.flags & ArchiveFlags.CompressionTypeMask); | ||
uncompressedSize = header.uncompressedBlocksInfoSize; | ||
MemoryStream blocksInfoUncompresseddStream = new MemoryStream((int)(uncompressedSize)); | ||
switch (compressionType) //kArchiveCompressionTypeMask | ||
{ | ||
case CompressionType.None: //None | ||
{ | ||
blocksInfoUncompresseddStream = new MemoryStream(blocksInfoCompressedData.ToArray()); | ||
break; | ||
} | ||
case CompressionType.Lzma: //LZMA | ||
{ | ||
Compression.DecompressToStream(blocksInfoCompressedData, blocksInfoUncompresseddStream, uncompressedSize, "lzma"); | ||
blocksInfoUncompresseddStream.Position = 0; | ||
break; | ||
} | ||
case CompressionType.Lz4: //LZ4 | ||
case CompressionType.Lz4HC: //LZ4HC | ||
{ | ||
Compression.DecompressToStream(blocksInfoCompressedData, blocksInfoUncompresseddStream, uncompressedSize, "lz4"); | ||
blocksInfoUncompresseddStream.Position = 0; | ||
break; | ||
} | ||
default: | ||
throw new IOException($"Unsupported compression type {compressionType}"); | ||
} | ||
|
||
using AssetReader blocksInfoReader = new AssetReader(blocksInfoUncompresseddStream); | ||
|
||
UncompressedDataHash = blocksInfoReader.ReadBytes(16);// 除了ENCR | ||
var blocksInfoCount = blocksInfoReader.ReadInt32(); | ||
BlocksInfo = new List<StorageBlockInfo>(); | ||
for (int i = 0; i < blocksInfoCount; i++) | ||
{ | ||
BlocksInfo.Add(new StorageBlockInfo(blocksInfoReader)); | ||
} | ||
|
||
var directoryInfoCount = blocksInfoReader.ReadInt32(); | ||
DirectoryInfo = new List<CABInfo>(); | ||
for (int i = 0; i < directoryInfoCount; i++) | ||
{ | ||
DirectoryInfo.Add(new CABInfo(blocksInfoReader)); | ||
} | ||
} | ||
|
||
public void uncompresseFlags() | ||
{ | ||
foreach (var block in BlocksInfo) | ||
{ | ||
block.flags &= ~StorageBlockFlags.CompressionTypeMask; | ||
block.compressedSize = block.uncompressedSize; | ||
} | ||
Update(); | ||
} | ||
|
||
public void merge() | ||
{ | ||
List<StorageBlockInfo> newBlocksInfo = new List<StorageBlockInfo>(); | ||
|
||
foreach (var block in BlocksInfo) | ||
{ | ||
if (newBlocksInfo.Count > 0) | ||
{ | ||
if (newBlocksInfo.Last().flags != block.flags) | ||
{ | ||
throw new Exception("Expected all blocks to have the same flags"); | ||
} | ||
long newSize = (long)newBlocksInfo.Last().uncompressedSize + block.uncompressedSize; | ||
if (newSize <= uint.MaxValue) | ||
{ | ||
newBlocksInfo.Last().uncompressedSize = (uint)newSize; | ||
newBlocksInfo.Last().compressedSize += block.compressedSize; | ||
} | ||
else | ||
{ | ||
newBlocksInfo.Add(block); | ||
} | ||
} | ||
else | ||
{ | ||
newBlocksInfo.Add(block); | ||
} | ||
} | ||
|
||
BlocksInfo = newBlocksInfo; | ||
Update(); | ||
} | ||
|
||
public void Update(string compressionType = "none") | ||
{ | ||
// 写入数据到BlocksInfoBytes | ||
var BlocksInfoStream = new MemoryStream(); | ||
using AssetWriter writer = new AssetWriter(BlocksInfoStream); | ||
writer.Write(UncompressedDataHash); | ||
writer.WriteInt32(BlocksInfo.Count); | ||
foreach (var block in BlocksInfo) | ||
{ | ||
block.Write(writer); | ||
} | ||
writer.WriteInt32(DirectoryInfo.Count); | ||
foreach (var node in DirectoryInfo) | ||
{ | ||
node.Write(writer); | ||
} | ||
|
||
uncompressedSize = (uint)BlocksInfoStream.Length; | ||
BlocksInfoBytes = Compression.CompressStream(BlocksInfoStream, compressionType); | ||
} | ||
|
||
public void calculateSize(ref uint uncompressedBlocksInfoSize, ref uint compressedBlocksInfoSize) | ||
{ | ||
uncompressedBlocksInfoSize = uncompressedSize; | ||
compressedBlocksInfoSize = (uint)BlocksInfoBytes.Count; | ||
} | ||
|
||
public void FixSize(int diff) | ||
{ | ||
|
||
if (DirectoryInfo[^1].size + diff <= UInt32.MaxValue && DirectoryInfo[^1].size + diff > 0) | ||
{ | ||
if (BlocksInfo[^1].uncompressedSize + diff <= UInt32.MaxValue && BlocksInfo[^1].uncompressedSize + diff > 0) | ||
{ | ||
if (diff > 0) | ||
{ | ||
BlocksInfo[^1].uncompressedSize += (uint)diff; | ||
BlocksInfo[^1].compressedSize += (uint)diff; | ||
DirectoryInfo[^1].size += (uint)diff; | ||
} | ||
else | ||
{ | ||
BlocksInfo[^1].uncompressedSize -= (uint)-diff; | ||
BlocksInfo[^1].compressedSize -= (uint)-diff; | ||
DirectoryInfo[^1].size -= (uint)-diff; | ||
} | ||
} | ||
else | ||
{ | ||
if (BlocksInfo[^1].uncompressedSize + diff <= UInt32.MaxValue) | ||
{ | ||
BlocksInfo.Add(new StorageBlockInfo((uint)diff, (uint)diff ,BlocksInfo[^1].flags)); | ||
DirectoryInfo[^1].size += (uint)diff; | ||
} | ||
else | ||
{ | ||
diff += (int)BlocksInfo[^1].uncompressedSize; | ||
diff = -diff; | ||
BlocksInfo.Remove(BlocksInfo[^1]); | ||
BlocksInfo[^1].uncompressedSize -= (uint)diff; | ||
BlocksInfo[^1].compressedSize -= (uint)diff; | ||
DirectoryInfo[^1].size -= (uint)diff; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
throw new Exception("DirectoryInfo size overflow"); | ||
} | ||
} | ||
|
||
public void Write(AssetWriter writer) | ||
{ | ||
writer.Write(BlocksInfoBytes.ToArray()); | ||
} | ||
} |
Oops, something went wrong.