using Ghost.Core; namespace Ghost.Editor.Core.AssetHandle; /// /// Base class for all asset importers. /// Asset importers process source files and convert them into engine-ready formats. /// /// The type of importer settings this importer uses. internal abstract class AssetImporter where TSettings : ImporterSettings, new() { /// /// Import the asset at the specified path with the given settings. /// /// Full path to the source asset file. /// Metadata for the asset. /// Result indicating success or failure. public abstract Task ImportAsync(string assetPath, AssetMeta meta); /// /// 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. /// Result indicating success or failure. public virtual Task ExportAsync(string assetPath, T assetData, AssetMeta meta) where T : class { return Task.FromResult(Result.Failure("This importer does not support exporting assets.")); } /// /// 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; } /// /// 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. /// Result indicating if all dependencies are valid. protected virtual ValueTask ValidateDependenciesAsync(List dependencies) { foreach (var dependencyGuid in dependencies) { var path = AssetDatabase.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()); } }