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