Skip to content

Commit

Permalink
Added some barebones test coverage for Lz77 compression.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Dec 1, 2024
1 parent 009f9c9 commit 420e75a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Fin.Compression_Tests</RootNamespace>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />

</ItemGroup>

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

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions FinModelUtility/Fin/Fin.Compression Tests/Lz770x10GoldenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Reflection;

using fin.compression;
using fin.io;
using fin.testing;

namespace Fin.Compression_Tests;

public class Lz77GoldenTests {
[Test]
[TestCaseSource(nameof(Get0x10GoldenDirectories_))]
public void Test0x10(
IFileHierarchyDirectory goldenDirectory)
=> this.AssertGolden(goldenDirectory);

private static IFileHierarchyDirectory[] Get0x10GoldenDirectories_()
=> GoldenAssert.GetGoldenDirectories(
GoldenAssert.GetRootGoldensDirectory(
Assembly.GetExecutingAssembly())
.AssertGetExistingSubdir("Lz77/0x10"))
.ToArray();

public void AssertGolden(IFileHierarchyDirectory goldenSubdir) {
var inputDirectory = goldenSubdir.AssertGetExistingSubdir("input");
var lz10File = inputDirectory.FilesWithExtension(".lz77").Single();

var outputDirectory = goldenSubdir.AssertGetExistingSubdir("output");
var hasGoldenExport = !outputDirectory.IsEmpty;

GoldenAssert.RunInTestDirectory(
goldenSubdir,
tmpDirectory => {
var targetDirectory =
hasGoldenExport ? tmpDirectory : outputDirectory.Impl;

using var br = lz10File.OpenReadAsBinary();
var decompressedBytes = new Lz77Decompressor().Decompress(br);

var targetFile = new FinFile(
Path.Combine(targetDirectory.FullPath,
$"{lz10File.NameWithoutExtension}.bin"));
targetFile.WriteAllBytes(decompressedBytes);

if (hasGoldenExport) {
GoldenAssert.AssertFilesInDirectoriesAreIdentical(
tmpDirectory,
outputDirectory.Impl);
}
});
}
}
Binary file not shown.
11 changes: 11 additions & 0 deletions FinModelUtility/FinModelUtility.sln
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brres", "Formats\Brres\Brre
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fin.Compression", "Fin\Fin.Compression\Fin.Compression.csproj", "{01B2C0BD-02AA-4273-8277-8D3016D7A738}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fin.Compression Tests", "Fin\Fin.Compression Tests\Fin.Compression Tests.csproj", "{A5BE17A0-2910-4791-90AC-74FA8A924E48}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -687,6 +689,14 @@ Global
{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
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Debug|x64.ActiveCfg = Debug|x64
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Debug|x64.Build.0 = Debug|x64
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Debug|x86.ActiveCfg = Debug|x86
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Debug|x86.Build.0 = Debug|x86
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Release|x64.ActiveCfg = Release|x64
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Release|x64.Build.0 = Release|x64
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Release|x86.ActiveCfg = Release|x86
{A5BE17A0-2910-4791-90AC-74FA8A924E48}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -772,6 +782,7 @@ Global
{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}
{A5BE17A0-2910-4791-90AC-74FA8A924E48} = {98E57180-48B9-4648-AE42-7C7C67120471}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4CFECBD7-FE88-4CC9-8E1B-6F7805F4F45A}
Expand Down
48 changes: 48 additions & 0 deletions FinModelUtility/Formats/Nitro/Nitro/src/api/NsbmdModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using fin.model.io.importers;
using fin.util.sets;

using schema.binary;
using schema.binary.attributes;

namespace nitro.api;

public class NsbmdModelImporter : IModelImporter<NsbmdModelFileBundle> {
Expand All @@ -20,6 +23,51 @@ public IModel Import(NsbmdModelFileBundle fileBundle) {
var nsbmdData
= new Lz77Decompressor().Decompress(nsbmdFile.OpenReadAsBinary());

var ms = new MemoryStream(nsbmdData);
var br = new SchemaBinaryReader(ms);
var nsbmd = br.ReadNew<Nsbmd>();

return model;
}
}

[BinarySchema]
public partial class Nsbmd : IBinaryDeserializable {
public uint SectionSize { get; set; }
public uint RenderCommandsOffset { get; set; }
public uint MaterialsOffset { get; set; }
public uint PiecesOffset { get; set; }
public uint InverseBindsOffset { get; set; }

[SequenceLengthSource(3)]
public byte[] Unknown1 { get; set; }

public byte NumObjects { get; set; }
public byte NumMaterials { get; set; }
public byte NumPieces { get; set; }

[SequenceLengthSource(2)]
public byte[] Unknown2 { get; set; }

// TODO: Fixed-point float
public uint UpScale { get; set; }

// TODO: Fixed-point float
public uint DownScale { get; set; }

public ushort NumVerts { get; set; }
public ushort NumSurfs { get; set; }
public ushort NumTris { get; set; }
public ushort NumQuads { get; set; }

// TODO: Fixed-point half
public ushort BoundingBoxXMin { get; set; }
public ushort BoundingBoxYMin { get; set; }
public ushort BoundingBoxZMin { get; set; }
public ushort BoundingBoxXMax { get; set; }
public ushort BoundingBoxYMax { get; set; }
public ushort BoundingBoxZMax { get; set; }

[SequenceLengthSource(8)]
public byte[] Unknown3 { get; set; }
}

0 comments on commit 420e75a

Please sign in to comment.