forked from UnknownX7/NoClippy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DalamudApi.cs
255 lines (206 loc) · 8.04 KB
/
DalamudApi.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
global using Dalamud;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Dalamud.Data;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Buddy;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Fates;
using Dalamud.Game.ClientState.JobGauge;
using Dalamud.Game.ClientState.Keys;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.ClientState.Party;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Game.Gui.FlyText;
using Dalamud.Game.Gui.PartyFinder;
using Dalamud.Game.Gui.Toast;
using Dalamud.Game.Libc;
using Dalamud.Game.Network;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
// ReSharper disable CheckNamespace
// ReSharper disable UnusedAutoPropertyAccessor.Local
namespace Dalamud
{
public class DalamudApi
{
[PluginService]
//[RequiredVersion("1.0")]
public static DalamudPluginInterface PluginInterface { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static BuddyList BuddyList { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static ChatGui ChatGui { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static ChatHandlers ChatHandlers { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static ClientState ClientState { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static CommandManager CommandManager { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static Condition Condition { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static DataManager DataManager { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static FateTable FateTable { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static FlyTextGui FlyTextGui { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static Framework Framework { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static GameGui GameGui { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static GameNetwork GameNetwork { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static JobGauges JobGauges { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static KeyState KeyState { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static LibcFunction LibcFunction { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static ObjectTable ObjectTable { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static PartyFinderGui PartyFinderGui { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static PartyList PartyList { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static SigScanner SigScanner { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static TargetManager TargetManager { get; private set; }
[PluginService]
//[RequiredVersion("1.0")]
public static ToastGui ToastGui { get; private set; }
private static PluginCommandManager<IDalamudPlugin> pluginCommandManager;
public DalamudApi() { }
public DalamudApi(IDalamudPlugin plugin) => pluginCommandManager ??= new(plugin);
public DalamudApi(IDalamudPlugin plugin, DalamudPluginInterface pluginInterface)
{
if (!pluginInterface.Inject(this))
{
PluginLog.LogError("Failed loading DalamudApi!");
return;
}
pluginCommandManager ??= new(plugin);
}
public static DalamudApi operator +(DalamudApi container, object o)
{
foreach (var f in typeof(DalamudApi).GetProperties())
{
if (f.PropertyType != o.GetType()) continue;
if (f.GetValue(container) != null) break;
f.SetValue(container, o);
return container;
}
throw new InvalidOperationException();
}
public static void Initialize(IDalamudPlugin plugin, DalamudPluginInterface pluginInterface) => _ = new DalamudApi(plugin, pluginInterface);
public static void Dispose() => pluginCommandManager?.Dispose();
}
#region PluginCommandManager
public class PluginCommandManager<T> : IDisposable where T : IDalamudPlugin
{
private readonly T plugin;
private readonly (string, CommandInfo)[] pluginCommands;
public PluginCommandManager(T p)
{
plugin = p;
pluginCommands = plugin.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)
.Where(method => method.GetCustomAttribute<CommandAttribute>() != null)
.SelectMany(GetCommandInfoTuple)
.ToArray();
AddCommandHandlers();
}
private void AddCommandHandlers()
{
foreach (var (command, commandInfo) in pluginCommands)
DalamudApi.CommandManager.AddHandler(command, commandInfo);
}
private void RemoveCommandHandlers()
{
foreach (var (command, _) in pluginCommands)
DalamudApi.CommandManager.RemoveHandler(command);
}
private IEnumerable<(string, CommandInfo)> GetCommandInfoTuple(MethodInfo method)
{
var handlerDelegate = (CommandInfo.HandlerDelegate)Delegate.CreateDelegate(typeof(CommandInfo.HandlerDelegate), plugin, method);
var command = handlerDelegate.Method.GetCustomAttribute<CommandAttribute>();
var aliases = handlerDelegate.Method.GetCustomAttribute<AliasesAttribute>();
var helpMessage = handlerDelegate.Method.GetCustomAttribute<HelpMessageAttribute>();
var doNotShowInHelp = handlerDelegate.Method.GetCustomAttribute<DoNotShowInHelpAttribute>();
var commandInfo = new CommandInfo(handlerDelegate)
{
HelpMessage = helpMessage?.HelpMessage ?? string.Empty,
ShowInHelp = doNotShowInHelp == null,
};
// Create list of tuples that will be filled with one tuple per alias, in addition to the base command tuple.
var commandInfoTuples = new List<(string, CommandInfo)> { (command?.Command, commandInfo) };
if (aliases != null)
commandInfoTuples.AddRange(aliases.Aliases.Select(alias => (alias, commandInfo)));
return commandInfoTuples;
}
public void Dispose()
{
RemoveCommandHandlers();
GC.SuppressFinalize(this);
}
}
#endregion
#region Attributes
[AttributeUsage(AttributeTargets.Method)]
public class AliasesAttribute : Attribute
{
public string[] Aliases { get; }
public AliasesAttribute(params string[] aliases)
{
Aliases = aliases;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class CommandAttribute : Attribute
{
public string Command { get; }
public CommandAttribute(string command)
{
Command = command;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class DoNotShowInHelpAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method)]
public class HelpMessageAttribute : Attribute
{
public string HelpMessage { get; }
public HelpMessageAttribute(string helpMessage)
{
HelpMessage = helpMessage;
}
}
#endregion
}