Exemplo n.º 1
0
        public static string GetUIControlTemplate(UIControlType type, string key)
        {
            switch (type)
            {
            case UIControlType.TextBox:
                return(UIInfo_TextBox.Template(key));

            case UIControlType.TextLabel:
                return(UIInfo_TextLabel.Template(key));

            case UIControlType.NumberBox:
                return(UIInfo_NumberBox.Template(key));

            case UIControlType.CheckBox:
                return(UIInfo_CheckBox.Template(key));

            case UIControlType.ComboBox:
                return(UIInfo_ComboBox.Template(key));

            case UIControlType.Image:
                return(UIInfo_Image.Template(key));

            case UIControlType.TextFile:
                return(UIInfo_TextFile.Template(key));

            case UIControlType.Button:
                return(UIInfo_Button.Template(key));

            case UIControlType.WebLabel:
                return(UIInfo_WebLabel.Template(key));

            case UIControlType.RadioButton:
                return(UIInfo_RadioButton.Template(key));

            case UIControlType.Bevel:
                return(UIInfo_Bevel.Template(key));

            case UIControlType.FileBox:
                return(UIInfo_FileBox.Template(key));

            case UIControlType.RadioGroup:
                return(UIInfo_RadioGroup.Template(key));

            default:
                throw new InvalidOperationException("Internal Logic Error at UIControl.GetUIControlTemplate");
            }
        }
Exemplo n.º 2
0
        public bool SetValue(string newValue, bool update, out List <LogInfo> logs)
        {
            logs = new List <LogInfo>(1);
            bool success = false;

            switch (Type)
            {
            case UIControlType.TextLabel:
                // Text
                Text = StringEscaper.Escape(newValue);

                logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [{newValue}]"));
                success = true;
                break;

            case UIControlType.TextBox:
            {         // Value
                UIInfo_TextBox uiInfo = Info.Cast <UIInfo_TextBox>();
                uiInfo.Value = StringEscaper.Escape(newValue);

                logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [{newValue}]"));
                success = true;
            }
            break;

            case UIControlType.NumberBox:
            {         // Value
                UIInfo_NumberBox uiInfo = Info.Cast <UIInfo_NumberBox>();

                // WB082 just write string value in case of error, but PEBakery will throw error
                if (!NumberHelper.ParseInt32(newValue, out int intVal))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] is not a valid integer"));
                    return(false);
                }

                if (uiInfo.Min <= intVal && intVal <= uiInfo.Max)
                {
                    uiInfo.Value = intVal;
                }
                else
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] should be inside of [{uiInfo.Min}] ~ [{uiInfo.Max}]"));
                    return(false);
                }

                logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [{newValue}]"));
                success = true;
            }
            break;

            case UIControlType.CheckBox:
            {         // Value
                UIInfo_CheckBox uiInfo = Info.Cast <UIInfo_CheckBox>();

                if (newValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    uiInfo.Value = true;

                    logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [True]"));
                    success = true;
                }
                else if (newValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                {
                    uiInfo.Value = false;

                    logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [False]"));
                    success = true;
                }
                else
                {         // WB082 just write string value in case of error, but PEBakery will throw error
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] is not a valid boolean value"));
                    return(false);
                }
            }
            break;

            case UIControlType.ComboBox:
            {         // Text
                UIInfo_ComboBox uiInfo = Info.Cast <UIInfo_ComboBox>();

                int idx = uiInfo.Items.FindIndex(x => newValue.Equals(StringEscaper.Unescape(x), StringComparison.OrdinalIgnoreCase));
                if (idx == -1)
                {         // Invalid index
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] was not found in the item list"));
                    return(false);
                }

                uiInfo.Index = idx;
                Text         = uiInfo.Items[idx];

                logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [{Text}]"));
                success = true;
            }
            break;

            case UIControlType.RadioButton:
            {
                UIInfo_RadioButton uiInfo = Info.Cast <UIInfo_RadioButton>();

                if (newValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    uiInfo.Selected = true;

                    logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [True]"));
                    success = true;
                }
                else if (newValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                {
                    uiInfo.Selected = false;

                    logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [False]"));
                    success = true;
                }
                else
                {         // WB082 just write string value, but PEBakery will throw error
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] is not a valid boolean value"));
                    return(false);
                }
            }
            break;

            case UIControlType.FileBox:
                Text = StringEscaper.Escape(newValue);

                logs.Add(new LogInfo(LogState.Success, $"Interface Control [{Key}] set to [{newValue}]"));
                success = true;
                break;

            case UIControlType.RadioGroup:
            {
                UIInfo_RadioGroup uiInfo = Info.Cast <UIInfo_RadioGroup>();

                if (!NumberHelper.ParseInt32(newValue, out int idx))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{newValue}] is not a valid integer"));
                    return(false);
                }

                if (0 <= idx && idx < uiInfo.Items.Count)
                {
                    uiInfo.Selected = idx;
                }
                else
                {         // Invalid Index
                    logs.Add(new LogInfo(LogState.Error, $"Index [{newValue}] is invalid"));
                    return(false);
                }

                logs.Add(new LogInfo(LogState.Success, $"Interface control [{Key}] set to [{newValue}]"));
                success = true;
            }
            break;
            }

            if (success && update)
            {
                Update();
            }

            return(success);
        }
