Skip to content

Commit

Permalink
Merge pull request #757 from erdelf/master
Browse files Browse the repository at this point in the history
cfg adjustments
  • Loading branch information
erdelf authored Jan 11, 2025
2 parents 8f0ac67 + 62585eb commit 541a8f1
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 56 deletions.
15 changes: 13 additions & 2 deletions AutoDuty/AutoDuty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
namespace AutoDuty;

using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Data;
using ECommons.Reflection;
using FFXIVClientStructs.FFXIV.Client.UI;
Expand Down Expand Up @@ -1554,7 +1555,17 @@ public void Dispose()
private unsafe void OnCommand(string command, string args)
{
// in response to the slash command
var argsArray = args.ToLower().Split(" ");
Match match = RegexHelper.ArgumentParserRegex().Match(args.ToLower());
List<string> matches = [];

while (match.Success)
{
matches.Add(match.Groups[match.Groups[1].Length > 0 ? 1 : 0].Value);
match = match.NextMatch();
}

string[] argsArray = matches.ToArray();

switch (argsArray[0])
{
case "config" or "cfg":
Expand All @@ -1563,7 +1574,7 @@ private unsafe void OnCommand(string command, string args)
else if (argsArray[1].Equals("list"))
ConfigHelper.ListConfig();
else
ConfigHelper.ModifyConfig(argsArray[1], argsArray[2]);
ConfigHelper.ModifyConfig(argsArray[1], argsArray[2..]);
break;
case "start":
StartNavigation();
Expand Down
177 changes: 123 additions & 54 deletions AutoDuty/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace AutoDuty.Helpers
{
using System.Collections;
using FFXIVClientStructs.FFXIV.Common.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

internal static class ConfigHelper
{
Expand All @@ -21,88 +25,153 @@ internal static string GetConfig(string configName)
Svc.Log.Error($"Unable to find config: {configName}, please type /autoduty cfg list to see all available configs");
return string.Empty;
}
else if (field.FieldType.ToString().Contains("System.Collections", StringComparison.InvariantCultureIgnoreCase) || field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase))
else if (field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase))
return string.Empty;
else
return field.GetValue(Plugin.Configuration)?.ToString() ?? string.Empty;
}

internal static bool ModifyConfig(string configName, string configValue)
private static object? ModifyConfig(Type configType, string configValue, out string failReason)
{
failReason = $"value must be of type: {configType.ToString().Replace("System.", "")}";

if (configType == typeof(string))
return configValue;
else if (configType.IsEnum)
{
if (Enum.TryParse(configType, configValue, true, out object? configEnum))
return configEnum;
} else if (configType is { IsGenericType: true, IsGenericTypeDefinition: false } && configType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return Activator.CreateInstance(configType, ModifyConfig(configType.GetGenericArguments()[0], configValue, out failReason));
}
else if (configType.GetInterface(nameof(IConvertible)) != null)
{
return Convert.ChangeType(configValue, configType, CultureInfo.InvariantCulture);
}


return null;
}

