From 2f6905cf357b81aed33e67476575a39809538db5 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sun, 7 Jan 2024 14:42:16 +0100 Subject: [PATCH] Minimal cleanup. --- Penumbra/Communication/ChangedItemClick.cs | 1 - .../Import/Models/Export/ModelExporter.cs | 5 ++- Penumbra/Import/Models/ModelManager.cs | 43 +++++++++---------- .../ModEditWindow.Models.MdlTab.cs | 13 +++--- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Penumbra/Communication/ChangedItemClick.cs b/Penumbra/Communication/ChangedItemClick.cs index b11f2306..754570e2 100644 --- a/Penumbra/Communication/ChangedItemClick.cs +++ b/Penumbra/Communication/ChangedItemClick.cs @@ -1,6 +1,5 @@ using OtterGui.Classes; using Penumbra.Api.Enums; -using Penumbra.GameData.Enums; namespace Penumbra.Communication; diff --git a/Penumbra/Import/Models/Export/ModelExporter.cs b/Penumbra/Import/Models/Export/ModelExporter.cs index 07b37eeb..8271f266 100644 --- a/Penumbra/Import/Models/Export/ModelExporter.cs +++ b/Penumbra/Import/Models/Export/ModelExporter.cs @@ -56,11 +56,12 @@ public class ModelExporter var names = new Dictionary(); var joints = new List(); - // Flatten out the bones across all the recieved skeletons, but retain a reference to the parent skeleton for lookups. + // Flatten out the bones across all the received skeletons, but retain a reference to the parent skeleton for lookups. var iterator = skeletons.SelectMany(skeleton => skeleton.Bones.Select(bone => (skeleton, bone))); foreach (var (skeleton, bone) in iterator) { - if (names.ContainsKey(bone.Name)) continue; + if (names.ContainsKey(bone.Name)) + continue; var node = new NodeBuilder(bone.Name); names[bone.Name] = joints.Count; diff --git a/Penumbra/Import/Models/ModelManager.cs b/Penumbra/Import/Models/ModelManager.cs index 692a214f..f4c17080 100644 --- a/Penumbra/Import/Models/ModelManager.cs +++ b/Penumbra/Import/Models/ModelManager.cs @@ -29,16 +29,17 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect _tasks.Clear(); } - public Task ExportToGltf(MdlFile mdl, IEnumerable? sklbs, string outputPath) + public Task ExportToGltf(MdlFile mdl, IEnumerable sklbs, string outputPath) => Enqueue(new ExportToGltfAction(this, mdl, sklbs, outputPath)); /// Try to find the .sklb paths for a .mdl file. /// .mdl file to look up the skeletons for. - public string[]? ResolveSklbsForMdl(string mdlPath, EstManipulation[] estManipulations) + /// Modified extra skeleton template parameters. + public string[] ResolveSklbsForMdl(string mdlPath, EstManipulation[] estManipulations) { var info = parser.GetFileInfo(mdlPath); if (info.FileType is not FileType.Model) - return null; + return []; var baseSkeleton = GamePaths.Skeleton.Sklb.Path(info.GenderRace, "base", 1); @@ -59,7 +60,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect ObjectType.DemiHuman => [GamePaths.DemiHuman.Sklb.Path(info.PrimaryId)], ObjectType.Monster => [GamePaths.Monster.Sklb.Path(info.PrimaryId)], ObjectType.Weapon => [GamePaths.Weapon.Sklb.Path(info.PrimaryId)], - _ => null, + _ => [], }; } @@ -113,31 +114,38 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect return task; } - private class ExportToGltfAction(ModelManager manager, MdlFile mdl, IEnumerable? sklbs, string outputPath) + private class ExportToGltfAction(ModelManager manager, MdlFile mdl, IEnumerable sklbs, string outputPath) : IAction { public void Execute(CancellationToken cancel) { - Penumbra.Log.Debug("Reading skeletons."); + Penumbra.Log.Debug($"[GLTF Export] Exporting model to {outputPath}..."); + Penumbra.Log.Debug("[GLTF Export] Reading skeletons..."); var xivSkeletons = BuildSkeletons(cancel); - Penumbra.Log.Debug("Converting model."); + Penumbra.Log.Debug("[GLTF Export] Converting model..."); var model = ModelExporter.Export(mdl, xivSkeletons); - Penumbra.Log.Debug("Building scene."); + Penumbra.Log.Debug("[GLTF Export] Building scene..."); var scene = new SceneBuilder(); model.AddToScene(scene); - Penumbra.Log.Debug("Saving."); + Penumbra.Log.Debug("[GLTF Export] Saving..."); var gltfModel = scene.ToGltf2(); gltfModel.SaveGLTF(outputPath); + Penumbra.Log.Debug("[GLTF Export] Done."); } /// Attempt to read out the pertinent information from a .sklb. - private IEnumerable? BuildSkeletons(CancellationToken cancel) + private IEnumerable BuildSkeletons(CancellationToken cancel) { - if (sklbs == null) - return null; + var havokTasks = sklbs + .WithIndex() + .Select(CreateHavokTask) + .ToArray(); + + // Result waits automatically. + return havokTasks.Select(task => SkeletonConverter.FromXml(task.Result)); // The havok methods we're relying on for this conversion are a bit // finicky at the best of times, and can outright cause a CTD if they @@ -146,16 +154,7 @@ public sealed class ModelManager(IFramework framework, ActiveCollections collect Task CreateHavokTask((SklbFile Sklb, int Index) pair) => manager._framework.RunOnTick( () => HavokConverter.HkxToXml(pair.Sklb.Skeleton), - delayTicks: pair.Index - ); - - var havokTasks = sklbs - .WithIndex() - .Select(CreateHavokTask) - .ToArray(); - Task.WaitAll(havokTasks, cancel); - - return havokTasks.Select(task => SkeletonConverter.FromXml(task.Result)); + delayTicks: pair.Index, cancellationToken: cancel); } public bool Equals(IAction? other) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs index d4e75487..dbedc164 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Models.MdlTab.cs @@ -86,31 +86,30 @@ public partial class ModEditWindow .Where(subMod => subMod != option) .Prepend(option) .SelectMany(subMod => subMod.Manipulations) - .Where(manipulation => manipulation.ManipulationType == MetaManipulation.Type.Est) + .Where(manipulation => manipulation.ManipulationType is MetaManipulation.Type.Est) .Select(manipulation => manipulation.Est) .ToArray(); } /// Export model to an interchange format. /// Disk path to save the resulting file to. + /// The game path of the model. public void Export(string outputPath, Utf8GamePath mdlPath) { - IEnumerable? sklbs = null; + IEnumerable skeletons; try { var sklbPaths = _edit._models.ResolveSklbsForMdl(mdlPath.ToString(), GetCurrentEstManipulations()); - sklbs = sklbPaths != null - ? sklbPaths.Select(ReadSklb).ToArray() - : null; + skeletons = sklbPaths.Select(ReadSklb).ToArray(); } catch (Exception exception) { - IoException = exception?.ToString(); + IoException = exception.ToString(); return; } PendingIo = true; - _edit._models.ExportToGltf(Mdl, sklbs, outputPath) + _edit._models.ExportToGltf(Mdl, skeletons, outputPath) .ContinueWith(task => { IoException = task.Exception?.ToString();