Пример #1
0
        public void Branch_IfExistRegSubKey()
        {
            EngineState         s    = EngineTests.CreateEngineState();
            BranchConditionType type = BranchConditionType.ExistRegSubKey;
            BranchCondition     cond;

            cond = new BranchCondition(type, false, "HKLM", @"SOFTWARE\Microsoft\DirectMusic");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "HKLM", @"SOFTWARE\Microsoft\DirectMusicNotExist");
            Assert.IsFalse(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "HKLM", @"SOFTWARE\Microsoft\DirectMusic");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "HKLM", @"SOFTWARE\Microsoft\DirectMusicNotExist");
            Assert.IsTrue(cond.Check(s, out d));

            BranchCondition_Single_Template(s, @"If,ExistRegSection,HKLM,SOFTWARE\Microsoft\DirectMusic,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistRegSubKey,HKLM,SOFTWARE\Microsoft\DirectMusicNotExist,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistRegSection,HKLM,SOFTWARE\Microsoft\DirectMusic,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistRegSubKey,HKLM,SOFTWARE\Microsoft\DirectMusicNotExist,Set,%Dest%,T", "T");
        }
Пример #2
0
        public override List <CodeCommand> VisitIfStmt([NotNull] PEBakeryScriptParser.IfStmtContext context)
        {
            List <CodeCommand> codes = new List <CodeCommand>();

            IfCondVisitor   ifVisitor = new IfCondVisitor(addr);
            BranchCondition cond      = context.ifCond().Accept(ifVisitor);

            List <CodeCommand> ifBlock = Visit(context.branchBlock());

            CodeCommand ifCmd = new CodeCommand(context.GetText(), CodeType.If, new CodeInfo_If(cond, ifBlock), 0);

            codes.Add(ifCmd);

            if (context.elseStmt() != null)
            {
                StmtVisitor elseBlockVisitor = new StmtVisitor(addr);
                CodeCommand elseCmd          = context.elseStmt().Accept(elseBlockVisitor);
                codes.Add(elseCmd);
            }

            return(codes);
        }
Пример #3
0
        public void Branch_IfExistMacro()
        {
            EngineState         s    = EngineTests.CreateEngineState();
            BranchConditionType type = BranchConditionType.ExistMacro;
            BranchCondition     cond;

            // Test if Unicode can be used in macro name
            s.Macro.MacroDict["대한"]    = CodeParser.ParseStatement("Echo,054-790-6641", EngineTests.DummySectionAddress());
            s.Macro.LocalDict["Sonic"] = CodeParser.ParseStatement("Echo,Tails", EngineTests.DummySectionAddress());
            s.Variables.SetValue(VarsType.Local, "Tails", "Sonic");

            cond = new BranchCondition(type, false, "대한");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "민국");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "Sonic");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%Tails%");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "대한");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "민국");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "Sonic");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%Tails%");
            Assert.IsFalse(cond.Check(s, out d));

            BranchCondition_Single_Template(s, @"If,ExistMacro,대한,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistMacro,민국,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,ExistMacro,Sonic,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistMacro,%Tails%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,대한,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,민국,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,Sonic,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistMacro,%Tails%,Set,%Dest%,T", "F");
        }
Пример #4
0
        public void Branch_IfWimExistDir()
        {
            EngineState         s = EngineTests.CreateEngineState();
            BranchCondition     cond;
            BranchConditionType type = BranchConditionType.WimExistDir;

            string srcWim = Path.Combine("%TestBench%", "CommandWim", "MultiImage.wim");

            cond = new BranchCondition(type, false, srcWim, "1", "A.txt");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, srcWim, "1", "B");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));

            cond = new BranchCondition(type, true, srcWim, "1", "A.txt");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, true, srcWim, "1", "B");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            BranchCondition_Single_Template(s, $"If,WimExistDir,{srcWim},1,A.txt,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, $"If,WimExistDir,{srcWim},1,B,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, $"If,Not,WimExistDir,{srcWim},1,A.txt,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, $"If,Not,WimExistDir,{srcWim},1,B,Set,%Dest%,T", "F");
        }
