Added the ability to not create an empty variable when the Parse Block fails.

This commit is contained in:
ruri
2019-05-09 01:14:12 +02:00
parent a2cc07a100
commit 74d5aa3436
7 changed files with 72 additions and 22 deletions
+42 -6
View File
@@ -288,15 +288,51 @@ namespace RuriLib
/// <param name="prefix">The string to add at the start of the value</param>
/// <param name="suffix">The string to add at the end of the value</param>
/// <param name="urlEncode">Whether to URLencode the values before creating the variables</param>
public static void InsertVariables(BotData data, bool isCapture, bool recursive, List<string> values, string variableName, string prefix, string suffix, bool urlEncode)
/// <param name="createEmpty">Whether to create an empty (single) variable if the list of values is empty</param>
public static void InsertVariables(BotData data, bool isCapture, bool recursive, List<string> 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