public void DeserializeEntity(BinaryReader reader) { int uniqueId = reader.ReadInt32(); int ownerUniqueId = reader.ReadInt32(); int templateId = reader.ReadInt32(); EntityTemplate entityTemplate = EntityTemplateManager.GetTemplateById(templateId); Entity entity = Allocate(uniqueId); entity.TemplateId = templateId; entity.OwnerUniqueId = ownerUniqueId; for (int i = 0; i < EntityManager.MaxComponentTypes; i++) { deserializationComponentWorker1[i] = false; } int componentCount = reader.ReadByte(); Debug.Assert((componentCount > 0) && (componentCount < componentManagers.Length), "Corrupt file"); for (int i = 0; i < componentCount; i++) { int componentTypeId = reader.ReadByte(); bool allocatedNew; Component component = GetOrAllocateComponentAndUpdateBitField(entity, componentTypeId, out allocatedNew); component.Deserialize(reader); deserializationComponentWorker1[componentTypeId] = true; } // Fill in the missing ones from the template. Mark off which ones we did and go do the missing ones. for (int i = 0; i < EntityManager.MaxComponentTypes; i++) { Component componentTemplate = entityTemplate.ComponentArray[i]; if (!deserializationComponentWorker1[i] && (componentTemplate != null)) { //Debug.Assert(i != ComponentTypeIds.TransformChildren, "TransformChildren should not be directly in template"); // If change this, beware of if statement below bool allocatedNew; Component component = GetOrAllocateComponentAndUpdateBitField(entity, i, out allocatedNew); component.ApplyFromTemplate(componentTemplate); } } // Handle adding any children if we didn't encounter a serialized set of them. /* * if (!deserializationComponentWorker1[ComponentTypeIds.TransformChildren]) * { * AddChildrenBasedOnTemplate(entity, entityTemplate); * }*/ // The component bits should be set now. AddNewEntityToSystems(entity); }
/* * private void AddChildrenBasedOnTemplate(Entity entity, EntityTemplate entityTemplate) * { * if ((entityTemplate.ChildTemplates != null) && (entityTemplate.ChildTemplates.Count > 0)) * { * if ((entity.OwnerUniqueId != InvalidEntityUniqueId) && !Universe.IsTopLevelGroupId(entity.OwnerUniqueId)) * { * throw new InvalidOperationException("Haven't yet implementing cloning owned entities"); * } * * bool allocatedNew; * TransformChildren transformChildren = (TransformChildren)this.GetOrAllocateComponentAndUpdateBitField(entity, ComponentTypeIds.TransformChildren, out allocatedNew); * Debug.Assert(allocatedNew); * foreach (EntityTemplate childTemplate in entityTemplate.ChildTemplates) * { * // REVIEW: Re-entrancy, but it should be ok. * Entity childEntity = this.AllocateForStaticContent(childTemplate, entity.OwnerUniqueId); * transformChildren.AddItem(this, entity.LiveId, childEntity); * } * } * }*/ private void ApplyFromTemplate(Entity entity, int templateId) { EntityTemplate entityTemplate = EntityTemplateManager.GetTemplateById(templateId); foreach (Component componentTemplate in entityTemplate.ComponentTemplates) { bool allocatedNew; Component component = this.GetOrAllocateComponentAndUpdateBitField(entity, typeToComponentId[componentTemplate.GetType()], out allocatedNew); if (allocatedNew) { // That means it didn't exist before. So use the template version. component.ApplyFromTemplate(componentTemplate); } } // Handle adding any children. //AddChildrenBasedOnTemplate(entity, entityTemplate); entity.TemplateId = templateId; }
public static EntityTemplate NewInheritFrom(string name, string parent) { EntityTemplate cloneSource = EntityTemplateManager.GetTemplateByName(parent); return(CreateFromOther(name, cloneSource)); }
public void SerializeEntity(BinaryWriter writer, Entity entity, List <int> extraIdsToSerialize, EntityEnumeration enumerationType, List <int> liveIdsWeSkippedSerializing) { Debug.Assert(((enumerationType == EntityEnumeration.Deep) && (extraIdsToSerialize != null)) || ((enumerationType == EntityEnumeration.Shallow) && (extraIdsToSerialize == null))); writer.Write(entity.UniqueId); writer.Write(entity.OwnerUniqueId); writer.Write(entity.TemplateId); EntityTemplate entityTemplate = EntityTemplateManager.GetTemplateById(entity.TemplateId); //bool addTransformChildrenToExtraIdsList = false; // Now we write the number of components. To know how many we're going to write out, // we need to figure out how many are different than the template ones. int saveCount = 0; for (int i = 0; i < EntityManager.MaxComponentTypes; i++) { Component componentCurrent = GetComponent(entity, i); serializationComponentWorker1[i] = null; if (componentCurrent != null) { bool exactMatch = false; //if ((i == ComponentTypeIds.TransformChildren) && (enumerationType == EntityEnumeration.Deep)) if (enumerationType == EntityEnumeration.Deep) { throw new NotImplementedException(); /* * // Big optimization: For TransformChildren, if the set of transform children for this entity matches * // *exactly* what's in the template's children, then we can skip adding this component altogether, * // saving a lot of space. We''ll assume the same order too. * TransformChildren transformChildren = componentCurrent as TransformChildren; * List<int> containedItemUniqueIds = transformChildren.GetContainedItemUniqueIds(); * exactMatch = (containedItemUniqueIds.Count == entityTemplate.ChildTemplates.Count); * if (exactMatch) * { * for (int matchIndex = 0; exactMatch && (matchIndex < containedItemUniqueIds.Count); matchIndex++) * { * Entity childEntity = GetEntityByUniqueId(containedItemUniqueIds[matchIndex]); * EntityTemplate childEntityTemplate = entityTemplate.ChildTemplates[matchIndex]; * exactMatch = IsEntityExactMatchForTemplate(childEntity, childEntityTemplate); * } * } * * // We'll need to serialize the extra ids if this wasn't an exact match. * addTransformChildrenToExtraIdsList = !exactMatch; * * // If we're not adding them to the "please serialize" list, we need to add them to the * // list of things we skipped serializing (in case they need to be removed) * if (!addTransformChildrenToExtraIdsList) * { * foreach (int uniqueId in containedItemUniqueIds) * { * liveIdsWeSkippedSerializing.Add(GetEntityByUniqueId(uniqueId).LiveId); * } * }*/ } else { Component templateComponent = entityTemplate.ComponentArray[i]; exactMatch = (templateComponent != null) && !componentCurrent.IsDifferent(entity, templateComponent); } if (!exactMatch) { // Need to serialize it, because it doesn't match the template. serializationComponentWorker1[i] = componentCurrent; saveCount++; } } } writer.Write((byte)saveCount); for (int i = 0; i < EntityManager.MaxComponentTypes; i++) { Component componentCurrent = serializationComponentWorker1[i]; if (componentCurrent != null) { writer.Write(typeToComponentId[componentCurrent.GetType()]); componentCurrent.Serialize(writer); // Inventory is straightforward, because these are never part of a template, so we don't need to optimize them out. // So always add them to the extra ids to serialize. if (enumerationType == EntityEnumeration.Deep) { throw new NotImplementedException(); } /* * if ((i == ComponentTypeIds.Inventory) && (enumerationType == EntityEnumeration.Deep)) * { * Inventory inventory = componentCurrent as Inventory; * foreach (int uniqueId in inventory.GetContainedItemUniqueIds()) * { * extraIdsToSerialize.Add(GetEntityByUniqueId(uniqueId).LiveId); * } * } * else if ((i == ComponentTypeIds.TransformChildren) && addTransformChildrenToExtraIdsList) * { * TransformChildren transformChildren = componentCurrent as TransformChildren; * foreach (int uniqueId in transformChildren.GetContainedItemUniqueIds()) * { * extraIdsToSerialize.Add(GetEntityByUniqueId(uniqueId).LiveId); * } * }*/ } } }
public Entity AllocateForGeneratedContent(string templateName, int ownerId) { return(AllocateForGeneratedContent(EntityTemplateManager.GetTemplateByName(templateName), ownerId)); }