Пример #5
0
        public void Branch_IfExistVar()
        {
            EngineState         s    = EngineTests.CreateEngineState();
            BranchConditionType type = BranchConditionType.ExistVar;
            BranchCondition     cond;

            s.Variables.SetValue(VarsType.Fixed, "F", "ixed");
            s.Variables.SetValue(VarsType.Fixed, "G", "lobal");
            s.Variables.SetValue(VarsType.Fixed, "L", "ocal");

            cond = new BranchCondition(type, false, "%F%");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "%G%");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%L%");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "%N%");
            Assert.IsFalse(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "%F%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%G%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%L%");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "%N%");
            Assert.IsTrue(cond.Check(s, out d));

            BranchCondition_Single_Template(s, @"If,ExistVar,%F%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%G%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%L%,Set,%Dest%,T", "T");
            BranchCondition_Single_Template(s, @"If,ExistVar,%N%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%F%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%G%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%L%,Set,%Dest%,T", "F");
            BranchCondition_Single_Template(s, @"If,Not,ExistVar,%N%,Set,%Dest%,T", "T");
        }
Пример #6
0
        public static bool CheckBranchCondition(EngineState s, BranchCondition c, out string logMessage)
        {
            {
                bool match = false;
                switch (c.Type)
                {
                case BranchConditionType.Equal:
                case BranchConditionType.Smaller:
                case BranchConditionType.Bigger:
                case BranchConditionType.SmallerEqual:
                case BranchConditionType.BiggerEqual:
                case BranchConditionType.EqualX:
                {
                    string compArg1 = StringEscaper.Preprocess(s, c.Arg1);
                    string compArg2 = StringEscaper.Preprocess(s, c.Arg2);

                    bool ignoreCase = true;
                    if (c.Type == BranchConditionType.EqualX)
                    {
                        ignoreCase = false;
                    }

                    NumberHelper.CompareStringNumberResult comp = NumberHelper.CompareStringNumber(compArg1, compArg2, ignoreCase);
                    switch (comp)
                    {
                    case NumberHelper.CompareStringNumberResult.Equal:             // For String and Number
                    {
                        if (c.Type == BranchConditionType.Equal && !c.NotFlag ||
                            c.Type == BranchConditionType.SmallerEqual && !c.NotFlag ||
                            c.Type == BranchConditionType.BiggerEqual && !c.NotFlag ||
                            c.Type == BranchConditionType.Smaller && c.NotFlag ||
                            c.Type == BranchConditionType.Bigger && c.NotFlag ||
                            c.Type == BranchConditionType.EqualX && !c.NotFlag)
                        {
                            match = true;
                        }
                        logMessage = $"[{compArg1}] is equal to [{compArg2}]";
                    }
                    break;

                    case NumberHelper.CompareStringNumberResult.Smaller:             // For Number
                    {
                        if (c.Type == BranchConditionType.Smaller && !c.NotFlag ||
                            c.Type == BranchConditionType.SmallerEqual && !c.NotFlag ||
                            c.Type == BranchConditionType.Bigger && c.NotFlag ||
                            c.Type == BranchConditionType.BiggerEqual && c.NotFlag ||
                            c.Type == BranchConditionType.Equal && c.NotFlag ||
                            c.Type == BranchConditionType.EqualX && c.NotFlag)
                        {
                            match = true;
                        }
                        logMessage = $"[{compArg1}] is smaller than [{compArg2}]";
                    }
                    break;

                    case NumberHelper.CompareStringNumberResult.Bigger:             // For Number
                    {
                        if (c.Type == BranchConditionType.Bigger && !c.NotFlag ||
                            c.Type == BranchConditionType.BiggerEqual && !c.NotFlag ||
                            c.Type == BranchConditionType.Smaller && c.NotFlag ||
                            c.Type == BranchConditionType.SmallerEqual && c.NotFlag ||
                            c.Type == BranchConditionType.Equal && c.NotFlag ||
                            c.Type == BranchConditionType.EqualX && c.NotFlag)
                        {
                            match = true;
                        }
                        logMessage = $"[{compArg1}] is bigger than [{compArg2}]";
                    }
                    break;

                    case NumberHelper.CompareStringNumberResult.NotEqual:             // For String
                    {
                        if (c.Type == BranchConditionType.Equal && c.NotFlag ||
                            c.Type == BranchConditionType.EqualX && c.NotFlag)
                        {
                            match = true;
                        }
                        logMessage = $"[{compArg1}] is not equal to [{compArg2}]";
                    }
                    break;

                    default:
                        throw new InternalException($"Cannot compare [{compArg1}] and [{compArg2}]");
                    }
                }
                break;

                case BranchConditionType.ExistFile:
                {
                    string filePath = StringEscaper.Preprocess(s, c.Arg1);

                    // Check filePath contains wildcard
                    bool containsWildcard = true;
                    if (Path.GetFileName(filePath).IndexOfAny(new char[] { '*', '?' }) == -1)         // No wildcard
                    {
                        containsWildcard = false;
                    }

                    // Check if file exists
                    if (filePath.Trim().Equals(string.Empty, StringComparison.Ordinal))
                    {
                        match = false;
                    }
                    else if (containsWildcard)
                    {
                        if (Directory.Exists(FileHelper.GetDirNameEx(filePath)) == false)
                        {
                            match = false;
                        }
                        else
                        {
                            string[] list = Directory.GetFiles(FileHelper.GetDirNameEx(filePath), Path.GetFileName(filePath));
                            if (0 < list.Length)
                            {
                                match = true;
                            }
                            else
                            {
                                match = false;
                            }
                        }
                    }
                    else
                    {
                        match = File.Exists(filePath);
                    }

                    if (match)
                    {
                        logMessage = $"File [{filePath}] exists";
                    }
                    else
                    {
                        logMessage = $"File [{filePath}] does not exist";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistDir:
                {
                    string dirPath = StringEscaper.Preprocess(s, c.Arg1);

                    // Check filePath contains wildcard
                    bool containsWildcard = true;
                    if (Path.GetFileName(dirPath).IndexOfAny(new char[] { '*', '?' }) == -1)         // No wildcard
                    {
                        containsWildcard = false;
                    }

                    // Check if directory exists
                    if (dirPath.Trim().Equals(string.Empty, StringComparison.Ordinal))
                    {
                        match = false;
                    }
                    else if (containsWildcard)
                    {
                        if (Directory.Exists(FileHelper.GetDirNameEx(dirPath)) == false)
                        {
                            match = false;
                        }
                        else
                        {
                            string[] list = Directory.GetDirectories(FileHelper.GetDirNameEx(dirPath), Path.GetFileName(dirPath));
                            if (0 < list.Length)
                            {
                                match = true;
                            }
                            else
                            {
                                match = false;
                            }
                        }
                    }
                    else
                    {
                        match = Directory.Exists(dirPath);
                    }

                    if (match)
                    {
                        logMessage = $"Directory [{dirPath}] exists";
                    }
                    else
                    {
                        logMessage = $"Directory [{dirPath}] does not exist";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistSection:
                {
                    string iniFile = StringEscaper.Preprocess(s, c.Arg1);
                    string section = StringEscaper.Preprocess(s, c.Arg2);

                    match = Ini.CheckSectionExist(iniFile, section);
                    if (match)
                    {
                        logMessage = $"Section [{section}] exists in INI file [{iniFile}]";
                    }
                    else
                    {
                        logMessage = $"Section [{section}] does not exist in INI file [{iniFile}]";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistRegSection:
                case BranchConditionType.ExistRegSubKey:
                {
                    string rootKey = StringEscaper.Preprocess(s, c.Arg1);
                    string subKey  = StringEscaper.Preprocess(s, c.Arg2);

                    RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey);
                    if (regRoot == null)
                    {
                        throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]");
                    }
                    using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey))
                    {
                        match = (regSubKey != null);
                        if (match)
                        {
                            logMessage = $"Registry SubKey [{rootKey}\\{subKey}] exists";
                        }
                        else
                        {
                            logMessage = $"Registry SubKey [{rootKey}\\{subKey}] does not exist";
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistRegKey:
                case BranchConditionType.ExistRegValue:
                {
                    string rootKey   = StringEscaper.Preprocess(s, c.Arg1);
                    string subKey    = StringEscaper.Preprocess(s, c.Arg2);
                    string valueName = StringEscaper.Preprocess(s, c.Arg3);

                    match = true;
                    RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey);
                    if (regRoot == null)
                    {
                        throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]");
                    }
                    using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey))
                    {
                        if (regSubKey == null)
                        {
                            match = false;
                        }
                        else
                        {
                            object value = regSubKey.GetValue(valueName);
                            if (value == null)
                            {
                                match = false;
                            }
                        }

                        if (match)
                        {
                            logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] exists";
                        }
                        else
                        {
                            logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not exist";
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistRegMulti:
                {
                    string rootKey   = StringEscaper.Preprocess(s, c.Arg1);
                    string subKey    = StringEscaper.Preprocess(s, c.Arg2);
                    string valueName = StringEscaper.Preprocess(s, c.Arg3);
                    string subStr    = StringEscaper.Preprocess(s, c.Arg4);

                    match = false;
                    RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey);
                    if (regRoot == null)
                    {
                        throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]");
                    }
                    using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey))
                    {
                        if (regSubKey == null)
                        {
                            logMessage = $"Registry SubKey [{rootKey}\\{subKey}] does not exist";
                        }
                        else
                        {
                            object valueData = regSubKey.GetValue(valueName, null);
                            if (valueData == null)
                            {
                                logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not exist";
                            }
                            else
                            {
                                RegistryValueKind kind = regSubKey.GetValueKind(valueName);
                                if (kind != RegistryValueKind.MultiString)
                                {
                                    logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] is not REG_MULTI_SZ";
                                }
                                else
                                {
                                    string[] strs = (string[])valueData;
                                    if (strs.Contains(subStr, StringComparer.OrdinalIgnoreCase))
                                    {
                                        match      = true;
                                        logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] contains substring [{subStr}]";
                                    }
                                    else
                                    {
                                        logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not contain substring [{subStr}]";
                                    }
                                }
                            }
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistVar:
                {
                    Variables.VarKeyType type = Variables.DetermineType(c.Arg1);
                    if (type == Variables.VarKeyType.Variable)
                    {
                        match = s.Variables.ContainsKey(Variables.TrimPercentMark(c.Arg1));
                        if (match)
                        {
                            logMessage = $"Variable [{c.Arg1}] exists";
                        }
                        else
                        {
                            logMessage = $"Variable [{c.Arg1}] does not exist";
                        }
                    }
                    else
                    {
                        match      = false;
                        logMessage = $"[{c.Arg1}] is not a variable";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.ExistMacro:
                {
                    string macroName = StringEscaper.Preprocess(s, c.Arg1);
                    match = s.Macro.MacroDict.ContainsKey(macroName) || s.Macro.LocalDict.ContainsKey(macroName);

                    if (match)
                    {
                        logMessage = $"Macro [{macroName}] exists";
                    }
                    else
                    {
                        logMessage = $"Macro [{macroName}] does not exist";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.WimExistIndex:
                {
                    string wimFile       = StringEscaper.Preprocess(s, c.Arg1);
                    string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2);

                    if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex))
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else if (imageIndex < 1)
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else
                    {
                        if (File.Exists(wimFile))
                        {
                            try
                            {
                                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT))
                                {
                                    WimInfo wi = wim.GetWimInfo();
                                    if (imageIndex <= wi.ImageCount)
                                    {
                                        match      = true;
                                        logMessage = $"ImageIndex [{imageIndex}] exists in [{wimFile}]";
                                    }
                                    else
                                    {
                                        logMessage = $"ImageIndex [{imageIndex}] does not exist in [{wimFile}]";
                                    }
                                }
                            }
                            catch (WimLibException e)
                            {
                                logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]";
                            }
                        }
                        else
                        {
                            logMessage = $"Wim [{wimFile}] does not exist";
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.WimExistFile:
                {
                    string wimFile       = StringEscaper.Preprocess(s, c.Arg1);
                    string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2);
                    string filePath      = StringEscaper.Preprocess(s, c.Arg3);

                    if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex))
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else if (imageIndex < 1)
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else
                    {
                        if (File.Exists(wimFile))
                        {
                            try
                            {
                                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT))
                                {
                                    bool isFile = false;
                                    CallbackStatus WimExistFileCallback(DirEntry dentry, object userData)
                                    {
                                        if ((dentry.Attributes & FileAttribute.DIRECTORY) == 0)
                                        {
                                            isFile = true;
                                        }

                                        return(CallbackStatus.CONTINUE);
                                    }

                                    try
                                    {
                                        wim.IterateDirTree(imageIndex, filePath, IterateFlags.DEFAULT, WimExistFileCallback, null);

                                        if (isFile)
                                        {
                                            match      = true;
                                            logMessage = $"File [{filePath}] exists in [{wimFile}]";
                                        }
                                        else
                                        {
                                            logMessage = $"File [{filePath}] does not exist in [{wimFile}]";
                                        }
                                    }
                                    catch (WimLibException e)
                                    {
                                        switch (e.ErrorCode)
                                        {
                                        case ErrorCode.INVALID_IMAGE:
                                            logMessage = $"File [{filePath}] does not have image index [{imageIndex}]";
                                            break;

                                        case ErrorCode.PATH_DOES_NOT_EXIST:
                                            logMessage = $"File [{filePath}] does not exist in [{wimFile}]";
                                            break;

                                        default:
                                            logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]";
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (WimLibException e)
                            {
                                logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]";
                            }
                        }
                        else
                        {
                            logMessage = $"Wim [{wimFile}] does not exist";
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.WimExistDir:
                {
                    string wimFile       = StringEscaper.Preprocess(s, c.Arg1);
                    string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2);
                    string dirPath       = StringEscaper.Preprocess(s, c.Arg3);

                    if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex))
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else if (imageIndex < 1)
                    {
                        logMessage = $"Index [{imageIndexStr}] is not a positive integer";
                    }
                    else
                    {
                        if (File.Exists(wimFile))
                        {
                            try
                            {
                                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT))
                                {
                                    bool isDir = false;
                                    CallbackStatus WimExistFileCallback(DirEntry dentry, object userData)
                                    {
                                        if ((dentry.Attributes & FileAttribute.DIRECTORY) != 0)
                                        {
                                            isDir = true;
                                        }

                                        return(CallbackStatus.CONTINUE);
                                    }

                                    try
                                    {
                                        wim.IterateDirTree(imageIndex, dirPath, IterateFlags.DEFAULT, WimExistFileCallback, null);

                                        if (isDir)
                                        {
                                            match      = true;
                                            logMessage = $"Dir [{dirPath}] exists in [{wimFile}]";
                                        }
                                        else
                                        {
                                            logMessage = $"Dir [{dirPath}] does not exist in [{wimFile}]";
                                        }
                                    }
                                    catch (WimLibException e)
                                    {
                                        switch (e.ErrorCode)
                                        {
                                        case ErrorCode.INVALID_IMAGE:
                                            logMessage = $"Dir [{dirPath}] does not have image index [{imageIndex}]";
                                            break;

                                        case ErrorCode.PATH_DOES_NOT_EXIST:
                                            logMessage = $"Dir [{dirPath}] does not exist in [{wimFile}]";
                                            break;

                                        default:
                                            logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]";
                                            break;
                                        }
                                    }
                                }
                            }
                            catch (WimLibException e)
                            {
                                logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]";
                            }
                        }
                        else
                        {
                            logMessage = $"Wim [{wimFile}] does not exist";
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.Ping:
                {
                    string host = StringEscaper.Preprocess(s, c.Arg1);

                    Ping pinger = new Ping();
                    try
                    {
                        try
                        {
                            PingReply reply = pinger.Send(host);
                            if (reply.Status == IPStatus.Success)
                            {
                                match = true;
                            }
                            else
                            {
                                match = false;
                            }
                        }
                        catch
                        {
                            match = false;
                        }

                        if (match)
                        {
                            logMessage = $"[{host}] responded to Ping";
                        }
                        else
                        {
                            logMessage = $"[{host}] did not respond to Ping";
                        }
                    }
                    catch (PingException e)
                    {
                        match      = false;
                        logMessage = $"Error while pinging [{host}] : [{e.Message}]";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.Online:
                {
                    // Note that system connected only to local network also returns true
                    match = NetworkInterface.GetIsNetworkAvailable();

                    if (match)
                    {
                        logMessage = "System is online";
                    }
                    else
                    {
                        logMessage = "System is offline";
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }
                }
                break;

                case BranchConditionType.Question:     // can have 1 or 3 argument
                {
                    string question = StringEscaper.Preprocess(s, c.Arg1);

                    bool autoTimeout = false;

                    if (c.Arg2 != null && c.Arg3 != null)
                    {
                        autoTimeout = true;
                    }

                    int  timeout       = 0;
                    bool defaultChoice = false;
                    if (autoTimeout)
                    {
                        string timeoutStr = StringEscaper.Preprocess(s, c.Arg2);
                        if (NumberHelper.ParseInt32(timeoutStr, out timeout) == false)
                        {
                            autoTimeout = false;
                        }
                        if (timeout <= 0)
                        {
                            autoTimeout = false;
                        }

                        string defaultChoiceStr = StringEscaper.Preprocess(s, c.Arg3);
                        if (defaultChoiceStr.Equals("True", StringComparison.OrdinalIgnoreCase))
                        {
                            defaultChoice = true;
                        }
                        else if (defaultChoiceStr.Equals("False", StringComparison.OrdinalIgnoreCase))
                        {
                            defaultChoice = false;
                        }
                    }

                    System.Windows.Shell.TaskbarItemProgressState oldTaskbarItemProgressState = s.MainViewModel.TaskbarProgressState;         // Save our progress state
                    s.MainViewModel.TaskbarProgressState = System.Windows.Shell.TaskbarItemProgressState.Paused;

                    if (autoTimeout)
                    {
                        MessageBoxResult result = MessageBoxResult.None;
                        Application.Current.Dispatcher.Invoke(() =>
                            {
                                result = CustomMessageBox.Show(question, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, timeout);
                            });

                        if (result == MessageBoxResult.None)
                        {
                            match = defaultChoice;
                            if (defaultChoice)
                            {
                                logMessage = "[Yes] was automatically chosen";
                            }
                            else
                            {
                                logMessage = "[No] was automatically chosen";
                            }
                        }
                        else if (result == MessageBoxResult.Yes)
                        {
                            match      = true;
                            logMessage = "[Yes] was chosen";
                        }
                        else if (result == MessageBoxResult.No)
                        {
                            match      = false;
                            logMessage = "[No] was chosen";
                        }
                        else
                        {
                            throw new InternalException("Internal Error at Check() of If,Question");
                        }
                    }
                    else
                    {
                        MessageBoxResult result = MessageBox.Show(question, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            match      = true;
                            logMessage = "[Yes] was chosen";
                        }
                        else if (result == MessageBoxResult.No)
                        {
                            match      = false;
                            logMessage = "[No] was chosen";
                        }
                        else
                        {
                            throw new InternalException("Internal Error at Check() of If,Question");
                        }
                    }

                    if (c.NotFlag)
                    {
                        match = !match;
                    }

                    s.MainViewModel.TaskbarProgressState = oldTaskbarItemProgressState;
                }
                break;

                default:
                    throw new InternalException($"Internal BranchCondition check error");
                }
                return(match);
            }
        }
