Skip to content

Commit

Permalink
Pulled out Fin's compression logic into a new project.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Dec 1, 2024
1 parent 4afa7db commit 009f9c9
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 55 deletions.
15 changes: 15 additions & 0 deletions FinModelUtility/Fin/Fin.Compression/Fin.Compression.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>fin.compression</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Fin\Fin.csproj" />
<PackageReference Include="schema" Version="0.6.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
using System;
using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;
using System.Text.RegularExpressions;

using fin.math;
using fin.schema;
using fin.util.asserts;
using fin.util.strings;

using schema.binary;

namespace fin.decompression;
namespace fin.compression;

/// <summary>
/// Shamelessly stolen from:
/// https://github.com/scurest/apicula/blob/3d4e91e14045392a49c89e86dab8cb936225588c/src/decompress/mod.rs
/// </summary>
public class Lz77Decompressor : BBinaryReaderToArrayDecompressor {
public override bool TryDecompress(IBinaryReader br, out byte[] data) {
br.PushContainerEndianness(Endianness.LittleEndian);
br.AssertString("LZ77");

var compressionType = br.ReadByte();
var decompressedSize = ReadDecompressedSize_(br);
br.PopEndianness();

br.PushContainerEndianness(Endianness.BigEndian);
switch (compressionType) {
case 0x10: {
data = Decompress10_(br);
data = Decompress10_(br, decompressedSize);
return true;
}
case 0x11: {
data = Decompress11_(br);
data = Decompress11_(br, decompressedSize);
return true;
}
}
br.PopEndianness();

data = default;
return false;
}

private static byte[] Decompress10_(IBinaryReader br) {
var decompressedSize = ReadDecompressedSize_(br);
var data = new List<byte>((int) decompressedSize);
private static uint ReadDecompressedSize_(IBinaryReader br) {
var decompressedSize = br.ReadUInt24();
if (decompressedSize == 0) {
decompressedSize = br.ReadUInt32();
}

if (decompressedSize < 40) {
Asserts.Fail($"LZ77 decompressed size is too small: {decompressedSize}");
}

if (decompressedSize > (1 << 19) * 4) {
Asserts.Fail($"LZ77 decompressed size is too big: {decompressedSize}");
}

return decompressedSize;
}

private static byte[] Decompress10_(IBinaryReader br, uint decompressedSize) {
var data = new List<byte>((int) decompressedSize);
while (data.Count < decompressedSize) {
var flags = br.ReadByte();

Expand Down Expand Up @@ -67,7 +80,7 @@ private static byte[] Decompress10_(IBinaryReader br) {
Asserts.Fail("Not enough data!");
}

for (var ii = 0; ii < 8; ++ii) {
for (var ii = 0; ii < n; ++ii) {
var x = data[data.Count - ofs];
data.Add(x);
}
Expand All @@ -82,10 +95,8 @@ private static byte[] Decompress10_(IBinaryReader br) {
return data.ToArray();
}

private static byte[] Decompress11_(IBinaryReader br) {
var decompressedSize = ReadDecompressedSize_(br);
private static byte[] Decompress11_(IBinaryReader br, uint decompressedSize) {
var data = new List<byte>((int) decompressedSize);

while (data.Count < decompressedSize) {
var flags = br.ReadByte();
for (var i = 0; i < 8; ++i) {
Expand Down Expand Up @@ -159,21 +170,4 @@ private static byte[] Decompress11_(IBinaryReader br) {

return data.ToArray();
}

private static uint ReadDecompressedSize_(IBinaryReader br) {
var decompressedSize = br.ReadUInt24();
if (decompressedSize == 0) {
decompressedSize = br.ReadUInt32();
}

if (decompressedSize < 40) {
Asserts.Fail($"LZ77 decompressed size is too small: {decompressedSize}");
}

if (decompressedSize > (1 << 19) * 4) {
Asserts.Fail($"LZ77 decompressed size is too big: {decompressedSize}");
}

return decompressedSize;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;

using fin.math;
using fin.schema;
using fin.util.asserts;
using fin.util.strings;

using schema.binary;

namespace fin.decompression;
using Asserts = fin.util.asserts.Asserts;

namespace fin.compression;

public class LzssDecompressor {
public bool TryToDecompress(IBinaryReader br, out byte[]? data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace fin.decompression;
namespace fin.compression;

public interface IArrayToArrayDecompressor {
bool TryDecompress(byte[] src, out byte[] dst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using schema.binary;

namespace fin.decompression;
namespace fin.compression;

public interface IBinaryReaderToArrayDecompressor {
bool TryDecompress(IBinaryReader br, out byte[] dst);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace fin.decompression;
namespace fin.compression;

public interface ISpanDecompressor {
bool TryToGetLength(ReadOnlySpan<byte> src, out int length);
Expand Down
11 changes: 11 additions & 0 deletions FinModelUtility/FinModelUtility.sln
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Brres", "Brres", "{6A9BD73D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brres", "Formats\Brres\Brres\Brres.csproj", "{BB25DCC2-44E0-4CD7-B666-3BC2CD3EB650}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fin.Compression", "Fin\Fin.Compression\Fin.Compression.csproj", "{01B2C0BD-02AA-4273-8277-8D3016D7A738}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -677,6 +679,14 @@ Global
{BB25DCC2-44E0-4CD7-B666-3BC2CD3EB650}.Release|x64.Build.0 = Release|x64
{BB25DCC2-44E0-4CD7-B666-3BC2CD3EB650}.Release|x86.ActiveCfg = Release|x86
{BB25DCC2-44E0-4CD7-B666-3BC2CD3EB650}.Release|x86.Build.0 = Release|x86
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Debug|x64.ActiveCfg = Debug|x64
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Debug|x64.Build.0 = Debug|x64
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Debug|x86.ActiveCfg = Debug|x86
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Debug|x86.Build.0 = Debug|x86
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Release|x64.ActiveCfg = Release|x64
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Release|x64.Build.0 = Release|x64
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Release|x86.ActiveCfg = Release|x86
{01B2C0BD-02AA-4273-8277-8D3016D7A738}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -761,6 +771,7 @@ Global
{7E7898D3-BA81-41C8-95F5-C7A5266C1392} = {27A04A78-53EB-4B6A-BE48-8345642D1B52}
{6A9BD73D-0443-491E-BC65-9656F197571F} = {F825789E-A737-4B16-AB6B-37342AC56705}
{BB25DCC2-44E0-4CD7-B666-3BC2CD3EB650} = {6A9BD73D-0443-491E-BC65-9656F197571F}
{01B2C0BD-02AA-4273-8277-8D3016D7A738} = {98E57180-48B9-4648-AE42-7C7C67120471}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4CFECBD7-FE88-4CC9-8E1B-6F7805F4F45A}
Expand Down
2 changes: 1 addition & 1 deletion FinModelUtility/Formats/F3dzex2/io/IReadOnlyN64Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;

using fin.data.dictionaries;
using fin.decompression;
using fin.compression;
using fin.io;
using fin.util.asserts;
using fin.util.hex;
Expand Down
1 change: 1 addition & 0 deletions FinModelUtility/Formats/Grezzo/Grezzo/Grezzo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Fin\Fin.Compression\Fin.Compression.csproj" />
<ProjectReference Include="..\..\..\Fin\Fin\Fin.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;

using fin.decompression;
using fin.compression;

using schema.binary;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using fin.decompression;
using fin.compression;

namespace level5.decompression;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using fin.decompression;
using fin.compression;

namespace level5.decompression;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.CompilerServices;

using fin.decompression;
using fin.compression;

namespace level5.decompression;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using fin.decompression;
using fin.compression;

namespace level5.decompression;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO.Compression;

using fin.decompression;
using fin.compression;

namespace level5.decompression;

Expand Down
2 changes: 1 addition & 1 deletion FinModelUtility/Formats/Level5/Level5/src/schema/Xc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using fin.data.dictionaries;
using fin.decompression;
using fin.compression;

using level5.decompression;

Expand Down
1 change: 1 addition & 0 deletions FinModelUtility/Formats/Nitro/Nitro/Nitro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Fin\Fin.Compression\Fin.Compression.csproj" />
<ProjectReference Include="..\..\..\Fin\Fin\Fin.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using fin.model;
using fin.compression;
using fin.io;
using fin.model;
using fin.model.impl;
using fin.model.io.importers;
using fin.util.sets;
Expand All @@ -15,6 +17,9 @@ public IModel Import(NsbmdModelFileBundle fileBundle) {
Files = files,
};

var nsbmdData
= new Lz77Decompressor().Decompress(nsbmdFile.OpenReadAsBinary());

return model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* distribution.
*/

using fin.decompression;
using fin.compression;

namespace visceral.decompression;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using f3dzex2.io;

using fin.decompression;
using fin.compression;
using fin.util.asserts;
using fin.util.enumerables;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using fin.decompression;
using fin.io;
using fin.io;
using fin.io.archive;
using fin.util.strings;

using schema.binary;

using uni.platforms.threeDs.tools.gar.schema;

using LzssDecompressor = fin.compression.LzssDecompressor;

namespace uni.platforms.threeDs.tools.gar;

public class GarReader : IArchiveReader<SubArchiveContentFile> {
Expand Down

0 comments on commit 009f9c9

Please sign in to comment.