示例#1
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);
        }