예제 #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);
        }
예제 #2
0
        public override MyObjectBuilder_ComponentBase Serialize(bool copy = false)
        {
            MyObjectBuilder_ContainerDropComponent component1 = MyComponentFactory.CreateObjectBuilder(this) as MyObjectBuilder_ContainerDropComponent;

            component1.Competetive  = this.Competetive;
            component1.GPSName      = this.GPSName;
            component1.Owner        = this.Owner;
            component1.PlayingSound = this.m_playingSound;
            return(component1);
        }
예제 #3
0
        public override MyObjectBuilder_ComponentBase Serialize(bool copy = false)
        {
            var builder = MyComponentFactory.CreateObjectBuilder(this) as MyObjectBuilder_TimerComponent;

            builder.Repeat              = Repeat;
            builder.TimeToEvent         = TimeToEvent;
            builder.SetTimeMinutes      = m_setTimeMin;
            builder.TimerEnabled        = TimerEnabled;
            builder.RemoveEntityOnTimer = RemoveEntityOnTimer;
            return(builder);
        }
예제 #4
0
        public override MyObjectBuilder_ComponentBase Serialize(bool copy = false)
        {
            MyObjectBuilder_TimerComponent component1 = MyComponentFactory.CreateObjectBuilder(this) as MyObjectBuilder_TimerComponent;

            component1.Repeat              = this.Repeat;
            component1.TimeToEvent         = this.TimeToEvent;
            component1.SetTimeMinutes      = this.m_setTimeMin;
            component1.TimerEnabled        = this.TimerEnabled;
            component1.RemoveEntityOnTimer = this.RemoveEntityOnTimer;
            return(component1);
        }
예제 #5
0
        public override void Deserialize(MyObjectBuilder_ComponentBase builder)
        {
            base.Deserialize(builder);
            MyObjectBuilder_InventoryAggregate aggregate = builder as MyObjectBuilder_InventoryAggregate;

            if ((aggregate != null) && (aggregate.Inventories != null))
            {
                foreach (MyObjectBuilder_InventoryBase base2 in aggregate.Inventories)
                {
                    MyComponentBase component = MyComponentFactory.CreateInstanceByTypeId(base2.TypeId);
                    component.Deserialize(base2);
                    this.AddComponent(component);
                }
            }
        }
예제 #6
0
        public override void Deserialize(MyObjectBuilder_ComponentBase builder)
        {
            base.Deserialize(builder);

            var ob = builder as MyObjectBuilder_InventoryAggregate;

            if (ob != null && ob.Inventories != null)
            {
                foreach (var obInv in ob.Inventories)
                {
                    var comp = MyComponentFactory.CreateInstance(obInv.GetType());
                    comp.Deserialize(obInv);
                    this.AddComponent(comp);
                }
            }
        }
        public override void Deserialize(MyObjectBuilder_ComponentBase builder)
        {
            base.Deserialize(builder);

            var ob = builder as MyObjectBuilder_TriggerAggregate;

            if (ob != null && ob.AreaTriggers != null)
            {
                foreach (var obTrigger in ob.AreaTriggers)
                {
                    var comp = MyComponentFactory.CreateInstanceByTypeId(obTrigger.TypeId);
                    comp.Deserialize(obTrigger);
                    this.AddComponent(comp);
                }
            }
        }
