/// <summary> /// Tries to retrieve entity definition of the entity owning this container, check if the definition has some DefaultComponents, /// tries to retrieve these components definitions, create these components instances and add them /// /// TODO: This should be ideally a behavior of the MyEntityComponentContainer when it is initialized (deserialized).. or by the factory, for now, this is an extension method /// </summary> public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder) { if (MyDefinitionManager.Static != null) { MyContainerDefinition definition = null; bool IsFirstInit = builder == null; if (TryGetContainerDefinition(type, subtypeName, out definition)) { container.Init(definition); if (definition.DefaultComponents != null) { foreach (var component in definition.DefaultComponents) { MyComponentDefinitionBase componentDefinition = null; MyObjectBuilder_ComponentBase componentBuilder = FindComponentBuilder(component, builder); bool IsComponentSerialized = componentBuilder != null; Type componentType = null; MyComponentBase componentInstance = null; var componentSubtype = subtypeName; if (component.SubtypeId.HasValue) { componentSubtype = component.SubtypeId.Value; } // Create component instance if (TryGetComponentDefinition(component.BuilderType, componentSubtype, out componentDefinition)) { componentInstance = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId); componentInstance.Init(componentDefinition); } else if (component.IsValid()) { if (!component.BuilderType.IsNull) { componentInstance = MyComponentFactory.CreateInstanceByTypeId(component.BuilderType); } else { componentInstance = MyComponentFactory.CreateInstanceByType(component.InstanceType); } } // Check component type from attributes. if (componentInstance != null) { var componentTypeFromAttr = MyComponentTypeFactory.GetComponentType(componentInstance.GetType()); if (componentTypeFromAttr != null) { componentType = componentTypeFromAttr; } else { if (componentDefinition != null) { System.Diagnostics.Debug.Fail("Unknown component type - component type attribute not specified for component class: " + componentInstance.GetType()); } } } //TODO: this should be avoided! Component type MUST be set via MyComponentType attribute. if (componentType == null && componentInstance != null) { componentType = componentInstance.GetType(); } // If everything passed, go on.. if (componentInstance != null && componentType != null) { bool componentShouldBeAdded = IsFirstInit || IsComponentSerialized || component.ForceCreate; if (componentShouldBeAdded) { if (componentBuilder != null) { componentInstance.Deserialize(componentBuilder); } // Add only fully initialized component.. container.Add(componentType, componentInstance); } } else { System.Diagnostics.Debug.Fail("Component instance wasn't created or it's base type couldn't been determined!"); } } } else { System.Diagnostics.Debug.Fail("Got definition for container, but DefaultComponents is null!"); } } // This may rewrite already once deserialized data, but will also add missing components in definition container.Deserialize(builder); } else { System.Diagnostics.Debug.Fail("Trying to init enabled components on entity, but definition manager is null"); } }
public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder) { if (MyDefinitionManager.Static != null) { MyContainerDefinition definition = null; bool flag = ReferenceEquals(builder, null); if (TryGetContainerDefinition(type, subtypeName, out definition)) { container.Init(definition); if (definition.DefaultComponents != null) { foreach (MyContainerDefinition.DefaultComponent component in definition.DefaultComponents) { MyComponentDefinitionBase componentDefinition = null; MyObjectBuilder_ComponentBase base3 = FindComponentBuilder(component, builder); bool flag2 = base3 != null; Type type2 = null; MyComponentBase base4 = null; MyStringHash hash = subtypeName; if (component.SubtypeId != null) { hash = component.SubtypeId.Value; } if (TryGetComponentDefinition(component.BuilderType, hash, out componentDefinition)) { base4 = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId); base4.Init(componentDefinition); } else if (component.IsValid()) { base4 = component.BuilderType.IsNull ? MyComponentFactory.CreateInstanceByType(component.InstanceType) : MyComponentFactory.CreateInstanceByTypeId(component.BuilderType); } if (base4 != null) { Type componentType = MyComponentTypeFactory.GetComponentType(base4.GetType()); if (componentType != null) { type2 = componentType; } else { MyComponentDefinitionBase base1 = componentDefinition; } } if ((type2 == null) && (base4 != null)) { type2 = base4.GetType(); } if (((base4 != null) && (type2 != null)) && ((flag | flag2) || component.ForceCreate)) { if (base3 != null) { base4.Deserialize(base3); } container.Add(type2, base4); } } } } container.Deserialize(builder); } }