From 636b2c91ccfe2d7e0483a2ac7cf6ca35ab0cac67 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 18 Feb 2024 11:22:35 +0100 Subject: [PATCH] Refactor `Editor.CreateAsset` to use named tags for better extensibility with custom assets in plugins --- .../Create/ParticleEmitterCreateEntry.cs | 2 +- .../Proxy/AnimationGraphFunctionProxy.cs | 2 +- .../Content/Proxy/AnimationGraphProxy.cs | 2 +- Source/Editor/Content/Proxy/AnimationProxy.cs | 2 +- .../Editor/Content/Proxy/BehaviorTreeProxy.cs | 2 +- .../Content/Proxy/CollisionDataProxy.cs | 2 +- .../Content/Proxy/MaterialFunctionProxy.cs | 2 +- .../Content/Proxy/MaterialInstanceProxy.cs | 2 +- Source/Editor/Content/Proxy/MaterialProxy.cs | 2 +- .../Proxy/ParticleEmitterFunctionProxy.cs | 2 +- .../Content/Proxy/ParticleSystemProxy.cs | 2 +- .../Content/Proxy/SceneAnimationProxy.cs | 2 +- .../Editor/Content/Proxy/SkeletonMaskProxy.cs | 2 +- Source/Editor/Editor.cs | 55 ++++++++++-- .../Editor/Managed/ManagedEditor.Internal.cpp | 83 ++----------------- Source/Editor/Managed/ManagedEditor.h | 7 ++ .../AssetsImportingManager.cpp | 1 - Source/Engine/ContentImporters/Types.h | 4 +- 18 files changed, 79 insertions(+), 97 deletions(-) diff --git a/Source/Editor/Content/Create/ParticleEmitterCreateEntry.cs b/Source/Editor/Content/Create/ParticleEmitterCreateEntry.cs index 1be5330e7..76edde777 100644 --- a/Source/Editor/Content/Create/ParticleEmitterCreateEntry.cs +++ b/Source/Editor/Content/Create/ParticleEmitterCreateEntry.cs @@ -81,7 +81,7 @@ namespace FlaxEditor.Content.Create switch (_options.Template) { case Templates.Empty: - return Editor.CreateAsset(Editor.NewAssetType.ParticleEmitter, ResultUrl); + return Editor.CreateAsset("ParticleEmitter", ResultUrl); case Templates.ConstantBurst: templateName = "Constant Burst"; break; diff --git a/Source/Editor/Content/Proxy/AnimationGraphFunctionProxy.cs b/Source/Editor/Content/Proxy/AnimationGraphFunctionProxy.cs index f714ddbeb..7efc02368 100644 --- a/Source/Editor/Content/Proxy/AnimationGraphFunctionProxy.cs +++ b/Source/Editor/Content/Proxy/AnimationGraphFunctionProxy.cs @@ -38,7 +38,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.AnimationGraphFunction, outputPath)) + if (Editor.CreateAsset("AnimationGraphFunction", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Content/Proxy/AnimationGraphProxy.cs b/Source/Editor/Content/Proxy/AnimationGraphProxy.cs index 3e6c35c6d..20d3c5a2c 100644 --- a/Source/Editor/Content/Proxy/AnimationGraphProxy.cs +++ b/Source/Editor/Content/Proxy/AnimationGraphProxy.cs @@ -38,7 +38,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.AnimationGraph, outputPath)) + if (Editor.CreateAsset("AnimationGraph", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Content/Proxy/AnimationProxy.cs b/Source/Editor/Content/Proxy/AnimationProxy.cs index 2cf46d3a3..6636fccb8 100644 --- a/Source/Editor/Content/Proxy/AnimationProxy.cs +++ b/Source/Editor/Content/Proxy/AnimationProxy.cs @@ -47,7 +47,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.Animation, outputPath)) + if (Editor.CreateAsset("Animation", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/BehaviorTreeProxy.cs b/Source/Editor/Content/Proxy/BehaviorTreeProxy.cs index 33ad0862f..234dfaf7d 100644 --- a/Source/Editor/Content/Proxy/BehaviorTreeProxy.cs +++ b/Source/Editor/Content/Proxy/BehaviorTreeProxy.cs @@ -47,7 +47,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.BehaviorTree, outputPath)) + if (Editor.CreateAsset("BehaviorTree", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/CollisionDataProxy.cs b/Source/Editor/Content/Proxy/CollisionDataProxy.cs index 55e8c6327..df26dca75 100644 --- a/Source/Editor/Content/Proxy/CollisionDataProxy.cs +++ b/Source/Editor/Content/Proxy/CollisionDataProxy.cs @@ -71,7 +71,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.CollisionData, outputPath)) + if (Editor.CreateAsset("CollisionData", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/MaterialFunctionProxy.cs b/Source/Editor/Content/Proxy/MaterialFunctionProxy.cs index ab59f11f3..ca012f70a 100644 --- a/Source/Editor/Content/Proxy/MaterialFunctionProxy.cs +++ b/Source/Editor/Content/Proxy/MaterialFunctionProxy.cs @@ -38,7 +38,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.MaterialFunction, outputPath)) + if (Editor.CreateAsset("MaterialFunction", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Content/Proxy/MaterialInstanceProxy.cs b/Source/Editor/Content/Proxy/MaterialInstanceProxy.cs index 331ff81c3..cd245b149 100644 --- a/Source/Editor/Content/Proxy/MaterialInstanceProxy.cs +++ b/Source/Editor/Content/Proxy/MaterialInstanceProxy.cs @@ -43,7 +43,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.MaterialInstance, outputPath)) + if (Editor.CreateAsset("MaterialInstance", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/MaterialProxy.cs b/Source/Editor/Content/Proxy/MaterialProxy.cs index a7fcfecc8..f7db2c83d 100644 --- a/Source/Editor/Content/Proxy/MaterialProxy.cs +++ b/Source/Editor/Content/Proxy/MaterialProxy.cs @@ -44,7 +44,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.Material, outputPath)) + if (Editor.CreateAsset("Material", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/ParticleEmitterFunctionProxy.cs b/Source/Editor/Content/Proxy/ParticleEmitterFunctionProxy.cs index 3a2ed749f..aaf9445e2 100644 --- a/Source/Editor/Content/Proxy/ParticleEmitterFunctionProxy.cs +++ b/Source/Editor/Content/Proxy/ParticleEmitterFunctionProxy.cs @@ -38,7 +38,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.ParticleEmitterFunction, outputPath)) + if (Editor.CreateAsset("ParticleEmitterFunction", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Content/Proxy/ParticleSystemProxy.cs b/Source/Editor/Content/Proxy/ParticleSystemProxy.cs index 047853f0b..74f391513 100644 --- a/Source/Editor/Content/Proxy/ParticleSystemProxy.cs +++ b/Source/Editor/Content/Proxy/ParticleSystemProxy.cs @@ -75,7 +75,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.ParticleSystem, outputPath)) + if (Editor.CreateAsset("ParticleSystem", outputPath)) throw new Exception("Failed to create new asset."); } diff --git a/Source/Editor/Content/Proxy/SceneAnimationProxy.cs b/Source/Editor/Content/Proxy/SceneAnimationProxy.cs index fe31f0e34..5f7fa800a 100644 --- a/Source/Editor/Content/Proxy/SceneAnimationProxy.cs +++ b/Source/Editor/Content/Proxy/SceneAnimationProxy.cs @@ -69,7 +69,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.SceneAnimation, outputPath)) + if (Editor.CreateAsset("SceneAnimation", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Content/Proxy/SkeletonMaskProxy.cs b/Source/Editor/Content/Proxy/SkeletonMaskProxy.cs index f300a4c61..560df0a7c 100644 --- a/Source/Editor/Content/Proxy/SkeletonMaskProxy.cs +++ b/Source/Editor/Content/Proxy/SkeletonMaskProxy.cs @@ -38,7 +38,7 @@ namespace FlaxEditor.Content /// public override void Create(string outputPath, object arg) { - if (Editor.CreateAsset(Editor.NewAssetType.SkeletonMask, outputPath)) + if (Editor.CreateAsset("SkeletonMask", outputPath)) throw new Exception("Failed to create new asset."); } } diff --git a/Source/Editor/Editor.cs b/Source/Editor/Editor.cs index 393bf564e..266d8235f 100644 --- a/Source/Editor/Editor.cs +++ b/Source/Editor/Editor.cs @@ -869,7 +869,9 @@ namespace FlaxEditor /// /// New asset types allowed to create. + /// [Deprecated in v1.8] /// + [Obsolete("Use CreateAsset with named tag.")] public enum NewAssetType { /// @@ -1046,12 +1048,59 @@ namespace FlaxEditor /// /// Creates new asset at the target location. + /// [Deprecated in v1.8] /// /// New asset type. /// Output asset path. + [Obsolete("Use CreateAsset with named tag.")] public static bool CreateAsset(NewAssetType type, string outputPath) { - return Internal_CreateAsset(type, outputPath); + // [Deprecated on 18.02.2024, expires on 18.02.2025] + string tag; + switch (type) + { + case NewAssetType.Material: + tag = "Material"; + break; + case NewAssetType.MaterialInstance: + tag = "MaterialInstance"; + break; + case NewAssetType.CollisionData: + tag = "CollisionData"; + break; + case NewAssetType.AnimationGraph: + tag = "AnimationGraph"; + break; + case NewAssetType.SkeletonMask: + tag = "SkeletonMask"; + break; + case NewAssetType.ParticleEmitter: + tag = "ParticleEmitter"; + break; + case NewAssetType.ParticleSystem: + tag = "ParticleSystem"; + break; + case NewAssetType.SceneAnimation: + tag = "SceneAnimation"; + break; + case NewAssetType.MaterialFunction: + tag = "MaterialFunction"; + break; + case NewAssetType.ParticleEmitterFunction: + tag = "ParticleEmitterFunction"; + break; + case NewAssetType.AnimationGraphFunction: + tag = "AnimationGraphFunction"; + break; + case NewAssetType.Animation: + tag = "Animation"; + break; + case NewAssetType.BehaviorTree: + tag = "BehaviorTree"; + break; + default: return true; + } + return CreateAsset(tag, outputPath); } /// @@ -1588,10 +1637,6 @@ namespace FlaxEditor [LibraryImport("FlaxEngine", EntryPoint = "EditorInternal_CloseSplashScreen", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(StringMarshaller))] internal static partial void Internal_CloseSplashScreen(); - [LibraryImport("FlaxEngine", EntryPoint = "EditorInternal_CreateAsset", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(StringMarshaller))] - [return: MarshalAs(UnmanagedType.U1)] - internal static partial bool Internal_CreateAsset(NewAssetType type, string outputPath); - [LibraryImport("FlaxEngine", EntryPoint = "EditorInternal_CreateVisualScript", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(StringMarshaller))] [return: MarshalAs(UnmanagedType.U1)] internal static partial bool Internal_CreateVisualScript(string outputPath, string baseTypename); diff --git a/Source/Editor/Managed/ManagedEditor.Internal.cpp b/Source/Editor/Managed/ManagedEditor.Internal.cpp index ae8b71ee2..53c0fbab2 100644 --- a/Source/Editor/Managed/ManagedEditor.Internal.cpp +++ b/Source/Editor/Managed/ManagedEditor.Internal.cpp @@ -170,78 +170,6 @@ DEFINE_INTERNAL_CALL(bool) EditorInternal_CloneAssetFile(MString* dstPathObj, MS return Content::CloneAssetFile(dstPath, srcPath, *dstId); } -enum class NewAssetType -{ - Material = 0, - MaterialInstance = 1, - CollisionData = 2, - AnimationGraph = 3, - SkeletonMask = 4, - ParticleEmitter = 5, - ParticleSystem = 6, - SceneAnimation = 7, - MaterialFunction = 8, - ParticleEmitterFunction = 9, - AnimationGraphFunction = 10, - Animation = 11, - BehaviorTree = 12, -}; - -DEFINE_INTERNAL_CALL(bool) EditorInternal_CreateAsset(NewAssetType type, MString* outputPathObj) -{ - String tag; - switch (type) - { - case NewAssetType::Material: - tag = AssetsImportingManager::CreateMaterialTag; - break; - case NewAssetType::MaterialInstance: - tag = AssetsImportingManager::CreateMaterialInstanceTag; - break; - case NewAssetType::CollisionData: - tag = AssetsImportingManager::CreateCollisionDataTag; - break; - case NewAssetType::AnimationGraph: - tag = AssetsImportingManager::CreateAnimationGraphTag; - break; - case NewAssetType::SkeletonMask: - tag = AssetsImportingManager::CreateSkeletonMaskTag; - break; - case NewAssetType::ParticleEmitter: - tag = AssetsImportingManager::CreateParticleEmitterTag; - break; - case NewAssetType::ParticleSystem: - tag = AssetsImportingManager::CreateParticleSystemTag; - break; - case NewAssetType::SceneAnimation: - tag = AssetsImportingManager::CreateSceneAnimationTag; - break; - case NewAssetType::MaterialFunction: - tag = AssetsImportingManager::CreateMaterialFunctionTag; - break; - case NewAssetType::ParticleEmitterFunction: - tag = AssetsImportingManager::CreateParticleEmitterFunctionTag; - break; - case NewAssetType::AnimationGraphFunction: - tag = AssetsImportingManager::CreateAnimationGraphFunctionTag; - break; - case NewAssetType::Animation: - tag = AssetsImportingManager::CreateAnimationTag; - break; - case NewAssetType::BehaviorTree: - tag = AssetsImportingManager::CreateBehaviorTreeTag; - break; - default: - return true; - } - - String outputPath; - MUtils::ToString(outputPathObj, outputPath); - FileSystem::NormalizePath(outputPath); - - return AssetsImportingManager::Create(tag, outputPath); -} - DEFINE_INTERNAL_CALL(bool) EditorInternal_CreateVisualScript(MString* outputPathObj, MString* baseTypenameObj) { String outputPath; @@ -634,13 +562,11 @@ bool ManagedEditor::Import(const String& inputPath, const String& outputPath, co bool ManagedEditor::TryRestoreImportOptions(ModelTool::Options& options, String assetPath) { - // Initialize defaults + // Initialize defaults if (const auto* graphicsSettings = GraphicsSettings::Get()) { options.GenerateSDF = graphicsSettings->GenerateSDFOnModelImport; } - - // Get options from model FileSystem::NormalizePath(assetPath); return ImportModel::TryGetImportOptions(assetPath, options); } @@ -652,7 +578,12 @@ bool ManagedEditor::Import(const String& inputPath, const String& outputPath, co bool ManagedEditor::TryRestoreImportOptions(AudioTool::Options& options, String assetPath) { - // Get options from model FileSystem::NormalizePath(assetPath); return ImportAudio::TryGetImportOptions(assetPath, options); } + +bool ManagedEditor::CreateAsset(const String& tag, String outputPath) +{ + FileSystem::NormalizePath(outputPath); + return AssetsImportingManager::Create(tag, outputPath); +} diff --git a/Source/Editor/Managed/ManagedEditor.h b/Source/Editor/Managed/ManagedEditor.h index 8c9571cfd..6aa7514b0 100644 --- a/Source/Editor/Managed/ManagedEditor.h +++ b/Source/Editor/Managed/ManagedEditor.h @@ -210,6 +210,13 @@ public: API_FUNCTION() static bool TryRestoreImportOptions(API_PARAM(Ref) AudioTool::Options& options, String assetPath); #endif + /// + /// Creates a new asset at the target location. + /// + /// New asset type. + /// Output asset path. + API_FUNCTION() static bool CreateAsset(const String& tag, String outputPath); + public: API_STRUCT(Internal, NoDefault) struct VisualScriptStackFrame { diff --git a/Source/Engine/ContentImporters/AssetsImportingManager.cpp b/Source/Engine/ContentImporters/AssetsImportingManager.cpp index a0b28f4f8..5fb5ff0c0 100644 --- a/Source/Engine/ContentImporters/AssetsImportingManager.cpp +++ b/Source/Engine/ContentImporters/AssetsImportingManager.cpp @@ -234,7 +234,6 @@ bool AssetsImportingManager::Create(const String& tag, const StringView& outputP LOG(Warning, "Cannot find asset creator object for tag \'{0}\'.", tag); return true; } - return Create(creator->Callback, outputPath, assetId, arg); } diff --git a/Source/Engine/ContentImporters/Types.h b/Source/Engine/ContentImporters/Types.h index 388c533a8..0275edc35 100644 --- a/Source/Engine/ContentImporters/Types.h +++ b/Source/Engine/ContentImporters/Types.h @@ -113,7 +113,7 @@ private: /// /// Asset importer entry /// -struct AssetImporter +struct FLAXENGINE_API AssetImporter { public: /// @@ -135,7 +135,7 @@ public: /// /// Asset creator entry /// -struct AssetCreator +struct FLAXENGINE_API AssetCreator { public: ///