From 19b66d3968015f62202e27141e00edb8c8940489 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:48:10 +0200 Subject: [PATCH] IModelImportProvider - Add 'SupportImport' and 'SupportExport' property fields --- PCK-Studio/Interfaces/IModelImportProvider.cs | 3 ++ PCK-Studio/Internal/GameModelImporter.cs | 7 ++-- PCK-Studio/Internal/ModelImporter.cs | 35 +++++++++++++------ PCK-Studio/MainForm.cs | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/PCK-Studio/Interfaces/IModelImportProvider.cs b/PCK-Studio/Interfaces/IModelImportProvider.cs index befe3375..60033be6 100644 --- a/PCK-Studio/Interfaces/IModelImportProvider.cs +++ b/PCK-Studio/Interfaces/IModelImportProvider.cs @@ -15,6 +15,9 @@ internal interface IModelImportProvider where T : class public FileDialogFilter DialogFilter { get; } + public bool SupportImport { get; } + public bool SupportExport { get; } + public T Import(string filename); public T Import(Stream stream); diff --git a/PCK-Studio/Internal/GameModelImporter.cs b/PCK-Studio/Internal/GameModelImporter.cs index f97ade88..e522c493 100644 --- a/PCK-Studio/Internal/GameModelImporter.cs +++ b/PCK-Studio/Internal/GameModelImporter.cs @@ -18,10 +18,11 @@ internal sealed class GameModelImporter : ModelImporter private GameModelImporter() { - InternalAddProvider(new FileDialogFilter("", "*.bbmodel"), null, ExportBlockBenchModel); + // TODO: add import functionality -miku + InternalAddProvider(new FileDialogFilter("Block bench model(*.bbmodel)", "*.bbmodel"), null, ExportBlockBenchModel); } - internal static void ExportBlockBenchModel(string fileName, GameModelInfo modelInfo) + private static void ExportBlockBenchModel(string fileName, GameModelInfo modelInfo) { BlockBenchModel blockBenchModel = BlockBenchModel.Create(Path.GetFileNameWithoutExtension(fileName), modelInfo.Model.TextureSize, modelInfo.Textures.Select(nt => (Texture)nt)); @@ -61,7 +62,7 @@ Outline GetOrCreateOutline(string partName) } blockBenchModel.Elements = elements.ToArray(); - blockBenchModel.Outliner = JArray.FromObject(outliners); + blockBenchModel.Outliner = JArray.FromObject(outliners.Values); string content = JsonConvert.SerializeObject(blockBenchModel); File.WriteAllText(fileName, content); diff --git a/PCK-Studio/Internal/ModelImporter.cs b/PCK-Studio/Internal/ModelImporter.cs index e6e80303..21b2a60d 100644 --- a/PCK-Studio/Internal/ModelImporter.cs +++ b/PCK-Studio/Internal/ModelImporter.cs @@ -20,6 +20,10 @@ private sealed class SimpleSkinImportProvider : IModelImportProvider public FileDialogFilter DialogFilter => _dialogFilter; + public bool SupportImport => _import != null; + + public bool SupportExport => _export != null; + private FileDialogFilter _dialogFilter; private Func _import; private Action _export; @@ -58,11 +62,19 @@ public T Import(Stream stream) public T Import(string filename) { - if (HasProvider(filename)) - return GetProvider(filename).Import(filename); + if (!HasProvider(filename)) + { + Trace.TraceWarning($"[{nameof(SkinModelImporter)}:Import] No provider found for '{Path.GetExtension(filename)}'."); + return default; + } + + IModelImportProvider provider = GetProvider(filename); + if (!provider.SupportImport) + { + throw new NotSupportedException($"Provider '{provider.Name}' does not support importing."); + } - Trace.TraceWarning($"[{nameof(SkinModelImporter)}:Import] No provider found for '{Path.GetExtension(filename)}'."); - return default; + return provider.Import(filename); } public void Export(string filename, T model) @@ -72,13 +84,19 @@ public void Export(string filename, T model) Trace.TraceError($"[{nameof(SkinModelImporter)}:Export] Model is null."); return; } + if (!HasProvider(filename)) { - string fileExtension = Path.GetExtension(filename); - Trace.TraceWarning($"[{nameof(SkinModelImporter)}:Export] No provider found for '{fileExtension}'."); + Trace.TraceWarning($"[{nameof(SkinModelImporter)}:Export] No provider found for '{Path.GetExtension(filename)}'."); return; } - GetProvider(filename).Export(filename, model); + + IModelImportProvider provider = GetProvider(filename); + if (!provider.SupportExport) + { + throw new NotSupportedException($"Provider '{provider.Name}' does not support exporting."); + } + provider.Export(filename, model); } internal bool AddProvider(IModelImportProvider provider) @@ -104,9 +122,6 @@ protected IModelImportProvider GetProvider(string filename) protected bool InternalAddProvider(FileDialogFilter dialogFilter, Func import, Action export) { - if (import == null || export == null) - return false; - return AddProvider(new SimpleSkinImportProvider(dialogFilter, import, export)); } diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 412de70b..86dae099 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -628,7 +628,7 @@ public void HandleModelsFile(PckAsset file) using SaveFileDialog openFileDialog = new SaveFileDialog(); openFileDialog.FileName = model.Name; - openFileDialog.Filter = "Block bench model(*.bbmodel)|*.bbmodel"; + openFileDialog.Filter = GameModelImporter.Default.SupportedModelFileFormatsFilter; IEnumerable GetModelTextures(string modelName) {