Example #1
0
        private static void HandleMessageCmd(CommonMessage cm)
        {
            string          fullCmd = cm.FullCommand;
            CommandAnalyzer ca      = new CommandAnalyzer(new ParamDividerV2());

            ca.Analyze(fullCmd, cm);
            CommonMessageResponse replyObj = null;

            if (!PluginManager.CommandMap.ContainsKey(cm.Command))
            {
                return;
            }

            Type t = PluginManager.CommandMap[cm.Command];

            if (ValidateDisabled(cm, t))
            {
                SendMessage(new CommonMessageResponse("本群已禁用此命令...", cm));
                return;
            }

            CommandPlugin plugin = t == typeof(ExtendPlugin) ? PluginManager.CommandMapStatic[cm.Command] : GetInstance(t);

            if (!CommandHot.Keys.Contains(cm.Command))
            {
                CommandHot.TryAdd(cm.Command, 1);
            }
            else
            {
                CommandHot[cm.Command]++;
            }

            Settings.SaveSettings(CommandHot, "CommandHot");
            Task.Run(() =>
            {
                try
                {
                    SetValues(cm, t, plugin);
                    replyObj = plugin.Message_Received(cm);
                }
                catch (Exception ex)
                {
                    Exception(ex, fullCmd, plugin?.Name ?? "Unknown plugin");
                }

                if (replyObj == null)
                {
                    return;
                }
                SendMessage(replyObj);
            }
                     );
        }
Example #2
0
        protected virtual void InsertPlugin(Type type, StartupConfig startupConfig)
        {
            try
            {
                PluginBase plugin = (PluginBase)Activator.CreateInstance(type);
                string     pluginType, error = "", commands = "";
                switch (plugin.PluginType)
                {
                case PluginType.Command:
                    pluginType = "命令";
                    CommandPlugin cmdPlugin = (CommandPlugin)plugin;
                    if (cmdPlugin.Commands != null && cmdPlugin.Commands.Length > 0)
                    {
                        foreach (var cmd in cmdPlugin.Commands)
                        {
                            TaggedPlugins.Add(new TaggedClass <PluginBase>(cmd, cmdPlugin));
                            CachedCommands.Add(new TaggedClass <Type>(cmd, type));
                        }

                        commands = $"({string.Join(",", cmdPlugin.Commands)}) ";
                    }
                    else
                    {
                        error = "但此命令插件未设置命令。";
                    }

                    break;

                case PluginType.Unknown:
                    throw new NotSupportedException();

                case PluginType.Application:
                case PluginType.Service:
                default:
                    pluginType = plugin.PluginType == PluginType.Application ? "应用" : "服务";
                    TaggedPlugins.Add(new TaggedClass <PluginBase>(null, plugin));
                    break;
                }

                plugin.OnInitialized(startupConfig);
                AllPluginInitialized += plugin.AllPlugins_Initialized;
                Logger.Origin($"{pluginType} \"{plugin.Name}\" {commands}已经加载完毕。{error}");
            }
            catch (Exception ex)
            {
                Logger.Exception(ex.InnerException ?? ex);
                Logger.Error($"加载插件{type.Name}失败。");
            }
        }
Example #3
0
        private static void InsertPlugin(Type type, string[] args)
        {
            try
            {
                Plugin plugin = Activator.CreateInstance(type) as Plugin;
                if (plugin.PluginType != PluginType.Command)
                {
                    InsertPlugin(plugin, args);
                }
                else
                {
                    CommandPlugin cmdPlugin = (CommandPlugin)plugin;
                    cmdPlugin.Initialize(args);
                    string str = "";
                    if (cmdPlugin.Commands != null)
                    {
                        str = "(";
                        foreach (var cmd in cmdPlugin.Commands)
                        {
                            //CommandMap.TryAdd(cmd, (CommandApp)plugin);
                            CommandMap.TryAdd(cmd, type);
                            CommandMapStatic.TryAdd(cmd, cmdPlugin);
                            str += cmd + ",";
                        }

                        str = str.TrimEnd(',') + ") ";
                    }

                    Logger.Origin($"命令 \"{plugin.Name}\" {str}已经加载完毕。");
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex.InnerException ?? ex);
                Logger.Error($"加载插件{type.Name}失败。");
            }
        }
