Example #1
0
        private bool ImportFromLocalFolder()
        {
            bool failed = false;

            // Import the (copied and mapped) files this importer previously requested to handle
            this.output = new List <AssetImportOutput>();
            for (int assignmentIndex = 0; assignmentIndex < this.inputMapping.Count; assignmentIndex++)
            {
                ImportInputAssignment  assignment = this.inputMapping.Data[assignmentIndex];
                AssetImportEnvironment importEnv  = new AssetImportEnvironment(this.targetDir, this.sourceDir, assignment.HandledInputInSourceMedia);

                if (!this.RunImporter(importEnv, assignment, this.output))
                {
                    failed = true;
                }
            }

            // Save the newly imported Resources
            foreach (AssetImportOutput item in this.output)
            {
                item.Resource.Res.Save();
            }

            return(!failed);
        }
Example #2
0
        private bool DoesOverwriteData()
        {
            for (int assignmentIndex = 0; assignmentIndex < this.inputMapping.Count; assignmentIndex++)
            {
                ImportInputAssignment assignment = this.inputMapping.Data[assignmentIndex];

                // Determine if we'll be overwriting any Resource files
                for (int i = 0; i < assignment.ExpectedOutput.Length; i++)
                {
                    if (File.Exists(assignment.ExpectedOutput[i].Resource.Path))
                    {
                        return(true);
                    }
                }

                // Determine if we'll be overwriting any source / media files
                for (int i = 0; i < assignment.HandledInput.Length; i++)
                {
                    if (File.Exists(assignment.HandledInputInSourceMedia[i].Path))
                    {
                        if (!PathHelper.FilesEqual(assignment.HandledInput[i].Path, assignment.HandledInputInSourceMedia[i].Path))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #3
0
        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;

                    AssetInfo assetInfo = resource.AssetInfo ?? new AssetInfo();
                    assetInfo.ImporterId = assignment.Importer.Id;
                    assetInfo.NameHint   = resource.Name;

                    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);
        }
Example #4
0
        private void CopySourceToLocalFolder()
        {
            for (int assignmentIndex = 0; assignmentIndex < this.inputMapping.Count; assignmentIndex++)
            {
                ImportInputAssignment assignment = this.inputMapping.Data[assignmentIndex];

                // Copy all handled files to the media / source directory
                try
                {
                    // Copy each handled file into source / media, while preserving the original relative structure
                    for (int i = 0; i < assignment.HandledInput.Length; i++)
                    {
                        string filePath = assignment.HandledInput[i].Path;
                        string filePathInSourceMedia = assignment.HandledInputInSourceMedia[i].Path;
                        string dirPathInSourceMedia  = Path.GetDirectoryName(filePathInSourceMedia);

                        // If the file is already where it needs to be, skip it
                        if (PathOp.ArePathsEqual(filePath, filePathInSourceMedia))
                        {
                            continue;
                        }

                        // If there already is a similarly named file in the source directory, delete it.
                        if (File.Exists(filePathInSourceMedia))
                        {
                            File.Delete(filePathInSourceMedia);
                        }

                        // Assure the media / source directory exists
                        Directory.CreateDirectory(dirPathInSourceMedia);

                        // Copy file from its original location to the source / media directory
                        File.Copy(filePath, filePathInSourceMedia);

                        // Let the editor know that this was an internal operation - don't handle it like an external operation
                        FileEventManager.FlagPathEditorModified(filePathInSourceMedia);
                    }
                }
                catch (Exception ex)
                {
                    Logs.Editor.WriteError("Can't copy source files to the media / source directory: {0}", LogFormat.Exception(ex));
                    this.inputMapping.RemoveAt(assignmentIndex);
                    assignmentIndex--;
                }
            }
        }
Example #5
0
        private bool ImportFromLocalFolder()
        {
            bool failed = false;

            // Import all files this importer previously requested to handle
            this.output = new List <AssetImportOutput>();
            for (int assignmentIndex = 0; assignmentIndex < this.inputMapping.Count; assignmentIndex++)
            {
                ImportInputAssignment  assignment = this.inputMapping.Data[assignmentIndex];
                AssetImportEnvironment importEnv  = new AssetImportEnvironment(
                    DualityApp.DataDirectory,
                    EditorHelper.SourceMediaDirectory,
                    assignment.HandledInputInSourceMedia);
                importEnv.IsReImport = true;

                if (!this.RunImporter(importEnv, assignment, this.output))
                {
                    failed = true;
                }
            }

            return(!failed);
        }
Example #6
0
        private void DetermineLocalInputFilePaths()
        {
            for (int assignmentIndex = 0; assignmentIndex < this.inputMapping.Count; assignmentIndex++)
            {
                ImportInputAssignment assignment = inputMapping.Data[assignmentIndex];

                // Create a relative mapping for each of the handled files
                AssetImportInput[] handledInputInSourceMedia = new AssetImportInput[assignment.HandledInput.Length];
                for (int i = 0; i < assignment.HandledInput.Length; i++)
                {
                    string oldFullName = assignment.HandledInput[i].AssetName;
                    string newFullName;

                    // If there was an automatic rename of output Resources, reflect that with local source / media paths
                    if (this.assetRenameMap.TryGetValue(oldFullName, out newFullName))
                    {
                        string ext             = Path.GetExtension(assignment.HandledInput[i].Path);
                        string newRelativePath = newFullName + ext;
                        handledInputInSourceMedia[i] = new AssetImportInput(
                            Path.Combine(sourceDir, newRelativePath),
                            newRelativePath,
                            newFullName);
                    }
                    // Otherwise, perform a regular mapping from original input location to local source / media
                    else
                    {
                        handledInputInSourceMedia[i] = new AssetImportInput(
                            Path.Combine(sourceDir, assignment.HandledInput[i].RelativePath),
                            assignment.HandledInput[i].RelativePath,
                            assignment.HandledInput[i].AssetName);
                    }
                }

                // Assign the determined paths back to the input mapping
                this.inputMapping.Data[assignmentIndex].HandledInputInSourceMedia = handledInputInSourceMedia;
            }
        }
Example #7
0
        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;
        }
Example #8
0
 public ConflictData(ImportInputAssignment[] conflicts, int defaultIndex)
 {
     this.conflicts = conflicts;
     this.defaultIndex = defaultIndex;
 }
Example #9
0
        private int ResolveMappingConflict(ImportInputAssignment[] conflictingAssignments)
        {
            // Determine the preferred importer based on existing output Resources
            string preferredImporterId = null;
            {
                // Check each importer assignment individually, as each one represents a conflicting import operation
                for (int i = 0; i < conflictingAssignments.Length; i++)
                {
                    preferredImporterId = null;

                    // Find out whether all output Resources of this importer assignment exist and share the same preferred importer
                    AssetImportOutput[] output = conflictingAssignments[i].ExpectedOutput;
                    for (int j = 0; j < output.Length; j++)
                    {
                        Resource existingRes = output[j].Resource.Res;
                        if (existingRes == null)
                        {
                            preferredImporterId = null;
                            break;
                        }

                        // If at least one Resource doesn't have a preference, that's ambiuous. Cancel it.
                        string resImporterPref = existingRes.AssetInfo != null ? existingRes.AssetInfo.ImporterId : null;
                        if (resImporterPref == null)
                        {
                            preferredImporterId = null;
                            break;
                        }

                        // Set up the shared importer preference
                        if (preferredImporterId == null)
                        {
                            preferredImporterId = resImporterPref;
                        }
                        // If different outputs from this mapping report different preferred importers, that's ambiguous. Cancel it.
                        else if (preferredImporterId != resImporterPref)
                        {
                            preferredImporterId = null;
                            break;
                        }
                    }

                    // If this importer assignment's Resources have full confidence in a preferred importer, we have a match
                    if (preferredImporterId != null)
                    {
                        break;
                    }
                }
            }

            // If we have a preferred ID, see if it's an option
            if (preferredImporterId != null)
            {
                for (int i = 0; i < conflictingAssignments.Length; i++)
                {
                    // If we have a match with the preferred importer, this is definitely the correct assignment to handle this.
                    if (conflictingAssignments[i].Importer.Id == preferredImporterId)
                        return i;
                }
            }

            // By default, fall back on simply prefering the highest-priority importer
            int keepIndex = -1;
            int highestPrio = int.MinValue;
            for (int i = 0; i < conflictingAssignments.Length; i++)
            {
                if (conflictingAssignments[i].Importer.Priority > highestPrio)
                {
                    highestPrio = conflictingAssignments[i].Importer.Priority;
                    keepIndex = i;
                }
            }

            // If there is a conflict handler (such as "spawn a user dialog"), see if that can deal with it.
            if (this.conflictHandler != null)
            {
                ConflictData data = new ConflictData(conflictingAssignments, keepIndex);
                IAssetImporter selectedImporter = this.conflictHandler(data);
                int selectedIndex = conflictingAssignments.IndexOfFirst(assignment => assignment.Importer == selectedImporter);
                return selectedIndex;
            }

            return keepIndex;
        }
Example #10
0
		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;

					AssetInfo assetInfo = resource.AssetInfo ?? new AssetInfo();
					assetInfo.ImporterId = assignment.Importer.Id;
					assetInfo.NameHint = resource.Name;

					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;
		}
