From a05515b2d73efadb8f6db6b70eb006d7f0b370e0 Mon Sep 17 00:00:00 2001 From: Ruri Date: Sun, 15 Dec 2024 18:22:00 +0100 Subject: [PATCH] Refactored --- OpenBullet2.Web/Program.cs | 8 +- OpenBullet2.Web/Utils/BlockMapper.cs | 42 +- .../Helpers/Blocks/DescriptorsRepository.cs | 143 +++---- RuriLib/Helpers/LoliCode/LoliCodeParser.cs | 397 +++++++++--------- .../Blocks/Custom/HttpRequestBlockInstance.cs | 32 +- .../Blocks/Parameters/BlockParameter.cs | 56 ++- .../Models/Blocks/Parameters/BoolParameter.cs | 65 +-- .../Blocks/Parameters/ByteArrayParameter.cs | 67 +-- .../DictionaryOfStringsParameter.cs | 55 ++- .../Models/Blocks/Parameters/EnumParameter.cs | 62 +-- .../Blocks/Parameters/FloatParameter.cs | 64 +-- .../Models/Blocks/Parameters/IntParameter.cs | 64 +-- .../Parameters/ListOfStringsParameter.cs | 55 ++- .../Blocks/Parameters/StringParameter.cs | 47 ++- .../Models/Blocks/Settings/BlockSetting.cs | 61 ++- .../Blocks/Settings/BlockSettingFactory.cs | 12 +- RuriLib/Models/Blocks/Settings/BoolSetting.cs | 15 +- .../Blocks/Settings/ByteArraySetting.cs | 15 +- .../Settings/DictionaryOfStringsSetting.cs | 15 +- RuriLib/Models/Blocks/Settings/EnumSetting.cs | 80 ++-- .../Models/Blocks/Settings/FloatSetting.cs | 15 +- RuriLib/Models/Blocks/Settings/IntSetting.cs | 15 +- .../InterpolatedDictionaryOfStringsSetting.cs | 15 +- .../InterpolatedListOfStringsSetting.cs | 15 +- .../Interpolated/InterpolatedStringSetting.cs | 21 +- .../Blocks/Settings/ListOfStringsSetting.cs | 15 +- .../Models/Blocks/Settings/StringSetting.cs | 21 +- 27 files changed, 839 insertions(+), 633 deletions(-) diff --git a/OpenBullet2.Web/Program.cs b/OpenBullet2.Web/Program.cs index c20fadb0f..e15a4936c 100644 --- a/OpenBullet2.Web/Program.cs +++ b/OpenBullet2.Web/Program.cs @@ -130,7 +130,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(service => - new HybridWordlistRepository(service.GetService(), + new HybridWordlistRepository(service.GetRequiredService(), $"{Globals.UserDataFolder}/Wordlists")); builder.Services.AddScoped(); @@ -143,7 +143,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(service => - new DiskConfigRepository(service.GetService(), + new DiskConfigRepository(service.GetRequiredService(), $"{Globals.UserDataFolder}/Configs")); builder.Services.AddSingleton(); builder.Services.AddSingleton(service => @@ -155,7 +155,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(service => - new JobMonitorService(service.GetService(), + new JobMonitorService(service.GetRequiredService(), $"{Globals.UserDataFolder}/triggeredActions.json", false)); builder.Services.AddSingleton(); builder.Services.AddSingleton(_ => new RuriLibSettingsService(Globals.UserDataFolder)); @@ -166,7 +166,7 @@ _ => new IntoliRandomUAProvider("user-agents.json")); builder.Services.AddSingleton(); builder.Services.AddSingleton(service => - new FileJobLogger(service.GetService(), + new FileJobLogger(service.GetRequiredService(), $"{Globals.UserDataFolder}/Logs/Jobs")); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/OpenBullet2.Web/Utils/BlockMapper.cs b/OpenBullet2.Web/Utils/BlockMapper.cs index 7874d0397..05ecd44a8 100644 --- a/OpenBullet2.Web/Utils/BlockMapper.cs +++ b/OpenBullet2.Web/Utils/BlockMapper.cs @@ -10,14 +10,15 @@ using RuriLib.Models.Blocks.Settings; using RuriLib.Models.Blocks.Settings.Interpolated; using System.Text.Json; +using OpenBullet2.Web.Exceptions; namespace OpenBullet2.Web.Utils; -static internal class BlockMapper +internal static class BlockMapper { // Here I did the dto -> block mappings manually while I find // a way to make automapper work properly on BlockSetting... - static internal List MapStack( + internal static List MapStack( this List jsonElements, IMapper mapper) { var stack = new List(); @@ -28,6 +29,11 @@ static internal List MapStack( var id = jsonElement.GetProperty("id").GetString(); BlockInstance block; + if (id is null) + { + throw new MappingException("Block id not found"); + } + switch (id) { case "HttpRequest": @@ -212,7 +218,7 @@ private static void MapMultipartSettings(MultipartRequestParamsDto dto, foreach (var c in dto.Contents) { var contentDto = PolyMapper.ConvertPolyDto( - (JsonElement)c!); + (JsonElement)c); HttpContentSettingsGroup content; @@ -269,44 +275,44 @@ private static void MapSetting(BlockSettingDto? dto, BlockSetting setting) } setting.InputMode = dto.InputMode; - setting.InputVariableName = dto.InputVariableName; + setting.InputVariableName = dto.InputVariableName ?? string.Empty; var value = (JsonElement)dto.Value!; switch (dto.Type) { case BlockSettingType.String: - ((StringSetting)setting.FixedSetting).Value = value.GetString(); - ((InterpolatedStringSetting)setting.InterpolatedSetting).Value = value.GetString(); + ((StringSetting)setting.FixedSetting!).Value = value.GetString(); + ((InterpolatedStringSetting)setting.InterpolatedSetting!).Value = value.GetString(); break; case BlockSettingType.Int: - ((IntSetting)setting.FixedSetting).Value = value.GetInt32(); + ((IntSetting)setting.FixedSetting!).Value = value.GetInt32(); break; case BlockSettingType.Float: - ((FloatSetting)setting.FixedSetting).Value = value.GetSingle(); + ((FloatSetting)setting.FixedSetting!).Value = value.GetSingle(); break; case BlockSettingType.Bool: - ((BoolSetting)setting.FixedSetting).Value = value.GetBoolean(); + ((BoolSetting)setting.FixedSetting!).Value = value.GetBoolean(); break; case BlockSettingType.ByteArray: - ((ByteArraySetting)setting.FixedSetting).Value = value.GetBytesFromBase64(); + ((ByteArraySetting)setting.FixedSetting!).Value = value.GetBytesFromBase64(); break; case BlockSettingType.ListOfStrings: - ((ListOfStringsSetting)setting.FixedSetting).Value = value - .Deserialize>(Globals.JsonOptions); - ((InterpolatedListOfStringsSetting)setting.InterpolatedSetting).Value = value - .Deserialize>(Globals.JsonOptions); + ((ListOfStringsSetting)setting.FixedSetting!).Value = value + .Deserialize>(Globals.JsonOptions)!; + ((InterpolatedListOfStringsSetting)setting.InterpolatedSetting!).Value = value + .Deserialize>(Globals.JsonOptions)!; break; case BlockSettingType.DictionaryOfStrings: - ((DictionaryOfStringsSetting)setting.FixedSetting).Value = value - .Deserialize>(Globals.JsonOptions); - ((InterpolatedDictionaryOfStringsSetting)setting.InterpolatedSetting).Value = value - .Deserialize>(Globals.JsonOptions); + ((DictionaryOfStringsSetting)setting.FixedSetting!).Value = value + .Deserialize>(Globals.JsonOptions)!; + ((InterpolatedDictionaryOfStringsSetting)setting.InterpolatedSetting!).Value = value + .Deserialize>(Globals.JsonOptions)!; break; case BlockSettingType.Enum: diff --git a/RuriLib/Helpers/Blocks/DescriptorsRepository.cs b/RuriLib/Helpers/Blocks/DescriptorsRepository.cs index 2f1181822..6e2d842f8 100644 --- a/RuriLib/Helpers/Blocks/DescriptorsRepository.cs +++ b/RuriLib/Helpers/Blocks/DescriptorsRepository.cs @@ -20,6 +20,26 @@ namespace RuriLib.Helpers.Blocks /// public class DescriptorsRepository { + private static readonly Dictionary _variableTypes = new() + { + [ typeof(void) ] = null, + [ typeof(string) ] = VariableType.String, + [ typeof(int) ] = VariableType.Int, + [ typeof(float) ] = VariableType.Float, + [ typeof(bool) ] = VariableType.Bool, + [ typeof(List) ] = VariableType.ListOfStrings, + [ typeof(Dictionary) ] = VariableType.DictionaryOfStrings, + [ typeof(byte[]) ] = VariableType.ByteArray, + [ typeof(Task) ] = null, + [ typeof(Task) ] = VariableType.String, + [ typeof(Task) ] = VariableType.Int, + [ typeof(Task) ] = VariableType.Float, + [ typeof(Task) ] = VariableType.Bool, + [ typeof(Task>) ] = VariableType.ListOfStrings, + [ typeof(Task>) ] = VariableType.DictionaryOfStrings, + [ typeof(Task) ] = VariableType.ByteArray + }; + public Dictionary Descriptors { get; set; } = new Dictionary(); /// @@ -173,45 +193,13 @@ private static BlockParameter BuildBlockParameter(ParameterInfo info) /// /// Converts the return of a method to a . - /// Returns null if the method returns or . + /// Returns null if the method returns void or Task. /// public static VariableType? ToVariableType(Type type) { - if (type == typeof(void)) - return null; - - var dict = new Dictionary - { - { typeof(string), VariableType.String }, - { typeof(int), VariableType.Int }, - { typeof(float), VariableType.Float }, - { typeof(bool), VariableType.Bool }, - { typeof(List), VariableType.ListOfStrings }, - { typeof(Dictionary), VariableType.DictionaryOfStrings }, - { typeof(byte[]), VariableType.ByteArray } - }; - - if (dict.ContainsKey(type)) - return dict[type]; - - if (type == typeof(Task)) - return null; - - var taskDict = new Dictionary - { - { typeof(Task), VariableType.String }, - { typeof(Task), VariableType.Int }, - { typeof(Task), VariableType.Float }, - { typeof(Task), VariableType.Bool }, - { typeof(Task>), VariableType.ListOfStrings }, - { typeof(Task>), VariableType.DictionaryOfStrings }, - { typeof(Task), VariableType.ByteArray } - }; - - if (taskDict.ContainsKey(type)) - return taskDict[type]; - - throw new InvalidCastException($"The type {type} could not be casted to VariableType"); + _variableTypes.TryGetValue(type, out var value); + return value ?? throw new InvalidCastException( + $"The type {type} could not be casted to VariableType"); } /// @@ -223,7 +211,10 @@ public static Variable ToVariable(string name, Type type, dynamic value) var t = ToVariableType(type); if (!t.HasValue) - throw new InvalidCastException($"Cannot cast type {type} to a variable"); + { + throw new InvalidCastException( + $"Cannot cast type {type} to a variable"); + } Variable variable = t switch { @@ -244,36 +235,36 @@ public static Variable ToVariable(string name, Type type, dynamic value) private static BlockParameter ToBlockParameter(ParameterInfo parameter) { - var dict = new Dictionary> + var creators = new Dictionary> { - { typeof(string), () => new StringParameter + { typeof(string), () => new StringParameter(parameter.Name!) { - DefaultValue = parameter.HasDefaultValue ? (string)parameter.DefaultValue : "", + DefaultValue = parameter.HasDefaultValue ? (string)parameter.DefaultValue! : "", MultiLine = parameter.GetCustomAttribute() != null } }, - { typeof(int), () => new IntParameter - { DefaultValue = parameter.HasDefaultValue ? (int)parameter.DefaultValue : 0 } }, + { typeof(int), () => new IntParameter(parameter.Name!) + { DefaultValue = parameter.HasDefaultValue ? (int)parameter.DefaultValue! : 0 } }, - { typeof(float), () => new FloatParameter - { DefaultValue = parameter.HasDefaultValue ? (float)parameter.DefaultValue : 0.0f } }, + { typeof(float), () => new FloatParameter(parameter.Name!) + { DefaultValue = parameter.HasDefaultValue ? (float)parameter.DefaultValue! : 0.0f } }, - { typeof(bool), () => new BoolParameter - { DefaultValue = parameter.HasDefaultValue ? (bool)parameter.DefaultValue : false } }, + { typeof(bool), () => new BoolParameter(parameter.Name!) + { DefaultValue = parameter.HasDefaultValue && (bool)parameter.DefaultValue! } }, // TODO: Add defaults for these through parameter attributes - { typeof(List), () => new ListOfStringsParameter() }, - { typeof(Dictionary), () => new DictionaryOfStringsParameter() }, - { typeof(byte[]), () => new ByteArrayParameter() } + { typeof(List), () => new ListOfStringsParameter(parameter.Name!) }, + { typeof(Dictionary), () => new DictionaryOfStringsParameter(parameter.Name!) }, + { typeof(byte[]), () => new ByteArrayParameter(parameter.Name!) } }; var blockParamAttribute = parameter.GetCustomAttribute(); // If it's one of the standard types - if (dict.ContainsKey(parameter.ParameterType)) + if (creators.TryGetValue(parameter.ParameterType, out var creator)) { - var blockParam = dict[parameter.ParameterType].Invoke(); + var blockParam = creator.Invoke(); if (blockParamAttribute != null) { @@ -281,21 +272,19 @@ private static BlockParameter ToBlockParameter(ParameterInfo parameter) blockParam.Description = blockParamAttribute.description; } - blockParam.Name = parameter.Name; + blockParam.Name = parameter.Name!; return blockParam; } // If it's an enum type if (parameter.ParameterType.IsEnum) { - var blockParam = new EnumParameter - { - Name = parameter.Name, - EnumType = parameter.ParameterType, - DefaultValue = parameter.HasDefaultValue - ? parameter.DefaultValue.ToString() - : Enum.GetNames(parameter.ParameterType).First() - }; + var blockParam = new EnumParameter( + parameter.Name!, + parameter.ParameterType, + parameter.HasDefaultValue + ? parameter.DefaultValue!.ToString()! + : Enum.GetNames(parameter.ParameterType).First()); if (blockParamAttribute != null) { @@ -336,24 +325,27 @@ private void PushLeaves(CategoryTreeNode node, int level) var split = d.Category.Path.Split('.'); // Example: RuriLib.Blocks.Http // If a descriptor's category has a namespace which (split) is longer than the current tree level - if (split.Length > level) + // then it means that it has subcategories, otherwise it's a leaf + if (split.Length <= level) { - var subCat = split[level]; // Example: level 0 => RuriLib, level 1 => Http - - // Try to get an existing subcategory node - var subCatNode = node.SubCategories.FirstOrDefault(s => s.Name == subCat); + continue; + } + + var subCat = split[level]; // Example: level 0 => RuriLib, level 1 => Http - // Create the subcategory node if it doesn't exist - if (subCatNode == null) - { - subCatNode = new CategoryTreeNode { Parent = node, Name = subCat }; - node.SubCategories.Add(subCatNode); - } + // Try to get an existing subcategory node + var subCatNode = node.SubCategories.FirstOrDefault(s => s.Name == subCat); - subCatNode.Descriptors.Add(d); - node.Descriptors.RemoveAt(i); - i--; + // Create the subcategory node if it doesn't exist + if (subCatNode == null) + { + subCatNode = new CategoryTreeNode { Parent = node, Name = subCat }; + node.SubCategories.Add(subCatNode); } + + subCatNode.Descriptors.Add(d); + node.Descriptors.RemoveAt(i); + i--; } // Order them alphabetically @@ -361,9 +353,8 @@ private void PushLeaves(CategoryTreeNode node, int level) node.Descriptors = node.Descriptors.OrderBy(d => d.Name).ToList(); // Push leaves of subcategories recursively - for (var i = 0; i < node.SubCategories.Count; i++) + foreach (var s in node.SubCategories) { - var s = node.SubCategories[i]; PushLeaves(s, level + 1); } } diff --git a/RuriLib/Helpers/LoliCode/LoliCodeParser.cs b/RuriLib/Helpers/LoliCode/LoliCodeParser.cs index 77583a9ca..60039dd87 100644 --- a/RuriLib/Helpers/LoliCode/LoliCodeParser.cs +++ b/RuriLib/Helpers/LoliCode/LoliCodeParser.cs @@ -9,236 +9,235 @@ using RuriLib.Models.Blocks.Custom.Keycheck; using RuriLib.Models.Conditions.Comparisons; -namespace RuriLib.Helpers.LoliCode +namespace RuriLib.Helpers.LoliCode; + +/// +/// Has methods to parse LoliCode snippets. +/// +public static class LoliCodeParser { /// - /// Has methods to parse LoliCode snippets. + /// Parses a setting from a LoliCode string and assigns it to the + /// correct setting given a list of pre-initialized default settings. /// - public static class LoliCodeParser + public static void ParseSetting(ref string input, Dictionary settings, + BlockDescriptor descriptor) { - /// - /// Parses a setting from a LoliCode string and assigns it to the - /// correct setting given a list of pre-initialized default settings. - /// - public static void ParseSetting(ref string input, Dictionary settings, - BlockDescriptor descriptor) - { - input = input.TrimStart(); + input = input.TrimStart(); - // myParam = "myValue" - var name = LineParser.ParseToken(ref input); + // myParam = "myValue" + var name = LineParser.ParseToken(ref input); - if (!descriptor.Parameters.ContainsKey(name) || !settings.ContainsKey(name)) - throw new Exception($"Incorrect setting name: {name}"); + if (!descriptor.Parameters.ContainsKey(name) || !settings.ContainsKey(name)) + throw new Exception($"Incorrect setting name: {name}"); - var param = descriptor.Parameters[name]; - var setting = settings[name]; + var param = descriptor.Parameters[name]; + var setting = settings[name]; - input = input.TrimStart(); + input = input.TrimStart(); - // = "myValue" - if (input[0] != '=') - throw new Exception("Could not parse the setting"); + // = "myValue" + if (input[0] != '=') + throw new Exception("Could not parse the setting"); - input = input.Substring(1); - input = input.TrimStart(); + input = input.Substring(1); + input = input.TrimStart(); - ParseSettingValue(ref input, setting, param); - } + ParseSettingValue(ref input, setting, param); + } - /// - /// Parses a setting value from a LoliCode string (without the setting name) and - /// assigns it to the given . - /// - public static void ParseSettingValue(ref string input, BlockSetting setting, - T param) where T : BlockParameter + /// + /// Parses a setting value from a LoliCode string (without the setting name) and + /// assigns it to the given . + /// + public static void ParseSettingValue(ref string input, BlockSetting setting, + T param) where T : BlockParameter + { + // @myVariable + // $"interp" + // "fixedValue" + if (input.Length > 0 && input[0] == '@') // VARIABLE { - // @myVariable - // $"interp" - // "fixedValue" - if (input.Length > 0 && input[0] == '@') // VARIABLE - { - input = input[1..]; + input = input[1..]; - // If there is just @ without anything after it, - // the variable name is empty. Do not throw an exception here - // or it will prevent saving the config (even if invalid) - var variableName = input.Length == 0 || input[0] == ' ' || input[0] == '\t' - ? string.Empty - : LineParser.ParseToken(ref input); - - setting.InputMode = SettingInputMode.Variable; - setting.InputVariableName = variableName; - setting.InterpolatedSetting = param switch - { - StringParameter x => new InterpolatedStringSetting() { MultiLine = x.MultiLine }, - ListOfStringsParameter _ => new InterpolatedListOfStringsSetting(), - DictionaryOfStringsParameter _ => new InterpolatedDictionaryOfStringsSetting(), - _ => null - }; - setting.FixedSetting = param switch // Initialize fixed setting as well, used for type switching - { - BoolParameter _ => new BoolSetting(), - IntParameter _ => new IntSetting(), - FloatParameter _ => new FloatSetting(), - StringParameter x => new StringSetting() { MultiLine = x.MultiLine }, - ListOfStringsParameter _ => new ListOfStringsSetting(), - DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting(), - ByteArrayParameter _ => new ByteArraySetting(), - EnumParameter x => new EnumSetting() { EnumType = x.EnumType }, - _ => throw new NotSupportedException() - }; - } - else if (input.Length > 0 && input[0] == '$') // INTERPOLATED + // If there is just @ without anything after it, + // the variable name is empty. Do not throw an exception here + // or it will prevent saving the config (even if invalid) + var variableName = input.Length == 0 || input[0] == ' ' || input[0] == '\t' + ? string.Empty + : LineParser.ParseToken(ref input); + + setting.InputMode = SettingInputMode.Variable; + setting.InputVariableName = variableName; + setting.InterpolatedSetting = param switch { - input = input[1..]; - setting.InputMode = SettingInputMode.Interpolated; - setting.InterpolatedSetting = param switch - { - StringParameter x => new InterpolatedStringSetting { Value = LineParser.ParseLiteral(ref input), MultiLine = x.MultiLine }, - ListOfStringsParameter _ => new InterpolatedListOfStringsSetting { Value = LineParser.ParseList(ref input) }, - DictionaryOfStringsParameter _ => new InterpolatedDictionaryOfStringsSetting { Value = LineParser.ParseDictionary(ref input) }, - _ => throw new NotSupportedException() - }; - setting.FixedSetting = param switch // Initialize fixed setting as well, used for type switching - { - StringParameter x => new StringSetting { Value = (setting.InterpolatedSetting as InterpolatedStringSetting).Value, MultiLine = x.MultiLine }, - ListOfStringsParameter _ => new ListOfStringsSetting { Value = (setting.InterpolatedSetting as InterpolatedListOfStringsSetting).Value }, - DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting { Value = (setting.InterpolatedSetting as InterpolatedDictionaryOfStringsSetting).Value }, - _ => throw new NotSupportedException() - }; - } - else // FIXED + StringParameter x => new InterpolatedStringSetting() { MultiLine = x.MultiLine }, + ListOfStringsParameter _ => new InterpolatedListOfStringsSetting(), + DictionaryOfStringsParameter _ => new InterpolatedDictionaryOfStringsSetting(), + _ => null + }; + setting.FixedSetting = param switch // Initialize fixed setting as well, used for type switching { - setting.InputMode = SettingInputMode.Fixed; - setting.FixedSetting = param switch - { - StringParameter x => new StringSetting { Value = LineParser.ParseLiteral(ref input), MultiLine = x.MultiLine }, - BoolParameter _ => new BoolSetting { Value = LineParser.ParseBool(ref input) }, - ByteArrayParameter _ => new ByteArraySetting { Value = LineParser.ParseByteArray(ref input) }, - DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting { Value = LineParser.ParseDictionary(ref input) }, - EnumParameter x => new EnumSetting { EnumType = x.EnumType, Value = LineParser.ParseToken(ref input) }, - FloatParameter _ => new FloatSetting { Value = LineParser.ParseFloat(ref input) }, - IntParameter _ => new IntSetting { Value = LineParser.ParseInt(ref input) }, - ListOfStringsParameter _ => new ListOfStringsSetting { Value = LineParser.ParseList(ref input) }, - _ => throw new NotSupportedException() - }; - } + BoolParameter _ => new BoolSetting(), + IntParameter _ => new IntSetting(), + FloatParameter _ => new FloatSetting(), + StringParameter x => new StringSetting() { MultiLine = x.MultiLine }, + ListOfStringsParameter _ => new ListOfStringsSetting(), + DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting(), + ByteArrayParameter _ => new ByteArraySetting(), + EnumParameter x => new EnumSetting(x.EnumType), + _ => throw new NotSupportedException() + }; } - - /// - /// Checks whether a line is a valid LoliCode block setting. - /// - public static bool IsSetting(string input) - => Regex.IsMatch(input, "^( |\t)+([0-9A-Za-z]+) = .+$"); - - /// - /// Detects the type of the next token in the given . - /// If the token was a variable name, it returns null. - /// - public static VariableType? DetectTokenType(string input) + else if (input.Length > 0 && input[0] == '$') // INTERPOLATED { - if (input.StartsWith('"')) - return VariableType.String; + input = input[1..]; + setting.InputMode = SettingInputMode.Interpolated; + setting.InterpolatedSetting = param switch + { + StringParameter x => new InterpolatedStringSetting { Value = LineParser.ParseLiteral(ref input), MultiLine = x.MultiLine }, + ListOfStringsParameter _ => new InterpolatedListOfStringsSetting { Value = LineParser.ParseList(ref input) }, + DictionaryOfStringsParameter _ => new InterpolatedDictionaryOfStringsSetting { Value = LineParser.ParseDictionary(ref input) }, + _ => throw new NotSupportedException() + }; + setting.FixedSetting = param switch // Initialize fixed setting as well, used for type switching + { + StringParameter x => new StringSetting { Value = (setting.InterpolatedSetting as InterpolatedStringSetting).Value, MultiLine = x.MultiLine }, + ListOfStringsParameter _ => new ListOfStringsSetting { Value = (setting.InterpolatedSetting as InterpolatedListOfStringsSetting).Value }, + DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting { Value = (setting.InterpolatedSetting as InterpolatedDictionaryOfStringsSetting).Value }, + _ => throw new NotSupportedException() + }; + } + else // FIXED + { + setting.InputMode = SettingInputMode.Fixed; + setting.FixedSetting = param switch + { + StringParameter x => new StringSetting { Value = LineParser.ParseLiteral(ref input), MultiLine = x.MultiLine }, + BoolParameter _ => new BoolSetting { Value = LineParser.ParseBool(ref input) }, + ByteArrayParameter _ => new ByteArraySetting { Value = LineParser.ParseByteArray(ref input) }, + DictionaryOfStringsParameter _ => new DictionaryOfStringsSetting { Value = LineParser.ParseDictionary(ref input) }, + EnumParameter x => new EnumSetting(x.EnumType) { Value = LineParser.ParseToken(ref input) }, + FloatParameter _ => new FloatSetting { Value = LineParser.ParseFloat(ref input) }, + IntParameter _ => new IntSetting { Value = LineParser.ParseInt(ref input) }, + ListOfStringsParameter _ => new ListOfStringsSetting { Value = LineParser.ParseList(ref input) }, + _ => throw new NotSupportedException() + }; + } + } - if (Regex.IsMatch(input, "^([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])( |$)")) - return VariableType.Bool; + /// + /// Checks whether a line is a valid LoliCode block setting. + /// + public static bool IsSetting(string input) + => Regex.IsMatch(input, "^( |\t)+([0-9A-Za-z]+) = .+$"); - if (Regex.IsMatch(input, "^-?[0-9]+( |$)")) - return VariableType.Int; + /// + /// Detects the type of the next token in the given . + /// If the token was a variable name, it returns null. + /// + public static VariableType? DetectTokenType(string input) + { + if (input.StartsWith('"')) + return VariableType.String; - if (Regex.IsMatch(input, "^-?[0-9\\.]+( |$)")) - return VariableType.Float; + if (Regex.IsMatch(input, "^([Tt][Rr][Uu][Ee])|([Ff][Aa][Ll][Ss][Ee])( |$)")) + return VariableType.Bool; - if (input.StartsWith('[')) - return VariableType.ListOfStrings; + if (Regex.IsMatch(input, "^-?[0-9]+( |$)")) + return VariableType.Int; - if (input.StartsWith('{')) - return VariableType.DictionaryOfStrings; + if (Regex.IsMatch(input, "^-?[0-9\\.]+( |$)")) + return VariableType.Float; - if (Regex.IsMatch(input, "^[A-Za-z][A-Za-z0-9]*")) - return null; + if (input.StartsWith('[')) + return VariableType.ListOfStrings; - if (Regex.IsMatch(input, "^[A-Za-z0-9+/=]+")) - return VariableType.ByteArray; + if (input.StartsWith('{')) + return VariableType.DictionaryOfStrings; - throw new Exception("Could not detect the token type"); - } + if (Regex.IsMatch(input, "^[A-Za-z][A-Za-z0-9]*")) + return null; - /// - /// All the supported key identifiers. - /// - public static readonly string[] keyIdentifiers = new[] { "BOOLKEY", "STRINGKEY", "INTKEY", "FLOATKEY", "LISTKEY", "DICTKEY" }; - - /// - /// Parses a from the input and moves forward. - /// - /// - /// - /// - public static Key ParseKey(ref string line, string keyType) => keyType switch - { - "BOOLKEY" => ParseBoolKey(ref line), - "STRINGKEY" => ParseStringKey(ref line), - "INTKEY" => ParseIntKey(ref line), - "FLOATKEY" => ParseFloatKey(ref line), - "LISTKEY" => ParseListKey(ref line), - "DICTKEY" => ParseDictKey(ref line), - _ => throw new NotSupportedException() - }; - - private static BoolKey ParseBoolKey(ref string line) - { - var key = new BoolKey(); - ParseSettingValue(ref line, key.Left, new BoolParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new BoolParameter()); - return key; - } + if (Regex.IsMatch(input, "^[A-Za-z0-9+/=]+")) + return VariableType.ByteArray; - private static StringKey ParseStringKey(ref string line) - { - var key = new StringKey(); - ParseSettingValue(ref line, key.Left, new StringParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new StringParameter()); - return key; - } + throw new Exception("Could not detect the token type"); + } - private static IntKey ParseIntKey(ref string line) - { - var key = new IntKey(); - ParseSettingValue(ref line, key.Left, new IntParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new IntParameter()); - return key; - } + /// + /// All the supported key identifiers. + /// + public static readonly string[] keyIdentifiers = new[] { "BOOLKEY", "STRINGKEY", "INTKEY", "FLOATKEY", "LISTKEY", "DICTKEY" }; - private static FloatKey ParseFloatKey(ref string line) - { - var key = new FloatKey(); - ParseSettingValue(ref line, key.Left, new FloatParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new FloatParameter()); - return key; - } + /// + /// Parses a from the input and moves forward. + /// + /// + /// + /// + public static Key ParseKey(ref string line, string keyType) => keyType switch + { + "BOOLKEY" => ParseBoolKey(ref line), + "STRINGKEY" => ParseStringKey(ref line), + "INTKEY" => ParseIntKey(ref line), + "FLOATKEY" => ParseFloatKey(ref line), + "LISTKEY" => ParseListKey(ref line), + "DICTKEY" => ParseDictKey(ref line), + _ => throw new NotSupportedException() + }; + + private static BoolKey ParseBoolKey(ref string line) + { + var key = new BoolKey(); + ParseSettingValue(ref line, key.Left, new BoolParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new BoolParameter(string.Empty)); + return key; + } - private static ListKey ParseListKey(ref string line) - { - var key = new ListKey(); - ParseSettingValue(ref line, key.Left, new ListOfStringsParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new StringParameter()); - return key; - } + private static StringKey ParseStringKey(ref string line) + { + var key = new StringKey(); + ParseSettingValue(ref line, key.Left, new StringParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new StringParameter(string.Empty)); + return key; + } - private static DictionaryKey ParseDictKey(ref string line) - { - var key = new DictionaryKey(); - ParseSettingValue(ref line, key.Left, new DictionaryOfStringsParameter()); - key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); - ParseSettingValue(ref line, key.Right, new StringParameter()); - return key; - } + private static IntKey ParseIntKey(ref string line) + { + var key = new IntKey(); + ParseSettingValue(ref line, key.Left, new IntParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new IntParameter(string.Empty)); + return key; + } + + private static FloatKey ParseFloatKey(ref string line) + { + var key = new FloatKey(); + ParseSettingValue(ref line, key.Left, new FloatParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new FloatParameter(string.Empty)); + return key; + } + + private static ListKey ParseListKey(ref string line) + { + var key = new ListKey(); + ParseSettingValue(ref line, key.Left, new ListOfStringsParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new StringParameter(string.Empty)); + return key; + } + + private static DictionaryKey ParseDictKey(ref string line) + { + var key = new DictionaryKey(); + ParseSettingValue(ref line, key.Left, new DictionaryOfStringsParameter(string.Empty)); + key.Comparison = Enum.Parse(LineParser.ParseToken(ref line)); + ParseSettingValue(ref line, key.Right, new StringParameter(string.Empty)); + return key; } } diff --git a/RuriLib/Models/Blocks/Custom/HttpRequestBlockInstance.cs b/RuriLib/Models/Blocks/Custom/HttpRequestBlockInstance.cs index afbbbe106..0263bf738 100644 --- a/RuriLib/Models/Blocks/Custom/HttpRequestBlockInstance.cs +++ b/RuriLib/Models/Blocks/Custom/HttpRequestBlockInstance.cs @@ -180,13 +180,13 @@ public override void FromLC(ref string script, ref int lineNumber) line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, standardReqParams.Content, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, standardReqParams.Content, new StringParameter(string.Empty)); // Read another line to parse the content-type line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, standardReqParams.ContentType, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, standardReqParams.ContentType, new StringParameter(string.Empty)); RequestParams = standardReqParams; break; @@ -198,14 +198,14 @@ public override void FromLC(ref string script, ref int lineNumber) line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, rawReqParams.Content, new ByteArrayParameter()); + LoliCodeParser.ParseSettingValue(ref line, rawReqParams.Content, new ByteArrayParameter(string.Empty)); // Read another line to parse the content-type line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, rawReqParams.ContentType, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, rawReqParams.ContentType, new StringParameter(string.Empty)); RequestParams = rawReqParams; break; @@ -217,13 +217,13 @@ public override void FromLC(ref string script, ref int lineNumber) line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Username, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Username, new StringParameter(string.Empty)); // Read another line to parse the password line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Password, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, basicAuthReqParams.Password, new StringParameter(string.Empty)); RequestParams = basicAuthReqParams; break; @@ -235,7 +235,7 @@ public override void FromLC(ref string script, ref int lineNumber) line = reader.ReadLine().Trim(); lineCopy = line; lineNumber++; - LoliCodeParser.ParseSettingValue(ref line, multipartReqParams.Boundary, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, multipartReqParams.Boundary, new StringParameter(string.Empty)); RequestParams = multipartReqParams; break; @@ -266,15 +266,15 @@ public override void FromLC(ref string script, ref int lineNumber) { case "STRING": var stringContent = new StringHttpContentSettingsGroup(); - LoliCodeParser.ParseSettingValue(ref line, stringContent.Name, new StringParameter()); - LoliCodeParser.ParseSettingValue(ref line, stringContent.Data, new StringParameter()); - LoliCodeParser.ParseSettingValue(ref line, stringContent.ContentType, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, stringContent.Name, new StringParameter(string.Empty)); + LoliCodeParser.ParseSettingValue(ref line, stringContent.Data, new StringParameter(string.Empty)); + LoliCodeParser.ParseSettingValue(ref line, stringContent.ContentType, new StringParameter(string.Empty)); multipart.Contents.Add(stringContent); break; case "RAW": var rawContent = new RawHttpContentSettingsGroup(); - LoliCodeParser.ParseSettingValue(ref line, rawContent.Name, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, rawContent.Name, new StringParameter(string.Empty)); // HACK: Cache the line to prevent it from being modified by the parser // if the parse fails, we can still use the original line to parse the content-type @@ -286,22 +286,22 @@ public override void FromLC(ref string script, ref int lineNumber) try { LoliCodeParser.ParseSettingValue(ref line, rawContent.Data, - new ByteArrayParameter()); + new ByteArrayParameter(string.Empty)); } catch { line = lineCopyCache; } - LoliCodeParser.ParseSettingValue(ref line, rawContent.ContentType, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, rawContent.ContentType, new StringParameter(string.Empty)); multipart.Contents.Add(rawContent); break; case "FILE": var fileContent = new FileHttpContentSettingsGroup(); - LoliCodeParser.ParseSettingValue(ref line, fileContent.Name, new StringParameter()); - LoliCodeParser.ParseSettingValue(ref line, fileContent.FileName, new StringParameter()); - LoliCodeParser.ParseSettingValue(ref line, fileContent.ContentType, new StringParameter()); + LoliCodeParser.ParseSettingValue(ref line, fileContent.Name, new StringParameter(string.Empty)); + LoliCodeParser.ParseSettingValue(ref line, fileContent.FileName, new StringParameter(string.Empty)); + LoliCodeParser.ParseSettingValue(ref line, fileContent.ContentType, new StringParameter(string.Empty)); multipart.Contents.Add(fileContent); break; } diff --git a/RuriLib/Models/Blocks/Parameters/BlockParameter.cs b/RuriLib/Models/Blocks/Parameters/BlockParameter.cs index 055830a95..71c272db9 100644 --- a/RuriLib/Models/Blocks/Parameters/BlockParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/BlockParameter.cs @@ -2,18 +2,52 @@ using RuriLib.Models.Blocks.Settings; using System; -namespace RuriLib.Models.Blocks.Parameters +namespace RuriLib.Models.Blocks.Parameters; + +/// +/// A parameter of a block. +/// +public abstract class BlockParameter { - public abstract class BlockParameter + /// + protected BlockParameter(string name) { - public string Name { get; set; } - public string AssignedName { get; set; } - public string PrettyName => AssignedName ?? Name.ToReadableName(); - public string Description { get; set; } = null; - public SettingInputMode InputMode { get; set; } = SettingInputMode.Fixed; - public string DefaultVariableName { get; set; } = string.Empty; - - public virtual BlockSetting ToBlockSetting() - => throw new NotImplementedException(); + Name = name; } + + /// + /// The name of the parameter. + /// + public string Name { get; set; } + + /// + /// The assigned name of the parameter. + /// + public string? AssignedName { get; set; } + + /// + /// The pretty name of the parameter. + /// + public string PrettyName => AssignedName ?? Name.ToReadableName(); + + /// + /// The description of the parameter. + /// + public string? Description { get; set; } + + /// + /// The input mode of the parameter. + /// + public SettingInputMode InputMode { get; set; } = SettingInputMode.Fixed; + + /// + /// The default variable name for the parameter. + /// + public string DefaultVariableName { get; set; } = string.Empty; + + /// + /// Converts the parameter to a block setting. + /// + public virtual BlockSetting ToBlockSetting() + => throw new NotImplementedException(); } diff --git a/RuriLib/Models/Blocks/Parameters/BoolParameter.cs b/RuriLib/Models/Blocks/Parameters/BoolParameter.cs index 887801170..80594a509 100644 --- a/RuriLib/Models/Blocks/Parameters/BoolParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/BoolParameter.cs @@ -1,38 +1,45 @@ -using RuriLib.Extensions; -using RuriLib.Models.Blocks.Settings; +using RuriLib.Models.Blocks.Settings; -namespace RuriLib.Models.Blocks.Parameters +namespace RuriLib.Models.Blocks.Parameters; + +/// +/// A parameter of type bool. +/// +public class BoolParameter : BlockParameter { - public class BoolParameter : BlockParameter + /// + /// The default value of the parameter. + /// + public bool DefaultValue { get; set; } + + /// + public BoolParameter(string name) : base(name) { - public bool DefaultValue { get; set; } - public BoolParameter() - { + } - } + /// + public BoolParameter(string name, bool defaultValue = false) : base(name) + { + Name = name; + DefaultValue = defaultValue; + } - public BoolParameter(string name, bool defaultValue = false) - { - Name = name; - DefaultValue = defaultValue; - } + /// + public BoolParameter(string name, string defaultVariableName = "") : base(name) + { + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; + } - public BoolParameter(string name, string defaultVariableName = "") + /// + public override BlockSetting ToBlockSetting() + => new() { - Name = name; - DefaultVariableName = defaultVariableName; - InputMode = SettingInputMode.Variable; - } - - public override BlockSetting ToBlockSetting() - => new() - { - Name = Name, - Description = Description, - ReadableName = PrettyName ?? Name.ToReadableName(), - FixedSetting = new BoolSetting { Value = DefaultValue }, - InputMode = InputMode - }; - } + Name = Name, + Description = Description, + ReadableName = PrettyName, + FixedSetting = new BoolSetting { Value = DefaultValue }, + InputMode = InputMode + }; } diff --git a/RuriLib/Models/Blocks/Parameters/ByteArrayParameter.cs b/RuriLib/Models/Blocks/Parameters/ByteArrayParameter.cs index 3039cf3ac..b3704d651 100644 --- a/RuriLib/Models/Blocks/Parameters/ByteArrayParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/ByteArrayParameter.cs @@ -2,40 +2,49 @@ using RuriLib.Models.Blocks.Settings; using System; -namespace RuriLib.Models.Blocks.Parameters +namespace RuriLib.Models.Blocks.Parameters; + +/// +/// A parameter of type byte array. +/// +public class ByteArrayParameter : BlockParameter { - public class ByteArrayParameter : BlockParameter + /// + /// The default value of the parameter. + /// + public byte[] DefaultValue { get; set; } = []; + + /// + public ByteArrayParameter(string name) : base(name) { - public byte[] DefaultValue { get; set; } - public ByteArrayParameter() - { + } - } + /// + public ByteArrayParameter(string name, byte[]? defaultValue = null, + SettingInputMode inputMode = SettingInputMode.Fixed) : base(name) + { + InputMode = inputMode; + DefaultValue = defaultValue ?? []; + } - public ByteArrayParameter(string name, byte[] defaultValue = null, SettingInputMode inputMode = SettingInputMode.Fixed) - { - Name = name; - InputMode = inputMode; - DefaultValue = defaultValue ?? Array.Empty(); - } + /// + public ByteArrayParameter(string name, string defaultVariableName = "") + : base(name) + { + Name = name; + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; + } - public ByteArrayParameter(string name, string defaultVariableName = "") + /// + public override BlockSetting ToBlockSetting() + => new() { - Name = name; - DefaultVariableName = defaultVariableName; - DefaultValue = Array.Empty(); - InputMode = SettingInputMode.Variable; - } - - public override BlockSetting ToBlockSetting() - => new() - { - Name = Name, - Description = Description, - ReadableName = PrettyName ?? Name.ToReadableName(), - FixedSetting = new ByteArraySetting { Value = DefaultValue }, - InputMode = InputMode - }; - } + Name = Name, + Description = Description, + ReadableName = PrettyName, + FixedSetting = new ByteArraySetting { Value = DefaultValue }, + InputMode = InputMode + }; } diff --git a/RuriLib/Models/Blocks/Parameters/DictionaryOfStringsParameter.cs b/RuriLib/Models/Blocks/Parameters/DictionaryOfStringsParameter.cs index 8c5ca923a..123c08ab8 100644 --- a/RuriLib/Models/Blocks/Parameters/DictionaryOfStringsParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/DictionaryOfStringsParameter.cs @@ -1,33 +1,42 @@ using RuriLib.Models.Blocks.Settings; using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Parameters -{ - public class DictionaryOfStringsParameter : BlockParameter - { - public Dictionary DefaultValue { get; set; } +namespace RuriLib.Models.Blocks.Parameters; - public DictionaryOfStringsParameter() - { +/// +/// A parameter of type dictionary of strings. +/// +public class DictionaryOfStringsParameter : BlockParameter +{ + /// + /// The default value of the parameter. + /// + public Dictionary DefaultValue { get; set; } = []; - } + /// + public DictionaryOfStringsParameter(string name) : base(name) + { - public DictionaryOfStringsParameter(string name, Dictionary defaultValue = null, SettingInputMode inputMode = SettingInputMode.Fixed) - { - Name = name; - DefaultValue = defaultValue ?? new Dictionary(); - DefaultVariableName = string.Empty; - InputMode = SettingInputMode.Fixed; - } + } - public DictionaryOfStringsParameter(string name, string defaultVariableName = "") - { - Name = name; - DefaultVariableName = defaultVariableName; - InputMode = SettingInputMode.Variable; - } + /// + public DictionaryOfStringsParameter(string name, Dictionary? defaultValue = null, + SettingInputMode inputMode = SettingInputMode.Fixed) : base(name) + { + DefaultValue = defaultValue ?? []; + DefaultVariableName = string.Empty; + InputMode = inputMode; + } - public override BlockSetting ToBlockSetting() - => BlockSettingFactory.CreateDictionaryOfStringsSetting(Name, DefaultValue, InputMode, PrettyName, Description); + /// + public DictionaryOfStringsParameter(string name, string defaultVariableName = "") + : base(name) + { + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; } + + /// + public override BlockSetting ToBlockSetting() + => BlockSettingFactory.CreateDictionaryOfStringsSetting(Name, DefaultValue, InputMode, PrettyName, Description); } diff --git a/RuriLib/Models/Blocks/Parameters/EnumParameter.cs b/RuriLib/Models/Blocks/Parameters/EnumParameter.cs index b94503f65..2a3f4b971 100644 --- a/RuriLib/Models/Blocks/Parameters/EnumParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/EnumParameter.cs @@ -2,34 +2,44 @@ using RuriLib.Models.Blocks.Settings; using System; -namespace RuriLib.Models.Blocks.Parameters -{ - public class EnumParameter : BlockParameter - { - public Type EnumType { get; set; } - public string DefaultValue { get; set; } - public string[] Options => Enum.GetNames(EnumType); +namespace RuriLib.Models.Blocks.Parameters; - public EnumParameter() - { +/// +/// A parameter of type enum. +/// +public class EnumParameter : BlockParameter +{ + /// + /// The type of the enum. + /// + public Type EnumType { get; set; } + + /// + /// The default value of the parameter. + /// + public string DefaultValue { get; set; } + + /// + /// The available options for the parameter. + /// + public string[] Options => Enum.GetNames(EnumType); - } + /// + public EnumParameter(string name, Type enumType, string defaultValue) : base(name) + { + Name = name; + EnumType = enumType; + DefaultValue = defaultValue; + } - public EnumParameter(string name, Type enumType, string defaultValue) + /// + public override BlockSetting ToBlockSetting() + => new() { - Name = name; - EnumType = enumType; - DefaultValue = defaultValue; - } - - public override BlockSetting ToBlockSetting() - => new() - { - Name = Name, - Description = Description, - ReadableName = PrettyName ?? Name.ToReadableName(), - FixedSetting = new EnumSetting { EnumType = EnumType, Value = DefaultValue }, - InputMode = InputMode - }; - } + Name = Name, + Description = Description, + ReadableName = PrettyName ?? Name.ToReadableName(), + FixedSetting = new EnumSetting(EnumType) { Value = DefaultValue }, + InputMode = InputMode + }; } diff --git a/RuriLib/Models/Blocks/Parameters/FloatParameter.cs b/RuriLib/Models/Blocks/Parameters/FloatParameter.cs index 440a212c4..f0132b832 100644 --- a/RuriLib/Models/Blocks/Parameters/FloatParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/FloatParameter.cs @@ -1,38 +1,44 @@ -using RuriLib.Extensions; -using RuriLib.Models.Blocks.Settings; +using RuriLib.Models.Blocks.Settings; -namespace RuriLib.Models.Blocks.Parameters +namespace RuriLib.Models.Blocks.Parameters; + +/// +/// A parameter of type float. +/// +public class FloatParameter : BlockParameter { - public class FloatParameter : BlockParameter + /// + /// The default value of the parameter. + /// + public float DefaultValue { get; set; } + + /// + public FloatParameter(string name) : base(name) { - public float DefaultValue { get; set; } - public FloatParameter() - { + } - } + /// + public FloatParameter(string name, float defaultValue = 0) : base(name) + { + DefaultValue = defaultValue; + } - public FloatParameter(string name, float defaultValue = 0) - { - Name = name; - DefaultValue = defaultValue; - } + /// + public FloatParameter(string name, string defaultVariableName = "") : base(name) + { + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; + } - public FloatParameter(string name, string defaultVariableName = "") + /// + public override BlockSetting ToBlockSetting() + => new() { - Name = name; - DefaultVariableName = defaultVariableName; - InputMode = SettingInputMode.Variable; - } - - public override BlockSetting ToBlockSetting() - => new() - { - Name = Name, - Description = Description, - ReadableName = PrettyName ?? Name.ToReadableName(), - FixedSetting = new FloatSetting { Value = DefaultValue }, - InputMode = InputMode - }; - } + Name = Name, + Description = Description, + ReadableName = PrettyName, + FixedSetting = new FloatSetting { Value = DefaultValue }, + InputMode = InputMode + }; } diff --git a/RuriLib/Models/Blocks/Parameters/IntParameter.cs b/RuriLib/Models/Blocks/Parameters/IntParameter.cs index 9b3a162f7..65f8e42b4 100644 --- a/RuriLib/Models/Blocks/Parameters/IntParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/IntParameter.cs @@ -1,38 +1,44 @@ -using RuriLib.Extensions; -using RuriLib.Models.Blocks.Settings; +using RuriLib.Models.Blocks.Settings; -namespace RuriLib.Models.Blocks.Parameters +namespace RuriLib.Models.Blocks.Parameters; + +/// +/// A parameter of type int. +/// +public class IntParameter : BlockParameter { - public class IntParameter : BlockParameter + /// + /// The default value of the parameter. + /// + public int DefaultValue { get; set; } + + /// + public IntParameter(string name) : base(name) { - public int DefaultValue { get; set; } - public IntParameter() - { + } - } + /// + public IntParameter(string name, int defaultValue = 0) : base(name) + { + DefaultValue = defaultValue; + } - public IntParameter(string name, int defaultValue = 0) - { - Name = name; - DefaultValue = defaultValue; - } + /// + public IntParameter(string name, string defaultVariableName = "") : base(name) + { + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; + } - public IntParameter(string name, string defaultVariableName = "") + /// + public override BlockSetting ToBlockSetting() + => new() { - Name = name; - DefaultVariableName = defaultVariableName; - InputMode = SettingInputMode.Variable; - } - - public override BlockSetting ToBlockSetting() - => new() - { - Name = Name, - Description = Description, - ReadableName = PrettyName ?? Name.ToReadableName(), - FixedSetting = new IntSetting { Value = DefaultValue }, - InputMode = InputMode - }; - } + Name = Name, + Description = Description, + ReadableName = PrettyName, + FixedSetting = new IntSetting { Value = DefaultValue }, + InputMode = InputMode + }; } diff --git a/RuriLib/Models/Blocks/Parameters/ListOfStringsParameter.cs b/RuriLib/Models/Blocks/Parameters/ListOfStringsParameter.cs index 1989851a4..83897f82d 100644 --- a/RuriLib/Models/Blocks/Parameters/ListOfStringsParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/ListOfStringsParameter.cs @@ -1,33 +1,42 @@ using RuriLib.Models.Blocks.Settings; using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Parameters -{ - public class ListOfStringsParameter : BlockParameter - { - public List DefaultValue { get; set; } +namespace RuriLib.Models.Blocks.Parameters; - public ListOfStringsParameter() - { +/// +/// A parameter of type list of strings. +/// +public class ListOfStringsParameter : BlockParameter +{ + /// + /// The default value of the parameter. + /// + public List DefaultValue { get; set; } = []; - } + /// + public ListOfStringsParameter(string name) : base(name) + { - public ListOfStringsParameter(string name, List defaultValue = null, SettingInputMode inputMode = SettingInputMode.Fixed) - { - Name = name; - DefaultValue = defaultValue ?? new List(); - DefaultVariableName = string.Empty; - InputMode = SettingInputMode.Fixed; - } + } - public ListOfStringsParameter(string name, string defaultVariableName = "") - { - Name = name; - DefaultVariableName = defaultVariableName; - InputMode = SettingInputMode.Variable; - } + /// + public ListOfStringsParameter(string name, List? defaultValue = null, + SettingInputMode inputMode = SettingInputMode.Fixed) : base(name) + { + DefaultValue = defaultValue ?? []; + DefaultVariableName = string.Empty; + InputMode = inputMode; + } - public override BlockSetting ToBlockSetting() - => BlockSettingFactory.CreateListOfStringsSetting(Name, DefaultValue, InputMode, PrettyName, Description); + /// + public ListOfStringsParameter(string name, string defaultVariableName = "") + : base(name) + { + DefaultVariableName = defaultVariableName; + InputMode = SettingInputMode.Variable; } + + /// + public override BlockSetting ToBlockSetting() + => BlockSettingFactory.CreateListOfStringsSetting(Name, DefaultValue, InputMode, PrettyName, Description); } diff --git a/RuriLib/Models/Blocks/Parameters/StringParameter.cs b/RuriLib/Models/Blocks/Parameters/StringParameter.cs index 1e8017122..035010ec8 100644 --- a/RuriLib/Models/Blocks/Parameters/StringParameter.cs +++ b/RuriLib/Models/Blocks/Parameters/StringParameter.cs @@ -1,27 +1,38 @@ using RuriLib.Models.Blocks.Settings; -namespace RuriLib.Models.Blocks.Parameters -{ - public class StringParameter : BlockParameter - { - public string DefaultValue { get; set; } +namespace RuriLib.Models.Blocks.Parameters; - public bool MultiLine { get; set; } = false; +/// +/// A parameter of type string. +/// +public class StringParameter : BlockParameter +{ + /// + /// The default value of the parameter. + /// + public string? DefaultValue { get; set; } - public StringParameter() - { + /// + /// Whether the string should be displayed as a multiline textbox. + /// + public bool MultiLine { get; set; } = false; - } + /// + public StringParameter(string name) : base(name) + { - public StringParameter(string name, string defaultValue = "", SettingInputMode inputMode = SettingInputMode.Fixed) - { - Name = name; - DefaultValue = defaultValue ?? string.Empty; - DefaultVariableName = defaultValue ?? string.Empty; - InputMode = inputMode; - } + } - public override BlockSetting ToBlockSetting() - => BlockSettingFactory.CreateStringSetting(Name, DefaultValue, InputMode, MultiLine, PrettyName, Description); + /// + public StringParameter(string name, + string? defaultValue = "", SettingInputMode inputMode = SettingInputMode.Fixed) + : base(name) + { + DefaultValue = defaultValue ?? string.Empty; + InputMode = inputMode; } + + /// + public override BlockSetting ToBlockSetting() + => BlockSettingFactory.CreateStringSetting(Name, DefaultValue, InputMode, MultiLine, PrettyName, Description); } diff --git a/RuriLib/Models/Blocks/Settings/BlockSetting.cs b/RuriLib/Models/Blocks/Settings/BlockSetting.cs index b5225ae61..be614ef1e 100644 --- a/RuriLib/Models/Blocks/Settings/BlockSetting.cs +++ b/RuriLib/Models/Blocks/Settings/BlockSetting.cs @@ -1,25 +1,52 @@ using RuriLib.Extensions; using RuriLib.Models.Blocks.Settings.Interpolated; -namespace RuriLib.Models.Blocks.Settings -{ - public class BlockSetting - { - public SettingInputMode InputMode { get; set; } = SettingInputMode.Fixed; +namespace RuriLib.Models.Blocks.Settings; - public string Name { get; set; } = string.Empty; +/// +/// A setting of a block. +/// +public class BlockSetting +{ + /// + /// The input mode of the setting. + /// + public SettingInputMode InputMode { get; set; } = SettingInputMode.Fixed; - private string readableName = null; - public string ReadableName - { - // Failsafe in case ReadableName is never set - get => readableName ?? Name.ToReadableName(); - set => readableName = value; - } - public string Description { get; set; } = null; - public string InputVariableName { get; set; } = string.Empty; + /// + /// The name of the setting. + /// + public string Name { get; set; } = string.Empty; - public Setting FixedSetting { get; set; } - public InterpolatedSetting InterpolatedSetting { get; set; } + private string? _readableName; + + /// + /// The readable name of the setting. + /// + public string ReadableName + { + // Failsafe in case ReadableName is never set + get => _readableName ?? Name.ToReadableName(); + set => _readableName = value; } + + /// + /// The description of the setting. + /// + public string? Description { get; set; } + + /// + /// The input variable name of the setting, in case it's in variable mode. + /// + public string InputVariableName { get; set; } = string.Empty; + + /// + /// The fixed setting, in case it's in fixed mode. + /// + public Setting? FixedSetting { get; set; } + + /// + /// The interpolated setting, in case it's in interpolated mode. + /// + public InterpolatedSetting? InterpolatedSetting { get; set; } } diff --git a/RuriLib/Models/Blocks/Settings/BlockSettingFactory.cs b/RuriLib/Models/Blocks/Settings/BlockSettingFactory.cs index 22bc118a1..dc02adb89 100644 --- a/RuriLib/Models/Blocks/Settings/BlockSettingFactory.cs +++ b/RuriLib/Models/Blocks/Settings/BlockSettingFactory.cs @@ -58,10 +58,10 @@ public static BlockSetting CreateEnumSetting(string name, string defaultValue Description = description, ReadableName = readableName ?? name.ToReadableName(), InputMode = SettingInputMode.Fixed, - FixedSetting = new EnumSetting { EnumType = typeof(T), Value = defaultValue } + FixedSetting = new EnumSetting(typeof(T)) { Value = defaultValue } }; - public static BlockSetting CreateStringSetting(string name, string defaultValue = "", + public static BlockSetting CreateStringSetting(string name, string? defaultValue = "", SettingInputMode mode = SettingInputMode.Fixed, bool multiLine = false, string readableName = null, string description = null) => new() @@ -83,8 +83,8 @@ public static BlockSetting CreateStringSetting(string name, string defaultValue } }; - public static BlockSetting CreateListOfStringsSetting(string name, List defaultValue = null, - SettingInputMode mode = SettingInputMode.Fixed, string readableName = null, string description = null) + public static BlockSetting CreateListOfStringsSetting(string name, List? defaultValue = null, + SettingInputMode mode = SettingInputMode.Fixed, string? readableName = null, string? description = null) => new() { Name = name, @@ -93,11 +93,11 @@ public static BlockSetting CreateListOfStringsSetting(string name, List InputMode = mode, InterpolatedSetting = new InterpolatedListOfStringsSetting { - Value = defaultValue ?? new List() + Value = defaultValue ?? [] }, FixedSetting = new ListOfStringsSetting { - Value = defaultValue ?? new List() + Value = defaultValue ?? [] } }; diff --git a/RuriLib/Models/Blocks/Settings/BoolSetting.cs b/RuriLib/Models/Blocks/Settings/BoolSetting.cs index 467c35c19..e40c7b1da 100644 --- a/RuriLib/Models/Blocks/Settings/BoolSetting.cs +++ b/RuriLib/Models/Blocks/Settings/BoolSetting.cs @@ -1,7 +1,12 @@ -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a boolean value. +/// +public class BoolSetting : Setting { - public class BoolSetting : Setting - { - public bool Value { get; set; } - } + /// + /// The value of the setting. + /// + public bool Value { get; set; } } diff --git a/RuriLib/Models/Blocks/Settings/ByteArraySetting.cs b/RuriLib/Models/Blocks/Settings/ByteArraySetting.cs index f1367dc52..31833b592 100644 --- a/RuriLib/Models/Blocks/Settings/ByteArraySetting.cs +++ b/RuriLib/Models/Blocks/Settings/ByteArraySetting.cs @@ -1,7 +1,12 @@ -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a byte array value. +/// +public class ByteArraySetting : Setting { - public class ByteArraySetting : Setting - { - public byte[] Value { get; set; } - } + /// + /// The value of the setting. + /// + public byte[]? Value { get; set; } } diff --git a/RuriLib/Models/Blocks/Settings/DictionaryOfStringsSetting.cs b/RuriLib/Models/Blocks/Settings/DictionaryOfStringsSetting.cs index 08a3faefc..f61189ec4 100644 --- a/RuriLib/Models/Blocks/Settings/DictionaryOfStringsSetting.cs +++ b/RuriLib/Models/Blocks/Settings/DictionaryOfStringsSetting.cs @@ -1,9 +1,14 @@ using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a dictionary of strings. +/// +public class DictionaryOfStringsSetting : Setting { - public class DictionaryOfStringsSetting : Setting - { - public Dictionary Value { get; set; } - } + /// + /// The value of the setting. + /// + public Dictionary Value { get; set; } = []; } diff --git a/RuriLib/Models/Blocks/Settings/EnumSetting.cs b/RuriLib/Models/Blocks/Settings/EnumSetting.cs index 951825612..24175dd2c 100644 --- a/RuriLib/Models/Blocks/Settings/EnumSetting.cs +++ b/RuriLib/Models/Blocks/Settings/EnumSetting.cs @@ -3,41 +3,65 @@ using System.ComponentModel; using System.Linq; -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds an enum value. +/// +public class EnumSetting : Setting { - public class EnumSetting : Setting + private Type _enumType; + private readonly Dictionary _enumValues = []; + + public EnumSetting(Type enumType) { - private Type enumType = null; - public Type EnumType - { - get => enumType; - set + _enumType = enumType; + } + + /// + /// The type of the enum. + /// + public Type EnumType + { + get => _enumType; + set + { + _enumType = value; + + // Populate the enum values dictionary (used to have nicer enum names to display) + foreach (var name in _enumType.GetEnumNames()) { - enumType = value; + var fi = _enumType.GetField(name); - // Populate the enum values dictionary (used to have nicer enum names to display) - foreach (var name in enumType.GetEnumNames()) + if (fi.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attributes && attributes.Any()) { - var fi = enumType.GetField(name); - - if (fi.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attributes && attributes.Any()) - { - enumValues[attributes.First().Description] = name; - } - else - { - enumValues[name] = name; - } + _enumValues[attributes.First().Description] = name; + } + else + { + _enumValues[name] = name; } } } - public string Value { get; set; } - - public IEnumerable PrettyNames => enumValues.Keys; - public string PrettyName => enumValues.First(kvp => kvp.Value == Value).Key; - - private readonly Dictionary enumValues = new(); - - public void SetFromPrettyName(string prettyName) => Value = enumValues[prettyName]; } + + /// + /// The value of the setting. + /// + public string Value { get; set; } + + /// + /// The pretty names of the enum values. + /// + public IEnumerable PrettyNames => _enumValues.Keys; + + /// + /// The pretty name of the current value. + /// + public string PrettyName => _enumValues.First(kvp => kvp.Value == Value).Key; + + /// + /// Sets the value of the setting from a pretty name. + /// + public void SetFromPrettyName(string prettyName) => Value = _enumValues[prettyName]; } diff --git a/RuriLib/Models/Blocks/Settings/FloatSetting.cs b/RuriLib/Models/Blocks/Settings/FloatSetting.cs index 9b58c61f7..7a1cd60d1 100644 --- a/RuriLib/Models/Blocks/Settings/FloatSetting.cs +++ b/RuriLib/Models/Blocks/Settings/FloatSetting.cs @@ -1,7 +1,12 @@ -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a float value. +/// +public class FloatSetting : Setting { - public class FloatSetting : Setting - { - public float Value { get; set; } - } + /// + /// The value of the setting. + /// + public float Value { get; set; } } diff --git a/RuriLib/Models/Blocks/Settings/IntSetting.cs b/RuriLib/Models/Blocks/Settings/IntSetting.cs index 86c067e01..0d60de079 100644 --- a/RuriLib/Models/Blocks/Settings/IntSetting.cs +++ b/RuriLib/Models/Blocks/Settings/IntSetting.cs @@ -1,7 +1,12 @@ -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds an integer value. +/// +public class IntSetting : Setting { - public class IntSetting : Setting - { - public int Value { get; set; } - } + /// + /// The value of the setting. + /// + public int Value { get; set; } } diff --git a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedDictionaryOfStringsSetting.cs b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedDictionaryOfStringsSetting.cs index 4adcad6d2..3c98619f3 100644 --- a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedDictionaryOfStringsSetting.cs +++ b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedDictionaryOfStringsSetting.cs @@ -1,9 +1,14 @@ using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Settings.Interpolated +namespace RuriLib.Models.Blocks.Settings.Interpolated; + +/// +/// A setting that holds an interpolated dictionary of strings. +/// +public class InterpolatedDictionaryOfStringsSetting : InterpolatedSetting { - public class InterpolatedDictionaryOfStringsSetting : InterpolatedSetting - { - public Dictionary Value { get; set; } = new Dictionary(); - } + /// + /// The value of the setting. + /// + public Dictionary Value { get; set; } = []; } diff --git a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedListOfStringsSetting.cs b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedListOfStringsSetting.cs index 4d6653385..c85dc8e2b 100644 --- a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedListOfStringsSetting.cs +++ b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedListOfStringsSetting.cs @@ -1,9 +1,14 @@ using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Settings.Interpolated +namespace RuriLib.Models.Blocks.Settings.Interpolated; + +/// +/// A setting that holds an interpolated list of strings. +/// +public class InterpolatedListOfStringsSetting : InterpolatedSetting { - public class InterpolatedListOfStringsSetting : InterpolatedSetting - { - public List Value { get; set; } = new List(); - } + /// + /// The value of the setting. + /// + public List Value { get; set; } = []; } diff --git a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedStringSetting.cs b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedStringSetting.cs index ce4255164..4b652f7fa 100644 --- a/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedStringSetting.cs +++ b/RuriLib/Models/Blocks/Settings/Interpolated/InterpolatedStringSetting.cs @@ -1,8 +1,17 @@ -namespace RuriLib.Models.Blocks.Settings.Interpolated +namespace RuriLib.Models.Blocks.Settings.Interpolated; + +/// +/// A setting that holds an interpolated string value. +/// +public class InterpolatedStringSetting : InterpolatedSetting { - public class InterpolatedStringSetting : InterpolatedSetting - { - public string Value { get; set; } = string.Empty; - public bool MultiLine { get; set; } = false; - } + /// + /// The value of the setting. + /// + public string? Value { get; set; } = string.Empty; + + /// + /// Whether the string should be displayed as a multiline textbox. + /// + public bool MultiLine { get; set; } = false; } diff --git a/RuriLib/Models/Blocks/Settings/ListOfStringsSetting.cs b/RuriLib/Models/Blocks/Settings/ListOfStringsSetting.cs index c124d2798..3823b948c 100644 --- a/RuriLib/Models/Blocks/Settings/ListOfStringsSetting.cs +++ b/RuriLib/Models/Blocks/Settings/ListOfStringsSetting.cs @@ -1,9 +1,14 @@ using System.Collections.Generic; -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a list of strings. +/// +public class ListOfStringsSetting : Setting { - public class ListOfStringsSetting : Setting - { - public List Value { get; set; } - } + /// + /// The value of the setting. + /// + public List Value { get; set; } = []; } diff --git a/RuriLib/Models/Blocks/Settings/StringSetting.cs b/RuriLib/Models/Blocks/Settings/StringSetting.cs index 122eaf02e..f15777761 100644 --- a/RuriLib/Models/Blocks/Settings/StringSetting.cs +++ b/RuriLib/Models/Blocks/Settings/StringSetting.cs @@ -1,8 +1,17 @@ -namespace RuriLib.Models.Blocks.Settings +namespace RuriLib.Models.Blocks.Settings; + +/// +/// A setting that holds a string value. +/// +public class StringSetting : Setting { - public class StringSetting : Setting - { - public string Value { get; set; } - public bool MultiLine { get; set; } = false; - } + /// + /// The value of the setting. + /// + public string? Value { get; set; } + + /// + /// Whether the string should be displayed as a multiline textbox. + /// + public bool MultiLine { get; set; } = false; }