Skip to content

Commit

Permalink
IModelImportProvider - Add 'SupportImport' and 'SupportExport' proper…
Browse files Browse the repository at this point in the history
…ty fields
  • Loading branch information
NessieHax committed Aug 7, 2024
1 parent a1c564b commit 19b66d3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
3 changes: 3 additions & 0 deletions PCK-Studio/Interfaces/IModelImportProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal interface IModelImportProvider<T> 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);

Expand Down
7 changes: 4 additions & 3 deletions PCK-Studio/Internal/GameModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ internal sealed class GameModelImporter : ModelImporter<GameModelInfo>

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));

Expand Down Expand Up @@ -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);
Expand Down
35 changes: 25 additions & 10 deletions PCK-Studio/Internal/ModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ private sealed class SimpleSkinImportProvider : IModelImportProvider<T>

public FileDialogFilter DialogFilter => _dialogFilter;

public bool SupportImport => _import != null;

public bool SupportExport => _export != null;

private FileDialogFilter _dialogFilter;
private Func<string, T> _import;
private Action<string, T> _export;
Expand Down Expand Up @@ -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<T> 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)
Expand All @@ -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<T> provider = GetProvider(filename);
if (!provider.SupportExport)
{
throw new NotSupportedException($"Provider '{provider.Name}' does not support exporting.");
}
provider.Export(filename, model);
}

internal bool AddProvider(IModelImportProvider<T> provider)
Expand All @@ -104,9 +122,6 @@ protected IModelImportProvider<T> GetProvider(string filename)

protected bool InternalAddProvider(FileDialogFilter dialogFilter, Func<string, T> import, Action<string, T> export)
{
if (import == null || export == null)
return false;

return AddProvider(new SimpleSkinImportProvider(dialogFilter, import, export));
}

Expand Down
2 changes: 1 addition & 1 deletion PCK-Studio/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamedTexture> GetModelTextures(string modelName)
{
Expand Down

0 comments on commit 19b66d3

Please sign in to comment.