diff --git a/API/Configs/AbilityConfig.cs b/API/Configs/AbilityConfig.cs index 97e6599..0b93fc3 100644 --- a/API/Configs/AbilityConfig.cs +++ b/API/Configs/AbilityConfig.cs @@ -4,10 +4,13 @@ using Interfaces; + using YamlDotNet.Serialization; + public class AbilityConfig { - public Type[] AbilityTypes { get; set; } + public string[] AbilityTypes { get; set; } + [YamlIgnore] public IAbility[] Abilities { get; set; } } } \ No newline at end of file diff --git a/API/Controller/CooldownController.cs b/API/Controller/CooldownController.cs index 80abb84..988f737 100644 --- a/API/Controller/CooldownController.cs +++ b/API/Controller/CooldownController.cs @@ -15,7 +15,13 @@ public class CooldownController : MonoBehaviour public void Init(AbilityConfig abilityConfig) { - _abilityCooldown = abilityConfig.Abilities.ToDictionary(a => a.Name, _ => 0f); + _abilityCooldown = []; + + foreach (string name in abilityConfig.AbilityTypes) + { + _abilityCooldown.Add(name, 0); + } + InvokeRepeating(nameof(UpdateCooldown), 0f, 1f); Log.Debug($"[CooldownController] Invoke the cooldown cycle"); } diff --git a/API/ExtendedRole.cs b/API/ExtendedRole.cs index af9b56e..d810bed 100644 --- a/API/ExtendedRole.cs +++ b/API/ExtendedRole.cs @@ -47,7 +47,6 @@ public static IReadOnlyDictionary Instances public abstract AudioConfig AudioConfig { get; set; } - [YamlIgnore] public abstract AbilityConfig AbilityConfig { get; set; } public abstract List Effects { get; set; } diff --git a/API/Interfaces/Ability.cs b/API/Interfaces/Ability.cs index c12f4fe..f0b6034 100644 --- a/API/Interfaces/Ability.cs +++ b/API/Interfaces/Ability.cs @@ -42,7 +42,7 @@ private void OnKeybindActivateAbility(ReferenceHub referenceHub, ServerSpecificS // Check player and role if (!Player.TryGet(referenceHub, out Player player) || !ExtendedRole.Instances.TryGetValue(player, out ObjectManager manager) || - !manager.AllowedAbilities.Contains(GetType())) + !manager.AllowedAbilities.Contains(GetType().Name)) { return; } diff --git a/API/Managers/AbilityRegistrator.cs b/API/Managers/AbilityRegistrator.cs index e3bc8a3..d8c65a3 100644 --- a/API/Managers/AbilityRegistrator.cs +++ b/API/Managers/AbilityRegistrator.cs @@ -10,20 +10,33 @@ public static class AbilityRegistrator { - public static IAbility[] RegisterAbilities(Type[] abilityTypes) + public static IAbility[] RegisterAbilities(string[] abilityTypes) { List abilities = new(); - foreach (Type type in abilityTypes) + + foreach (string name in abilityTypes) { + Type type = Type.GetType(name); + + if (type == null) + { + Log.Error($"Type {name} not found."); + continue; + } + if (Activator.CreateInstance(type) is IAbility activator) { abilities.Add(activator); activator.Register(); Log.Debug($"Register the {activator.Name} ability."); } + else + { + Log.Debug($"Provided type {name} is not IAbility."); + } } - return abilities.ToArray(); + return [.. abilities]; } } } \ No newline at end of file diff --git a/API/Managers/ObjectManager.cs b/API/Managers/ObjectManager.cs index d08c871..b688452 100644 --- a/API/Managers/ObjectManager.cs +++ b/API/Managers/ObjectManager.cs @@ -35,7 +35,7 @@ public sealed class ObjectManager public CooldownController CooldownController { get; set; } - public Type[] AllowedAbilities { get; set; } + public string[] AllowedAbilities { get; set; } public void CreateObjects(Player player, ExtendedRole config) {