Example #1
0
        private bool DetermineImportInputMapping()
        {
            AssetImportEnvironment prepareEnv = new AssetImportEnvironment(
                DualityApp.DataDirectory,
                EditorHelper.SourceMediaDirectory,
                this.input);

            prepareEnv.IsPrepareStep = true;
            prepareEnv.IsReImport    = true;

            this.inputMapping = this.SelectImporter(prepareEnv);

            // Filter out mappings without existing target Resources - can't Re-Import what isn't there.
            for (int i = this.inputMapping.Count - 1; i >= 0; i--)
            {
                if (this.inputMapping.Data[i].ExpectedOutput.Any(item => !item.Resource.IsAvailable))
                {
                    this.inputMapping.RemoveAt(i);
                }
            }

            // Since this is a Re-Import, all input files are located in source / media
            for (int i = 0; i < this.inputMapping.Count; i++)
            {
                this.inputMapping.Data[i].HandledInputInSourceMedia = this.inputMapping.Data[i].HandledInput;
            }

            return(this.inputMapping.Count > 0);
        }
Example #2
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 #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 bool DetermineImportInputMapping()
        {
            AssetImportEnvironment prepareEnv = new AssetImportEnvironment(
                this.targetDir,
                this.sourceDir,
                this.input);

            prepareEnv.IsPrepareStep = true;

            this.assetRenameMap = new Dictionary <string, string>();
            this.inputMapping   = this.SelectImporter(prepareEnv);

            // If the preparation step auto-renamed output Resources, keep this in mind
            if (prepareEnv.AssetRenameMap.Count > 0)
            {
                foreach (var pair in prepareEnv.AssetRenameMap)
                {
                    this.assetRenameMap[pair.Key] = pair.Value;
                }
            }

            return(this.inputMapping.Count > 0);
        }
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
        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 #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);
        }