Пример #7
0
        public void Branch_IfEqual()
        {
            EngineState         s = EngineTests.CreateEngineState();
            BranchCondition     cond;
            BranchConditionType type = BranchConditionType.Equal;

            // Equal
            cond = new BranchCondition(type, false, "A", "A");
            Assert.IsTrue(cond.Check(s, out string d));
            cond = new BranchCondition(type, false, "A", "B");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "a", "A");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, true, "A", "A");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "A", "B");
            Assert.IsTrue(cond.Check(s, out d));

            cond = new BranchCondition(type, false, "11.1", "11.1.0");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, true, "11.1", "11.1.0");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "10.9", "11.1");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "12", "12");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "11.1.2.9", "11.1.2.3");
            Assert.IsFalse(cond.Check(s, out d));

            cond = new BranchCondition(type, false, "5", "5.0");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "5", "5.1.2600");
            Assert.IsFalse(cond.Check(s, out d));

            // WB082 does not recognize hex integer representation
            // PEBakery support hex integer representation
            cond = new BranchCondition(type, false, "11", "0xC");
            Assert.IsFalse(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "12", "0xC");
            Assert.IsTrue(cond.Check(s, out d));
            cond = new BranchCondition(type, false, "13", "0xC");
            Assert.IsFalse(cond.Check(s, out d));

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,Not,%Src%,Equal,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,NotEqual,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,NotEqual,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,11,Set,%Dest%,T", "T");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,11,Set,%Dest%,T", "T");
        }
