Skip to content

Commit

Permalink
Merge pull request #56 from burninrubber0/dotnetzip-removal
Browse files Browse the repository at this point in the history
Remove DotNetZip
  • Loading branch information
burninrubber0 authored Nov 13, 2024
2 parents 26c1f00 + dceec09 commit 63c2c97
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 72 deletions.
1 change: 1 addition & 0 deletions BaseHandlers/BaseHandlers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DebugHelper" Version="1.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)BundleManager\bin\$(ConfigurationName)\$(TargetFramework)\plugins\&quot; /s /e /y" />
Expand Down
4 changes: 2 additions & 2 deletions BundleFormat/BundleFormat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BundleUtilities\BundleUtilities.csproj" />
<PackageReference Include="LibDeflate.NET" Version="1.19.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DotNetZip" Version="1.16.0" />
<ProjectReference Include="..\BundleUtilities\BundleUtilities.csproj" />
</ItemGroup>
</Project>
63 changes: 10 additions & 53 deletions BundleFormat/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Text;
using System.Windows.Forms;
using Ionic.Zlib;
using LibDeflate;

namespace BundleFormat
{
public static class Extensions
{
private static Decompressor decompressor = new ZlibDecompressor();
private static Compressor compressor = new ZlibCompressor(9);

public static string AsString(this byte[] self)
{
if (self == null)
Expand Down Expand Up @@ -42,64 +45,18 @@ public static string MakePreview(this byte[] self, int start, int end)

public static byte[] Compress(this byte[] self)
{
byte[] compressedData = new byte[self.Length]; // Size not known yet, use uncompressed size as upper bound
ZlibStream zlibStream = new ZlibStream(new MemoryStream(self), CompressionMode.Compress, CompressionLevel.BestCompression);
zlibStream.Read(compressedData, 0, self.Length);
return new ArraySegment<byte>(compressedData, 0, (int)zlibStream.TotalOut).ToArray(); // Size known, return correctly sized segment
return compressor.Compress(self).Memory.ToArray();
}

public static byte[] Decompress(this byte[] self, int uncompressedSize)
{
byte[] uncompressedData = new byte[uncompressedSize];
ZlibStream zlibStream = new ZlibStream(new MemoryStream(uncompressedData), CompressionMode.Decompress);
try
{
zlibStream.Write(self, 0, self.Length);
}
catch (Exception e)
{
if (self[self.Length - 1] == 0
&& e.Message == "Bad state (incorrect data check)") // Likely a bugged resource
{
uncompressedData = GetDataFromBadAlignedResource(self, uncompressedSize);
if (uncompressedData != null)
return uncompressedData;
}
MessageBox.Show(e.ToString(), e.Source, MessageBoxButtons.OK);
return null;
}
return uncompressedData;
}

// Validate resources from BM versions <0.3.0 where alignment corrupted the checksum
public static byte[] GetDataFromBadAlignedResource(byte[] original, int uncompressedSize)
{
// For some reason, the data validates when length is 1 less than it should be.
// Use this to get the correct uncompressed data.
byte[] uncompressedData = new byte[uncompressedSize];
ZlibStream zlibStream = new ZlibStream(new MemoryStream(uncompressedData), CompressionMode.Decompress);
byte[] trimmed = new ArraySegment<byte>(original, 0, original.Length - 1).ToArray();
try
{
zlibStream.Write(trimmed, 0, trimmed.Length);
}
catch (Exception)
var status = decompressor.Decompress(self, uncompressedSize, out var owner, out var bytesRead);
if (status != OperationStatus.Done)
{
MessageBox.Show("Error decompressing data, status: " + status.ToString() + ", read: " + bytesRead.ToString(), "Error", MessageBoxButtons.OK);
return null;
}

byte[] compressed = Compress(uncompressedData);

// Test first three checksum bytes
if (original[original.Length - 4] == compressed[compressed.Length - 4]
&& original[original.Length - 3] == compressed[compressed.Length - 3]
&& original[original.Length - 2] == compressed[compressed.Length - 2])
{
// Testing of data not necessary, likelihood of 24 bits matching without data matching is negligible
return uncompressedData;
}

return null;
return owner.Memory.ToArray();
}

public static bool Matches(this byte[] self, byte[] other)
Expand Down
1 change: 1 addition & 0 deletions BundleManager/BundleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DebugHelper" Version="1.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(SolutionDir)libs\*&quot; &quot;$(ProjectDir)$(OutDir)&quot; /s /e /y&#xD;&#xA;xcopy &quot;$(SolutionDir)resources\*&quot; &quot;$(ProjectDir)$(OutDir)&quot; /s /e /y" />
Expand Down
1 change: 1 addition & 0 deletions BurnoutImage/BurnoutImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<ItemGroup>
<PackageReference Include="BCnEncoder.Net" Version="2.1.0" />
<PackageReference Include="BCnEncoder.Net.ImageSharp" Version="1.1.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
</Project>
32 changes: 16 additions & 16 deletions PVSFormat/PVSEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PVSFormat/PVSFormat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DebugHelper" Version="1.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)BundleManager\bin\$(ConfigurationName)\$(TargetFramework)\plugins\&quot; /s /e /y" />
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A Program to work with Burnout Paradise Bundle files.
# Libraries Used
* [BCnEncoder](https://github.com/Nominom/BCnEncoder.NET)
* [DebugHelper](https://gitlab.com/mattparizeau/DebugHelper)
* [DotNetZip](https://github.com/haf/DotNetZip.Semverd)
* [LibDeflate.NET](https://github.com/jzebedee/LibDeflate.NET)
* [OpenTK](https://github.com/opentk/opentk)

# Credits
Expand Down

0 comments on commit 63c2c97

Please sign in to comment.