/// <summary> /// Determines the source files of the specified <see cref="Resource"/>, which /// were used during the most recent import and can be re-used during export and re-import operations. /// </summary> /// <param name="resource"></param> /// <returns></returns> public static string[] GetAssetSourceFiles(ContentRef <Resource> resource) { // Early-out, if the input Resource isn't available if (!resource.IsAvailable) { return(new string[0]); } // If the Resoure provides an explicit hint which source files were used, rely on that. AssetInfo assetInfo = resource.Res.AssetInfo; string[] sourceFileHint = (assetInfo != null) ? assetInfo.SourceFileHint : null; if (sourceFileHint != null && sourceFileHint.Length > 0) { string baseDir = AssetInternalHelper.GetSourceMediaBaseDir(resource); RawList <string> sourceFilePaths = new RawList <string>(sourceFileHint.Length); for (int i = 0; i < sourceFileHint.Length; i++) { if (string.IsNullOrWhiteSpace(sourceFileHint[i])) { continue; } string path = Path.Combine(baseDir, sourceFileHint[i].Replace(AssetInfo.FileHintNameVariable, resource.Name)); sourceFilePaths.Add(path); } sourceFilePaths.ShrinkToFit(); return(sourceFilePaths.Data); } // As a fallback, use a simulated export to determine the imported source. // This is assuming a symmetrical import-export, which isn't always true. return(AssetManager.SimulateExportAssets(resource)); }
private static string[] ExportAssets(ContentRef <Resource> inputResource, string exportDir, bool simulate) { // Early-out, if the input Resource isn't available if (!inputResource.IsAvailable) { return(new string[0]); } // If there is no export directory set, derive it from the Resource path in the Data folder if (exportDir == null) { exportDir = AssetInternalHelper.GetSourceMediaBaseDir(inputResource); } bool userAbort = false; bool success = false; // Set up an export operation and process it AssetExportOperation exportOperation = new AssetExportOperation(inputResource.Res, exportDir); exportOperation.ImporterConflictHandler = data => { IAssetImporter userSelection = ResolveImporterConflict(data); if (userSelection == null) { userAbort = true; } return(userSelection); }; success = simulate ? exportOperation.SimulatePerform() : exportOperation.Perform(); // Did we actually do something? if (!simulate && !userAbort) { // If the exporter modified export parameters of the Resource, notify the editor that we have modified it. if (exportOperation.IsAssetInfoChanged) { DualityEditorApp.NotifyObjPropChanged(null, new ObjectSelection(inputResource.Res), ReflectionInfo.Property_Resource_AssetInfo); } // If the operation was a failure, display an error message in the editor UI. if (!success) { MessageBox.Show( string.Format(Properties.GeneralRes.Msg_CantExport_Text, inputResource.Path), Properties.GeneralRes.Msg_CantExport_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } // Global event handling OnExportFinished(exportOperation, success); } return(exportOperation.OutputPaths.ToArray()); }
/// <summary> /// Retrieves the value of an import parameter for the specified <see cref="Resource"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resource">A reference to the <see cref="Duality.Resource"/> that is parameterized.</param> /// <param name="parameterName">The name of the parameter.</param> /// <param name="value">An out reference to the variable to which the retrieved value will be assigned.</param> /// <returns>True, if the value was retrieved successfully. False otherwise.</returns> public bool GetParameter <T>(IContentRef resource, string parameterName, out T value) { // If we're importing this asset for the first time, there is no data available. if (!this.isReImport) { value = default(T); return(false); } return(AssetInternalHelper.GetAssetInfoCustomValue <T>(resource.Res, parameterName, out value)); }
/// <summary> /// Sets the value of an import parameter for the specified <see cref="Resource"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="resource">A reference to the <see cref="Duality.Resource"/> that is parameterized.</param> /// <param name="parameterName">The name of the parameter.</param> /// <param name="value">The new value that should be assigned to the parameter.</param> public void SetParameter <T>(IContentRef resource, string parameterName, T value) { // Disallow adjusting parameters in the preparation step. if (this.isPrepareStep) { throw new InvalidOperationException( "Cannot adjust parameter values in the preparation step. " + "At this point, any Resource data is considered read-only."); } AssetInternalHelper.SetAssetInfoCustomValue <T>(resource.Res, parameterName, value); }
protected bool RunImporter(AssetImportEnvironment env, ImportInputAssignment assignment, IList <AssetImportOutput> outputCollection) { try { assignment.Importer.Import(env); // Get a list on properly registered output Resources and report warnings on the rest List <AssetImportOutput> expectedOutput = new List <AssetImportOutput>(); foreach (AssetImportOutput output in env.Output) { if (!assignment.ExpectedOutput.Any(item => item.Resource == output.Resource)) { Log.Editor.WriteWarning( "AssetImporter '{0}' created an unpredicted output Resource: '{1}'. " + Environment.NewLine + "This may cause problems in the Asset Management system, especially during Asset re-import. " + Environment.NewLine + "Please fix the implementation of the PrepareImport method so it properly calls AddOutput for each predicted output Resource.", Log.Type(assignment.Importer.GetType()), output.Resource); } else { expectedOutput.Add(output); } } // Collect references to the imported Resources and save them foreach (AssetImportOutput output in expectedOutput) { Resource resource = output.Resource.Res; string sourceMediaBaseDir = AssetInternalHelper.GetSourceMediaBaseDir(resource); string[] sourceFileHints = new string[output.InputPaths.Count]; for (int i = 0; i < sourceFileHints.Length; i++) { string fileName = Path.GetFileName(output.InputPaths[i]); string directory = Path.GetDirectoryName(output.InputPaths[i]); fileName = fileName.Replace(resource.Name, AssetInfo.FileHintNameVariable); sourceFileHints[i] = PathHelper.MakeFilePathRelative( Path.Combine(directory, fileName), sourceMediaBaseDir); } AssetInfo assetInfo = resource.AssetInfo ?? new AssetInfo(); assetInfo.ImporterId = assignment.Importer.Id; assetInfo.NameHint = resource.Name; assetInfo.SourceFileHint = sourceFileHints; resource.AssetInfo = assetInfo; outputCollection.Add(output); } } catch (Exception ex) { Log.Editor.WriteError("An error occurred while trying to import files using '{1}': {0}", Log.Exception(ex), Log.Type(assignment.Importer.GetType())); return(false); } return(true); }
/// <summary> /// Retrieves the value of an export parameter for the exported <see cref="Resource"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="parameterName">The name of the parameter.</param> /// <param name="value">An out reference to the variable to which the retrieved value will be assigned.</param> /// <returns>True, if the value was retrieved successfully. False otherwise.</returns> public bool GetParameter <T>(string parameterName, out T value) { return(AssetInternalHelper.GetAssetInfoCustomValue <T>(this.input, parameterName, out value)); }