Esempio n. 1
0
        public CubeBlockEntity(CubeGridEntity parent, MyObjectBuilder_CubeBlock definition, Object backingObject)
            : base(definition, backingObject)
        {
            m_parent = parent;

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
            newEvent.type      = EntityEventManager.EntityEventType.OnCubeBlockCreated;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            if (m_parent.IsLoading)
            {
                newEvent.priority = 10;
            }
            else if (EntityId != 0)
            {
                newEvent.priority = 1;
            }
            else
            {
                newEvent.priority = 2;
            }
            EntityEventManager.Instance.AddEvent(newEvent);

            if (EntityId != 0)
            {
                GameEntityManager.AddEntity(EntityId, this);
            }

            m_buildPercent     = definition.BuildPercent;
            m_integrityPercent = definition.IntegrityPercent;
            m_owner            = definition.Owner;
            m_shareMode        = definition.ShareMode;
        }
Esempio n. 2
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)InvokeEntityMethod(BackingObject, 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);
        }
Esempio n. 3
0
 private static void TriggerWorldSendEvent(ulong steamId)
 {
     EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent( );
     newEvent.type      = EntityEventManager.EntityEventType.OnPlayerWorldSent;
     newEvent.timestamp = DateTime.Now;
     newEvent.entity    = steamId;
     newEvent.priority  = 0;
     EntityEventManager.Instance.AddEvent(newEvent);
 }
Esempio n. 4
0
        public CharacterEntity(MyObjectBuilder_Character definition, Object backingObject)
            : base(definition, backingObject)
        {
            m_inventory = new InventoryEntity(definition.Inventory, InternalGetCharacterInventory());

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
            newEvent.type      = EntityEventManager.EntityEventType.OnCharacterCreated;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            newEvent.priority  = 1;
            EntityEventManager.Instance.AddEvent(newEvent);
        }
Esempio n. 5
0
        public override void Dispose( )
        {
            m_isDisposed = true;

            ApplicationLog.BaseLog.Debug("Disposing CharacterEntity '" + Name + "'");

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent( );
            newEvent.type      = EntityEventManager.EntityEventType.OnCharacterDeleted;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            newEvent.priority  = 1;
            EntityEventManager.Instance.AddEvent(newEvent);

            base.Dispose( );
        }
Esempio n. 6
0
 protected void InternalCubeGridMovedEvent(Object entity)
 {
     try
     {
         EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
         newEvent.type      = EntityEventManager.EntityEventType.OnCubeGridMoved;
         newEvent.timestamp = DateTime.Now;
         newEvent.entity    = this;
         newEvent.priority  = 9;
         EntityEventManager.Instance.AddEvent(newEvent);
     }
     catch (Exception ex)
     {
         LogManager.ErrorLog.WriteLine(ex);
     }
 }
Esempio n. 7
0
 protected void InternalCubeGridMovedEvent(Object entity)
 {
     try
     {
         EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent
         {
             type      = EntityEventManager.EntityEventType.OnCubeGridMoved,
             timestamp = DateTime.Now,
             entity    = this,
             priority  = 9
         };
         EntityEventManager.Instance.AddEvent(newEvent);
     }
     catch (Exception ex)
     {
         ApplicationLog.BaseLog.Error(ex);
     }
 }
Esempio n. 8
0
        public override void Dispose()
        {
            m_isDisposed = true;

            Parent.DeleteCubeBlock(this);

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
            newEvent.type      = EntityEventManager.EntityEventType.OnCubeBlockDeleted;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            newEvent.priority  = (ushort)((EntityId != 0) ? 1 : 2);
            EntityEventManager.Instance.AddEvent(newEvent);

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

            base.Dispose();
        }
Esempio n. 9
0
        public CubeGridEntity(MyObjectBuilder_CubeGrid definition, Object backingObject)
            : base(definition, backingObject)
        {
            m_cubeBlockManager = new CubeBlockManager(this, backingObject, CubeGridGetCubeBlocksHashSetMethod);
            m_cubeBlockManager.Refresh();

            m_networkManager = new CubeGridNetworkManager(this);
            m_managerManager = new CubeGridManagerManager(this, GetManagerManager());

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
            newEvent.type      = EntityEventManager.EntityEventType.OnCubeGridCreated;
            newEvent.timestamp = DateTime.Now;
            newEvent.entity    = this;
            newEvent.priority  = 1;
            EntityEventManager.Instance.AddEvent(newEvent);

            m_lastNameRefresh          = DateTime.Now;
            m_lastBaseCubeBlockRefresh = DateTime.Now;
            m_name = "Cube Grid";
        }
