This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPluginCommandManager.cs
executable file
·67 lines (61 loc) · 3.03 KB
/
PluginCommandManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Dalamud.Game.Command;
using Dalamud.Plugin;
using DalamudPluginProjectTemplate.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DalamudPluginProjectTemplate
{
public class PluginCommandManager<THost> : IDisposable
{
private readonly (string, CommandInfo)[] pluginCommands;
private readonly THost host;
public PluginCommandManager(THost host, DalamudPluginInterface pluginInterface)
{
this.host = host;
pluginCommands = host.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Where(method => method.GetCustomAttribute<CommandAttribute>() != null).SelectMany(new Func<MethodInfo, IEnumerable<(string, CommandInfo)>>(GetCommandInfoTuple)).ToArray();
Array.Reverse((Array)pluginCommands);
AddCommandHandlers();
}
private void AddCommandHandlers()
{
for (int index = 0; index < pluginCommands.Length; ++index)
{
(string, CommandInfo) pluginCommand = pluginCommands[index];
Plugin.CommandManager.AddHandler(pluginCommand.Item1, pluginCommand.Item2);
}
}
private void RemoveCommandHandlers()
{
for (int index = 0; index < pluginCommands.Length; ++index)
Plugin.CommandManager.RemoveHandler(pluginCommands[index].Item1);
}
private IEnumerable<(string, CommandInfo)> GetCommandInfoTuple(
MethodInfo method)
{
CommandInfo.HandlerDelegate handlerDelegate = (CommandInfo.HandlerDelegate)Delegate.CreateDelegate(typeof(CommandInfo.HandlerDelegate), host, method);
CommandAttribute customAttribute1 = handlerDelegate.Method.GetCustomAttribute<CommandAttribute>();
AliasesAttribute customAttribute2 = handlerDelegate.Method.GetCustomAttribute<AliasesAttribute>();
HelpMessageAttribute customAttribute3 = handlerDelegate.Method.GetCustomAttribute<HelpMessageAttribute>();
DoNotShowInHelpAttribute customAttribute4 = handlerDelegate.Method.GetCustomAttribute<DoNotShowInHelpAttribute>();
CommandInfo commandInfo = new(handlerDelegate)
{
HelpMessage = customAttribute3?.HelpMessage ?? string.Empty,
ShowInHelp = customAttribute4 == null
};
List<(string, CommandInfo)> valueTupleList = new()
{
(customAttribute1.Command, commandInfo)
};
List<(string, CommandInfo)> commandInfoTuple = valueTupleList;
if (customAttribute2 != null)
{
for (int index = 0; index < customAttribute2.Aliases.Length; ++index)
commandInfoTuple.Add((customAttribute2.Aliases[index], commandInfo));
}
return commandInfoTuple;
}
public void Dispose() => RemoveCommandHandlers();
}
}