Пример #8
0
        public void Branch_IfEqual()
        {
            EngineState         s = EngineTests.CreateEngineState();
            BranchCondition     cond;
            BranchConditionType type = BranchConditionType.Equal;

            // Equal
            cond = new BranchCondition(type, false, "A", "A");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "A", "B");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "a", "A");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));

            cond = new BranchCondition(type, true, "A", "A");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, true, "A", "B");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));

            cond = new BranchCondition(type, false, "11.1", "11.1.0");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, true, "11.1", "11.1.0");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "10.9", "11.1");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "12", "12");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "11.1.2.9", "11.1.2.3");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            cond = new BranchCondition(type, false, "5", "5.0");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "5", "5.1.2600");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            // WB082 does not recognize hex integer representation
            // PEBakery support hex integer representation
            cond = new BranchCondition(type, false, "11", "0xC");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "12", "0xC");
            Assert.IsTrue(CommandBranch.CheckBranchCondition(s, cond, out _));
            cond = new BranchCondition(type, false, "13", "0xC");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            // Test for a bug reported in http://theoven.org/index.php?topic=2271.msg25381#msg25381
            cond = new BranchCondition(type, false, "-1", "0");
            Assert.IsFalse(CommandBranch.CheckBranchCondition(s, cond, out _));

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,Equal,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,Equal,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,A,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,==,a,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,09,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,10,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,==,11,Set,%Dest%,T", "F");

            BranchCondition_Comparison_Template(s, "A", "If,Not,%Src%,Equal,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,NotEqual,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,NotEqual,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,Not,%Src%,Equal,11,Set,%Dest%,T", "T");

            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,A,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "A", "If,%Src%,!=,a,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,09,Set,%Dest%,T", "T");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,10,Set,%Dest%,T", "F");
            BranchCondition_Comparison_Template(s, "10", "If,%Src%,!=,11,Set,%Dest%,T", "T");
        }
Пример #9
0
 public InterBranch(BranchCondition condition = BranchCondition.Always)
 {
     Condition = condition;
 }
Пример #10
0
 public InterBranch(CodeValue exp, BranchCondition condition = BranchCondition.Always)
 {
     _op       = exp;
     Condition = condition;
 }
Пример #11
0
 public GenCndJmpRO(BranchCondition cond)
 {
     m_cond = cond;
 }
Пример #12
0
 public GenCndJmpRO(BranchCondition cond)
 {
     m_cond = cond;
 }