Esempio n. 10
0
        protected void InternalEntityMovedEvent(Object entity)
        {
            try
            {
                if (IsDisposed)
                {
                    return;
                }

                EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent( );
                newEvent.type      = EntityEventManager.EntityEventType.OnBaseEntityMoved;
                newEvent.timestamp = DateTime.Now;
                newEvent.entity    = this;
                newEvent.priority  = 10;
                EntityEventManager.Instance.AddEvent(newEvent);
            }
            catch (Exception ex)
            {
                ApplicationLog.BaseLog.Error(ex);
            }
        }
Esempio n. 11
0
        public override void Dispose( )
        {
            if (SandboxGameAssemblyWrapper.IsDebugging)
            {
                LogManager.APILog.WriteLine("Disposing CubeGridEntity '" + Name + "' ...");
            }

            //Dispose the cube grid by disposing all of the blocks
            //This may be slow but it's reliable ... so far

            /*
             * List<CubeBlockEntity> blocks = CubeBlocks;
             * int blockCount = blocks.Count;
             * foreach (CubeBlockEntity cubeBlock in blocks)
             * {
             *      cubeBlock.Dispose();
             * }
             *
             * if (SandboxGameAssemblyWrapper.IsDebugging)
             *      LogManager.APILog.WriteLine("Disposed " + blockCount.ToString() + " blocks on CubeGridEntity '" + Name + "'");
             */
            //Broadcast the removal to the clients just to save processing time for the clients
            BaseNetworkManager.RemoveEntity( );

            m_isDisposed = true;

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

            EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent
            {
                type      = EntityEventManager.EntityEventType.OnCubeGridDeleted,
                timestamp = DateTime.Now,
                entity    = this,
                priority  = 1
            };
            EntityEventManager.Instance.AddEvent(newEvent);
        }
        public override void Dispose()
        {
            m_isDisposed = true;

            if (SandboxGameAssemblyWrapper.IsDebugging)
            {
                LogManager.APILog.WriteLine("Disposing CubeBlockEntity '" + Name + "'");
            }

            //Only enable events for non-structural blocks, for now
            if (ObjectBuilder.EntityId != 0)
            {
                EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
                newEvent.type      = EntityEventManager.EntityEventType.OnCubeBlockDeleted;
                newEvent.timestamp = DateTime.Now;
                newEvent.entity    = this;
                newEvent.priority  = 1;
                EntityEventManager.Instance.AddEvent(newEvent);
            }

            base.Dispose();
        }
        public CubeBlockEntity(CubeGridEntity parent, MyObjectBuilder_CubeBlock definition, Object backingObject)
            : base(definition, backingObject)
        {
            m_parent = parent;

            //Only enable events for non-structural blocks, for now
            if (definition.EntityId != 0)
            {
                EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
                newEvent.type      = EntityEventManager.EntityEventType.OnCubeBlockCreated;
                newEvent.timestamp = DateTime.Now;
                newEvent.entity    = this;
                if (m_parent.IsLoading)
                {
                    newEvent.priority = 10;
                }
                else
                {
                    newEvent.priority = 1;
                }
                EntityEventManager.Instance.AddEvent(newEvent);
            }
        }
