Skip to content

Commit

Permalink
Merge pull request donkeyProgramming#205 from mr-phazer/Unit-Tests-Fo…
Browse files Browse the repository at this point in the history
…r-Import

Unit testing for gltf importing
  • Loading branch information
donkeyProgramming authored Dec 10, 2024
2 parents c934d4a + c254e16 commit b6809ff
Show file tree
Hide file tree
Showing 19 changed files with 2,374 additions and 36 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Editors.ImportExport.Exporting.Presentation.RmvToGltf;
using Editors.ImportExport.Importing;
using Editors.ImportExport.Importing.Importers.GltfToRmv;
using Editors.ImportExport.Importing.Importers.GltfToRmv.Helper;
using Microsoft.Extensions.DependencyInjection;
using Shared.Core.DependencyInjection;
using Shared.Core.DevConfig;
Expand Down Expand Up @@ -53,9 +54,10 @@ public override void Register(IServiceCollection services)
services.AddTransient<GltfMeshBuilder>();
services.AddTransient<IGltfTextureHandler, GltfTextureHandler>();
services.AddTransient<IGltfSceneSaver, GltfSceneSaver>();
services.AddTransient<IGltfSceneLoader, GltfSceneLoader>();
services.AddTransient<GltfSkeletonBuilder>();
services.AddTransient<GltfAnimationBuilder>();

services.AddTransient<GltfAnimationBuilder>();
RegisterAllAsInterface<IDeveloperConfiguration>(services, ServiceLifetime.Transient);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Execute(TreeNode clickedNode)
if (string.IsNullOrEmpty(glftFilePath))
return;

