示例#1
0
        public ContentItem LoadContentItem(string filename,
                                           IContentImporter importer              = null,
                                           IContentProcessor processor            = null,
                                           Dictionary <string, object> opaqueData = null)
        {
            if (opaqueData == null)
            {
                opaqueData = new Dictionary <string, object>();
            }
            if (importer == null)
            {
                importer = CreateImporter(FindImporterByExtension(Path.GetExtension(filename)), opaqueData);
            }
            if (processor == null)
            {
                processor = CreateProcessor(FindDefaultProcessor(importer.GetType()), opaqueData);
            }

            object item;

            using (FileStream stream = File.OpenRead(filename))
            {
                item = importer.Import(stream, this);
            }
            _processorContext.PushDirectory(Path.GetDirectoryName(filename));
            item = processor.Process(item, _processorContext);
            _processorContext.PopDirectory();

            return((ContentItem)item);
        }
            /// <summary>
            /// Initializes a new instance of the <see cref="RegistryEntry"/> structure.
            /// </summary>
            /// <param name="importer">The registered importer instance.</param>
            public RegistryEntry(IContentImporter importer)
            {
                Contract.Require(importer, nameof(importer));

                var baseImporterType = ContentImporterRegistry.GetBaseContentImporterType(importer.GetType());
                var outputType = baseImporterType.GetGenericArguments().Single();

                this.importer = importer;
                this.outputType = outputType;
            }
示例#3
0
        static void Main(string[] args)
        {
            log4net.Config.BasicConfigurator.Configure(new log4net.Appender.DebugAppender());

            _opaqueData["TextureType"] = TextureType.Texture2D;
            if (!ParseCommandLine(args))
            {
                return;
            }

            if (string.IsNullOrEmpty(_outputName))
            {
                _outputName = string.Format("{0}.meb", Path.GetFileNameWithoutExtension(_imageName));
            }



            //GetTypeWriters();
            ProcessAssemblies();

            string ext = Path.GetExtension(_imageName).ToLower();
            var    imp = _importers.FirstOrDefault(i => i.attribue.FileExtensions.Contains(ext));

            if (imp.type == null)
            {
                Console.WriteLine("file format is not handled by TextureImporter.");
                return;
            }

            ContentManager           manager = new ContentManager(_log);
            ContentTypeWriterManager contentTypeWriterManager = new ContentTypeWriterManager();

            contentTypeWriterManager.RegisterTypeWriter <Texture2DContent>(new Texture2DWriter());

            IContentImporter importer = CreateImporter(imp.type, _opaqueData);

            var content = importer.Import(_imageName, manager);

            IContentProcessor processor = CreateProcessor(FindDefaultProcessor(importer.GetType()), _opaqueData);

            object processedContent = processor.Process(content, new ContentProcessorContext());

            ContentTypeWriter typeWriter = GetTypeWriter(processedContent.GetType());

            using (FileStream stream = new FileStream(_outputName, FileMode.OpenOrCreate))
            {
                ContentWriter writer = manager.CreateWriter(contentTypeWriterManager, stream);
                writer.WriteObject(processedContent, typeWriter);
                writer.Flush();
            }
        }
示例#4
0
            /// <summary>
            /// Initializes a new instance of the <see cref="RegistryEntry"/> structure.
            /// </summary>
            /// <param name="importer">The registered importer instance.</param>
            public RegistryEntry(IContentImporter importer)
            {
                Contract.Require(importer, nameof(importer));

                var baseImporterType = ContentImporterRegistry.GetBaseContentImporterType(importer.GetType());
                var outputType       = baseImporterType.GetGenericArguments().Single();

                this.importer   = importer;
                this.outputType = outputType;
            }
        protected override IContent Import(ImportedContentData importedContentData
                                           , AccessLevel requiredDestinationAccess, IContentTransferContext context
                                           , TransferImportOptions options, out Guid importedPageGuid)
        {
            if (options.TransferType != TypeOfTransfer.Importing)
            {
                return(base.Import(importedContentData
                                   , requiredDestinationAccess
                                   , context, options, out importedPageGuid));
            }

            CultureInfo originalSelectedLanguage = null;

            if (importedContentData.SelectedLanguage != null)
            {
                originalSelectedLanguage = CultureInfo.GetCultureInfo(importedContentData.SelectedLanguage.Name);
            }

            var selectedLanguageAction = importedContentData.GetType().GetProperty("SelectedLanguage");
            var contentLanguage        = importedContentData.GetLanguageBranch();

            if (!string.IsNullOrEmpty(contentLanguage))
            {
                selectedLanguageAction.SetValue(importedContentData, CultureInfo.GetCultureInfo(contentLanguage));
            }

            var        status              = importedContentData.GetStatus();
            var        propSaveAction      = context.GetType().GetProperty("SaveAction");
            SaveAction originalSaveActions = SaveAction.Publish | SaveAction.SkipValidation;

            if (!string.IsNullOrEmpty(status) && int.Parse(status) < (int)VersionStatus.Published)
            {
                propSaveAction.SetValue(context, SaveAction.CheckOut | SaveAction.ForceNewVersion | SaveAction.SkipValidation);
            }

            var orgPageSaveDBChangeBy  = ContextCache.Current["PageSaveDB:ChangedBy"];
            var orgPageSaveDBPageSaved = ContextCache.Current["PageSaveDB:PageSaved"];

            ContextCache.Current["PageSaveDB:ChangedBy"] = string.Empty;
            ContextCache.Current["PageSaveDB:PageSaved"] = string.Empty;

            try
            {
                var importedContentGuid     = new Guid(importedContentData.GetContentGuid());
                var handleContentGuidMethod = defaultContentImporter.GetType()
                                              .GetMethod("HandleContentGuid", BindingFlags.NonPublic | BindingFlags.Instance);
                var guid = (Guid)handleContentGuidMethod.Invoke(
                    defaultContentImporter
                    , new object[] { importedContentGuid, context });
                PermanentLinkMap permanentLinkMap = permanentLinkMapper.Find(guid);

                var baseContent = base.Import(importedContentData
                                              , requiredDestinationAccess
                                              , context, options, out importedPageGuid);

                if (permanentLinkMap != null && (context.SaveAction & SaveAction.Publish) != SaveAction.Publish)
                {
                    contentRepository.Save(baseContent, context.SaveAction, requiredDestinationAccess);
                }

                return(baseContent);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
            finally
            {
                selectedLanguageAction.SetValue(importedContentData, originalSelectedLanguage);
                propSaveAction.SetValue(context, originalSaveActions);
                ContextCache.Current["PageSaveDB:ChangedBy"] = orgPageSaveDBChangeBy;
                ContextCache.Current["PageSaveDB:PageSaved"] = orgPageSaveDBPageSaved;
            }
        }