internal static bool ModifyConfig(string configName, params string[] configValues)
{

FieldInfo? field;
if ((field = FindConfig(configName)) == null)
{
Svc.Log.Error($"Unable to find config: {configName}, please type /autoduty cfg list to see all available configs");
return false;
}
else if (field.FieldType.ToString().Contains("System.Collections", StringComparison.InvariantCultureIgnoreCase) || field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase))
else if (field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase))
return false;
else
{
var configType = field.FieldType;// ConfigType(field);

if (configType == typeof(string))
{
field.SetValue(Plugin.Configuration, configValue);
} else if (configType.IsEnum)
void PrintError(string failReason)
{
if(Enum.TryParse(configType, configValue, true, out object? configEnum))
{
field.SetValue(Plugin.Configuration, configEnum);
} else
{
Svc.Log.Error($"Unable to set config setting: {field.Name.Replace(">k__BackingField", "").Replace("<", "")}, value must be of type: {field.FieldType.ToString().Replace("System.", "")}");
return false;
}
Svc.Log.Error($"Unable to set config setting: {field.Name.Replace(" > k__BackingField", "").Replace(" < ", "")}: {failReason}");
}
else if (configType == typeof(TrustMemberName?[]))

var configType = field.FieldType;// ConfigType(field);

if (configType.IsAssignableTo(typeof(IList)))
{
string[] memberNames = configValue.Split(",");
IList valueList = (IList) field.GetValue(Plugin.Configuration)!;
Type enumerableType = configType.GetElementType() ?? configType.GenericTypeArguments.First();

if (memberNames.Length > 3)
switch (configValues[0])
{
Svc.Log.Error("Unable to set more than 3 trust members");
return false;
}
case "set":
// /ad cfg SelectedTrustMembers set Yshtola Graha Thancred
// /ad cfg CustomCommandsTermination set "/snd run Wep" "/snd run Leveling"

List<TrustMemberName> members = [];
if (!valueList.IsFixedSize)
valueList.Clear();

foreach (string memberName in memberNames)
if (Enum.TryParse(typeof(TrustMemberName), memberName, true, out object? member))
members.Add((TrustMemberName)member);
for (int i = 1; i < configValues.Length; i++)
{
object? val = ModifyConfig(enumerableType, configValues[i], out _);
if (val != null)
if (!valueList.IsFixedSize)
valueList.Add(val);
else
valueList[i] = val;
}
break;
case "add":
// /ad cfg CustomCommandsTermination add "/test"

if (members.Count <= 0)
{
Svc.Log.Error("No trust members recognized");
return false;
}
if (valueList.IsFixedSize)
PrintError("Can't use add on fixed size configs");

TrustMemberName?[] value = (TrustMemberName?[]) field.GetValue(Plugin.Configuration);
foreach (string t in configValues[1..])
{
object? val = ModifyConfig(enumerableType, t, out _);
if (val != null)
valueList.Add(val);
}
break;
case "del":
case "delete":
case "rem":
case "remove":
// /ad cfg CustomCommandsTermination del 2 1

for (int i = 0; i < members.Count; i++)
{
TrustMemberName member = members[i];
value[i] = member;
}
field.SetValue(Plugin.Configuration, value);
}
else if(configType.GetInterface(nameof(IConvertible)) != null)
{
object newConfigValue = Convert.ChangeType(configValue, configType, CultureInfo.InvariantCulture);
if(newConfigValue != null)
{
field.SetValue(Plugin.Configuration, newConfigValue);
}
else
{
Svc.Log.Error($"Unable to set config setting: {field.Name.Replace(">k__BackingField", "").Replace("<", "")}, value must be of type: {field.FieldType.ToString().Replace("System.", "")}");
return false;
if (valueList.IsFixedSize)
PrintError("Can't use delete on fixed size configs");

for (int i = 1; i < configValues.Length; i++)
{
if (int.TryParse(configValues[i], out int index))
valueList.RemoveAt(index);
}
break;
case "delentry":
case "deleteentry":
case "rementry":
case "removeentry":
// /ad cfg CustomCommandsTermination delEntry /test

if (valueList.IsFixedSize)
PrintError("Can't use delete on fixed size configs");

for (int i = 1; i < configValues.Length; i++)
{
object? entry = ModifyConfig(enumerableType, configValues[i], out string _);

if (entry != null)
{
int index = valueList.IndexOf(entry);
if (i >= 0)
valueList.RemoveAt(index);
}
}
break;
case "insert":
// /ad cfg CustomCommandsTermination insert 1 "/test 1" "/test 2"
if (valueList.IsFixedSize)
PrintError("Can't use insert on fixed size configs");

if (int.TryParse(configValues[1], out int insertIndex))
for (int i = 2; i < configValues.Length; i++)
{
object? entry = ModifyConfig(enumerableType, configValues[i], out string _);

if (entry != null)
valueList.Insert(insertIndex++, entry);
}

break;
}
}
else
{
Svc.Log.Error($"Unable to set config setting: {field.Name.Replace(">k__BackingField", "").Replace("<", "")}, value must be of type: {field.FieldType.ToString().Replace("System.", "")}");
return false;
object? newValue = ModifyConfig(configType, configValues[0], out string failReason);

if (newValue != null)
field.SetValue(Plugin.Configuration, newValue);
else
PrintError(failReason);
}

Plugin.Configuration.Save();
Expand All @@ -116,7 +185,7 @@ internal static void ListConfig()
if (i == null) return;
foreach (var field in i)
{
if (!field.FieldType.ToString().Contains("System.Collections",StringComparison.InvariantCultureIgnoreCase) && !field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase) && !field.Name.Replace(">k__BackingField", "").Replace("<", "").Equals("Version",StringComparison.InvariantCultureIgnoreCase))
if (!field.FieldType.ToString().Contains("Dalamud.Plugin", StringComparison.InvariantCultureIgnoreCase) && !field.Name.Replace(">k__BackingField", "").Replace("<", "").Equals("Version",StringComparison.InvariantCultureIgnoreCase))
Svc.Log.Info($"{field.Name.Replace(">k__BackingField", "").Replace("<", "")} = {field.GetValue(Plugin.Configuration)} ({field.FieldType.ToString().Replace("System.", "")})");
}
}
Expand Down
3 changes: 3 additions & 0 deletions AutoDuty/Helpers/RegExHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public static partial class RegexHelper

[GeneratedRegex(@"([0-9]{3,})", RegexOptions.CultureInvariant)]
public static partial Regex ObjectIdRegex();

[GeneratedRegex(@"""([^""]+)""|\S+", RegexOptions.CultureInvariant)]
public static partial Regex ArgumentParserRegex();
}

public static class PathIdentifiers
Expand Down

0 comments on commit 541a8f1

Please sign in to comment.