Example #4
0
        public static List <LogInfo> ExecuteCommand(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs     = new List <LogInfo>();
            int            curDepth = s.CurDepth;

            if (CodeCommand.DeprecatedCodeType.Contains(cmd.Type))
            {
                logs.Add(new LogInfo(LogState.Warning, $"Command [{cmd.Type}] is deprecated"));
            }

            try
            {
                switch (cmd.Type)
                {
                    #region 00 Misc
                case CodeType.None:
                    logs.Add(new LogInfo(LogState.Ignore, string.Empty));
                    break;

                case CodeType.Comment:
                {
                    if (s.LogComment)
                    {
                        logs.Add(new LogInfo(LogState.Ignore, string.Empty));
                    }
                }
                break;

                case CodeType.Error:
                {
                    Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Error));
                    CodeInfo_Error info = cmd.Info as CodeInfo_Error;

                    logs.Add(new LogInfo(LogState.Error, info.ErrorMessage));
                }
                break;

                    #endregion
                    #region 01 File
                case CodeType.FileCopy:
                    logs.AddRange(CommandFile.FileCopy(s, cmd));
                    break;

                case CodeType.FileDelete:
                    logs.AddRange(CommandFile.FileDelete(s, cmd));
                    break;

                case CodeType.FileRename:
                case CodeType.FileMove:
                    logs.AddRange(CommandFile.FileRename(s, cmd));
                    break;

                case CodeType.FileCreateBlank:
                    logs.AddRange(CommandFile.FileCreateBlank(s, cmd));
                    break;

                case CodeType.FileSize:
                    logs.AddRange(CommandFile.FileSize(s, cmd));
                    break;

                case CodeType.FileVersion:
                    logs.AddRange(CommandFile.FileVersion(s, cmd));
                    break;

                case CodeType.DirCopy:
                    logs.AddRange(CommandFile.DirCopy(s, cmd));
                    break;

                case CodeType.DirDelete:
                    logs.AddRange(CommandFile.DirDelete(s, cmd));
                    break;

                case CodeType.DirMove:
                    logs.AddRange(CommandFile.DirMove(s, cmd));
                    break;

                case CodeType.DirMake:
                    logs.AddRange(CommandFile.DirMake(s, cmd));
                    break;

                case CodeType.DirSize:
                    logs.AddRange(CommandFile.DirSize(s, cmd));
                    break;

                case CodeType.PathMove:
                    logs.AddRange(CommandFile.PathMove(s, cmd));
                    break;

                    #endregion
                    #region 02 Registry
                case CodeType.RegHiveLoad:
                    logs.AddRange(CommandRegistry.RegHiveLoad(s, cmd));
                    break;

                case CodeType.RegHiveUnload:
                    logs.AddRange(CommandRegistry.RegHiveUnload(s, cmd));
                    break;

                case CodeType.RegRead:
                    logs.AddRange(CommandRegistry.RegRead(s, cmd));
                    break;

                case CodeType.RegWrite:
                    logs.AddRange(CommandRegistry.RegWrite(s, cmd));
                    break;

                case CodeType.RegWriteLegacy:     // WB082 Compatibility Shim
                    logs.AddRange(CommandRegistry.RegWriteLegacy(s, cmd));
                    break;

                case CodeType.RegDelete:
                    logs.AddRange(CommandRegistry.RegDelete(s, cmd));
                    break;

                case CodeType.RegMulti:
                    logs.AddRange(CommandRegistry.RegMulti(s, cmd));
                    break;

                case CodeType.RegImport:
                    logs.AddRange(CommandRegistry.RegImport(s, cmd));
                    break;

                case CodeType.RegExport:
                    logs.AddRange(CommandRegistry.RegExport(s, cmd));
                    break;

                    #endregion
                    #region 03 Text
                case CodeType.TXTAddLine:
                    logs.AddRange(CommandText.TXTAddLine(s, cmd));
                    break;

                case CodeType.TXTAddLineOp:
                    logs.AddRange(CommandText.TXTAddLineOp(s, cmd));
                    break;

                case CodeType.TXTReplace:
                    logs.AddRange(CommandText.TXTReplace(s, cmd));
                    break;

                case CodeType.TXTReplaceOp:
                    logs.AddRange(CommandText.TXTReplaceOp(s, cmd));
                    break;

                case CodeType.TXTDelLine:
                    logs.AddRange(CommandText.TXTDelLine(s, cmd));
                    break;

                case CodeType.TXTDelLineOp:
                    logs.AddRange(CommandText.TXTDelLineOp(s, cmd));
                    break;

                case CodeType.TXTDelSpaces:
                    logs.AddRange(CommandText.TXTDelSpaces(s, cmd));
                    break;

                case CodeType.TXTDelEmptyLines:
                    logs.AddRange(CommandText.TXTDelEmptyLines(s, cmd));
                    break;

                    #endregion
                    #region 04 INI
                case CodeType.INIRead:
                    logs.AddRange(CommandIni.IniRead(s, cmd));
                    break;

                case CodeType.INIReadOp:
                    logs.AddRange(CommandIni.IniReadOp(s, cmd));
                    break;

                case CodeType.INIWrite:
                    logs.AddRange(CommandIni.IniWrite(s, cmd));
                    break;

                case CodeType.INIWriteOp:
                    logs.AddRange(CommandIni.IniWriteOp(s, cmd));
                    break;

                case CodeType.INIDelete:
                    logs.AddRange(CommandIni.IniDelete(s, cmd));
                    break;

                case CodeType.INIDeleteOp:
                    logs.AddRange(CommandIni.IniDeleteOp(s, cmd));
                    break;

                case CodeType.INIReadSection:
                    logs.AddRange(CommandIni.IniReadSection(s, cmd));
                    break;

                case CodeType.INIReadSectionOp:
                    logs.AddRange(CommandIni.IniReadSectionOp(s, cmd));
                    break;

                case CodeType.INIAddSection:
                    logs.AddRange(CommandIni.IniAddSection(s, cmd));
                    break;

                case CodeType.INIAddSectionOp:
                    logs.AddRange(CommandIni.IniAddSectionOp(s, cmd));
                    break;

                case CodeType.INIDeleteSection:
                    logs.AddRange(CommandIni.IniDeleteSection(s, cmd));
                    break;

                case CodeType.INIDeleteSectionOp:
                    logs.AddRange(CommandIni.IniDeleteSectionOp(s, cmd));
                    break;

                case CodeType.INIWriteTextLine:
                    logs.AddRange(CommandIni.IniWriteTextLine(s, cmd));
                    break;

                case CodeType.INIWriteTextLineOp:
                    logs.AddRange(CommandIni.IniWriteTextLineOp(s, cmd));
                    break;

                case CodeType.INIMerge:
                    logs.AddRange(CommandIni.IniMerge(s, cmd));
                    break;

                    #endregion
                    #region 05 Archive
                case CodeType.Compress:
                    logs.AddRange(CommandArchive.Compress(s, cmd));
                    break;

                case CodeType.Decompress:
                    logs.AddRange(CommandArchive.Decompress(s, cmd));
                    break;

                case CodeType.Expand:
                    logs.AddRange(CommandArchive.Expand(s, cmd));
                    break;

                case CodeType.CopyOrExpand:
                    logs.AddRange(CommandArchive.CopyOrExpand(s, cmd));
                    break;

                    #endregion
                    #region 06 Network
                case CodeType.WebGet:
                case CodeType.WebGetIfNotExist:     // Deprecated
                    logs.AddRange(CommandNetwork.WebGet(s, cmd));
                    break;

                    #endregion
                    #region 07 Plugin
                case CodeType.ExtractFile:
                    logs.AddRange(CommandPlugin.ExtractFile(s, cmd));
                    break;

                case CodeType.ExtractAndRun:
                    logs.AddRange(CommandPlugin.ExtractAndRun(s, cmd));
                    break;

                case CodeType.ExtractAllFiles:
                    logs.AddRange(CommandPlugin.ExtractAllFiles(s, cmd));
                    break;

                case CodeType.Encode:
                    logs.AddRange(CommandPlugin.Encode(s, cmd));
                    break;

                    #endregion
                    #region 08 Interface
                case CodeType.Visible:
                    logs.AddRange(CommandInterface.Visible(s, cmd));
                    break;

                case CodeType.VisibleOp:
                    logs.AddRange(CommandInterface.VisibleOp(s, cmd));
                    break;

                case CodeType.Message:
                    logs.AddRange(CommandInterface.Message(s, cmd));
                    break;

                case CodeType.Echo:
                    logs.AddRange(CommandInterface.Echo(s, cmd));
                    break;

                case CodeType.EchoFile:
                    logs.AddRange(CommandInterface.EchoFile(s, cmd));
                    break;

                case CodeType.UserInput:
                    logs.AddRange(CommandInterface.UserInput(s, cmd));
                    break;

                case CodeType.AddInterface:
                    logs.AddRange(CommandInterface.AddInterface(s, cmd));
                    break;

                    #endregion
                    #region 09 Hash
                case CodeType.Hash:
                    logs.AddRange(CommandHash.Hash(s, cmd));
                    break;

                    #endregion
                    #region 10 String
                case CodeType.StrFormat:
                    logs.AddRange(CommandString.StrFormat(s, cmd));
                    break;

                    #endregion
                    #region 11 Math
                case CodeType.Math:
                    logs.AddRange(CommandMath.Math(s, cmd));
                    break;

                    #endregion
                    #region 12 System
                case CodeType.System:
                    logs.AddRange(CommandSystem.SystemCmd(s, cmd));
                    break;

                case CodeType.ShellExecute:
                case CodeType.ShellExecuteEx:
                case CodeType.ShellExecuteDelete:
                case CodeType.ShellExecuteSlow:
                    logs.AddRange(CommandSystem.ShellExecute(s, cmd));
                    break;

                    #endregion
                    #region 13 Branch
                case CodeType.Run:
                case CodeType.Exec:
                    CommandBranch.RunExec(s, cmd);
                    break;

                case CodeType.Loop:
                    CommandBranch.Loop(s, cmd);
                    break;

                case CodeType.If:
                    CommandBranch.If(s, cmd);
                    break;

                case CodeType.Else:
                    CommandBranch.Else(s, cmd);
                    break;

                case CodeType.Begin:
                    throw new InternalParserException("CodeParser Error");

                case CodeType.End:
                    throw new InternalParserException("CodeParser Error");

                    #endregion
                    #region 14 Control
                case CodeType.Set:
                    logs.AddRange(CommandControl.Set(s, cmd));
                    break;

                case CodeType.SetMacro:
                    logs.AddRange(CommandControl.SetMacro(s, cmd));
                    break;

                case CodeType.AddVariables:
                    logs.AddRange(CommandControl.AddVariables(s, cmd));
                    break;

                case CodeType.Exit:
                    logs.AddRange(CommandControl.Exit(s, cmd));
                    break;

                case CodeType.Halt:
                    logs.AddRange(CommandControl.Halt(s, cmd));
                    break;

                case CodeType.Wait:
                    logs.AddRange(CommandControl.Wait(s, cmd));
                    break;

                case CodeType.Beep:
                    logs.AddRange(CommandControl.Beep(s, cmd));
                    break;

                case CodeType.GetParam:
                    logs.AddRange(CommandControl.GetParam(s, cmd));
                    break;

                case CodeType.PackParam:
                    logs.AddRange(CommandControl.PackParam(s, cmd));
                    break;

                    #endregion
                    #region 15 External Macro
                case CodeType.Macro:
                    CommandMacro.Macro(s, cmd);
                    break;

                    #endregion
                    #region Error
                // Error
                default:
                    logs.Add(new LogInfo(LogState.Error, $"Cannot execute [{cmd.Type}] command"));
                    break;
                    #endregion
                }
            }
            catch (CriticalErrorException)
            { // Stop Building
                logs.Add(new LogInfo(LogState.CriticalError, "Critical Error!", cmd, curDepth));
                throw new CriticalErrorException();
            }
            catch (InvalidCodeCommandException e)
            {
                logs.Add(new LogInfo(LogState.Error, e, e.Cmd, curDepth));
            }
            catch (Exception e)
            {
                logs.Add(new LogInfo(LogState.Error, e, cmd, curDepth));
            }

            // If ErrorOffCount is on, ignore LogState.Error and LogState.Warning
            ProcessErrorOff(s, cmd, logs);

            // Stop build on error
            if (StopBuildOnError)
            {
                if (0 < logs.Count(x => x.State == LogState.Error))
                {
                    s.ErrorHaltFlag = true;
                }
            }

            s.Logger.Build_Write(s, LogInfo.AddCommandDepth(logs, cmd, curDepth));

            // Increase only if cmd resides in CurrentPlugin.
            // So if a setion is from Macro, it will not be count.
            if (!s.ProcessedSectionHashes.Contains(cmd.Addr.Section.GetHashCode()) && s.CurrentPlugin.Equals(cmd.Addr.Plugin))
            {
                s.MainViewModel.BuildPluginProgressBarValue += 1;
            }

            // Return logs, used in unit test
            return(logs);
        }
