using Ghost.Core; using Ghost.Editor.Core.Contracts; namespace Ghost.Editor.Core.AssetHandle; public abstract class AssetImporter { /// /// Import the asset at the specified path with the given settings. /// /// Full path to the source asset file. /// Metadata for the asset. /// Cancellation token. /// Result indicating success or failure. public abstract ValueTask ImportAsync(string assetPath, AssetMeta meta, IAssetService assetService, CancellationToken token = default); /// /// Export in-memory asset data to disk. /// Override this method to support creating assets from code. /// /// Type of asset data to export. /// Full path where the asset should be saved. /// In-memory asset data to serialize. /// Metadata for the asset. /// Cancellation token. /// Result indicating success or failure. public virtual ValueTask ExportAsync(string assetPath, T assetData, AssetMeta meta, CancellationToken token = default) where T : class { return ValueTask.FromResult(Result.Failure("This importer does not support exporting assets.")); } /// /// Validate dependencies referenced by this asset. /// Dependencies are extracted from asset content during import and stored in the database. /// /// List of dependency GUIDs extracted from the asset. /// The asset service instance. /// Result indicating if all dependencies are valid. protected virtual ValueTask ValidateDependenciesAsync(List dependencies, IAssetService assetService, CancellationToken token = default) { foreach (var dependencyGuid in dependencies) { var path = assetService.GuidToPath(dependencyGuid); if (path.IsFailure) { return ValueTask.FromResult(Result.Failure($"Missing dependency: {dependencyGuid}")); } if (!File.Exists(path.Value)) { return ValueTask.FromResult(Result.Failure($"Dependency file does not exist: {path.Value}")); } } return ValueTask.FromResult(Result.Success()); } } public abstract class AssetImporter : AssetImporter where TSettings : ImporterSettings, new() { /// /// Get the settings for this importer from the metadata. /// Creates default settings if none exist. /// /// Asset metadata. /// The importer settings. protected TSettings GetSettings(AssetMeta meta) { var typeName = GetType().Name; var settings = meta.GetImporterSettings(typeName); if (settings != null) { return settings; } var defaultSettings = new TSettings(); meta.SetImporterSettings(typeName, defaultSettings); return defaultSettings; } }