Skip to content

Commit f71e195

Browse files
committed
Add DefaultUnityCNKey to Setting
1 parent 18434f2 commit f71e195

5 files changed

Lines changed: 13 additions & 156 deletions

File tree

UnityAsset.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34607.119
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnityAsset.NET", "UnityAsset.NET\UnityAsset.NET.csproj", "{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{49107F77-825A-493A-A7E7-2196DC40DE24}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{B83CD1B5-07A2-4DB7-92C2-AD1D692945DE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{49107F77-825A-493A-A7E7-2196DC40DE24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{49107F77-825A-493A-A7E7-2196DC40DE24}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{49107F77-825A-493A-A7E7-2196DC40DE24}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{49107F77-825A-493A-A7E7-2196DC40DE24}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

UnityAsset.NET/BundleFiles/BlocksAndCabsInfo.cs

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -32,156 +32,4 @@ public void Serialize(ref DataBuffer db) {
3232
public long SerializeSize => UncompressedDataHash.Length + 8 +
3333
BlocksInfo.Sum(item => item.SerializeSize) +
3434
DirectoryInfo.Sum(item => item.SerializeSize);
35-
36-
/*public void uncompresseFlags()
37-
{
38-
foreach (var block in BlocksInfo)
39-
{
40-
//block.flags &= ~StorageBlockFlags.CompressionTypeMask;
41-
block.Flags &= 0x0;
42-
block.CompressedSize = block.UncompressedSize;
43-
}
44-
Update();
45-
}
46-
47-
public void merge()
48-
{
49-
List<StorageBlockInfo> newBlocksInfo = new List<StorageBlockInfo>();
50-
51-
foreach (var block in BlocksInfo)
52-
{
53-
if (newBlocksInfo.Count > 0)
54-
{
55-
if (newBlocksInfo.Last().Flags != block.Flags)
56-
{
57-
throw new Exception("Expected all blocks to have the same flags");
58-
}
59-
long newSize = (long)newBlocksInfo.Last().UncompressedSize + block.UncompressedSize;
60-
if (newSize <= uint.MaxValue)
61-
{
62-
newBlocksInfo.Last().UncompressedSize = (uint)newSize;
63-
newBlocksInfo.Last().CompressedSize += block.CompressedSize;
64-
}
65-
else
66-
{
67-
newBlocksInfo.Add(block);
68-
}
69-
}
70-
else
71-
{
72-
newBlocksInfo.Add(block);
73-
}
74-
}
75-
76-
if (DirectoryInfo.Sum(x => x.Size) != newBlocksInfo.Sum(x => x.UncompressedSize))
77-
{
78-
throw new Exception("Expected all blocks size bigger than CAB size");
79-
}
80-
81-
BlocksInfo = newBlocksInfo;
82-
Update();
83-
}
84-
85-
public void ResizeBlocksInfos()
86-
{
87-
List<StorageBlockInfo> newBlocksInfo = new List<StorageBlockInfo>();
88-
foreach (var block in BlocksInfo)
89-
{
90-
if (block.UncompressedSize > 0x00020000)
91-
{
92-
uint num = block.UncompressedSize / 0x00020000;
93-
uint num2 = block.UncompressedSize % 0x00020000;
94-
for (uint i = 0; i < num; i++)
95-
{
96-
newBlocksInfo.Add(new StorageBlockInfo(0x00020000, 0x00020000, block.Flags));
97-
}
98-
if (num2 > 0)
99-
{
100-
newBlocksInfo.Add(new StorageBlockInfo(num2, num2, block.Flags));
101-
}
102-
}
103-
else
104-
{
105-
newBlocksInfo.Add(block);
106-
}
107-
}
108-
BlocksInfo = newBlocksInfo;
109-
Update();
110-
}
111-
112-
public void Update(CompressionType compressionType = CompressionType.None)
113-
{
114-
// 写入数据到BlocksInfoBytes
115-
var BlocksInfoStream = new MemoryStream();
116-
using AssetWriter writer = new AssetWriter(BlocksInfoStream);
117-
writer.Write(UncompressedDataHash);
118-
writer.WriteInt32(BlocksInfo.Count);
119-
foreach (var block in BlocksInfo)
120-
{
121-
//block.Write(writer);
122-
}
123-
writer.WriteInt32(DirectoryInfo.Count);
124-
foreach (var node in DirectoryInfo)
125-
{
126-
//node.Write(writer);
127-
}
128-
129-
uncompressedSize = (uint)BlocksInfoStream.Length;
130-
BlocksInfoBytes = Compression.CompressStream(BlocksInfoStream, compressionType);
131-
}
132-
133-
public void calculateSize(ref uint uncompressedBlocksInfoSize, ref uint compressedBlocksInfoSize)
134-
{
135-
uncompressedBlocksInfoSize = uncompressedSize;
136-
compressedBlocksInfoSize = (uint)BlocksInfoBytes.Count;
137-
}
138-
139-
public void FixSize(int diff)
140-
{
141-
142-
if (DirectoryInfo[^1].Size + diff <= UInt32.MaxValue && DirectoryInfo[^1].Size + diff > 0)
143-
{
144-
if (BlocksInfo[^1].UncompressedSize + diff <= UInt32.MaxValue && BlocksInfo[^1].UncompressedSize + diff > 0)
145-
{
146-
if (diff > 0)
147-
{
148-
BlocksInfo[^1].UncompressedSize += (uint)diff;
149-
BlocksInfo[^1].CompressedSize += (uint)diff;
150-
DirectoryInfo[^1].Size += (uint)diff;
151-
}
152-
else
153-
{
154-
BlocksInfo[^1].UncompressedSize -= (uint)-diff;
155-
BlocksInfo[^1].CompressedSize -= (uint)-diff;
156-
DirectoryInfo[^1].Size -= (uint)-diff;
157-
}
158-
}
159-
else
160-
{
161-
if (BlocksInfo[^1].UncompressedSize + diff <= UInt32.MaxValue)
162-
{
163-
BlocksInfo.Add(new StorageBlockInfo((uint)diff, (uint)diff ,BlocksInfo[^1].Flags));
164-
DirectoryInfo[^1].Size += (uint)diff;
165-
}
166-
else
167-
{
168-
diff += (int)BlocksInfo[^1].UncompressedSize;
169-
diff = -diff;
170-
BlocksInfo.Remove(BlocksInfo[^1]);
171-
BlocksInfo[^1].UncompressedSize -= (uint)diff;
172-
BlocksInfo[^1].CompressedSize -= (uint)diff;
173-
DirectoryInfo[^1].Size -= (uint)diff;
174-
}
175-
}
176-
}
177-
else
178-
{
179-
throw new Exception("DirectoryInfo size overflow");
180-
}
181-
}
182-
183-
public void Write(AssetWriter writer)
184-
{
185-
writer.Write(BlocksInfoBytes.ToArray());
186-
}*/
18735
}

UnityAsset.NET/BundleFiles/BundleFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class BundleFile
3535

3636
if ((header.Flags & unityCNMask) != 0)
3737
{
38+
if (key == null) key = Setting.DefaultUnityCNKey;
3839
if (key == null)
3940
{
4041
throw new Exception($"UnityCN key is required for decryption. UnityCN Flag Mask: {unityCNMask}");
@@ -224,7 +225,6 @@ public void Serialize(ref DataBuffer db, CompressionType infoPacker = Compressio
224225
offset += cabSize;
225226
}
226227
cabDataBuffer.Seek(0);
227-
cabDataBuffer.WriteToFile(@"F:\Rider\UnityAsset.NET\Test\cavn");
228228

229229
var blocksInfo = new List<StorageBlockInfo>();
230230
var blocksSize = offset;

UnityAsset.NET/Compression/Compression.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,20 @@ public static long CompressToBytes(ReadOnlySpan<byte> uncompressedData, Span<byt
118118
int compressedSizeHc = LZ4Codec.Encode(uncompressedData, compressedData, LZ4Level.L12_MAX);
119119
return compressedSizeHc;
120120
case CompressionType.Lzma:
121+
{
121122
var encoder = new Encoder();
122-
MemoryStream compressedStream = new MemoryStream();
123-
MemoryStream subStream = new MemoryStream();
123+
using MemoryStream compressedStream = new MemoryStream();
124+
using MemoryStream subStream = new MemoryStream();
124125
encoder.WriteCoderProperties(compressedStream);
125-
MemoryStream uncompressedStream = new MemoryStream(uncompressedData.ToArray());
126+
using MemoryStream uncompressedStream = new MemoryStream(uncompressedData.ToArray());
126127
encoder.Code(uncompressedStream, subStream, -1, -1, null);
127128
subStream.Position = 0;
128129
subStream.CopyTo(compressedStream);
129130
var size = compressedStream.Position;
130131
compressedStream.Position = 0;
131132
compressedStream.ReadExactly(compressedData.Slice(0, (int)size));
132133
return size;
134+
}
133135
default:
134136
throw new ArgumentException($"Unsupported compression type {compressionType}");
135137
}

UnityAsset.NET/Setting.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
public static class Setting
44
{
55
public static UInt32 DefaultChunkSize = 0x00020000;
6+
public static string? DefaultUnityCNKey = null;
67
}

0 commit comments

Comments
 (0)