コード例 #1
0
ファイル: TemplateGroup.cs プロジェクト: suzuke/forge
        /// <summary>
        /// Verifies that all of created templates in the template context have an associated
        /// definition in the list of template definitions.
        /// </summary>
        private void VerifyReferenceDefinitions(GameEngineContext engineContext, TemplateConversionContext templateContext, List <ContentTemplateSerializationFormat> templateDefinitions)
        {
            // We only verify template definitions if we're restoring an for an engine; it does not
            // matter if the templates are not fully instantiated if we're only in content editing
            // mode, as no game code will be executed.
            // TODO: do we really want to do this?
            if (engineContext.GameEngine.IsEmpty)
            {
                return;
            }

            // Get all of the ids for templates that we can restore
            HashSet <int> restoredTemplates = new HashSet <int>();

            foreach (var restoredTemplate in templateDefinitions)
            {
                restoredTemplates.Add(restoredTemplate.TemplateId);
            }

            // For every template that we have already created a reference for, verify that we have
            // an associated definition
            foreach (var pair in templateContext.CreatedTemplates)
            {
                ITemplate template = pair.Value;

                if (restoredTemplates.Contains(template.TemplateId) == false)
                {
                    throw new InvalidOperationException("Found template reference with id=" +
                                                        template.TemplateId + ", but the ITemplateGroup had no corresponding " +
                                                        "template definition");
                }
            }
        }
コード例 #2
0
ファイル: TemplateGroup.cs プロジェクト: suzuke/forge
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            TemplateSerializationContainer container =
                (TemplateSerializationContainer)existingValue ?? new TemplateSerializationContainer();

            List <ContentTemplateSerializationFormat> serializedTemplates = serializer.Deserialize <List <ContentTemplateSerializationFormat> >(reader);

            // We need to get our conversion context
            GeneralStreamingContext   generalContext    = (GeneralStreamingContext)serializer.Context.Context;
            TemplateConversionContext conversionContext = generalContext.Get <TemplateConversionContext>();
            GameEngineContext         engineContext     = generalContext.Get <GameEngineContext>();

            // TODO: if this method is really slow, then we can combine VerifyReferenceDefinitions
            //       and the
            // actual restoration process

            // Verify that we have restored all of our referenced templates
            VerifyReferenceDefinitions(engineContext, conversionContext, serializedTemplates);

            // Restore our created template instances
            foreach (ContentTemplateSerializationFormat format in serializedTemplates)
            {
                int       templateId = format.TemplateId;
                ITemplate template   = conversionContext.GetTemplateInstance(templateId, engineContext);

                if (template is ContentTemplate)
                {
                    ((ContentTemplate)template).Initialize(format);
                }
                else
                {
                    ((RuntimeTemplate)template).Initialize(format);
                }
            }

            container.Templates = conversionContext.CreatedTemplates.Select(p => p.Value).ToList();
            return(container);
        }