Ejemplo n.º 1
0
        public override void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

            base.Dispose();

            if (BackingObject != null)
            {
                //Only remove if the backing object isn't already disposed
                bool isDisposed = (bool)BaseEntity.InvokeEntityMethod(BackingObject, BaseEntity.BaseEntityGetIsDisposedMethod);
                if (!isDisposed)
                {
                    m_networkManager.RemoveEntity();

                    Action action = InternalRemoveEntity;
                    SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction(action);
                }
            }

            if (EntityId != 0)
            {
                GameEntityManager.RemoveEntity(EntityId);
            }

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
            newEvent.type      = EntityEventManager.EntityEventType.OnBaseEntityDeleted;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            newEvent.priority  = 1;
            EntityEventManager.Instance.AddEvent(newEvent);
        }
Ejemplo n.º 2
0
        protected void InternalAddEntity()
        {
            try
            {
                if (m_nextEntityToUpdate == null)
                {
                    return;
                }

                if (SandboxGameAssemblyWrapper.IsDebugging)
                {
                    Console.WriteLine(m_nextEntityToUpdate.GetType().Name + " '" + m_nextEntityToUpdate.GetType().Name + "': Adding to scene ...");
                }

                //Create the backing object
                Type entityType   = m_nextEntityToUpdate.GetType();
                Type internalType = (Type)BaseEntity.InvokeStaticMethod(entityType, "get_InternalType");
                if (internalType == null)
                {
                    throw new Exception("Could not get internal type of entity");
                }
                m_nextEntityToUpdate.BackingObject = Activator.CreateInstance(internalType);

                //Initialize the backing object
                BaseEntity.InvokeEntityMethod(m_nextEntityToUpdate.BackingObject, "Init", new object[] { m_nextEntityToUpdate.ObjectBuilder });

                //Add the backing object to the main game object manager
                BaseEntity.InvokeStaticMethod(InternalType, ObjectManagerAddEntity, new object[] { m_nextEntityToUpdate.BackingObject, true });

                if (m_nextEntityToUpdate is FloatingObject)
                {
                    InternalBroadcastAddFloatingObject();
                }
                else
                {
                    InternalBroadcastAddEntity();
                }

                if (SandboxGameAssemblyWrapper.IsDebugging)
                {
                    Type type = m_nextEntityToUpdate.GetType();
                    Console.WriteLine(type.Name + " '" + m_nextEntityToUpdate.Name + "': Finished adding to scene");
                }

                m_nextEntityToUpdate = null;
            }
            catch (Exception ex)
            {
                LogManager.APILog.WriteLineAndConsole("Failed to add new entity");
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 3
0
        protected override bool IsValidEntity(Object entity)
        {
            try
            {
                if (entity == null)
                {
                    return(false);
                }

                //Skip unknowns for now until we get the bugs sorted out with the other types
                Type entityType = entity.GetType();
                if (entityType != CharacterEntity.InternalType &&
                    entityType != CubeGridEntity.InternalType &&
                    entityType != VoxelMap.InternalType &&
                    entityType != FloatingObject.InternalType &&
                    entityType != Meteor.InternalType
                    )
                {
                    return(false);
                }

                //Skip disposed entities
                bool isDisposed = (bool)BaseEntity.InvokeEntityMethod(entity, BaseEntity.BaseEntityGetIsDisposedMethod);
                if (isDisposed)
                {
                    return(false);
                }

                //Skip entities that have invalid physics objects
                if (BaseEntity.GetRigidBody(entity) == null || BaseEntity.GetRigidBody(entity).IsDisposed)
                {
                    return(false);
                }

                //Skip entities that don't have a position-orientation matrix defined
                if (BaseEntity.InvokeEntityMethod(entity, BaseEntity.BaseEntityGetOrientationMatrixMethod) == null)
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
                return(false);
            }
        }
        public static void BroadcastRemoveEntity(IMyEntity entity, bool safe = true)
        {
            Object result = BaseEntity.InvokeEntityMethod(entity, BaseEntity.BaseEntityGetNetManagerMethod);

            if (result == null)
            {
                return;
            }

            if (safe)
            {
                SandboxGameAssemblyWrapper.Instance.GameAction(() =>
                {
                    BaseEntity.InvokeEntityMethod(result, BaseEntityBroadcastRemovalMethod);
                });
            }
            else
            {
                BaseEntity.InvokeEntityMethod(result, BaseEntityBroadcastRemovalMethod);
            }
        }
Ejemplo n.º 5
0
        protected void InternalBroadcastAddFloatingObject()
        {
            try
            {
                if (m_nextEntityToUpdate == null)
                {
                    return;
                }

                //Broadcast the new entity to the clients
                MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(m_nextEntityToUpdate.BackingObject, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });
                //TODO - Do stuff

                m_nextEntityToUpdate.ObjectBuilder = baseEntity;
            }
            catch (Exception ex)
            {
                LogManager.APILog.WriteLineAndConsole("Failed to broadcast new floating object");
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 6
0
        protected void InternalBroadcastAddEntity()
        {
            try
            {
                if (m_nextEntityToUpdate == null)
                {
                    return;
                }

                //Broadcast the new entity to the clients
                MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(m_nextEntityToUpdate.BackingObject, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });
                Type someManager = SandboxGameAssemblyWrapper.Instance.GetAssemblyType(EntityBaseNetManagerNamespace, EntityBaseNetManagerClass);
                BaseEntity.InvokeStaticMethod(someManager, EntityBaseNetManagerSendEntity, new object[] { baseEntity });

                m_nextEntityToUpdate.ObjectBuilder = baseEntity;
            }
            catch (Exception ex)
            {
                LogManager.APILog.WriteLineAndConsole("Failed to broadcast new entity");
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 7
0
        protected void InternalAddEntity()
        {
            try
            {
                if (m_addEntityQueue.Count == 0)
                {
                    return;
                }

                BaseEntity entityToAdd = m_addEntityQueue.Dequeue();

                if (SandboxGameAssemblyWrapper.IsDebugging)
                {
                    Console.WriteLine(entityToAdd.GetType().Name + " '" + entityToAdd.GetType().Name + "': Adding to scene ...");
                }

                //Create the backing object
                Type entityType   = entityToAdd.GetType();
                Type internalType = (Type)BaseEntity.InvokeStaticMethod(entityType, "get_InternalType");
                if (internalType == null)
                {
                    throw new Exception("Could not get internal type of entity");
                }
                entityToAdd.BackingObject = Activator.CreateInstance(internalType);

                //Initialize the backing object
                BaseEntity.InvokeEntityMethod(entityToAdd.BackingObject, "Init", new object[] { entityToAdd.ObjectBuilder });

                //Add the backing object to the main game object manager
                BaseEntity.InvokeStaticMethod(InternalType, ObjectManagerAddEntity, new object[] { entityToAdd.BackingObject, true });

                if (entityToAdd is FloatingObject)
                {
                    try
                    {
                        //Broadcast the new entity to the clients
                        MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(entityToAdd.BackingObject, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });
                        //TODO - Do stuff

                        entityToAdd.ObjectBuilder = baseEntity;
                    }
                    catch (Exception ex)
                    {
                        LogManager.APILog.WriteLineAndConsole("Failed to broadcast new floating object");
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }
                else
                {
                    try
                    {
                        //Broadcast the new entity to the clients
                        MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(entityToAdd.BackingObject, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });
                        Type someManager = SandboxGameAssemblyWrapper.Instance.GetAssemblyType(EntityBaseNetManagerNamespace, EntityBaseNetManagerClass);
                        BaseEntity.InvokeStaticMethod(someManager, EntityBaseNetManagerSendEntity, new object[] { baseEntity });

                        entityToAdd.ObjectBuilder = baseEntity;
                    }
                    catch (Exception ex)
                    {
                        LogManager.APILog.WriteLineAndConsole("Failed to broadcast new entity");
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                if (SandboxGameAssemblyWrapper.IsDebugging)
                {
                    Type type = entityToAdd.GetType();
                    Console.WriteLine(type.Name + " '" + entityToAdd.Name + "': Finished adding to scene");
                }
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 8
0
        internal static MyObjectBuilder_EntityBase GetObjectBuilder(Object entity)
        {
            MyObjectBuilder_EntityBase objectBuilder = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(entity, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });

            return(objectBuilder);
        }
Ejemplo n.º 9
0
        protected override void InternalRefreshObjectBuilderMap()
        {
            try
            {
                if (m_rawDataObjectBuilderListResourceLock.Owned)
                {
                    return;
                }
                if (WorldManager.Instance.IsWorldSaving)
                {
                    return;
                }
                if (WorldManager.Instance.InternalGetResourceLock() == null)
                {
                    return;
                }
                if (WorldManager.Instance.InternalGetResourceLock().Owned)
                {
                    return;
                }

                m_rawDataObjectBuilderListResourceLock.AcquireExclusive();
                m_rawDataHashSetResourceLock.AcquireExclusive();

                m_rawDataObjectBuilderList.Clear();
                foreach (Object entity in GetBackingDataHashSet())
                {
                    try
                    {
                        if (!IsValidEntity(entity))
                        {
                            continue;
                        }

                        MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)BaseEntity.InvokeEntityMethod(entity, BaseEntity.BaseEntityGetObjectBuilderMethod, new object[] { Type.Missing });
                        if (baseEntity == null)
                        {
                            continue;
                        }

                        m_rawDataObjectBuilderList.Add(entity, baseEntity);
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                m_rawDataHashSetResourceLock.ReleaseExclusive();
                m_rawDataObjectBuilderListResourceLock.ReleaseExclusive();
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
                m_rawDataHashSetResourceLock.ReleaseExclusive();
                m_rawDataObjectBuilderListResourceLock.ReleaseExclusive();
            }
        }