Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 33c11e9

Browse files
committed
Fix bundle saving
1 parent 4ef755d commit 33c11e9

11 files changed

+176
-153
lines changed

BundleExporter/BundleExporter.csproj

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<Copyright>Copyright © 2020 aianlinb</Copyright>
5+
<Authors>aianlinb</Authors>
6+
<Copyright>Copyright © 2020 aianlinb.</Copyright>
67
<AssemblyVersion>2.3.0.0</AssemblyVersion>
8+
<Platforms>x64</Platforms>
79
<OutputType>Exe</OutputType>
810
</PropertyGroup>
911

10-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
13+
<OutputPath>..\Release</OutputPath>
1114
<DefineConstants>DEBUG;TRACE</DefineConstants>
15+
<DebugType>full</DebugType>
16+
<PlatformTarget>x64</PlatformTarget>
17+
</PropertyGroup>
18+
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
20+
<OutputPath>..\Release\Release</OutputPath>
1221
<PlatformTarget>x64</PlatformTarget>
1322
<Optimize>true</Optimize>
14-
<OutputPath>..\Release</OutputPath>
15-
<DebugType>full</DebugType>
1623
</PropertyGroup>
1724

1825
<ItemGroup>

FileListGenerator/FileListGenerator.csproj

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5-
<Copyright>Copyright © 2020 aianlinb</Copyright>
5+
<Authors>aianlinb</Authors>
6+
<Copyright>Copyright © 2020 aianlinb.</Copyright>
67
<AssemblyVersion>2.3.0.0</AssemblyVersion>
8+
<Platforms>x64</Platforms>
79
<OutputType>Exe</OutputType>
810
</PropertyGroup>
911

10-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
13+
<OutputPath>..\Release</OutputPath>
1114
<DefineConstants>DEBUG;TRACE</DefineConstants>
15+
<DebugType>full</DebugType>
16+
<PlatformTarget>x64</PlatformTarget>
17+
</PropertyGroup>
18+
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
20+
<OutputPath>..\Release\Release</OutputPath>
1221
<PlatformTarget>x64</PlatformTarget>
1322
<Optimize>true</Optimize>
14-
<OutputPath>..\Release</OutputPath>
15-
<DebugType>full</DebugType>
1623
</PropertyGroup>
1724

1825
<ItemGroup>

