Esempio n. 1
0
        public static bool TryAddComponent(long entityId, string instanceTypeStr, string componentTypeStr)
        {
            MyEntity entity;
            Type     type  = null;
            Type     type2 = null;
            MyComponentDefinitionBase base3;

            try
            {
                type = Type.GetType(instanceTypeStr, true);
            }
            catch (Exception)
            {
            }
            try
            {
                type = Type.GetType(componentTypeStr, true);
            }
            catch (Exception)
            {
            }
            if (!MyEntities.TryGetEntityById(entityId, out entity, false) || (type == null))
            {
                return(false);
            }
            MyComponentBase component = MyComponentFactory.CreateInstanceByType(type);

            if ((entity.DefinitionId != null) && TryGetComponentDefinition(component.GetType(), entity.DefinitionId.Value.SubtypeId, out base3))
            {
                component.Init(base3);
            }
            entity.Components.Add(type2, component);
            return(true);
        }
        /// <summary>
        /// This will try to parse strings to types and create an instance of the component type. Don't use this in retail code, use for debug, modding etc.
        /// </summary>
        /// <param name="entityId">Id of entity which should get the component</param>
        /// <param name="instanceTypeStr">Type of the component instance, no the base type</param>
        /// <param name="componentTypeStr">The base type of the component to be added</param>
        /// <returns>true on success</returns>
        public static bool TryAddComponent(long entityId, string instanceTypeStr, string componentTypeStr)
        {
            MyEntity entity;
            Type     instanceType  = null;
            Type     componentType = null;

            try
            {
                instanceType = Type.GetType(instanceTypeStr, true);
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component type {0}", instanceTypeStr));
            }

            try
            {
                instanceType = Type.GetType(componentTypeStr, true);
            }
            catch (Exception)
            {
                System.Diagnostics.Debug.Fail(String.Format("Can not parse defined component type {0}", componentTypeStr));
            }

            if (MyEntities.TryGetEntityById(entityId, out entity) && instanceType != null)
            {
                var componentInstance = MyComponentFactory.CreateInstanceByType(instanceType);

                MyComponentDefinitionBase componentDefinition;
                if (entity.DefinitionId.HasValue && TryGetComponentDefinition(componentInstance.GetType(), entity.DefinitionId.Value.SubtypeId, out componentDefinition))
                {
                    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");
            }
        }
Esempio n. 4
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);
     }
 }