Skip to content

Commit eceb862

Browse files
committed
Some more refactoring
1 parent c69fc6f commit eceb862

File tree

2 files changed

+7
-44
lines changed

2 files changed

+7
-44
lines changed

src/CommunityFixes/CommunityFixesMod.cs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void Awake()
3737

3838
foreach (var type in types)
3939
{
40-
if (type.IsAbstract || !HasFixType(type))
40+
if (type.IsAbstract || !type.IsSubclassOf(typeof(BaseFix)))
4141
{
4242
continue;
4343
}
@@ -66,9 +66,7 @@ public override void OnInitialized()
6666

6767
private bool LoadFix(Type type)
6868
{
69-
var fixName = GetFixName(type);
70-
71-
if (!Config.IsFixEnabled(type, fixName))
69+
if (!Config.IsFixEnabled(type))
7270
{
7371
return false;
7472
}
@@ -84,40 +82,4 @@ private bool LoadFix(Type type)
8482

8583
return true;
8684
}
87-
88-
private static string GetFixName(Type type)
89-
{
90-
var attributes = Attribute.GetCustomAttributes(type);
91-
foreach (var attribute in attributes)
92-
{
93-
if (attribute is FixAttribute fix)
94-
{
95-
return fix.Name;
96-
}
97-
}
98-
99-
throw new Exception($"The attribute {typeof(FixAttribute).FullName} has to be declared on a fix class.");
100-
}
101-
102-
private static bool HasFixType(Type type)
103-
{
104-
if (type == null)
105-
{
106-
return false;
107-
}
108-
109-
// return all inherited types
110-
var currentBaseType = type.BaseType;
111-
while (currentBaseType != null)
112-
{
113-
if (currentBaseType == typeof(BaseFix))
114-
{
115-
return true;
116-
}
117-
118-
currentBaseType = currentBaseType.BaseType;
119-
}
120-
121-
return false;
122-
}
12385
}

src/CommunityFixes/Configuration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BepInEx.Configuration;
2+
using CommunityFixes.Fix;
23

34
namespace CommunityFixes;
45

@@ -22,9 +23,8 @@ public Configuration(ConfigFile file)
2223
/// Gets the toggle value for a fix class.
2324
/// </summary>
2425
/// <param name="type">Type of the fix class.</param>
25-
/// <param name="name">Name of the fix class.</param>
26-
/// <returns></returns>
27-
public bool IsFixEnabled(Type type, string name)
26+
/// <returns>The toggle value for the fix class.</returns>
27+
public bool IsFixEnabled(Type type)
2828
{
2929
// If the toggle value for a fix class is already defined, we return it
3030
if (_fixesEnabled.TryGetValue(type, out var isEnabled))
@@ -33,7 +33,8 @@ public bool IsFixEnabled(Type type, string name)
3333
}
3434

3535
// Otherwise create a new config entry for the fix class and return its default value (true)
36-
var configEntry = _file.Bind(TogglesSection, type.Name, true, name);
36+
var metadata = FixAttribute.GetForType(type);
37+
var configEntry = _file.Bind(TogglesSection, type.Name, true, metadata.Name);
3738
_fixesEnabled.Add(type, configEntry);
3839
return configEntry.Value;
3940
}

0 commit comments

Comments
 (0)