var settings = new GltfImporterSettings(glftFilePath, true, clickedNode);
var settings = new GltfImporterSettings(glftFilePath, clickedNode.GetFullPath(), clickedNode.FileOwner, true, true, true);
_importer.Import(settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@

namespace Editors.ImportExport.Importing.Importers.GltfToRmv
{
public record GltfImporterSettings
(
string InputGltfFile,
bool ConvertNormalTextureToOrnge,
TreeNode destinationPackNode
);

public class GltfImporter
{
private readonly IPackFileService _packFileService;
Expand Down Expand Up @@ -77,8 +70,8 @@ public void Import(GltfImporterSettings settings)

var packFileImported = new PackFile(importedFileName, new MemorySource(bytesRmv2));

var newFile = new NewPackFileEntry(settings.destinationPackNode.GetFullPath(), packFileImported);
_packFileService.AddFilesToPack(settings.destinationPackNode.FileOwner, [newFile]);
var newFile = new NewPackFileEntry(settings.DestinationPackPath, packFileImported);
_packFileService.AddFilesToPack(settings.DestinationPackFileContainer, [newFile]);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Shared.Core.PackFiles.Models;
using Shared.GameFormats.RigidModel;
using SharpGLTF.Schema2;
using Editors.ImportExport.Importing.Importers.GltfToRmv.Helper;
using System.IO;
using Shared.Core.PackFiles;
using Shared.Ui.BaseDialogs.PackFileBrowser;
using static Shared.Core.PackFiles.IPackFileService;
using Shared.Core.ErrorHandling.Exceptions;
using Shared.Core.Services;

namespace Editors.ImportExport.Importing.Importers.GltfToRmv
{
public record GltfImporterSettings
(
string InputGltfFile,
string DestinationPackPath,
PackFileContainer DestinationPackFileContainer,
bool ConvertNormalTextureToOrangeType,
bool ImportAnimations,
bool MirrorMesh
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared.Core.ErrorHandling.Exceptions;
using SharpGLTF.Schema2;
using System.Windows;
using Shared.Core.Services;

namespace Editors.ImportExport.Importing.Importers.GltfToRmv.Helper
{
public interface IGltfSceneLoader
{
public ModelRoot? Load(GltfImporterSettings settings);
}

public class GltfSceneLoader : IGltfSceneLoader
{
private readonly IStandardDialogs _exceptionService;

public GltfSceneLoader(IStandardDialogs exceptionService)
{
_exceptionService = exceptionService;
}

public ModelRoot? Load(GltfImporterSettings settings)
{
ModelRoot? modelRoot = null;
try
{
modelRoot = ModelRoot.Load(settings.InputGltfFile);
}
catch (Exception ex)
{
_exceptionService.ShowExceptionWindow(ex);
return null;
}

return modelRoot;

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Test.ImportExport.Exporting.Exporters.RmvToGlft

public class RmvToGltfExporterTests
{
private readonly string _inputPackFileKarl = PathHelper.Folder2("Karl_and_celestialgeneral_Pack");
private readonly string _inputPackFileKarl = PathHelper.GetDataFolder("Karl_and_celestialgeneral_Pack");
private readonly string _rmvFilePathKarl = @"variantmeshes\wh_variantmodels\hu1\emp\emp_karl_franz\emp_karl_franz.rigid_model_v2";

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Editors.ImportExport.Exporting.Exporters.DdsToMaterialPng;
using Editors.ImportExport.Exporting.Exporters.DdsToNormalPng;
using Editors.ImportExport.Exporting.Exporters.RmvToGltf.Helpers;
using Editors.ImportExport.Importing.Importers.GltfToRmv;
using Editors.ImportExport.Importing.Importers.GltfToRmv.Helper;
using Editors.Shared.Core.Services;
using Moq;
using Shared.Core.Events;
using Shared.Core.PackFiles;
using Shared.Core.PackFiles.Models;
using Shared.Core.Services;
using Shared.GameFormats.RigidModel;
using Shared.TestUtility;
using Shared.Ui.BaseDialogs.StandardDialog;
using SharpGLTF.Schema2;
using Test.ImportExport.Exporting.Exporters.RmvToGlft;

namespace Test.ImportExport.Importing.Importers.GltfImporterTest
{
class TestData
{
public static readonly string InputGtlfFile = PathHelper.GetDataFile(@"Karl_Gltf_Resaved_From_Blender\karl_franz_imported_and_saved_to_blendfile_and_exported_to_gltf_from_blender.gltf");
public static readonly string InputPack = PathHelper.GetDataFolder("Karl_and_celestialgeneral_Pack");

public static class GltfExpected
{
public const int logicalMaterials = 4;
public const int nodes = 78;
public const int skinCount = 1;
public const int textureCount = 12;
public const int logicalMeshes = 4;
public const int primtivesMesh0 = 1;
public const int primtivesMesh0IndexCount = 25539;
}

public static class Rmv2Expected
{
public const string skeletonName = "humanoid01";
public const int lodCount = 1;
public const int lod0MeshCount = 4;
public const int Lod0Mesh0IndexCount = 25539;
public const int Lod0Mesh0VertexCount = 6397;
}
}

// Tests Full Import (Importer.Import())
class GltfToRmv2ImporterTest
{
[Test]
public void Test()
{
// Arrange
var pfs = PackFileSerivceTestHelper.Create(TestData.InputPack);

var meshBuilder = new GltfMeshBuilder();
var normalExporter = new Mock<IDdsToNormalPngExporter>();
var materialExporter = new Mock<IDdsToMaterialPngExporter>();
var eventHub = new Mock<IGlobalEventHub>();
var skeletontonLookupHelper = new SkeletonAnimationLookUpHelper(pfs, eventHub.Object);
var skeletontonBuilder = new GltfSkeletonBuilder(pfs);
var animationBuilder = new GltfAnimationBuilder(pfs);
var textureHandler = new GltfTextureHandler(normalExporter.Object, materialExporter.Object);
var sceneSaver = new TestGltfSceneSaver();
var standardDialog = new Mock<IStandardDialogs>();
var sceneLoader = new GltfSceneLoader(standardDialog.Object);
var importer = new GltfImporter(pfs, standardDialog.Object, skeletontonLookupHelper);

var packFileContainer = new PackFileContainer("new");
var settings = new GltfImporterSettings(TestData.InputGtlfFile, "skeletons", packFileContainer, true, true, true);

// Act
// Do full import
importer.Import(settings);
var rmv2FileName = @$"skeletons\{Path.GetFileNameWithoutExtension(TestData.InputGtlfFile)}.rigid_model_v2".ToLower();
var isPackFileAdded = packFileContainer.FileList.ContainsKey(rmv2FileName);

// Assert
// Last step of Importer, is adding the file to PackFileContainer, so we check wether this has happened
Assert.That(isPackFileAdded, Is.EqualTo(true));
}
}

// Tests some Components of Importer pipeline
class GltfToRmv2ImporterComponentTest
{

[Test]
public void Test()
{
// Arrange
var pfs = PackFileSerivceTestHelper.Create(TestData.InputPack);

var meshBuilder = new GltfMeshBuilder();
var normalExporter = new Mock<IDdsToNormalPngExporter>();
var materialExporter = new Mock<IDdsToMaterialPngExporter>();
var eventHub = new Mock<IGlobalEventHub>();
var standardDialog = new Mock<IStandardDialogs>();
var skeletontonLookupHelper = new SkeletonAnimationLookUpHelper(pfs, eventHub.Object);
var skeletontonBuilder = new GltfSkeletonBuilder(pfs);
var animationBuilder = new GltfAnimationBuilder(pfs);
var textureHandler = new GltfTextureHandler(normalExporter.Object, materialExporter.Object);
var sceneLoader = new GltfSceneLoader(standardDialog.Object);
var skeletonFile = skeletontonLookupHelper.GetSkeletonFileFromName(TestData.Rmv2Expected.skeletonName);
var packFileContainer = new PackFileContainer("new");
var settings = new GltfImporterSettings(TestData.InputGtlfFile, "skeletons", packFileContainer, true, true, true);

// Act....
var modelRoot = sceneLoader.Load(settings);

// Assert
// Assert API correct load
Assert.That(modelRoot, Is.Not.Null);
Assert.That(modelRoot.LogicalMaterials!.Count(), Is.EqualTo(TestData.GltfExpected.logicalMaterials));
Assert.That(modelRoot.LogicalNodes!.Count(), Is.EqualTo(TestData.GltfExpected.nodes));
Assert.That(modelRoot.LogicalSkins!.Count(), Is.EqualTo(TestData.GltfExpected.skinCount));
Assert.That(modelRoot.LogicalTextures!.Count(), Is.EqualTo(TestData.GltfExpected.textureCount));
Assert.That(modelRoot.LogicalMeshes!.Count(), Is.EqualTo(TestData.GltfExpected.logicalMeshes));
Assert.That(modelRoot.LogicalMeshes[0].Primitives!.Count(), Is.EqualTo(TestData.GltfExpected.primtivesMesh0));
Assert.That(modelRoot.LogicalMeshes[0].Primitives[0].GetIndices().Count(), Is.EqualTo(TestData.GltfExpected.primtivesMesh0IndexCount));

// Assert
// Test Importer code, check if Rmv2 file is created correctly

Assert.That(skeletonFile, Is.Not.Null);
var rmv2Mesh = RmvMeshBuilder.Build(settings, modelRoot, skeletonFile, TestData.Rmv2Expected.skeletonName);
Assert.That(rmv2Mesh, Is.Not.Null);
Assert.That(rmv2Mesh.Header.SkeletonName, Is.EqualTo(TestData.Rmv2Expected.skeletonName));
Assert.That(rmv2Mesh.ModelList, Is.Not.Null);
Assert.That(rmv2Mesh.ModelList.Length, Is.EqualTo(TestData.Rmv2Expected.lodCount));
Assert.That(rmv2Mesh.ModelList[0].Length, Is.EqualTo(TestData.Rmv2Expected.lod0MeshCount));
Assert.That(rmv2Mesh.ModelList[0][0].Mesh, Is.Not.Null);
Assert.That(rmv2Mesh.ModelList[0][0].Material, Is.Not.Null);
Assert.That(rmv2Mesh.ModelList[0][0].Material.MaterialId, Is.EqualTo(ModelMaterialEnum.weighted));
Assert.That(rmv2Mesh.ModelList[0][0].Material.BinaryVertexFormat, Is.EqualTo(VertexFormat.Cinematic));
Assert.That(rmv2Mesh.ModelList[0][0].Mesh.IndexList, Is.Not.Null);
Assert.That(rmv2Mesh.ModelList[0][0].Mesh.IndexList.Length, Is.EqualTo(TestData.Rmv2Expected.Lod0Mesh0IndexCount));
Assert.That(rmv2Mesh.ModelList[0][0].Mesh.VertexList.Length, Is.EqualTo(TestData.Rmv2Expected.Lod0Mesh0VertexCount));
}
}
}
6 changes: 3 additions & 3 deletions Testing/E2EVerification/KitbashEditor_SaveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace E2EVerification
{
public class KitbashEditor_SaveTests
{
private readonly string _inputPackFileKarl = PathHelper.File("Karl_and_celestialgeneral.pack");
private readonly string _inputPackFileKarl = PathHelper.GetDataFile("Karl_and_celestialgeneral.pack");
private readonly string _rmvFilePathKarl = @"variantmeshes\wh_variantmodels\hu1\emp\emp_karl_franz\emp_karl_franz.rigid_model_v2";
private readonly string _wsFilePathKarl = @"variantmeshes\wh_variantmodels\hu1\emp\emp_karl_franz\emp_karl_franz.wsmodel";

Expand Down Expand Up @@ -123,7 +123,7 @@ public void Warhammer3_SaveKarl_Lod0ForAll()
public void Rome2_LoadAndSaveDirtHelmet()
{
var runner = new AssetEditorTestRunner(GameTypeEnum.Rome_2);
var outputPackFile = runner.LoadFolderPackFile(PathHelper.Folder("Rome_Man_And_Shield_Pack"), true);
var outputPackFile = runner.LoadFolderPackFile(PathHelper.GetDataFolder("Rome_Man_And_Shield_Pack"), true);

// Load the a rmv2 and open the kitbash editor
var meshPath = "variantmeshes/_variantmodels/man/helmets/carthaginian_pylos.rigid_model_v2";
Expand Down Expand Up @@ -157,7 +157,7 @@ public void Rome2_LoadAndSaveDirtHelmet()
public void Rome2_LoadAndSaveDirtAndDecalShield()
{
var runner = new AssetEditorTestRunner(GameTypeEnum.Rome_2);
var outputPackFile = runner.LoadFolderPackFile(PathHelper.Folder("Rome_Man_And_Shield_Pack"), true);
var outputPackFile = runner.LoadFolderPackFile(PathHelper.GetDataFolder(("Rome_Man_And_Shield_Pack")));

// Load the a rmv2 and open the kitbash editor
var meshPath = "variantmeshes/_variantmodels/man/shield/celtic_oval_shield_a.rigid_model_v2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void CreateDecalAndDirt_FromRmvMaterial()
var settings = new ApplicationSettingsService(GameTypeEnum.Attila);
var materialFactory = new CapabilityMaterialFactory(settings, null);

var path = PathHelper.File("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//shield//celtic_oval_shield_a.rigid_model_v2");
var path = PathHelper.GetDataFile("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//shield//celtic_oval_shield_a.rigid_model_v2");
var packFile = PackFile.CreateFromFileSystem("mymodel.rigid_model_v2", path);
var rmvFile = ModelFactory.Create().Load(packFile.DataSource.ReadData());

Expand Down Expand Up @@ -52,7 +52,7 @@ public void CreateDirt_FromRmvMaterial()
var settings = new ApplicationSettingsService(GameTypeEnum.Attila);
var materialFactory = new CapabilityMaterialFactory(settings, null);

var path = PathHelper.File("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//helmets//carthaginian_pylos.rigid_model_v2");
var path = PathHelper.GetDataFile("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//helmets//carthaginian_pylos.rigid_model_v2");
var packFile = PackFile.CreateFromFileSystem("mymodel.rigid_model_v2", path);
var rmvFile = ModelFactory.Create().Load(packFile.DataSource.ReadData());

Expand All @@ -78,7 +78,7 @@ public void CreateDirt_FromRmvMaterial()
public void DecalAndDirt_GenerateRmvMaterial()
{
// Arrange
var path = PathHelper.File("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//shield//celtic_oval_shield_a.rigid_model_v2");
var path = PathHelper.GetDataFile("Rome_Man_And_Shield_Pack//variantmeshes//_variantmodels//man//shield//celtic_oval_shield_a.rigid_model_v2");
var packFile = PackFile.CreateFromFileSystem("mymodel.rigid_model_v2", path);
var rmvFile = ModelFactory.Create().Load(packFile.DataSource.ReadData());

Expand Down
2 changes: 1 addition & 1 deletion Testing/Shared/TestUtility/PackFileSerivceTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IPackFileService CreateFromFolder(GameTypeEnum selectedGame, strin
var pfs = new PackFileService(null);
var loader = new PackFileContainerLoader(new ApplicationSettingsService(selectedGame), new GameInformationFactory());

var container = loader.LoadSystemFolderAsPackFileContainer(PathHelper.Folder(path));
var container = loader.LoadSystemFolderAsPackFileContainer(PathHelper.GetDataFolder(path));
container.IsCaPackFile = true;
pfs.AddContainer(container);
return pfs;
Expand Down
Loading

0 comments on commit b6809ff

Please sign in to comment.