public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { EntitySerializationContainer container = (EntitySerializationContainer)existingValue ?? new EntitySerializationContainer(); var serializedEntities = serializer.Deserialize <List <ContentEntitySerializationFormat> >(reader); // We need to get our conversion context GeneralStreamingContext generalContext = (GeneralStreamingContext)serializer.Context.Context; EntityConversionContext conversionContext = generalContext.Get <EntityConversionContext>(); GameEngineContext engineContext = generalContext.Get <GameEngineContext>(); // There is only an EventDispatcherContext when we have a GameEngineContext IEventDispatcher eventDispatcher = null; if (engineContext.GameEngine.Exists) { EventDispatcherContext eventContext = generalContext.Get <EventDispatcherContext>(); eventDispatcher = eventContext.Dispatcher; } // Restore our created entity instances List <IEntity> restored = new List <IEntity>(); foreach (ContentEntitySerializationFormat format in serializedEntities) { int entityId = format.UniqueId; IEntity entity = conversionContext.GetEntityInstance(entityId, engineContext); restored.Add(entity); if (entity is ContentEntity) { ((ContentEntity)entity).Initialize(format); } else { ((RuntimeEntity)entity).Initialize(format, eventDispatcher); } } container.Entities = restored; return(container); }
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); }