Example #5
0
        private static bool TrySetValues(CommonMessage cm, Type t, CommandPlugin plugin)
        {
            var props     = t.GetProperties();
            int freeIndex = 0;

            string[] freeArray = cm.FreeArgs.ToArray();
            int      freeCount = freeArray.Length;
            int      swCount   = cm.Switches.Count;
            int      argCount  = cm.Args.Count;

            foreach (var prop in props)
            {
                var infos = prop.GetCustomAttributes(false);
                if (infos.Length == 0)
                {
                    continue;
                }
                foreach (var info in infos)
                {
                    switch (info)
                    {
                    case ArgAttribute argAttrib:
                        if (cm.Switches.ContainsKey(argAttrib.Name))
                        {
                            if (argAttrib.IsSwitch)
                            {
                                prop.SetValue(plugin, true);
                                swCount--;
                            }
                        }
                        else if (cm.Args.ContainsKey(argAttrib.Name))
                        {
                            if (!argAttrib.IsSwitch)
                            {
                                if (TryParse(prop, cm.Args[argAttrib.Name], out var parsed))
                                {
                                    //dynamic obj = TryParse(prop, cm.Args[argAttrib.Name]);
                                    prop.SetValue(plugin, parsed);
                                    argCount--;
                                }
                                else
                                {
                                    SendMessage(
                                        new CommonMessageResponse($"参数有误...发送 \"/help {cm.Command}\" 了解如何使用。", cm));
                                    return(false);
                                }
                            }
                        }
                        else if (argAttrib.Default != null)
                        {
                            prop.SetValue(plugin, argAttrib.Default);     //不再转换,提升效率
                        }

                        break;

                    case FreeArgAttribute freeArgAttrib:
                    {
                        if (freeIndex > freeCount - 1)
                        {
                            if (freeArgAttrib.Default != null)
                            {
                                prop.SetValue(plugin, freeArgAttrib.Default);         //不再转换,提升效率
                            }
                            break;
                        }

                        if (TryParse(prop, freeArray[freeIndex], out var parsed))
                        {
                            prop.SetValue(plugin, parsed);
                            freeIndex++;
                            break;
                        }
                        else
                        {
                            SendMessage(new CommonMessageResponse($"参数有误...发送 \"/help {cm.Command}\" 了解如何使用。",
                                                                  cm));
                            return(false);
                        }
                    }
                    }
                }
            }

            if (swCount <= 0 && argCount <= 0)
            {
                return(true);
            }
            SendMessage(new CommonMessageResponse($"包含多余的参数. 发送 \"/help {cm.Command}\" 了解如何使用。", cm));
            return(false);
        }
