Example #1
0
        private void LoadAsync(long entityId, Action <T> loadingDoneHandler)
        {
            var             savedUnderType = MyComponentTypeFactory.GetComponentType(typeof(T));
            MyEntity        entity         = null;
            MyComponentBase component      = null;

            if (!MyEntities.TryGetEntityById(entityId, out entity) ||
                !entity.Components.TryGet(savedUnderType, out component))
            {
                Debug.Fail("MyEntityComponentReplicableBase - trying to init component on an entity, but either entity or component wasn't found!");
            }

            loadingDoneHandler(component as T);
        }
Example #2
0
        public static bool TryAddComponent(long entityId, MyDefinitionId componentDefinitionId)
        {
            MyEntity entity;
            MyComponentDefinitionBase base2;

            if (!MyEntities.TryGetEntityById(entityId, out entity, false))
            {
                return(false);
            }
            if (TryGetComponentDefinition(componentDefinitionId.TypeId, componentDefinitionId.SubtypeId, out base2))
            {
                MyComponentBase component     = MyComponentFactory.CreateInstanceByTypeId(base2.Id.TypeId);
                Type            componentType = MyComponentTypeFactory.GetComponentType(component.GetType());
                if (componentType == null)
                {
                    return(false);
                }
                component.Init(base2);
                entity.Components.Add(componentType, component);
            }
            return(true);
        }
        /// <summary>
        /// This will look for the component definition and if found, it will create its instance and add to the entity with the give id
        /// </summary>
        /// <returns>true on success</returns>
        public static bool TryAddComponent(long entityId, MyDefinitionId componentDefinitionId)
        {
            MyEntity entity;

            if (MyEntities.TryGetEntityById(entityId, out entity))
            {
                MyComponentDefinitionBase componentDefinition;
                if (TryGetComponentDefinition(componentDefinitionId.TypeId, componentDefinitionId.SubtypeId, out componentDefinition))
                {
                    var componentInstance = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId);
                    var componentType     = MyComponentTypeFactory.GetComponentType(componentInstance.GetType());
                    if (componentType == null)
                    {
                        System.Diagnostics.Debug.Fail("Unknown component type - component type attribute not specified for component class: " + componentInstance.GetType());
                        return(false);
                    }

                    componentInstance.Init(componentDefinition);
                    entity.Components.Add(componentType, componentInstance);
                }
                return(true);
            }
            return(false);
        }
        /// <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");
            }
        }
Example #5
0
 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);
     }
 }