LibBundle.sln

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30503.244
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibBundle", "LibBundle\LibBundle.csproj", "{9B9CD245-6053-4F42-8256-0C4B413D82C8}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibBundle", "LibBundle\LibBundle.csproj", "{9B9CD245-6053-4F42-8256-0C4B413D82C8}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BundleExporter", "BundleExporter\BundleExporter.csproj", "{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BundleExporter", "BundleExporter\BundleExporter.csproj", "{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisualBundle", "VisualBundle\VisualBundle.csproj", "{FBDFA120-5123-4A64-B378-B6C252C5D978}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisualBundle", "VisualBundle\VisualBundle.csproj", "{FBDFA120-5123-4A64-B378-B6C252C5D978}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileListGenerator", "FileListGenerator\FileListGenerator.csproj", "{25E83531-FD68-4483-A133-C1A40EC762D7}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileListGenerator", "FileListGenerator\FileListGenerator.csproj", "{25E83531-FD68-4483-A133-C1A40EC762D7}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16-
Debug|Any CPU = Debug|Any CPU
17-
Release|Any CPU = Release|Any CPU
16+
Debug|x64 = Debug|x64
17+
Release|x64 = Release|x64
1818
EndGlobalSection
1919
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20-
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Release|Any CPU.Build.0 = Release|Any CPU
24-
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
27-
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Release|Any CPU.Build.0 = Release|Any CPU
28-
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29-
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Debug|Any CPU.Build.0 = Debug|Any CPU
30-
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Release|Any CPU.ActiveCfg = Release|Any CPU
31-
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{25E83531-FD68-4483-A133-C1A40EC762D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{25E83531-FD68-4483-A133-C1A40EC762D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{25E83531-FD68-4483-A133-C1A40EC762D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{25E83531-FD68-4483-A133-C1A40EC762D7}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Debug|x64.ActiveCfg = Debug|x64
21+
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Debug|x64.Build.0 = Debug|x64
22+
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Release|x64.ActiveCfg = Release|x64
23+
{9B9CD245-6053-4F42-8256-0C4B413D82C8}.Release|x64.Build.0 = Release|x64
24+
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Debug|x64.ActiveCfg = Debug|x64
25+
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Debug|x64.Build.0 = Debug|x64
26+
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Release|x64.ActiveCfg = Release|x64
27+
{EC9F6B3B-0B47-4DF2-9272-7372312ECDAF}.Release|x64.Build.0 = Release|x64
28+
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Debug|x64.ActiveCfg = Debug|x64
29+
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Debug|x64.Build.0 = Debug|x64
30+
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Release|x64.ActiveCfg = Release|x64
31+
{FBDFA120-5123-4A64-B378-B6C252C5D978}.Release|x64.Build.0 = Release|x64
32+
{25E83531-FD68-4483-A133-C1A40EC762D7}.Debug|x64.ActiveCfg = Debug|x64
33+
{25E83531-FD68-4483-A133-C1A40EC762D7}.Debug|x64.Build.0 = Debug|x64
34+
{25E83531-FD68-4483-A133-C1A40EC762D7}.Release|x64.ActiveCfg = Release|x64
35+
{25E83531-FD68-4483-A133-C1A40EC762D7}.Release|x64.Build.0 = Release|x64
3636
EndGlobalSection
3737
GlobalSection(SolutionProperties) = preSolution
3838
HideSolutionNode = FALSE

LibBundle/BundleContainer.cs

+39-45
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
43
using System.IO;
54
using System.Linq;
5+
using System.Runtime.InteropServices;
66
using System.Threading;
77
using System.Threading.Tasks;
88

99
namespace LibBundle
1010
{
1111
public class BundleContainer
1212
{
13-
[System.Runtime.InteropServices.DllImport("oo2core_8_win64.dll")]
14-
public static extern int OodleLZ_Decompress(byte[] buffer, int bufferSize, byte[] result, long outputBufferSize, int a, int b, int c, IntPtr d, long e, IntPtr f, IntPtr g, IntPtr h, long i, int ThreadModule);
15-
[System.Runtime.InteropServices.DllImport("oo2core_8_win64.dll")]
13+
[DllImport("oo2core_8_win64.dll")]
14+
[SuppressMessage("Interoperability", "CA1401:不應看得見 P/Invoke")]
15+
public static extern int OodleLZ_Decompress(byte[] buffer, int bufferSize, byte[] result, long outputBufferSize, int a, int b, int c, IntPtr d, long e, IntPtr f, IntPtr g, IntPtr h, long i, int ThreadModule);
16+
[DllImport("oo2core_8_win64.dll")]
17+
[SuppressMessage("Interoperability", "CA1401:不應看得見 P/Invoke")]
1618
public static extern int OodleLZ_Compress(ENCODE_TYPES format, byte[] buffer, long bufferSize, byte[] outputBuffer, COMPRESSTION_LEVEL level, IntPtr opts, long offs, long unused, IntPtr scratch, long scratch_size);
1719
public enum ENCODE_TYPES
1820
{
@@ -63,15 +65,15 @@ public enum COMPRESSTION_LEVEL
6365
public int unknown5; // 0
6466
public int unknown6; // 0
6567

66-
//For UnPacking
68+
// For UnPacking
6769
public BundleContainer(string path)
6870
{
6971
this.path = path;
7072
var br = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
7173
Initialize(br);
7274
br.Close();
7375
}
74-
//For UnPacking
76+
// For UnPacking
7577
public BundleContainer(BinaryReader br)
7678
{
7779
Initialize(br);
@@ -94,7 +96,7 @@ private void Initialize(BinaryReader br)
9496
unknown6 = br.ReadInt32();
9597
}
9698

97-
//For Packing
99+
// For Packing
98100
public BundleContainer()
99101
{
100102
offset = 0;
@@ -104,36 +106,34 @@ public BundleContainer()
104106
unknown3 = unknown4 = unknown5 = unknown6 = 0;
105107
}
106108

107-
//UnPacking
109+
// UnPacking
108110
public virtual MemoryStream Read(string path = null)
109111
{
110-
if (path == null)
111-
path = this.path;
112112
offset = 0;
113-
var br = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
113+
var br = new BinaryReader(File.OpenRead(path ?? this.path));
114114
var ms = Read(br);
115115
br.Close();
116116
return ms;
117117
}
118-
//UnPacking
118+
// UnPacking
119119
public virtual MemoryStream Read(BinaryReader br)
120120
{
121121
br.BaseStream.Seek(offset + 60, SeekOrigin.Begin);
122122

123123
var chunks = new int[entry_count];
124-
for (int i = 0; i < entry_count; i++)
124+
for (var i = 0; i < entry_count; i++)
125125
chunks[i] = br.ReadInt32();
126126

127127
var compressed = new byte[entry_count][];
128128
for (int i = 0; i < entry_count; i++)
129129
compressed[i] = br.ReadBytes(chunks[i]);
130130

131-
Parallel.For(0, entry_count, i => {
132-
var size = (i + 1 == entry_count) ? uncompressed_size - (chunk_size * (entry_count - 1)) : chunk_size; // isLast ?
133-
var toSave = new byte[size + 64];
134-
OodleLZ_Decompress(compressed[i], compressed[i].Length, toSave, size, 0, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, 3);
135-
compressed[i] = toSave.Take(size).ToArray();
136-
});
131+
Parallel.For(0, entry_count, i => {
132+
var size = (i + 1 == entry_count) ? uncompressed_size - (chunk_size * (entry_count - 1)) : chunk_size; // isLast ?
133+
var toSave = new byte[size + 64];
134+
_ = OodleLZ_Decompress(compressed[i], compressed[i].Length, toSave, size, 0, 0, 0, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, 3);
135+
compressed[i] = toSave.Take(size).ToArray();
136+
});
137137

138138
var data = new MemoryStream(uncompressed_size);
139139
for (int i = 0; i < entry_count; i++)
@@ -144,10 +144,8 @@ public virtual MemoryStream Read(BinaryReader br)
144144

145145
public virtual byte[] AppendAndSave(Stream newData, string path = null)
146146
{
147-
if (path == null)
148-
path = this.path;
149147
offset = 0;
150-
return AppendAndSave(newData, File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
148+
return AppendAndSave(newData, File.Open(path ?? this.path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
151149
}
152150

153151
public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
@@ -158,7 +156,7 @@ public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
158156

159157
var lastCunkCompressedSize = originalData.ReadByte() | originalData.ReadByte() << 8 | originalData.ReadByte() << 16 | originalData.ReadByte() << 24; // ReadInt32
160158

161-
var lastCunkDecompressedSize = uncompressed_size - (chunk_size * (entry_count - 1));
159+
var lastCunkDecompressedSize = uncompressed_size - chunk_size * (entry_count - 1);
162160

163161
uncompressed_size = (int)(size_decompressed += newData.Length);
164162
entry_count = uncompressed_size / chunk_size;
@@ -168,7 +166,7 @@ public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
168166
var msToSave = new MemoryStream();
169167
var bw = new BinaryWriter(msToSave);
170168

171-
msToSave.Seek(60 + (entry_count * 4), SeekOrigin.Begin);
169+
msToSave.Seek(60 + entry_count * 4, SeekOrigin.Begin);
172170
var o = new byte[compressed_size - lastCunkCompressedSize];
173171
originalData.Read(o);
174172
bw.Write(o);
@@ -180,19 +178,19 @@ public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
180178

181179
newData.Seek(0, SeekOrigin.Begin);
182180
compressed_size -= lastCunkCompressedSize;
183-
var NewChunkCompressedSizes = new int[entry_count - (OldChunkCompressedSizes.Length / 4)];
181+
var NewChunkCompressedSizes = new int[entry_count - OldChunkCompressedSizes.Length / 4];
184182

185183
var FirstNewDataChunk = new byte[Math.Min(chunk_size - lastCunkDecompressedSize, newData.Length)];
186184
newData.Read(FirstNewDataChunk);
187-
FirstNewDataChunk = lastCunkDecompressedData.Take(lastCunkDecompressedSize).Concat(FirstNewDataChunk).ToArray(); // Decompressed
185+
FirstNewDataChunk = lastCunkDecompressedData.Take(lastCunkDecompressedSize).Concat(FirstNewDataChunk).ToArray();
188186
var CompressedChunk = new byte[FirstNewDataChunk.Length + 548];
189187
var CompressedLength = OodleLZ_Compress(encoder, FirstNewDataChunk, FirstNewDataChunk.Length, CompressedChunk, Compression_Level, IntPtr.Zero, 0, 0, IntPtr.Zero, 0);
190188
compressed_size += NewChunkCompressedSizes[0] = CompressedLength;
191-
bw.Write(CompressedChunk, 0, CompressedLength); // Compressed
189+
bw.Write(CompressedChunk, 0, CompressedLength); // The Length is important
192190
var byteArrays = new byte[NewChunkCompressedSizes.Length][];
193-
for (int i = 1; i < NewChunkCompressedSizes.Length; i++)
191+
for (var i = 1; i < NewChunkCompressedSizes.Length; i++)
194192
{
195-
var size = (i + 1 == NewChunkCompressedSizes.Length) ? uncompressed_size - (chunk_size * (entry_count - 1)) : chunk_size;
193+
var size = (i + 1 == NewChunkCompressedSizes.Length) ? uncompressed_size - chunk_size * (entry_count - 1) : chunk_size;
196194
newData.Read(byteArrays[i] = new byte[size]);
197195
}
198196
Parallel.For(1, NewChunkCompressedSizes.Length, i => {
@@ -201,16 +199,14 @@ public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
201199
byteArrays[i] = by;
202200
Interlocked.Add(ref compressed_size, NewChunkCompressedSizes[i] = l);
203201
});
204-
for (int i = 0; i < NewChunkCompressedSizes.Length - 1; i++)
202+
for (int i = 1; i < NewChunkCompressedSizes.Length; i++)
205203
bw.Write(byteArrays[i], 0, NewChunkCompressedSizes[i]);
206204

207205
size_compressed = compressed_size;
208206

209207
msToSave.Seek(60, SeekOrigin.Begin);
210208
bw.Write(OldChunkCompressedSizes);
211-
for (int i = 0; i < NewChunkCompressedSizes.Length; i++)
212-
bw.Write(NewChunkCompressedSizes[i]);
213-
209+
foreach (var t in NewChunkCompressedSizes) bw.Write(t);
214210
msToSave.Seek(0, SeekOrigin.Begin);
215211
bw.Write(uncompressed_size);
216212
bw.Write(compressed_size);
@@ -232,21 +228,21 @@ public virtual byte[] AppendAndSave(Stream newData, Stream originalData)
232228
return result;
233229
}
234230

235-
//Packing
231+
// Packing
236232
public virtual void Save(Stream newData, string path)
237233
{
238-
var bw = new BinaryWriter(File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite));
234+
var bw = new BinaryWriter(File.OpenWrite(path));
239235

240236
uncompressed_size = (int)(size_decompressed = newData.Length);
241237
entry_count = uncompressed_size / chunk_size;
242238
if (uncompressed_size % chunk_size != 0) entry_count++;
243239
head_size = entry_count * 4 + 48;
244240

245-
bw.BaseStream.Seek(60 + (entry_count * 4), SeekOrigin.Begin);
241+
bw.BaseStream.Seek(60 + entry_count * 4, SeekOrigin.Begin);
246242
newData.Seek(0, SeekOrigin.Begin);
247243
compressed_size = 0;
248244
var chunks = new int[entry_count];
249-
for (int i = 0; i < entry_count; i++)
245+
for (var i = 0; i < entry_count; i++)
250246
{
251247
var b = new byte[i + 1 == entry_count ? uncompressed_size - (entry_count - 1) * chunk_size : chunk_size];
252248
newData.Read(b, 0, b.Length);
@@ -258,8 +254,7 @@ public virtual void Save(Stream newData, string path)
258254
size_compressed = compressed_size;
259255

260256
bw.BaseStream.Seek(60, SeekOrigin.Begin);
261-
for (int i = 0; i < entry_count; i++)
262-
bw.Write(chunks[i]);
257+
foreach (var c in chunks) bw.Write(c);
263258

264259
bw.BaseStream.Seek(0, SeekOrigin.Begin);
265260
bw.Write(uncompressed_size);
@@ -279,7 +274,7 @@ public virtual void Save(Stream newData, string path)
279274
bw.Flush();
280275
bw.Close();
281276
}
282-
//Packing
277+
// Packing
283278
public virtual byte[] Save(Stream newData)
284279
{
285280
var msToSave = new MemoryStream();
@@ -290,12 +285,12 @@ public virtual byte[] Save(Stream newData)
290285
if (uncompressed_size % chunk_size != 0) entry_count++;
291286
head_size = entry_count * 4 + 48;
292287

293-
msToSave.Seek(60 + (entry_count * 4), SeekOrigin.Begin);
288+
msToSave.Seek(60 + entry_count * 4, SeekOrigin.Begin);
294289
newData.Seek(0, SeekOrigin.Begin);
295290
compressed_size = 0;
296291
var chunks = new int[entry_count];
297292
var byteArrays = new byte[entry_count][];
298-
for (int i = 0; i < entry_count; i++)
293+
for (var i = 0; i < entry_count; i++)
299294
{
300295
var b = new byte[i + 1 == entry_count ? uncompressed_size - (entry_count - 1) * chunk_size : chunk_size];
301296
newData.Read(byteArrays[i] = b);
@@ -311,8 +306,7 @@ public virtual byte[] Save(Stream newData)
311306
bw.Write(byteArrays[i], 0, chunks[i]);
312307

313308
msToSave.Seek(60, SeekOrigin.Begin);
314-
for (int i = 0; i < entry_count; i++)
315-
bw.Write(chunks[i]);
309+
foreach (var c in chunks) bw.Write(c);
316310

317311
msToSave.Seek(0, SeekOrigin.Begin);
318312
bw.Write(uncompressed_size);

0 commit comments

Comments
 (0)