Skip to content

Commit

Permalink
More settings!
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed May 16, 2024
1 parent 1e71e8f commit 7fb0714
Show file tree
Hide file tree
Showing 27 changed files with 543 additions and 244 deletions.
12 changes: 11 additions & 1 deletion .idea/.idea.ConfigurableWarning/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions ConfigurableWarning/Source/API/Attributes/CompatGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using JetBrains.Annotations;

namespace ConfigurableWarning.API.Attributes;

/// <summary>
/// Registers any contained options as a part of this category.
/// This is specifically for compat modules.
/// </summary>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class CompatGroup : Attribute {
/// <summary>
/// The category.
/// </summary>
public readonly string Category;

/// <summary>
/// The tab. If this is null, it will default to the parent's
/// tab.
/// </summary>
public readonly string? Tab;

/// <summary>
/// Registers any contained options as a part of this category.
/// This is specifically for compat modules.
/// </summary>
/// <param name="category">The category to register to.</param>
public CompatGroup(string category) {
Category = category;
Tab = null;
}

/// <summary>
/// Registers any contained options as a part of this category,
/// overriding the parent's tab.
/// This is specifically for compat modules.
/// </summary>
/// <param name="tab">The tab</param>
/// <param name="category">The category</param>
public CompatGroup(string tab, string category) {
Tab = tab;
Category = category;
}
}
25 changes: 25 additions & 0 deletions ConfigurableWarning/Source/API/Attributes/CompatTab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using JetBrains.Annotations;

namespace ConfigurableWarning.API.Attributes;

/// <summary>
/// Registers any contained groups as a part of this tab.
/// This is specifically for compat modules.
/// </summary>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class CompatTab : Attribute {
/// <summary>
/// The tab.
/// </summary>
public readonly string? Name;

/// <summary>
/// Registers any contained groups as a part of this tab.
/// This is specifically for compat modules.
/// </summary>
public CompatTab(string tab) {
Name = tab;
}
}
42 changes: 42 additions & 0 deletions ConfigurableWarning/Source/API/Attributes/Group.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using JetBrains.Annotations;

namespace ConfigurableWarning.API.Attributes;

/// <summary>
/// Registers any contained options as a part of this category.
/// </summary>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class Group : Attribute {
/// <summary>
/// The category.
/// </summary>
public readonly string Category;

/// <summary>
/// The tab. If this is null, it will default to the parent's
/// tab.
/// </summary>
public readonly string? Tab;

/// <summary>
/// Registers any contained options as a part of this category.
/// </summary>
/// <param name="category">The category to register to.</param>
public Group(string category) {
Category = category;
Tab = null;
}

