From a79ad615faab3ec850977914e3492e96ccb45056 Mon Sep 17 00:00:00 2001 From: NostraThomas99 <34015422+NostraThomas99@users.noreply.github.com> Date: Mon, 27 May 2024 00:21:33 -0500 Subject: [PATCH] Improve error handling and value parsing in RSCommands_OtherCommand.cs Refactored the value parsing in RSCommands_OtherCommand to improve handling of enum and boolean types. Added functionality for incrementing invalid enum values to the next valid value. Improved usability by displaying changes made to the user. --- .gitignore | 3 +- .../Commands/RSCommands_OtherCommand.cs | 42 ++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 653bee16a..e8f7642cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vs/ bin/ obj/ -lib/ \ No newline at end of file +lib/ +.idea/ \ No newline at end of file diff --git a/RotationSolver/Commands/RSCommands_OtherCommand.cs b/RotationSolver/Commands/RSCommands_OtherCommand.cs index 473d665a4..5202e3df2 100644 --- a/RotationSolver/Commands/RSCommands_OtherCommand.cs +++ b/RotationSolver/Commands/RSCommands_OtherCommand.cs @@ -42,7 +42,6 @@ private static void DoSettingCommand(string str) var strs = str.Split(' '); var settingName = strs[0]; var value = strs.LastOrDefault(); - foreach (var property in typeof(Configs).GetRuntimeProperties().Where(p => p.GetMethod?.IsPublic ?? false)) { if (!settingName.Equals(property.Name, StringComparison.OrdinalIgnoreCase)) @@ -53,22 +52,32 @@ private static void DoSettingCommand(string str) type = typeof(bool); object convertedValue = null; - try + bool valueParsedSuccessfully = true; + + if (type.IsEnum) { - if (type.IsEnum) + valueParsedSuccessfully = Enum.TryParse(type, value, ignoreCase: true, out var parsedEnum); + if (valueParsedSuccessfully) { - convertedValue = Enum.Parse(type, value, ignoreCase: true); + convertedValue = parsedEnum; } - else + } + else + { + try { convertedValue = Convert.ChangeType(value, type); } + catch + { + valueParsedSuccessfully = false; + } } - catch + + if (!valueParsedSuccessfully) { if (type == typeof(bool)) { - // Toggle the boolean value if no value is specified var config = property.GetValue(Service.Config) as ConditionBoolean; if (config != null) { @@ -76,6 +85,12 @@ private static void DoSettingCommand(string str) convertedValue = config.Value; } } + else if (type.IsEnum) + { + // If invalid enum value provided - increment to the next enum value + var currentEnumValue = (Enum)property.GetValue(Service.Config); + convertedValue = GetNextEnumValue(currentEnumValue); + } } if (convertedValue == null) @@ -86,7 +101,6 @@ private static void DoSettingCommand(string str) continue; } - // If it's a ConditionBoolean, handle it specifically if (property.PropertyType == typeof(ConditionBoolean)) { var relay = (ConditionBoolean)property.GetValue(Service.Config)!; @@ -97,14 +111,22 @@ private static void DoSettingCommand(string str) property.SetValue(Service.Config, convertedValue); value = convertedValue.ToString(); - // Notify the user of the change Svc.Chat.Print(string.Format(UiString.CommandsChangeSettingsValue.Local(), property.Name, value)); + return; } Svc.Chat.PrintError(UiString.CommandsCannotFindConfig.Local()); } + private static Enum GetNextEnumValue(Enum currentEnumValue) + { + var enumValues = Enum.GetValues(currentEnumValue.GetType()).Cast().ToArray(); + var nextIndex = Array.IndexOf(enumValues, currentEnumValue) + 1; + + return enumValues.Length == nextIndex ? enumValues[0] : enumValues[nextIndex]; + } + private static void ToggleActionCommand(string str) { foreach (var act in RotationUpdater.RightRotationActions) @@ -172,4 +194,4 @@ private static void DoRotationCommand(ICustomRotation customCombo, string str) Svc.Chat.PrintError(UiString.CommandsInsertActionFailure.Local()); } -} +} \ No newline at end of file