From 74d5aa34363c08fbc12523cd5ebfd1dffe49b1b4 Mon Sep 17 00:00:00 2001 From: ruri Date: Thu, 9 May 2019 01:14:12 +0200 Subject: [PATCH] Added the ability to not create an empty variable when the Parse Block fails. --- OpenBullet/LSDoc.xml | 8 ++-- .../Pages/StackerBlocks/PageBlockParse.xaml | 6 ++- OpenBullet/SyntaxHelper.xml | 8 ++-- RuriLib/Blocks/BlockBase.cs | 48 ++++++++++++++++--- RuriLib/Blocks/BlockFunction.cs | 2 +- RuriLib/Blocks/BlockParse.cs | 20 ++++++-- RuriLib/Blocks/SBlockElementAction.cs | 2 +- 7 files changed, 72 insertions(+), 22 deletions(-) diff --git a/OpenBullet/LSDoc.xml b/OpenBullet/LSDoc.xml index d9326f7..6130edf 100644 --- a/OpenBullet/LSDoc.xml +++ b/OpenBullet/LSDoc.xml @@ -321,10 +321,10 @@ CAPTCHA "http://example.com/image.png" -> VAR "CAP" This will spawn and execute a Parse Block. Syntax: -PARSE "TARGET" LR "LEFT" "RIGHT" [Recursive?] [UseRegexLR?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" CSS "SELECTOR" "ATTRIBUTE" [INDEX / Recursive?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" JSON "FIELD" [Recursive?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" LR "LEFT" "RIGHT" [Recursive?] [EncodeOutput?] [CreateEmpty?] [UseRegexLR?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" CSS "SELECTOR" "ATTRIBUTE" [INDEX / Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" JSON "FIELD" [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] Examples: PARSE "<SOURCE>" LR "<span>" "</span>" Recursive=True -> CAP "BALANCE" "$" "" diff --git a/OpenBullet/Pages/StackerBlocks/PageBlockParse.xaml b/OpenBullet/Pages/StackerBlocks/PageBlockParse.xaml index 9746bee..bf2643d 100644 --- a/OpenBullet/Pages/StackerBlocks/PageBlockParse.xaml +++ b/OpenBullet/Pages/StackerBlocks/PageBlockParse.xaml @@ -76,7 +76,8 @@ - + + Whether the program matches the LEFT and RIGHT strings through a regex pattern or with simple string in string searches @@ -114,6 +115,7 @@ + @@ -126,6 +128,7 @@ + @@ -142,6 +145,7 @@ + diff --git a/OpenBullet/SyntaxHelper.xml b/OpenBullet/SyntaxHelper.xml index 03e6ed2..47937c1 100644 --- a/OpenBullet/SyntaxHelper.xml +++ b/OpenBullet/SyntaxHelper.xml @@ -28,10 +28,10 @@ RECAPTCHA "URL" "SITEKEY" -> VAR "NAME" CAPTCHA "URL" [Base64?] [SendScreenshot?] -> VAR "NAME" -PARSE "TARGET" LR "LEFT" "RIGHT" [Recursive?] [EncodeOutput?] [UseRegexLR?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" CSS "SELECTOR" "ATTRIBUTE" [INDEX / Recursive?] [EncodeOutput?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" JSON "FIELD" [Recursive?] [EncodeOutput?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] -PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] [EncodeOutput?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" LR "LEFT" "RIGHT" [Recursive?] [EncodeOutput?] [CreateEmpty?] [UseRegexLR?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" CSS "SELECTOR" "ATTRIBUTE" [INDEX / Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" JSON "FIELD" [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] +PARSE "TARGET" REGEX "REGEX" "OUTPUT" [Recursive?] [EncodeOutput?] [CreateEmpty?] -> VAR/CAP "NAME" ["PREFIX" "SUFFIX"] UTILITY LIST "List Name" ACTION [PARAMETERS] [-> VAR/CAP "NAME"] diff --git a/RuriLib/Blocks/BlockBase.cs b/RuriLib/Blocks/BlockBase.cs index 22c75fd..80d76c0 100644 --- a/RuriLib/Blocks/BlockBase.cs +++ b/RuriLib/Blocks/BlockBase.cs @@ -288,15 +288,51 @@ namespace RuriLib /// The string to add at the start of the value /// The string to add at the end of the value /// Whether to URLencode the values before creating the variables - public static void InsertVariables(BotData data, bool isCapture, bool recursive, List values, string variableName, string prefix, string suffix, bool urlEncode) + /// Whether to create an empty (single) variable if the list of values is empty + public static void InsertVariables(BotData data, bool isCapture, bool recursive, List values, string variableName, string prefix, string suffix, bool urlEncode, bool createEmpty) { var list = values.Select(v => ReplaceValues(prefix, data) + v.Trim() + ReplaceValues(suffix, data)).ToList(); if (urlEncode) list = list.Select(v => System.Uri.EscapeDataString(v)).ToList(); - CVar variable; - if (recursive) variable = new CVar(variableName, list, isCapture); - else variable = new CVar(variableName, list.Count == 0 ? "" : list.First(), isCapture); - data.Variables.Set(variable); - data.Log(new LogEntry("Parsed variable" + " | Name: " + variable.Name + " | Value: " + variable.ToString() + Environment.NewLine, isCapture ? Colors.OrangeRed : Colors.Gold)); + + CVar variable = null; + if (recursive) + { + if (list.Count == 0) + { + if (createEmpty) + { + variable = new CVar(variableName, list, isCapture); + } + } + else + { + variable = new CVar(variableName, list, isCapture); + } + } + else + { + if (list.Count == 0) + { + if (createEmpty) + { + variable = new CVar(variableName, "", isCapture); + } + } + else + { + variable = new CVar(variableName, list.First(), isCapture); + } + } + + if (variable != null) + { + data.Variables.Set(variable); + data.Log(new LogEntry("Parsed variable" + " | Name: " + variable.Name + " | Value: " + variable.ToString() + Environment.NewLine, isCapture ? Colors.OrangeRed : Colors.Gold)); + } + else + { + data.Log(new LogEntry("Could not parse any data. The variable was not created.", Colors.White)); + } } #region File Utilities diff --git a/RuriLib/Blocks/BlockFunction.cs b/RuriLib/Blocks/BlockFunction.cs index 64ab1df..58e4b7d 100644 --- a/RuriLib/Blocks/BlockFunction.cs +++ b/RuriLib/Blocks/BlockFunction.cs @@ -690,7 +690,7 @@ namespace RuriLib } var isList = outputs.Count > 1 || InputString.Contains("[*]") || InputString.Contains("(*)") || InputString.Contains("{*}"); - InsertVariables(data, isCapture, isList, outputs, variableName, "", "", false); + InsertVariables(data, isCapture, isList, outputs, variableName, "", "", false, true); } #region Base64 diff --git a/RuriLib/Blocks/BlockParse.cs b/RuriLib/Blocks/BlockParse.cs index d14dd4f..84ec5db 100644 --- a/RuriLib/Blocks/BlockParse.cs +++ b/RuriLib/Blocks/BlockParse.cs @@ -63,6 +63,10 @@ namespace RuriLib /// Whether to URL encode the parsed text. public bool EncodeOutput { get { return encodeOutput; } set { encodeOutput = value; OnPropertyChanged(); } } + private bool createEmpty = true; + /// Whether to create the variable with an empty value if the parsing was not successful. + public bool CreateEmpty { get { return createEmpty; } set { createEmpty = value; OnPropertyChanged(); } } + private ParseType type = ParseType.LR; /// The parsing algorithm being used. public ParseType Type { get { return type; } set { type = value; OnPropertyChanged(); } } @@ -151,7 +155,7 @@ namespace RuriLib LineParser.SetBool(ref input, this); else if (LineParser.Lookahead(ref input) == TokenType.Integer) CssElementIndex = LineParser.ParseInt(ref input, "INDEX"); - if (LineParser.Lookahead(ref input) == TokenType.Boolean) + while (LineParser.Lookahead(ref input) == TokenType.Boolean) LineParser.SetBool(ref input, this); break; @@ -216,6 +220,7 @@ namespace RuriLib .Literal(RightString) .Boolean(Recursive, "Recursive") .Boolean(EncodeOutput, "EncodeOutput") + .Boolean(CreateEmpty, "CreateEmpty") .Boolean(UseRegexLR, "UseRegexLR"); break; @@ -225,14 +230,18 @@ namespace RuriLib .Literal(AttributeName); if (Recursive) writer.Boolean(Recursive, "Recursive"); else writer.Integer(CssElementIndex, "CssElementIndex"); - writer.Boolean(EncodeOutput, "EncodeOutput"); + + writer + .Boolean(EncodeOutput, "EncodeOutput") + .Boolean(CreateEmpty, "CreateEmpty"); break; case ParseType.JSON: writer .Literal(JsonField) .Boolean(Recursive, "Recursive") - .Boolean(EncodeOutput, "EncodeOutput"); + .Boolean(EncodeOutput, "EncodeOutput") + .Boolean(CreateEmpty, "CreateEmpty"); break; case ParseType.REGEX: @@ -240,7 +249,8 @@ namespace RuriLib .Literal(RegexString) .Literal(RegexOutput) .Boolean(Recursive, "Recursive") - .Boolean(EncodeOutput, "EncodeOutput"); + .Boolean(EncodeOutput, "EncodeOutput") + .Boolean(CreateEmpty, "CreateEmpty"); break; } @@ -260,7 +270,7 @@ namespace RuriLib { base.Process(data); - InsertVariables(data, isCapture, recursive, Parse(data), variableName, prefix, suffix, encodeOutput); + InsertVariables(data, isCapture, recursive, Parse(data), variableName, prefix, suffix, encodeOutput, createEmpty); } private List Parse(BotData data) diff --git a/RuriLib/Blocks/SBlockElementAction.cs b/RuriLib/Blocks/SBlockElementAction.cs index 05b6da8..46f7d8b 100644 --- a/RuriLib/Blocks/SBlockElementAction.cs +++ b/RuriLib/Blocks/SBlockElementAction.cs @@ -341,7 +341,7 @@ namespace RuriLib if (outputs.Count != 0) { - InsertVariables(data, isCapture, recursive, outputs, outputVariable, "", "", false); + InsertVariables(data, isCapture, recursive, outputs, outputVariable, "", "", false, true); } }