예제 #8
0
        private static void AddFractureComponent(MyObjectBuilder_FractureComponentBase obFractureComponent, MyEntity entity)
        {
            var component         = MyComponentFactory.CreateInstanceByTypeId(obFractureComponent.TypeId);
            var fractureComponent = component as MyFractureComponentBase;

            Debug.Assert(fractureComponent != null);
            if (fractureComponent != null)
            {
                try
                {
                    bool hasComponent = entity.Components.Has <MyFractureComponentBase>();
                    Debug.Assert(!hasComponent);
                    if (!hasComponent)
                    {
                        entity.Components.Add <MyFractureComponentBase>(fractureComponent);
                        fractureComponent.Deserialize(obFractureComponent);
                    }
                }
                catch (Exception e)
                {
                    MyLog.Default.WriteLine("Cannot add received fracture component: " + e.Message);
                    if (entity.Components.Has <MyFractureComponentBase>())
                    {
                        MyCubeBlock block = entity as MyCubeBlock;
                        if (block != null && block.SlimBlock != null)
                        {
                            block.SlimBlock.RemoveFractureComponent();
                        }
                        else
                        {
                            Debug.Fail("Fracture component not supported for other entities than MyCubeBlock for now!");
                            // Renderer has to be set to entity default because it is changed in fracture component.
                            entity.Components.Remove <MyFractureComponentBase>();
                        }
                    }

                    StringBuilder sb = new StringBuilder();
                    foreach (var shape in obFractureComponent.Shapes)
                    {
                        sb.Append(shape.Name).Append(" ");
                    }

                    Debug.Fail("Recieved fracture component not added");
                    MyLog.Default.WriteLine("Received fracture component not added, no shape found. Shapes: " + sb.ToString());
                }
            }
        }
예제 #9
0
        private static void AddFractureComponent(MyObjectBuilder_FractureComponentBase obFractureComponent, MyEntity entity)
        {
            var component         = MyComponentFactory.CreateInstance(obFractureComponent.GetType());
            var fractureComponent = component as MyFractureComponentBase;

            Debug.Assert(fractureComponent != null);
            if (fractureComponent != null)
            {
                bool hasComponent = entity.Components.Has <MyFractureComponentBase>();
                Debug.Assert(!hasComponent);
                if (!hasComponent)
                {
                    entity.Components.Add <MyFractureComponentBase>(fractureComponent);
                    fractureComponent.Deserialize(obFractureComponent);
                }
            }
        }
예제 #10
0
        public override MyObjectBuilder_ComponentBase Serialize(bool copy = false)
        {
            MyObjectBuilder_UseObjectsComponent component = MyComponentFactory.CreateObjectBuilder(this) as MyObjectBuilder_UseObjectsComponent;

            component.CustomDetectorsCount = (uint)this.m_customAddedDetectors.Count;
            int index = 0;

            if (component.CustomDetectorsCount > 0)
            {
                component.CustomDetectorsMatrices = new Matrix[component.CustomDetectorsCount];
                component.CustomDetectorsNames    = new string[component.CustomDetectorsCount];
                foreach (uint num2 in this.m_customAddedDetectors)
                {
                    component.CustomDetectorsNames[index]    = this.m_detectorInteractiveObjects[num2].DetectorName;
                    component.CustomDetectorsMatrices[index] = this.m_detectorInteractiveObjects[num2].Matrix;
                    index++;
                }
            }
            return(component);
        }
        /// <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);
        }
예제 #12
0
        public override VRage.Game.ObjectBuilders.ComponentSystem.MyObjectBuilder_ComponentBase Serialize()
        {
            var builder = MyComponentFactory.CreateObjectBuilder(this) as MyObjectBuilder_UseObjectsComponent;

            builder.CustomDetectorsCount = (uint)m_customAddedDetectors.Count;

            int i = 0;

            if (builder.CustomDetectorsCount > 0)
            {
                builder.CustomDetectorsMatrices = new Matrix[builder.CustomDetectorsCount];
                builder.CustomDetectorsNames    = new string[builder.CustomDetectorsCount];
                foreach (var detector in m_customAddedDetectors)
                {
                    builder.CustomDetectorsNames[i]    = m_detectorInteractiveObjects[detector].DetectorName;
                    builder.CustomDetectorsMatrices[i] = m_detectorInteractiveObjects[detector].Matrix;
                    i++;
                }
            }

            return(builder);
        }
예제 #13
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");
            }
        }
예제 #16
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);
     }
 }