Example #11
0
        protected RawList <ImportInputAssignment> SelectImporter(AssetImportEnvironment env)
        {
            if (!env.IsPrepareStep)
            {
                throw new ArgumentException(
                          "The specified import environment must be configured as a preparation environment.",
                          "env");
            }

            // Find an importer to handle some or all of the unhandled input files
            RawList <ImportInputAssignment> candidateMapping = new RawList <ImportInputAssignment>();

            foreach (IAssetImporter importer in AssetManager.Importers)
            {
                env.ResetAcquiredData();

                try
                {
                    importer.PrepareImport(env);
                }
                catch (Exception ex)
                {
                    Log.Editor.WriteError("An error occurred in the preparation step of '{1}': {0}",
                                          Log.Exception(ex),
                                          Log.Type(importer.GetType()));
                    continue;
                }

                if (env.HandledInput.Any())
                {
                    candidateMapping.Add(new ImportInputAssignment
                    {
                        Importer       = importer,
                        HandledInput   = env.HandledInput.ToArray(),
                        ExpectedOutput = env.Output.ToArray()
                    });
                }
            }

            // Sort candidate mapping from most files to least files, so we can solve the biggest conflicts first
            candidateMapping.Sort((a, b) => b.HandledInput.Length - a.HandledInput.Length);

            // Determine if multiple importers intend to handle the same files and resolve conflicts
            List <int>    conflictingIndices = new List <int>();
            List <string> conflictingFiles   = new List <string>();

            for (int mainIndex = 0; mainIndex < candidateMapping.Count; mainIndex++)
            {
                ImportInputAssignment assignment = candidateMapping[mainIndex];

                // Find all conflicts related to this assignment
                conflictingIndices.Clear();
                conflictingFiles.Clear();
                for (int secondIndex = 0; secondIndex < candidateMapping.Count; secondIndex++)
                {
                    if (secondIndex == mainIndex)
                    {
                        continue;
                    }

                    ImportInputAssignment conflictAssignment = candidateMapping[secondIndex];
                    IEnumerable <string>  mainFiles          = assignment.HandledInput.Select(item => item.Path);
                    IEnumerable <string>  secondFiles        = conflictAssignment.HandledInput.Select(item => item.Path);
                    string[] conflicts = mainFiles.Intersect(secondFiles).ToArray();
                    if (conflicts.Length > 0)
                    {
                        if (conflictingIndices.Count == 0)
                        {
                            conflictingIndices.Add(mainIndex);
                        }
                        conflictingIndices.Add(secondIndex);
                        conflictingFiles.AddRange(conflicts);
                    }
                }

                // Resolve conflicts with this assignment
                if (conflictingIndices.Count > 0)
                {
                    // Determine which importer to prefer for this conflict
                    ImportInputAssignment[] conflictingAssignments = conflictingIndices.Select(i => candidateMapping[i]).ToArray();
                    int keepIndex = this.ResolveMappingConflict(conflictingAssignments);

                    // If we somehow decided that none of the options is viable, abort the operation
                    if (keepIndex == -1)
                    {
                        candidateMapping.Clear();
                        return(candidateMapping);
                    }

                    // Sort indices to remove in declining order and remove their mappings
                    conflictingIndices.Remove(keepIndex);
                    conflictingIndices.Sort((a, b) => b - a);
                    foreach (int index in conflictingIndices)
                    {
                        candidateMapping.RemoveAt(index);
                    }

                    // Start over with the conflict search
                    mainIndex = -1;
                    continue;
                }
            }

            return(candidateMapping);
        }
Example #12
0
        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);
        }