Esempio n. 14
0
        protected override void LoadDynamic()
        {
            try
            {
                HashSet <Object> rawEntities = GetBackingDataHashSet();
                Dictionary <long, BaseObject> internalDataCopy = new Dictionary <long, BaseObject>(GetInternalData());

                //Update the main data mapping
                foreach (Object entity in rawEntities)
                {
                    try
                    {
                        if (!IsValidEntity(entity))
                        {
                            continue;
                        }

                        MyObjectBuilder_CubeBlock baseEntity = (MyObjectBuilder_CubeBlock)CubeBlockEntity.InvokeEntityMethod(entity, CubeBlockEntity.CubeBlockGetObjectBuilderMethod);
                        if (baseEntity == null)
                        {
                            continue;
                        }

                        Vector3I cubePosition           = baseEntity.Min;
                        long     packedBlockCoordinates = (long)cubePosition.X + (long)cubePosition.Y * 10000 + (long)cubePosition.Z * 100000000;

                        //If the original data already contains an entry for this, skip creation
                        if (internalDataCopy.ContainsKey(packedBlockCoordinates))
                        {
                            CubeBlockEntity matchingCubeBlock = (CubeBlockEntity)GetEntry(packedBlockCoordinates);
                            if (matchingCubeBlock.IsDisposed)
                            {
                                continue;
                            }

                            matchingCubeBlock.BackingObject = entity;
                            matchingCubeBlock.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            CubeBlockEntity newCubeBlock = null;

                            if (BlockRegistry.Instance.ContainsGameType(baseEntity.TypeId))
                            {
                                //Get the matching API type from the registry
                                Type apiType = BlockRegistry.Instance.GetAPIType(baseEntity.TypeId);

                                //Create a new API cube block
                                newCubeBlock = (CubeBlockEntity)Activator.CreateInstance(apiType, new object[] { m_parent, baseEntity, entity });
                            }

                            if (newCubeBlock == null)
                            {
                                newCubeBlock = new CubeBlockEntity(m_parent, baseEntity, entity);
                            }

                            AddEntry(packedBlockCoordinates, newCubeBlock);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                //Cleanup old entities
                foreach (var entry in internalDataCopy)
                {
                    try
                    {
                        if (!rawEntities.Contains(entry.Value.BackingObject))
                        {
                            DeleteEntry(entry.Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                if (GetInternalData().Count > 0 && m_isLoading)
                {
                    //Trigger an event now that this cube grid has finished loading
                    EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent();
                    newEvent.type      = EntityEventManager.EntityEventType.OnCubeGridLoaded;
                    newEvent.timestamp = DateTime.Now;
                    newEvent.entity    = this.m_parent;
                    newEvent.priority  = 1;
                    EntityEventManager.Instance.AddEvent(newEvent);

                    m_isLoading = false;
                }
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
        public void Update()
        {
            if (!Loaded)
            {
                return;
            }
            if (!Initialized)
            {
                return;
            }
            if (!SandboxGameAssemblyWrapper.Instance.IsGameStarted)
            {
                return;
            }

            m_lastUpdateTime        = DateTime.Now - m_lastUpdate;
            m_averageUpdateInterval = (m_averageUpdateTime + m_lastUpdateTime.TotalMilliseconds) / 2;
            m_lastUpdate            = DateTime.Now;

            EntityEventManager.Instance.ResourceLocked = true;

            List <EntityEventManager.EntityEvent> events     = EntityEventManager.Instance.EntityEvents;
            List <ChatManager.ChatEvent>          chatEvents = ChatManager.Instance.ChatEvents;

            //Generate the player join/leave events here
            List <ulong> connectedPlayers = PlayerManager.Instance.ConnectedPlayers;

            try
            {
                foreach (ulong steamId in connectedPlayers)
                {
                    if (!m_lastConnectedPlayerList.Contains(steamId))
                    {
                        EntityEventManager.EntityEvent playerEvent = new EntityEventManager.EntityEvent();
                        playerEvent.priority  = 1;
                        playerEvent.timestamp = DateTime.Now;
                        playerEvent.type      = EntityEventManager.EntityEventType.OnPlayerJoined;
                        //TODO - Find a way to stall the event long enough for a linked character entity to exist - this is impossible because of cockpits and respawnships
                        //For now, create a dummy entity just for passing the player's steam id along
                        playerEvent.entity = (Object)steamId;
                        events.Add(playerEvent);
                    }
                }
                foreach (ulong steamId in m_lastConnectedPlayerList)
                {
                    if (!connectedPlayers.Contains(steamId))
                    {
                        EntityEventManager.EntityEvent playerEvent = new EntityEventManager.EntityEvent();
                        playerEvent.priority  = 1;
                        playerEvent.timestamp = DateTime.Now;
                        playerEvent.type      = EntityEventManager.EntityEventType.OnPlayerLeft;
                        //TODO - Find a way to stall the event long enough for a linked character entity to exist - this is impossible because of cockpits and respawnships
                        //For now, create a dummy entity just for passing the player's steam id along
                        playerEvent.entity = (Object)steamId;
                        events.Add(playerEvent);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.APILog.WriteLine("PluginManager.Update() Exception in player discovery: " + ex.ToString());
            }
            m_lastConnectedPlayerList = new List <ulong>(connectedPlayers);

            //Run the update threads on the plugins
            foreach (var key in m_plugins.Keys)
            {
                var plugin = m_plugins[key];

                if (!m_pluginState.ContainsKey(key))
                {
                    continue;
                }

                PluginManagerThreadParams parameters = new PluginManagerThreadParams();
                parameters.plugin     = plugin;
                parameters.key        = key;
                parameters.events     = new List <EntityEventManager.EntityEvent>(events);
                parameters.chatEvents = new List <ChatManager.ChatEvent>(chatEvents);

                Thread pluginThread = new Thread(DoUpdate);
                pluginThread.Start(parameters);
            }

            //Capture profiling info if debugging is on
            if (SandboxGameAssemblyWrapper.IsDebugging)
            {
                m_averageEvents = (m_averageEvents + (events.Count + chatEvents.Count)) / 2;

                TimeSpan updateTime = DateTime.Now - m_lastUpdate;
                m_averageUpdateTime = (m_averageUpdateTime + updateTime.TotalMilliseconds) / 2;

                TimeSpan timeSinceAverageOutput = DateTime.Now - m_lastAverageOutput;
                if (timeSinceAverageOutput.TotalSeconds > 30)
                {
                    m_lastAverageOutput = DateTime.Now;

                    LogManager.APILog.WriteLine("PluginManager - Update interval = " + m_averageUpdateInterval.ToString() + "ms");
                    LogManager.APILog.WriteLine("PluginManager - Update time = " + m_averageUpdateTime.ToString() + "ms");
                    LogManager.APILog.WriteLine("PluginManager - Events per update = " + m_averageEvents.ToString());
                }
            }

            //Clean up the event managers
            EntityEventManager.Instance.ClearEvents();
            EntityEventManager.Instance.ResourceLocked = false;
            ChatManager.Instance.ClearEvents();
        }
Esempio n. 16
0
        public void Update( )
        {
            if (!Loaded)
            {
                return;
            }
            if (!Initialized)
            {
                return;
            }
            if (!MySandboxGameWrapper.IsGameStarted)
            {
                return;
            }

            _lastUpdateTime        = DateTime.Now - _lastUpdate;
            _averageUpdateInterval = (_averageUpdateTime + _lastUpdateTime.TotalMilliseconds) / 2;
            _lastUpdate            = DateTime.Now;

            EntityEventManager.Instance.ResourceLocked = true;

            List <EntityEventManager.EntityEvent> events     = EntityEventManager.Instance.EntityEvents;
            List <ChatManager.ChatEvent>          chatEvents = ChatManager.Instance.ChatEvents;

            //Generate the player join/leave events here
            List <ulong> connectedPlayers = PlayerManager.Instance.ConnectedPlayers;

            try
            {
                foreach (ulong steamId in connectedPlayers)
                {
                    if (!_lastConnectedPlayerList.Contains(steamId))
                    {
                        EntityEventManager.EntityEvent playerEvent = new EntityEventManager.EntityEvent
                        {
                            priority  = 1,
                            timestamp = DateTime.Now,
                            type      = EntityEventManager.EntityEventType.OnPlayerJoined,
                            entity    = steamId
                        };
                        //TODO - Find a way to stall the event long enough for a linked character entity to exist - this is impossible because of cockpits and respawnships
                        //For now, create a dummy entity just for passing the player's steam id along
                        events.Add(playerEvent);
                    }
                }
                foreach (ulong steamId in _lastConnectedPlayerList)
                {
                    if (!connectedPlayers.Contains(steamId))
                    {
                        EntityEventManager.EntityEvent playerEvent = new EntityEventManager.EntityEvent
                        {
                            priority  = 1,
                            timestamp = DateTime.Now,
                            type      = EntityEventManager.EntityEventType.OnPlayerLeft,
                            entity    = steamId
                        };
                        //TODO - Find a way to stall the event long enough for a linked character entity to exist - this is impossible because of cockpits and respawnships
                        //For now, create a dummy entity just for passing the player's steam id along
                        events.Add(playerEvent);
                    }
                }
            }
            catch (Exception ex)
            {
                ApplicationLog.BaseLog.Error(ex);
            }
            _lastConnectedPlayerList = new List <ulong>(connectedPlayers);

            //Run the update threads on the plugins
            foreach (Guid key in Plugins.Keys)
            {
                object plugin = Plugins[key];

                if (!PluginStates.ContainsKey(key))
                {
                    continue;
                }

                PluginManagerThreadParams parameters = new PluginManagerThreadParams
                {
                    Plugin      = plugin,
                    Key         = key,
                    Plugins     = Plugins,
                    PluginState = PluginStates,
                    Events      = new List <EntityEventManager.EntityEvent>(events),
                    ChatEvents  = new List <ChatManager.ChatEvent>(chatEvents)
                };

                ThreadPool.QueueUserWorkItem(DoUpdate, parameters);
                //				Thread pluginThread = new Thread(DoUpdate);
                //				pluginThread.Start(parameters);
            }

            //Capture profiling info if debugging is on
            if (ExtenderOptions.IsDebugging)
            {
                _averageEvents = (_averageEvents + (events.Count + chatEvents.Count)) / 2;

                TimeSpan updateTime = DateTime.Now - _lastUpdate;
                _averageUpdateTime = (_averageUpdateTime + updateTime.TotalMilliseconds) / 2;

                TimeSpan timeSinceAverageOutput = DateTime.Now - _lastAverageOutput;
                if (timeSinceAverageOutput.TotalSeconds > 30)
                {
                    _lastAverageOutput = DateTime.Now;

                    ApplicationLog.BaseLog.Debug("PluginManager - Update interval = {0}ms", _averageUpdateInterval);
                    ApplicationLog.BaseLog.Debug("PluginManager - Update time = {0}ms", _averageUpdateTime);
                    ApplicationLog.BaseLog.Debug("PluginManager - Events per update = {0}", _averageEvents);
                }
            }

            //Clean up the event managers
            EntityEventManager.Instance.ClearEvents( );
            EntityEventManager.Instance.ResourceLocked = false;
            ChatManager.Instance.ClearEvents( );
        }