Skip to content

Commit

Permalink
Merge pull request #163 from FFXIV-CombatReborn/fix-aoe-toggle
Browse files Browse the repository at this point in the history
Improve error handling and value parsing in RSCommands_OtherCommand.cs
  • Loading branch information
NostraThomas99 authored May 27, 2024
2 parents a72fc4c + a79ad61 commit 31c9985
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vs/
bin/
obj/
lib/
lib/
.idea/
42 changes: 32 additions & 10 deletions RotationSolver/Commands/RSCommands_OtherCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -53,29 +52,45 @@ 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)
{
config.Value = !config.Value;
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)
Expand All @@ -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)!;
Expand All @@ -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<Enum>().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)
Expand Down Expand Up @@ -172,4 +194,4 @@ private static void DoRotationCommand(ICustomRotation customCombo, string str)

Svc.Chat.PrintError(UiString.CommandsInsertActionFailure.Local());
}
}
}

0 comments on commit 31c9985

Please sign in to comment.