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

LoadPlugins() public method

public LoadPlugins ( bool forceLoad = false ) : void
forceLoad bool
return void
        public void LoadPlugins( bool forceLoad = false )
        {
            if ( Loaded && !forceLoad )
                return;

            ApplicationLog.BaseLog.Info( "Loading plugins ..." );

            try
            {
                Loaded = true;
                //				m_initialized = false;   // THIS CAUSES all plugins to stop working

                string modsPath = Path.Combine( Server.Instance.Path, "Mods" );
                ApplicationLog.BaseLog.Info( "Scanning: {0}", modsPath );
                if ( !Directory.Exists( modsPath ) )
                {
                    ApplicationLog.BaseLog.Error( "Invalid directory" );
                    return;
                }

                string[ ] files = Directory.GetFiles( modsPath, "*.dll", SearchOption.AllDirectories );
                if(files.Length==0)
                    ApplicationLog.BaseLog.Info( "Found no dll files" );

                foreach ( string file in files )
                {
                    try
                    {
                        ApplicationLog.BaseLog.Info( $"Trying to load {file}" );
                        // Load assembly from file into memory, so we can hotswap it if we want
                        byte[ ] b = File.ReadAllBytes( file );
                        Assembly pluginAssembly = Assembly.Load( b );
                        if ( IsOldPlugin( pluginAssembly ) )
                        {
                            if ( IsValidPlugin( pluginAssembly ) )
                                pluginAssembly = Assembly.UnsafeLoadFrom( file );
                            else
                                continue;
                        }
                        ApplicationLog.BaseLog.Info( $"Loaded assembly {pluginAssembly.FullName}" );
                        //Get the assembly GUID
                        GuidAttribute guid = (GuidAttribute)pluginAssembly.GetCustomAttributes( typeof( GuidAttribute ), true )[ 0 ];
                        Guid guidValue = new Guid( guid.Value );

                        if ( _pluginPaths.ContainsKey( guidValue ) )
                            _pluginPaths[ guidValue ] = file;
                        else
                            _pluginPaths.Add( guidValue, file );

                        if ( _pluginAssemblies.ContainsKey( guidValue ) )
                            _pluginAssemblies[ guidValue ] = pluginAssembly;
                        else
                            _pluginAssemblies.Add( guidValue, pluginAssembly );

                        //Look through the exported types to find the one that implements PluginBase
                        Type[ ] types = pluginAssembly.GetExportedTypes( );
                        foreach ( Type type in types )
                        {
                            //Check that we don't have an entry already for this GUID
                            if ( Plugins.ContainsKey( guidValue ) )
                                break;

                            //if (type.BaseType == null)
                            //                                    continue;

                            //Type[] filteredTypes = type.BaseType.GetInterfaces();
                            Type[ ] filteredTypes = type.GetInterfaces( );
                            foreach ( Type interfaceType in filteredTypes )
                            {
                                if ( interfaceType.Name == typeof( IPlugin ).Name )
                                {
                                    try
                                    {
                                        //Create an instance of the plugin object
                                        IPlugin pluginObject = (IPlugin)Activator.CreateInstance( type );

                                        //And add it to the dictionary
                                        Plugins.Add( guidValue, pluginObject );

                                        break;
                                    }
                                    catch ( Exception ex )
                                    {
                                        ApplicationLog.BaseLog.Error( ex );
                                    }
                                }
                            }
                        }
                    }
                    catch ( Exception ex )
                    {
                        ApplicationLog.BaseLog.Error( ex );
                    }
                }
            }
            catch ( Exception ex )
            {
                ApplicationLog.BaseLog.Error( ex );
            }

            ApplicationLog.BaseLog.Info( "Finished loading plugins" );
        }

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::LoadPlugins