Integrated Controller control. The methods InvokeEventControllerClass and ExecuteController should be used by the user.
InvokeEventControllerClassis to be put in the Discord Controllers that use the Discord Ids and the ExecuteController should be put in their own separated classes.
This commit is contained in:
parent
a85f817eea
commit
42bf94c00a
5 changed files with 167 additions and 7 deletions
17
Discord/Attribute/EventControllerAttribute.cs
Normal file
17
Discord/Attribute/EventControllerAttribute.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
namespace Discord.Attribute
|
||||
{
|
||||
using Controller;
|
||||
/// <summary>
|
||||
/// To be placed over dynamic methods that will be executed by the <see cref="EventController"></see>
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
|
||||
public class EventControllerAttribute<T> : System.Attribute
|
||||
{
|
||||
public T Event { get; set; }
|
||||
public EventControllerAttribute(T Event)
|
||||
{
|
||||
this.Event = Event;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
namespace Discord
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
71
Discord/Controller/EventController.cs
Normal file
71
Discord/Controller/EventController.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using Discord.Attribute;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
|
||||
namespace Discord.Controller
|
||||
{
|
||||
public static class EventController
|
||||
{
|
||||
/// <summary>
|
||||
/// To be executed by the Discord Events that use Ids.
|
||||
/// </summary>
|
||||
/// <typeparam name="CLASS">The class that should be looked in by the controller, defined by using <see cref="ExecuteController{T}(string[], string, object)"></see></typeparam>
|
||||
/// <typeparam name="ENUM">The base enum that will get separated and passed to the actual controller</typeparam>
|
||||
/// <param name="ids">Passed Ids by the event e.g e.Interaction.Data.CustomId</param>
|
||||
/// <param name="e">The passed Event args</param>
|
||||
public static void InvokeEventControllerClass<CLASS, ENUM>(string ids, object e) where ENUM : class
|
||||
{
|
||||
string[] splittedIds = ids.Split(',');
|
||||
var category = Converter.InteractionFormatter.GetFirstEnumFromIdAndRemove<ENUM>(ref splittedIds);
|
||||
InvokeEventController<CLASS, ENUM>(category, splittedIds, e);
|
||||
}
|
||||
/// <summary>
|
||||
/// Invokes the event controller by passing over the given class and enum. This method takes a look at all the created Controllers and picks one that gets executed and invokes the correct method.
|
||||
/// </summary>
|
||||
/// <typeparam name="CLASS">Passed method from the controller invoker</typeparam>
|
||||
/// <typeparam name="ENUM">Passed enum from the controller invoker</typeparam>
|
||||
/// <param name="PassedEnum"></param>
|
||||
/// <param name="ids"></param>
|
||||
/// <param name="e"></param>
|
||||
private static void InvokeEventController<CLASS, ENUM>(ENUM PassedEnum, string[] ids, object e)
|
||||
{
|
||||
var type = typeof(CLASS);
|
||||
var methods = type.GetMethods();
|
||||
EventControllerAttribute<ENUM>? attribute;
|
||||
foreach (var method in methods)
|
||||
{
|
||||
attribute = method.GetCustomAttribute<EventControllerAttribute<ENUM>>();
|
||||
if (attribute is not null && EqualityComparer<ENUM>.Default.Equals(attribute.Event, PassedEnum))
|
||||
method.Invoke(null, new object[] { ids, e });
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Method To be placed in a separate Class that will be passed into the <see cref="InvokeEventControllerClass"></see>
|
||||
/// For the EventControllerAttribute you have to pass the enum type e.g. of all categories and pass the enum value for a specific category. Finally you have to execute the controller and use the category's enum for the generic.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The enum of the category</typeparam>
|
||||
/// <param name="ids">The given Ids without category enum.</param>
|
||||
/// <param name="nspace">The namespace with all the required events. E.g. Project_Sipster.DiscordBarista.Function.Partnership.Events</param>
|
||||
/// <param name="e">The passed event from the <see cref="InvokeEventControllerClass"></see> - just needs to be put it as a param and passed over to the controller.</param>
|
||||
public static void ExecuteController<T>(string[] ids, string nspace, object e)
|
||||
{
|
||||
var category = Converter.InteractionFormatter.GetFirstEnumFromId<T>(ref ids);
|
||||
var q = (from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||
where t.IsClass && t.Namespace == nspace
|
||||
select t).ToList();
|
||||
List<MethodInfo> methods = new();
|
||||
foreach (var type in q)
|
||||
{
|
||||
methods.AddRange(type.GetMethods());
|
||||
}
|
||||
EventControllerAttribute<T>? attribute;
|
||||
foreach (var method in methods)
|
||||
{
|
||||
attribute = method.GetCustomAttribute<EventControllerAttribute<T>>();
|
||||
if (attribute is not null && EqualityComparer<T>.Default.Equals(attribute.Event, category))
|
||||
method.Invoke(null, new object[] { ids, e });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
64
Discord/Converter/InteractionFormatter.cs
Normal file
64
Discord/Converter/InteractionFormatter.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using Discord.Exception;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Converter
|
||||
{
|
||||
#region Public Area
|
||||
|
||||
public static class InteractionFormatter
|
||||
{
|
||||
public static string TransformEnumsToId(params Enum[] enums)
|
||||
{
|
||||
string output = "";
|
||||
foreach (var num in enums)
|
||||
{
|
||||
output += num.ToString("D") + ",";
|
||||
}
|
||||
output = output.Remove(output.Length - 1);
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the given values to the given enums. CAUTION: The length of the given strings and enums must be the same.
|
||||
/// </summary>
|
||||
/// <param name="value">The list of values separated by comma (e.g. "0,5,3")</param>
|
||||
/// <param name="enums">The list of enums that should be given out (e.g. typeof(Enum), typeof(Enum)"/></param>
|
||||
/// <returns>A list of converted enums that were passed in.</returns>
|
||||
/// <exception cref="MismatchedEnumLengthsException">Thrown when the length of enums and values are different.</exception>
|
||||
public static IEnumerable<Enum> TransformIdToEnums(string value, params Type[] enums)
|
||||
{
|
||||
string[] values = value.Split(',');
|
||||
if (values.Length != enums.Length)
|
||||
{
|
||||
throw new MismatchedEnumLengthsException();
|
||||
}
|
||||
for (int i = 0; i < enums.Length; i++)
|
||||
{
|
||||
yield return (Enum)Enum.Parse(enums[i], value);
|
||||
}
|
||||
}
|
||||
|
||||
public static T GetFirstEnumFromIdAndRemove<T>(ref string[] values)
|
||||
{
|
||||
string firstValue = values[0];
|
||||
values = values.Skip(1).ToArray();
|
||||
return (T)Enum.Parse(typeof(T), firstValue);
|
||||
}
|
||||
|
||||
public static T GetFirstEnumFromId<T>(ref string[] values)
|
||||
{
|
||||
return (T)Enum.Parse(typeof(T), values[0]);
|
||||
}
|
||||
|
||||
public static T GetEnumFromId<T>(string value)
|
||||
{
|
||||
return (T)Enum.Parse(typeof(T), value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public Area
|
||||
}
|
||||
15
Discord/Exception/MismatchedEnumLengthsException.cs
Normal file
15
Discord/Exception/MismatchedEnumLengthsException.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Exception
|
||||
{
|
||||
public class MismatchedEnumLengthsException : System.Exception
|
||||
{
|
||||
public MismatchedEnumLengthsException() : base("Length of values and enums are different. You are required to pass as many values as there are enums.")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue