SEModAPIExtensions.API.PluginManager.Update C# (CSharp) Method

Update() public method

public Update ( ) : void
return void
        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( );
        }

Usage Example

        private void PluginManagerMain(object sender, EventArgs e)
        {
            if (!Server.Instance.IsRunning)
            {
                m_pluginMainLoop.Stop();
                return;
            }

            if (m_pluginManager == null)
            {
                m_pluginMainLoop.Stop();
                return;
            }

            if (!m_pluginManager.Initialized && !m_pluginManager.Loaded)
            {
                if (SandboxGameAssemblyWrapper.Instance.IsGameStarted)
                {
                    m_pluginManager.LoadPlugins();
                    m_pluginManager.Init();
                }
            }
            else
            {
                //Force a refresh of the chat messages before running the plugin update
                List <string> messages = ChatManager.Instance.ChatMessages;

                //Run the plugin update
                m_pluginManager.Update();
            }
        }
All Usage Examples Of SEModAPIExtensions.API.PluginManager::Update