From 698c323c792a68bb9de81c96b9ff13bf2ae4ba11 Mon Sep 17 00:00:00 2001 From: VALERA771 <72030575+VALERA771@users.noreply.github.com> Date: Sun, 11 Feb 2024 04:33:08 +0300 Subject: [PATCH] `[Exiled::API]` EnumClasses fix (#2445) * enum fix * fix --- Exiled.API/Features/Core/Generic/EnumClass.cs | 4 ++-- .../Core/Generic/UniqueUnmanagedEnumClass.cs | 4 ++-- .../Features/Core/Generic/UnmanagedEnumClass.cs | 4 ++-- Exiled.API/Interfaces/IEnumClass.cs | 16 ++++++++++++++++ Exiled.Events/Events.cs | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 Exiled.API/Interfaces/IEnumClass.cs diff --git a/Exiled.API/Features/Core/Generic/EnumClass.cs b/Exiled.API/Features/Core/Generic/EnumClass.cs index cf9413f66a..de21ddfaac 100644 --- a/Exiled.API/Features/Core/Generic/EnumClass.cs +++ b/Exiled.API/Features/Core/Generic/EnumClass.cs @@ -13,7 +13,7 @@ namespace Exiled.API.Features.Core.Generic using System.Reflection; using Exiled.API.Features.Core.Generic.Pools; - + using Exiled.API.Interfaces; using LiteNetLib.Utils; /// @@ -22,7 +22,7 @@ namespace Exiled.API.Features.Core.Generic /// /// The type of the source object to handle the instance of. /// The type of the child object to handle the instance of. - public abstract class EnumClass : IComparable, IEquatable, IComparable, IComparer + public abstract class EnumClass : IComparable, IEquatable, IComparable, IComparer, IEnumClass where TSource : Enum where TObject : EnumClass { diff --git a/Exiled.API/Features/Core/Generic/UniqueUnmanagedEnumClass.cs b/Exiled.API/Features/Core/Generic/UniqueUnmanagedEnumClass.cs index 94a91cac48..f71d7366f6 100644 --- a/Exiled.API/Features/Core/Generic/UniqueUnmanagedEnumClass.cs +++ b/Exiled.API/Features/Core/Generic/UniqueUnmanagedEnumClass.cs @@ -13,7 +13,7 @@ namespace Exiled.API.Features.Core.Generic using System.Reflection; using Exiled.API.Features.Core.Generic.Pools; - + using Exiled.API.Interfaces; using LiteNetLib.Utils; /// @@ -22,7 +22,7 @@ namespace Exiled.API.Features.Core.Generic /// /// The type of the source object to handle the instance of. /// The type of the child object to handle the instance of. - public abstract class UniqueUnmanagedEnumClass : IComparable, IEquatable, IComparable, IComparer, IConvertible + public abstract class UniqueUnmanagedEnumClass : IComparable, IEquatable, IComparable, IComparer, IConvertible, IEnumClass where TSource : unmanaged, IComparable, IFormattable, IConvertible, IComparable, IEquatable where TObject : UniqueUnmanagedEnumClass { diff --git a/Exiled.API/Features/Core/Generic/UnmanagedEnumClass.cs b/Exiled.API/Features/Core/Generic/UnmanagedEnumClass.cs index 63308fa4e1..a4697db89c 100644 --- a/Exiled.API/Features/Core/Generic/UnmanagedEnumClass.cs +++ b/Exiled.API/Features/Core/Generic/UnmanagedEnumClass.cs @@ -13,7 +13,7 @@ namespace Exiled.API.Features.Core.Generic using System.Reflection; using Exiled.API.Features.Core.Generic.Pools; - + using Exiled.API.Interfaces; using LiteNetLib.Utils; /// @@ -22,7 +22,7 @@ namespace Exiled.API.Features.Core.Generic /// /// The type of the source object to handle the instance of. /// The type of the child object to handle the instance of. - public abstract class UnmanagedEnumClass : IComparable, IEquatable, IComparable, IComparer, IConvertible + public abstract class UnmanagedEnumClass : IComparable, IEquatable, IComparable, IComparer, IConvertible, IEnumClass where TSource : unmanaged, IComparable, IFormattable, IConvertible, IComparable, IEquatable where TObject : UnmanagedEnumClass { diff --git a/Exiled.API/Interfaces/IEnumClass.cs b/Exiled.API/Interfaces/IEnumClass.cs new file mode 100644 index 0000000000..5d83750967 --- /dev/null +++ b/Exiled.API/Interfaces/IEnumClass.cs @@ -0,0 +1,16 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.API.Interfaces +{ + /// + /// An interface for all enum classes. + /// + public interface IEnumClass + { + } +} \ No newline at end of file diff --git a/Exiled.Events/Events.cs b/Exiled.Events/Events.cs index 4520134870..b0c7f5386f 100644 --- a/Exiled.Events/Events.cs +++ b/Exiled.Events/Events.cs @@ -9,10 +9,13 @@ namespace Exiled.Events { using System; using System.Diagnostics; + using System.Linq; + using System.Reflection; using API.Enums; using API.Features; using CentralAuth; + using Exiled.API.Interfaces; using Exiled.Events.Features; using HarmonyLib; using InventorySystem.Items.Pickups; @@ -83,6 +86,19 @@ public override void OnEnabled() ServerConsole.ReloadServerName(); EventManager.RegisterEvents(this); + + foreach (Type type in typeof(IEnumClass).Assembly.GetTypes().Where(x => x.GetInterface(nameof(IEnumClass)) == typeof(IEnumClass))) + { + FieldInfo[] fieldInfos = type.GetFields(); + + if (fieldInfos.All(x => !x.IsInitOnly)) + continue; + + foreach (FieldInfo field in fieldInfos.Where(x => x.IsStatic)) + { + field.GetValue(null); + } + } } ///