Пример #1
0
        /// <summary>Updates the plugin dictionary with new and changed plugins.</summary>
        /// <param name="bot">A bot instance when the plugin is a bot local plugin.</param>
        private void CheckLocalPlugins(Bot bot)
        {
            var dir = new DirectoryInfo(config.Path);

            if (!dir.Exists)
            {
                return;
            }

            foreach (var file in dir.EnumerateFiles())
            {
                if (plugins.TryGetValue(file.Name, out var plugin))
                {
                    var status = plugin.CheckStatus(bot);
                    switch (status)
                    {
                    case PluginStatus.Disabled:
                    case PluginStatus.Active:
                    case PluginStatus.NotAvailable:
                        continue;

                    case PluginStatus.Ready:
                    case PluginStatus.Off:
                    case PluginStatus.Error:
                        plugin.Load();
                        break;

                    default:
                        throw Tools.UnhandledDefault(status);
                    }
                }
                else
                {
                    if (IsIgnored(file))
                    {
                        continue;
                    }

                    plugin = new Plugin(file, GetFreeId());

                    if (plugin.Load() == PluginResponse.Disabled)
                    {
                        RemovePlugin(plugin);
                        continue;
                    }

                    coreInjector.FillProperties(plugin);
                    plugins.Add(file.Name, plugin);
                }
            }
        }
Пример #2
0
        private bool StartInternal(Bot bot)
        {
            if (CheckStatus(bot) != PluginStatus.Ready)
            {
                throw new InvalidOperationException("This plugin has not yet been prepared");
            }

            try
            {
                switch (Type)
                {
                case PluginType.None:
                    throw new InvalidOperationException("A 'None' plugin cannot be loaded");

                case PluginType.BotPlugin:
                    if (bot is null)
                    {
                        Log.Error("This plugin needs to be activated on a bot instance.");
                        status = PluginStatus.Error;
                        return(false);
                    }
                    if (pluginObjectList.ContainsKey(bot))
                    {
                        throw new InvalidOperationException("Plugin is already instantiated on this bot");
                    }
                    var pluginInstance = (IBotPlugin)Activator.CreateInstance(coreType);
                    if (pluginObjectList.Count == 0)
                    {
                        RegisterCommands(pluginInstance, coreType);
                    }
                    pluginObjectList.Add(bot, pluginInstance);
                    bot.Injector.FillProperties(pluginInstance);
                    pluginInstance.Initialize();
                    break;

                case PluginType.CorePlugin:
                    pluginObject = (ICorePlugin)Activator.CreateInstance(coreType);
                    RegisterCommands(pluginObject, coreType);
                    CoreInjector.FillProperties(pluginObject);
                    pluginObject.Initialize();
                    break;

                case PluginType.Factory:
                    factoryObject = (IFactory)Activator.CreateInstance(coreType);
                    ResourceFactory.AddFactory(factoryObject);
                    break;

                case PluginType.Commands:
                    RegisterCommands(null, coreType);
                    break;

                default:
                    throw Util.UnhandledDefault(Type);
                }
            }
            catch (Exception ex)
            {
                if (ex is MissingMethodException)
                {
                    Log.Error(ex, "Plugins and Factories needs a parameterless constructor.");
                }
                else
                {
                    Log.Error(ex, "Plugin '{0}' failed to load: {1}.", Name, ex.Message);
                }
                Stop(bot);
                if (Type != PluginType.BotPlugin)
                {
                    status = PluginStatus.Error;
                }
                return(false);
            }

            if (Type != PluginType.BotPlugin)
            {
                status = PluginStatus.Active;
            }

            return(true);
        }