Skip to content

Commit

Permalink
Refactor Skill Upgrade collection to the ASU class
Browse files Browse the repository at this point in the history
  • Loading branch information
flibber-hk committed Jan 14, 2022
1 parent e3baed1 commit 83a76c4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SkillUpgrades/SkillUpgradeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static SkillUpgradeSettings()
{
Fields = new Dictionary<string, FieldInfo>();

foreach (Type t in typeof(SkillUpgradeSettings).Assembly.GetTypesSafely().Where(t => t.IsSubclassOf(typeof(AbstractSkillUpgrade)) && !t.IsAbstract))
foreach (Type t in AbstractSkillUpgrade.GetAvailableSkillUpgradeTypes())
{
foreach (FieldInfo fi in t.GetFields(BindingFlags.Public | BindingFlags.Static))
{
Expand Down
2 changes: 1 addition & 1 deletion SkillUpgrades/SkillUpgrades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Initialize()
instance.Log("Initializing");
DebugMod.AddActionToKeyBindList(() => { ApplyGlobalToggle(!GS.GlobalToggle); RefreshMainMenu(); }, "Global Toggle", "SkillUpgrades");

foreach (Type t in typeof(SkillUpgrades).Assembly.GetTypesSafely().Where(x => x.IsSubclassOf(typeof(AbstractSkillUpgrade))))
foreach (Type t in AbstractSkillUpgrade.GetAvailableSkillUpgradeTypes())
{
AbstractSkillUpgrade skill = (AbstractSkillUpgrade)Activator.CreateInstance(t);
_skills.Add(skill.Name, skill);
Expand Down
2 changes: 1 addition & 1 deletion SkillUpgrades/SkillUpgrades.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net472</TargetFramework>
<RootNamespace>SkillUpgrades</RootNamespace>
<AssemblyTitle>SkillUpgrades</AssemblyTitle>
<AssemblyVersion>0.9.0</AssemblyVersion>
<AssemblyVersion>0.9.2</AssemblyVersion>
<Deterministic>true</Deterministic>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<LangVersion>latest</LangVersion>
Expand Down
22 changes: 21 additions & 1 deletion SkillUpgrades/Skills/AbstractSkillUpgrade.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Modding;
using SkillUpgrades.Util;
Expand Down Expand Up @@ -135,5 +138,22 @@ protected virtual string FormatLogMessage(string message)
}
private string FormatLogMessage(object message) => FormatLogMessage(message?.ToString() ?? "null");
#endregion

/// <summary>
/// Returns an enumerable over the Skill Upgrades in this assembly.
/// </summary>
public static IEnumerable<Type> GetAvailableSkillUpgradeTypes()
{
Assembly asm = typeof(AbstractSkillUpgrade).Assembly;
// TODO - take GetTypesSafely from MAPI when I can
try
{
return asm.GetTypes().Where(type => type.IsSubclassOf(typeof(AbstractSkillUpgrade)) && !type.IsAbstract);
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(type => type is not null && type.IsSubclassOf(typeof(AbstractSkillUpgrade)) && !type.IsAbstract);
}
}
}
}

0 comments on commit 83a76c4

Please sign in to comment.