Пример #1
0
        public static void Macro(EngineState s, CodeCommand cmd)
        {
            CodeInfo_Macro info = cmd.Info as CodeInfo_Macro;

            if (info == null)
            {
                throw new InvalidCodeCommandException("Command [Macro] should have [CodeInfo_Macro]", cmd);
            }

            CodeCommand macroCmd;

            if (s.Macro.MacroDict.ContainsKey(info.MacroType))
            {
                macroCmd         = s.Macro.MacroDict[info.MacroType];
                macroCmd.RawCode = cmd.RawCode;
            }
            else if (s.Macro.LocalDict.ContainsKey(info.MacroType))
            { // Try to find [infoMacroType] in [Variables] <- I hate undocumented behaviors!
                macroCmd         = s.Macro.LocalDict[info.MacroType];
                macroCmd.RawCode = cmd.RawCode;
            }
            else
            {
                throw new CodeCommandException($"Invalid Command [{info.MacroType}]", cmd);
            }

            Dictionary <int, string> paramDict = new Dictionary <int, string>();

            for (int i = 0; i < info.Args.Count; i++)
            {
                paramDict[i + 1] = StringEscaper.ExpandSectionParams(s, info.Args[i]);
            }

            s.CurSectionParams = paramDict;

            if (s.LogMacro)
            {
                s.InMacro = true;
                CommandBranch.RunExec(s, macroCmd, true);
                s.InMacro = false;
            }
            else // Do not log macro
            {
                s.Logger.Build_Write(s, new LogInfo(LogState.Info, $"Macro [{info.MacroType}] ({cmd.RawCode})", s.CurDepth + 1));
                s.Logger.TurnOff.Push(true);
                CommandBranch.RunExec(s, macroCmd, true);
                s.Logger.TurnOff.TryPop(out bool dummy);
            }
        }
Пример #2
0
        private static void CheckAndRunCallback(EngineState s, ref CodeCommand cbCmd, string eventParam, string eventName, bool changeCurrentPlugin = false)
        {
            if (cbCmd == null)
            {
                return;
            }

            s.Logger.Build_Write(s, $"Processing callback of event [{eventName}]");

            if (changeCurrentPlugin)
            {
                s.CurrentPlugin = cbCmd.Addr.Plugin;
            }

            s.CurDepth = 0;
            if (cbCmd.Type == CodeType.Run || cbCmd.Type == CodeType.Exec)
            {
                Debug.Assert(cbCmd.Info.GetType() == typeof(CodeInfo_RunExec));
                CodeInfo_RunExec info = cbCmd.Info as CodeInfo_RunExec;
                if (1 <= info.Parameters.Count)
                {
                    info.Parameters[0] = eventParam;
                }
                else
                {
                    info.Parameters.Add(eventParam);
                }

                CommandBranch.RunExec(s, cbCmd, false, false, true);
            }
            else
            {
                ExecuteCommand(s, cbCmd);
            }

            s.Logger.Build_Write(s, new LogInfo(LogState.Info, $"End of callback [{eventName}]", s.CurDepth));
            s.Logger.Build_Write(s, Logger.LogSeperator);
            cbCmd = null;
        }
Пример #3
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);
        }