/// <summary>
/// Registers any contained options as a part of this category,
/// overriding the parent's tab.
/// </summary>
/// <param name="tab">The tab</param>
/// <param name="category">The category</param>
public Group(string tab, string category) {
Tab = tab;
Category = category;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using JetBrains.Annotations;

namespace ConfigurableWarning.API;
namespace ConfigurableWarning.API.Attributes;

/// <summary>
/// Automatically registers the option. This will initialize it in the state,
Expand All @@ -10,4 +10,4 @@ namespace ConfigurableWarning.API;
/// </summary>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class RegisterOption : Attribute;
public class Register : Attribute;
24 changes: 24 additions & 0 deletions ConfigurableWarning/Source/API/Attributes/Tab.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using JetBrains.Annotations;

namespace ConfigurableWarning.API.Attributes;

/// <summary>
/// Registers any contained groups as a part of this tab.
/// </summary>
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class Tab : Attribute {
/// <summary>
/// The tab.
/// </summary>
public readonly string? Name;

/// <summary>
/// Registers any contained groups as a part of this tab.
/// </summary>
/// <param name="tab">The tab.</param>
public Tab(string tab) {
Name = tab;
}
}
91 changes: 76 additions & 15 deletions ConfigurableWarning/Source/API/Compat/CompatLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ConfigurableWarning.API.Options;
using Zorro.Settings;
using ConfigurableWarning.API.Attributes;

namespace ConfigurableWarning.API.Compat;

/// <summary>
/// A loader for compat modules.
/// </summary>
public static class CompatLoader {
internal static Dictionary<Type, ICompatModule> RegisteredModules { get; } = new();
/// <summary>
/// A list of all registered compat modules.
/// </summary>
public static readonly Dictionary<Type, ICompatModule> RegisteredModules = new();

/// <summary>
/// A list of all registered compat tabs.
/// </summary>
public static readonly Dictionary<Type, CompatTab> RegisteredTabs = new();

internal static bool NamespaceExists(string ns) {
/// <summary>
/// A list of all registered compat groups.
/// </summary>
public static readonly Dictionary<Type, CompatGroup> RegisteredGroups = new();

/// <summary>
/// Checks if a namespace exists.
/// </summary>
/// <param name="ns">The namespace.</param>
/// <returns>If it exists.</returns>
public static bool NamespaceExists(string ns) {
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes().Select(t => t.Namespace))
.Where(v => v != null)
Expand Down Expand Up @@ -49,24 +66,68 @@ public static void LoadModules() {
Plugin.Logger.LogInfo($"Initializing compat module: {type.Name}");

foreach (var ty in type.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public)) {
Plugin.Logger.LogInfo($"Checking compat setting: {ty.Name} (from {ty.Assembly.GetName().Name}.dll");
TryRegisterCompatTab(ty);
TryRegisterCompatGroup(null, ty);
}

if (ty.IsInterface || ty.IsAbstract || !ty.IsSubclassOf(typeof(Setting))) continue;
var it = (ICompatModule) Activator.CreateInstance(type);

var attr = ty.GetCustomAttribute<CompatSetting>(false);
it.Init();
RegisteredModules[type] = it;
}
}

if (attr == null) continue;
/// <summary>
/// Try to register a compat group from a type.
/// </summary>
/// <param name="tab">The tab</param>
/// <param name="type">The type</param>
/// <returns>If it could be registered</returns>
public static bool TryRegisterCompatGroup(string? tab, Type type) {
if (RegisteredGroups.ContainsKey(type)) return false;

Plugin.Logger.LogInfo(
$"[COMPAT] Initializing option: {ty.Name} (from {ty.Assembly.GetName().Name}.dll");
var group = type.GetCustomAttribute<CompatGroup>(false);
if (group == null) return false;

OptionLoader.RegisteredOptions[ty] = (IUntypedOption) Activator.CreateInstance(ty);
}
Plugin.Logger.LogInfo($"Found compat group {group.Category} in {type.FullName}");

var it = (ICompatModule) Activator.CreateInstance(type);
if (tab == null && group.Tab == null) {
Plugin.Logger.LogError("Cannot register a CompatGroup with no tab!");
return false;
}

it.Init();
RegisteredModules[type] = it;
var settings = type.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public);

foreach (var setting in settings) OptionLoader.TryRegisterSetting(tab ?? group.Tab!, group.Category, setting);

RegisteredGroups.Add(type, group);

return true;
}

/// <summary>
/// Try to register a compat tab from a type.
/// </summary>
/// <param name="type">The type</param>
/// <returns>If it could be registered</returns>
public static bool TryRegisterCompatTab(Type type) {
if (RegisteredTabs.ContainsKey(type)) return false;

var tab = type.GetCustomAttribute<CompatTab>(false);
if (tab == null) return false;

Plugin.Logger.LogInfo(
$"Found compat tab {tab.Name} in {type.FullName} (from {type.Assembly.GetName().Name}.dll");

var subClasses = type.GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public);

foreach (var subClass in subClasses) {
OptionLoader.TryRegisterGroup(tab.Name, subClass);
TryRegisterCompatGroup(tab.Name, subClass);
}

RegisteredTabs.Add(type, tab);

return true;
}
}
2 changes: 1 addition & 1 deletion ConfigurableWarning/Source/API/Compat/CompatModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public sealed class CompatModule(string[] dependencies) : Attribute {
/// A list of namespaces this module depends on.
/// </summary>
public readonly string[] Dependencies = dependencies;
}
}
13 changes: 0 additions & 13 deletions ConfigurableWarning/Source/API/Compat/CompatSetting.cs

This file was deleted.

6 changes: 3 additions & 3 deletions ConfigurableWarning/Source/API/EnumUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public static class EnumUtil {
public static string[] GetOptions<T>() where T : struct {
return Enum.GetNames(typeof(T));
}

public static T Parse<T>(string value) where T : struct {
return (T) Enum.Parse(typeof(T), value);
}

public static int GetIndex<T>(T value) where T : struct {
return Array.IndexOf(GetOptions<T>(), value.ToString());
}
}
}
Loading

0 comments on commit 7fb0714

Please sign in to comment.