Skip to content

Commit

Permalink
Core: ClassConfiguration: GetByType returns with IEnumerable - added …
Browse files Browse the repository at this point in the history
…GetByTypeAsList as overload
  • Loading branch information
Xian55 committed Jan 18, 2025
1 parent 0213304 commit 94a2159
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
21 changes: 13 additions & 8 deletions Core/ClassConfig/ClassConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void Initialise(IServiceProvider sp, Dictionary<int, string> overridePath
SetBaseActions(Combat,
Interact, Approach, AutoAttack, StopAttack, PetAttack);

var groups = GetByType<KeyActions>();
var groups = GetByTypeAsList<KeyActions>();

foreach ((string name, KeyActions keyActions) in groups)
{
Expand Down Expand Up @@ -268,20 +268,25 @@ private static void SetBaseActions(
}
}

public List<(string name, T)> GetByType<T>()
public IEnumerable<(string name, T)> GetByType<T>()
{
return GetType()
.GetProperties(BindingFlags.Instance |
BindingFlags.Public | BindingFlags.FlattenHierarchy)
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)
.Where(OfType)
.Select(pInfo =>
{
return (pInfo.Name, (T)pInfo.GetValue(this)!);
})
.ToList();
});

static bool OfType(PropertyInfo pInfo) =>
typeof(T).IsAssignableFrom(pInfo.PropertyType);
static bool OfType(PropertyInfo pInfo)
{
return typeof(T).IsAssignableFrom(pInfo.PropertyType);
}
}

public List<(string name, T)> GetByTypeAsList<T>()
{
return GetByType<T>().ToList();
}

[LoggerMessage(
Expand Down
32 changes: 13 additions & 19 deletions Core/Requirement/RequirementFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public sealed partial class RequirementFactory

private readonly FrozenDictionary<int, SchoolMask> npcSchoolImmunity;

private static readonly string[] negateKeywords =
[
"not ",
SymbolNegate
];

private readonly SearchValues<char> negateKeywordsSpan = SearchValues.Create(['!', 'n', 'o', 't', ' ']);

private readonly Dictionary<string, Func<int>> intVariables;
Expand Down Expand Up @@ -415,22 +409,22 @@ public void InitUserDefinedIntVariables(Dictionary<string, int> intKeyValues,
throw new Exception($"Unable to add user defined variable to values. [{key} -> {value}]");
}

if (key.StartsWith("Buff_"))
if (key.StartsWith("Buff_", StringComparison.InvariantCultureIgnoreCase))
{
int l() => playerBuffTimeReader.GetRemainingTimeMs(value);
intVariables.TryAdd($"{value}", l);
}
else if (key.StartsWith("Debuff_"))
else if (key.StartsWith("Debuff_", StringComparison.InvariantCultureIgnoreCase))
{
int l() => targetDebuffTimeReader.GetRemainingTimeMs(value);
intVariables.TryAdd($"{value}", l);
}
else if (key.StartsWith("TBuff_"))
else if (key.StartsWith("TBuff_", StringComparison.InvariantCultureIgnoreCase))
{
int l() => targetBuffTimeReader.GetRemainingTimeMs(value);
intVariables.TryAdd($"{value}", l);
}
else if (key.StartsWith("FBuff_"))
else if (key.StartsWith("FBuff_", StringComparison.InvariantCultureIgnoreCase))
{
int l() => focusBuffTimeReader.GetRemainingTimeMs(value);
intVariables.TryAdd($"{value}", l);
Expand Down Expand Up @@ -706,9 +700,9 @@ public Requirement CreateRequirement(ReadOnlySpan<char> requirement)

requirement = requirement[negateLength..];

string requirentStr = requirement.ToString();
string requirementStr = requirement.ToString();

string? key = requirementMap.Keys.FirstOrDefault(requirentStr.Contains);
string? key = requirementMap.Keys.FirstOrDefault(requirementStr.Contains);
if (!string.IsNullOrEmpty(key) && requirementMap.TryGetValue(key, out var createRequirement))
{
Requirement r = createRequirement(requirement);
Expand All @@ -722,14 +716,14 @@ public Requirement CreateRequirement(ReadOnlySpan<char> requirement)
var spanLookupBool = boolVariables.GetAlternateLookup<ReadOnlySpan<char>>();
if (!spanLookupBool.TryGetValue(requirement, out Func<bool>? value))
{
LogUnknown(logger, requirentStr, string.Join(", ", boolVariables.Keys));
LogUnknown(logger, requirementStr, string.Join(", ", boolVariables.Keys));
return new Requirement
{
LogMessage = () => $"UNKNOWN REQUIREMENT! {requirentStr}"
LogMessage = () => $"UNKNOWN REQUIREMENT! {requirementStr}"
};
}

string s() => requirentStr;
string s() => requirementStr;
Requirement req = new()
{
HasRequirement = value,
Expand Down Expand Up @@ -1096,7 +1090,7 @@ private Requirement CreateUsable(ReadOnlySpan<char> requirement)
int sep = requirement.IndexOf(SEP1);
ReadOnlySpan<char> name = requirement[(sep + 1)..].Trim();

List<(string _, KeyActions)> groups = classConfig.GetByType<KeyActions>();
var groups = classConfig.GetByType<KeyActions>();

foreach ((string _, KeyActions keyActions) in groups)
{
Expand All @@ -1111,7 +1105,7 @@ private Requirement CreateUsable(ReadOnlySpan<char> requirement)
}

throw new InvalidOperationException($"'{requirement}' " +
$"related named '{name}' {nameof(Core.KeyAction)} not found!");
$"related named '{name}' {nameof(KeyAction)} not found!");
}

private Requirement CreateCanRun(ReadOnlySpan<char> requirement)
Expand All @@ -1120,7 +1114,7 @@ private Requirement CreateCanRun(ReadOnlySpan<char> requirement)
int sep = requirement.IndexOf(SEP1);
ReadOnlySpan<char> name = requirement[(sep + 1)..].Trim();

List<(string _, KeyActions)> groups = classConfig.GetByType<KeyActions>();
var groups = classConfig.GetByType<KeyActions>();

foreach ((string _, KeyActions keyActions) in groups)
{
Expand All @@ -1134,7 +1128,7 @@ private Requirement CreateCanRun(ReadOnlySpan<char> requirement)
}

throw new InvalidOperationException($"'{requirement}' " +
$"related named '{name}' {nameof(Core.KeyAction)} not found!");
$"related named '{name}' {nameof(KeyAction)} not found!");
}


Expand Down

0 comments on commit 94a2159

Please sign in to comment.