Example #6
0
        private static void SetValues(CommonMessage cm, Type t, CommandPlugin plugin)
        {
            var props     = t.GetProperties();
            int freeIndex = 0;

            string[] freeArray = cm.FreeArgs.ToArray();
            int      length    = freeArray.Length;

            foreach (var prop in props)
            {
                var infos = prop.GetCustomAttributes(false);
                if (infos.Length == 0)
                {
                    continue;
                }
                foreach (var info in infos)
                {
                    switch (info)
                    {
                    case ArgAttribute argAttrib:
                        if (cm.Switches.ContainsKey(argAttrib.Name))
                        {
                            if (argAttrib.IsSwitch)
                            {
                                prop.SetValue(plugin, true);
                            }
                        }
                        else if (cm.Args.ContainsKey(argAttrib.Name))
                        {
                            if (!argAttrib.IsSwitch)
                            {
                                dynamic obj = ParseStr(prop, cm.Args[argAttrib.Name]);
                                prop.SetValue(plugin, obj);
                            }
                        }
                        else if (argAttrib.Default != null)
                        {
                            prop.SetValue(plugin, argAttrib.Default);     //不再转换,提升效率
                        }

                        break;

                    case FreeArgAttribute freeArgAttrib:
                    {
                        if (freeIndex > length - 1)
                        {
                            if (freeArgAttrib.Default != null)
                            {
                                prop.SetValue(plugin, freeArgAttrib.Default);         //不再转换,提升效率
                            }
                            break;
                        }

                        dynamic obj = ParseStr(prop, freeArray[freeIndex]);
                        prop.SetValue(plugin, obj);
                        freeIndex++;
                        break;
                    }
                    }
                }
            }
        }