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" );
}