Exemplo n.º 3
0
        private List <LogInfo> CheckInterfaceSection(ScriptSection section, string rawLine = null, int lineIdx = 0)
        {
            // If this section was already visited, return.
            if (_visitedSections.Contains(section.Name))
            {
                return(new List <LogInfo>());
            }
            _visitedSections.Add(section.Name);

            // Force parsing of code, bypassing caching by section.GetUICtrls()
            string[] lines = section.Lines;
            if (lines == null)
            {
                string msg = $"Section [{section.Name}] is not a valid interface section";
                if (rawLine != null)
                {
                    msg += $" ({rawLine})";
                }
                if (0 < lineIdx)
                {
                    msg += $" (Line {lineIdx})";
                }

                return(new List <LogInfo> {
                    new LogInfo(LogState.Error, msg)
                });
            }

            (List <UIControl> uiCtrls, List <LogInfo> logs) = UIParser.ParseStatements(lines, section);
            foreach (UIControl uiCtrl in uiCtrls)
            {
                switch (uiCtrl.Type)
                {
                case UIControlType.CheckBox:
                {
                    UIInfo_CheckBox info = uiCtrl.Info.Cast <UIInfo_CheckBox>();

                    if (info.SectionName != null)
                    {
                        if (_sc.Sections.ContainsKey(info.SectionName))         // Only if section exists
                        {
                            logs.AddRange(CheckCodeSection(_sc.Sections[info.SectionName], uiCtrl.RawLine, uiCtrl.LineIdx));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exist", uiCtrl));
                        }
                    }
                }
                break;

                case UIControlType.ComboBox:
                {
                    UIInfo_ComboBox info = uiCtrl.Info.Cast <UIInfo_ComboBox>();

                    // Practically, this means info.Index is -1 -> uiCtrl.Text not being one of info.Items
                    if (info.Index < 0 || info.Items.Count <= info.Index)
                    {
                        logs.Add(new LogInfo(LogState.Warning, $"Incorrect selected value [{uiCtrl.Text}]", uiCtrl));
                    }
                }
                break;

                case UIControlType.Image:
                {
                    // Check encoded image
                    string imageSection = StringEscaper.Unescape(uiCtrl.Text);
                    if (!imageSection.Equals(UIInfo_Image.NoResource, StringComparison.OrdinalIgnoreCase) &&
                        !EncodedFile.ContainsInterface(_sc, imageSection))
                    {
                        logs.Add(new LogInfo(LogState.Warning, $"Image resource [{imageSection}] does not exist", uiCtrl));
                    }

                    UIInfo_Image info = uiCtrl.Info.Cast <UIInfo_Image>();

                    // Check if image control have empty or invalid url.
                    // Ex) Colors_Image=ThemeColors.jpg,1,5,11,228,260,80,
                    if (info.Url != null)
                    {
                        string url = StringEscaper.Unescape(info.Url);
                        if (!StringEscaper.IsUrlValid(url))
                        {
                            if (url.IndexOf("://", StringComparison.Ordinal) != -1)
                            {
                                logs.Add(new LogInfo(LogState.Warning, $"Incorrect URL [{url}]", uiCtrl));
                            }
                            else
                            {
                                logs.Add(new LogInfo(LogState.Warning, "URL does not have a scheme. Did you omit \"http(s)://\"?", uiCtrl));
                            }
                        }
                    }
                }
                break;

                case UIControlType.TextFile:
                {
                    string textSection = StringEscaper.Unescape(uiCtrl.Text);
                    if (!textSection.Equals(UIInfo_TextFile.NoResource, StringComparison.OrdinalIgnoreCase) &&
                        !EncodedFile.ContainsInterface(_sc, textSection))
                    {
                        logs.Add(new LogInfo(LogState.Warning, $"Text resource [{textSection}] does not exist", uiCtrl));
                    }
                }
                break;

                case UIControlType.Button:
                {
                    UIInfo_Button info = uiCtrl.Info.Cast <UIInfo_Button>();

                    string pictureSection = info.Picture;
                    if (pictureSection != null &&
                        !pictureSection.Equals(UIInfo_Button.NoPicture, StringComparison.OrdinalIgnoreCase) &&
                        !EncodedFile.ContainsInterface(_sc, pictureSection))
                    {
                        if (pictureSection.Length == 0)         // Due to quirks of WinBuilder's editor, many buttons have '' instead of '0' in the place of <Picture>.
                        {
                            logs.Add(new LogInfo(LogState.Warning, "Image resource entry is empty. Use [0] to represent not having an image resource.", uiCtrl));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Warning, $"Image resource [{pictureSection}] does not exist", uiCtrl));
                        }
                    }

                    if (info.SectionName != null)
                    {
                        if (_sc.Sections.ContainsKey(info.SectionName))         // Only if section exists
                        {
                            logs.AddRange(CheckCodeSection(_sc.Sections[info.SectionName], uiCtrl.RawLine, uiCtrl.LineIdx));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exist", uiCtrl));
                        }
                    }
                }
                break;

                case UIControlType.WebLabel:
                {
                    UIInfo_WebLabel info = uiCtrl.Info.Cast <UIInfo_WebLabel>();

                    // Sometime developers forget to put proper scheme in WebLabel's url.
                    // Ex) PStart_WebLabel="PStart Homepage",1,10,668,122,98,18,www.pegtop.de/start/
                    string url = StringEscaper.Unescape(info.Url);
                    if (!StringEscaper.IsUrlValid(url))
                    {
                        if (url.IndexOf("://", StringComparison.Ordinal) != -1)
                        {
                            logs.Add(new LogInfo(LogState.Warning, $"Incorrect URL [{url}]", uiCtrl));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Warning, "URL does not have scheme. Did you omit \"http(s)://\"?", uiCtrl));
                        }
                    }
                }
                break;

                case UIControlType.RadioButton:
                {
                    UIInfo_RadioButton info = uiCtrl.Info.Cast <UIInfo_RadioButton>();

                    if (info.SectionName != null)
                    {
                        if (_sc.Sections.ContainsKey(info.SectionName))         // Only if section exists
                        {
                            logs.AddRange(CheckCodeSection(_sc.Sections[info.SectionName], uiCtrl.RawLine, uiCtrl.LineIdx));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exist", uiCtrl));
                        }
                    }
                }
                break;

                case UIControlType.FileBox:
                {
                    UIInfo_FileBox info = uiCtrl.Info.Cast <UIInfo_FileBox>();

                    if (info.IsFile)
                    {         // Select File
                        if (info.Filter != null)
                        {
                            string filter = StringEscaper.Unescape(info.Filter);
                            if (StringEscaper.IsFileFilterValid(filter) == false)
                            {
                                logs.Add(new LogInfo(LogState.Warning, $"File filter pattern [{filter}] is invalid", uiCtrl));
                            }
                        }
                    }
                    else
                    {         // Select Folder
                        if (info.Filter != null)
                        {
                            logs.Add(new LogInfo(LogState.Warning, $"File filters cannot be used for folder selection", uiCtrl));
                        }
                    }
                }
                break;

                case UIControlType.RadioGroup:
                {
                    UIInfo_RadioGroup info = uiCtrl.Info.Cast <UIInfo_RadioGroup>();

                    if (info.SectionName != null)
                    {
                        if (_sc.Sections.ContainsKey(info.SectionName))         // Only if section exists
                        {
                            logs.AddRange(CheckCodeSection(_sc.Sections[info.SectionName], uiCtrl.RawLine, uiCtrl.LineIdx));
                        }
                        else
                        {
                            logs.Add(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exist", uiCtrl));
                        }
                    }

                    // Practically, this means info.Index is -1 -> uiCtrl.Text not being one of info.Items
                    if (info.Selected < 0 || info.Items.Count <= info.Selected)
                    {
                        logs.Add(new LogInfo(LogState.Warning, $"Incorrect selected index [{info.Selected}]", uiCtrl));
                    }
                }
                break;
                }
            }
            return(logs);
        }