示例#1
0
        // Note, the 'isStatic' flag is only temporary while StaticPlugins are being
        // deprecated, after that this distinction is not necessary anymore and
        // can be removed.
        public PluginObjects CreatePluginObjects(IInjector injector, bool isStatic)
        {
            object pluginInstance = null;

            if (!isStatic)
            {
                if (!injector.TryCreate(pluginType, out pluginInstance))
                {
                    return(null);                    // TODO
                }
                injector.FillProperties(pluginInstance);
            }
            if (!injector.TryGet <CommandManager>(out var commandManager))
            {
                return(null);                //TODO
            }
            var pluginObjs = new PluginObjects
            {
                Plugin         = (ITabPlugin)pluginInstance,
                Bag            = new PluginCommandBag(pluginInstance, pluginType),
                CommandManager = commandManager,
            };

            pluginObjs.CommandManager.RegisterCollection(pluginObjs.Bag);
            return(pluginObjs);
        }
示例#2
0
        /// <summary>
        /// Stops the plugin and removes all its functionality available in the bot.
        /// Changes the status from <see cref="PluginStatus.Active"/> to <see cref="PluginStatus.Ready"/> when successful or <see cref="PluginStatus.Error"/> otherwise.
        /// </summary>
        /// <param name="bot">The bot instance where this plugin should be stopped. Can be null when not required.</param>
        public PluginResponse Stop(Bot bot)
        {
            switch (Type)
            {
            case PluginType.None:
                break;

            case PluginType.BotPlugin:
                if (bot is null)
                {
                    foreach (var pluginObjs in botPluginList.Values)
                    {
                        DestroyPluginObjects(pluginObjs);
                    }
                    botPluginList.Clear();
                }
                else
                {
                    if (botPluginList.TryGetValue(bot, out var pluginObjs))
                    {
                        botPluginList.Remove(bot);
                        DestroyPluginObjects(pluginObjs);
                    }
                }
                break;

            case PluginType.CorePlugin:
                if (corePlugin != null)
                {
                    BotManager.IterateAll(b =>
                    {
                        if (b.Injector.TryGet <CommandManager>(out var commandManager))
                        {
                            commandManager.UnregisterCollection(corePlugin.Bag);
                        }
                    });
                    DestroyPluginObjects(corePlugin);
                    corePlugin = null;
                }
                break;

            case PluginType.Factory:
                ResourceResolver.RemoveResolver(factoryObject);
                break;

            case PluginType.Commands:
                if (corePlugin != null)
                {
                    DestroyPluginObjects(corePlugin);
                }
                break;

            default:
                throw Tools.UnhandledDefault(Type);
            }

            status = PluginStatus.Ready;

            return(PluginResponse.Ok);
        }
示例#3
0
 public Plugin(FileInfo file, int id)
 {
     corePlugin = null;
     File       = file;
     Id         = id;
     status     = PluginStatus.Off;
     Type       = PluginType.None;
 }
示例#4
0
        private void DestroyPluginObjects(PluginObjects pluginObjs)
        {
            pluginObjs.CommandManager.UnregisterCollection(pluginObjs.Bag);

            try
            {
                pluginObjs.Plugin?.Dispose();
            }
            catch (Exception ex)
            {
                Log.Warn(ex, "Plugin '{0}' threw an exception while disposing", Name);
            }
        }
示例#5
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 (botPluginList.ContainsKey(bot))
                    {
                        throw new InvalidOperationException("Plugin is already instantiated on this bot");
                    }

                    var botPluginObjs = CreatePluginObjects(bot.Injector, false);
                    botPluginList.Add(bot, botPluginObjs);
                    botPluginObjs.Plugin.Initialize();
                    break;

                case PluginType.CorePlugin:
                    corePlugin = CreatePluginObjects(CoreInjector, false);
                    BotManager.IterateAll(b =>
                    {
                        try
                        {
                            if (b.Injector.TryGet <CommandManager>(out var commandManager))
                            {
                                commandManager.RegisterCollection(corePlugin.Bag);
                            }
                        }
                        catch (Exception ex) { Log.Error(ex, "Faile to register commands from plugin '{0}' for bot '{1}'", Name, b.Id); }
                    });
                    corePlugin.Plugin.Initialize();
                    break;

                case PluginType.Factory:
                    factoryObject = (IResolver)Activator.CreateInstance(pluginType);
                    ResourceResolver.AddResolver(factoryObject);
                    break;

                case PluginType.Commands:
                    corePlugin = CreatePluginObjects(CoreInjector, true);
                    break;

                default:
                    throw Tools.UnhandledDefault(Type);
                }
            }
            catch (Exception ex)
            {
                if (ex is MissingMethodException)
                {
                    Log.Error(ex, "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);
        }