Refactor Editor.CreateAsset to use named tags for better extensibility with custom assets in plugins

This commit is contained in:
Wojtek Figat
2024-02-18 11:22:35 +01:00
parent d76b5234c5
commit 636b2c91cc
18 changed files with 79 additions and 97 deletions

View File

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

View File

@@ -38,7 +38,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -38,7 +38,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -47,7 +47,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -47,7 +47,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -71,7 +71,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -38,7 +38,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -43,7 +43,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -44,7 +44,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -38,7 +38,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -75,7 +75,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}

View File

@@ -69,7 +69,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -38,7 +38,7 @@ namespace FlaxEditor.Content
/// <inheritdoc />
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.");
}
}

View File

@@ -869,7 +869,9 @@ namespace FlaxEditor
/// <summary>
/// New asset types allowed to create.
/// [Deprecated in v1.8]
/// </summary>
[Obsolete("Use CreateAsset with named tag.")]
public enum NewAssetType
{
/// <summary>
@@ -1046,12 +1048,59 @@ namespace FlaxEditor
/// <summary>
/// Creates new asset at the target location.
/// [Deprecated in v1.8]
/// </summary>
/// <param name="type">New asset type.</param>
/// <param name="outputPath">Output asset path.</param>
[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);
}
/// <summary>
@@ -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);

View File

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

View File

@@ -210,6 +210,13 @@ public:
API_FUNCTION() static bool TryRestoreImportOptions(API_PARAM(Ref) AudioTool::Options& options, String assetPath);
#endif
/// <summary>
/// Creates a new asset at the target location.
/// </summary>
/// <param name="tag">New asset type.</param>
/// <param name="outputPath">Output asset path.</param>
API_FUNCTION() static bool CreateAsset(const String& tag, String outputPath);
public:
API_STRUCT(Internal, NoDefault) struct VisualScriptStackFrame
{

View File

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

View File

@@ -113,7 +113,7 @@ private:
/// <summary>
/// Asset importer entry
/// </summary>
struct AssetImporter
struct FLAXENGINE_API AssetImporter
{
public:
/// <summary>
@@ -135,7 +135,7 @@ public:
/// <summary>
/// Asset creator entry
/// </summary>
struct AssetCreator
struct FLAXENGINE_API AssetCreator
{
public:
/// <summary>