public override TOutput Convert <TInput, TOutput>(TInput input,
                                                          string processorName,
                                                          OpaqueDataDictionary processorParameters)
        {
            var processor      = _manager.CreateProcessor(processorName, processorParameters);
            var processContext = new PipelineProcessorContext(_manager, new PipelineBuildEvent {
                Parameters = processorParameters
            });
            var processedObject = processor.Process(input, processContext);

            // Add its dependencies and built assets to ours.
            _pipelineEvent.Dependencies.AddRangeUnique(processContext._pipelineEvent.Dependencies);
            _pipelineEvent.BuildAsset.AddRangeUnique(processContext._pipelineEvent.BuildAsset);

            return((TOutput)processedObject);
        }
Example #2
0
        public object ProcessContent(PipelineBuildEvent pipelineEvent)
        {
            if (!File.Exists(pipelineEvent.SourceFile))
            {
                throw new PipelineException("The source file '{0}' does not exist!", pipelineEvent.SourceFile);
            }

            // Store the last write time of the source file
            // so we can detect if it has been changed.
            pipelineEvent.SourceTime = File.GetLastWriteTime(pipelineEvent.SourceFile);

            // Make sure we can find the importer and processor.
            var importer = CreateImporter(pipelineEvent.Importer);

            if (importer == null)
            {
                throw new PipelineException("Failed to create importer '{0}'", pipelineEvent.Importer);
            }

            // Try importing the content.
            object importedObject;

            if (RethrowExceptions)
            {
                try
                {
                    var importContext = new PipelineImporterContext(this);
                    importedObject = importer.Import(pipelineEvent.SourceFile, importContext);
                }
                catch (PipelineException)
                {
                    throw;
                }
                catch (Exception inner)
                {
                    throw new PipelineException(string.Format("Importer '{0}' had unexpected failure!", pipelineEvent.Importer), inner);
                }
            }
            else
            {
                var importContext = new PipelineImporterContext(this);
                importedObject = importer.Import(pipelineEvent.SourceFile, importContext);
            }

            // The pipelineEvent.Processor can be null or empty. In this case the
            // asset should be imported but not processed.
            if (string.IsNullOrEmpty(pipelineEvent.Processor))
            {
                return(importedObject);
            }

            var processor = CreateProcessor(pipelineEvent.Processor, pipelineEvent.Parameters);

            if (processor == null)
            {
                throw new PipelineException("Failed to create processor '{0}'", pipelineEvent.Processor);
            }

            // Make sure the input type is valid.
            if (!processor.InputType.IsAssignableFrom(importedObject.GetType()))
            {
                throw new PipelineException(
                          string.Format("The type '{0}' cannot be processed by {1} as a {2}!",
                                        importedObject.GetType().FullName,
                                        pipelineEvent.Processor,
                                        processor.InputType.FullName));
            }

            // Process the imported object.

            object processedObject;

            if (RethrowExceptions)
            {
                try
                {
                    var processContext = new PipelineProcessorContext(this, pipelineEvent);
                    processedObject = processor.Process(importedObject, processContext);
                }
                catch (PipelineException)
                {
                    throw;
                }
                catch (InvalidContentException)
                {
                    throw;
                }
                catch (Exception inner)
                {
                    throw new PipelineException(string.Format("Processor '{0}' had unexpected failure!", pipelineEvent.Processor), inner);
                }
            }
            else
            {
                var processContext = new PipelineProcessorContext(this, pipelineEvent);
                processedObject = processor.Process(importedObject, processContext);
            }

            return(processedObject);
        }