-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.cs
296 lines (243 loc) · 10.4 KB
/
Module.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using Blish_HUD;
using Blish_HUD.Modules;
using Blish_HUD.Modules.Managers;
using Blish_HUD.Settings;
using Flyga.PositionEventsModule;
using Flyga.PositionEventsModule.Contexts;
using Microsoft.Xna.Framework;
using PositionEvents;
using PositionEvents.Area;
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
namespace PositionEventsExample
{
[Export(typeof(Blish_HUD.Modules.Module))]
public class Module : Blish_HUD.Modules.Module
{
private static readonly Logger Logger = Logger.GetLogger<Module>();
private const string POSITION_EVENTS_MODULE_NAMESPACE = "Flyga.PositionEvents";
private ModuleManager _positionEventsModuleManager;
private ModuleManager _thisModuleManager;
private PositionEventsContext _positionEventsContext;
#region Service Managers
internal SettingsManager SettingsManager => this.ModuleParameters.SettingsManager;
internal ContentsManager ContentsManager => this.ModuleParameters.ContentsManager;
internal DirectoriesManager DirectoriesManager => this.ModuleParameters.DirectoriesManager;
internal Gw2ApiManager Gw2ApiManager => this.ModuleParameters.Gw2ApiManager;
#endregion
[ImportingConstructor]
public Module([Import("ModuleParameters")] ModuleParameters moduleParameters) : base(moduleParameters) { }
protected override void DefineSettings(SettingCollection settings)
{
}
protected override void Initialize()
{
}
private void OnAreaJoinedOrLeft(PositionData positionData, bool isInside)
{
if (isInside)
{
// do something, if the player joined an area.
Logger.Info("Area joined.");
return;
}
// do something else, if the player left an area.
Logger.Info("Area left.");
}
private void OnPositionEventsLoaded(object _, EventArgs _1)
{
OnPositionEventsEnabled(_positionEventsModuleManager);
}
private void OnPositionEventsEnabled(ModuleManager moduleManager)
{
if (!(moduleManager.ModuleInstance is PositionEventsModule positionEventsModule))
{
Logger.Error($"Unable to detect required Position Events Module: {moduleManager.ModuleInstance?.GetType()}");
// disable this module
_thisModuleManager?.Disable();
return;
}
// save a reference to the ModuleManager for later use
_positionEventsModuleManager = moduleManager;
if (!positionEventsModule.Loaded)
{
// if the Position Events Module is not loaded yet, come back when it is
positionEventsModule.ModuleLoaded += OnPositionEventsLoaded;
return;
}
else
{
positionEventsModule.ModuleLoaded -= OnPositionEventsLoaded;
}
_positionEventsModuleManager.ModuleDisabled += OnOtherModuleDisabled;
// Retrieve the context, once you're sure the Position Events Module has been loaded
RetrieveContext();
// Add your areas, once you're sure the Position Events Module has been loaded
AddTestAreas();
}
private void OnPositionEventsDisabled(ModuleManager moduleManager)
{
// disable this module since it's dependent on the Position Events Module
_thisModuleManager?.Disable();
}
private void OnOtherModuleEnabled(object sender, EventArgs e)
{
if (!(sender is ModuleManager moduleManager))
{
throw new ArgumentException("OnOtherModuleEnabled must be called " +
"by a ModuleManager.", nameof(sender));
}
if (moduleManager.Manifest.Namespace != POSITION_EVENTS_MODULE_NAMESPACE)
{
throw new ArgumentException("OnOtherModuleEnabled must be called " +
$"by the ModuleManager of the {POSITION_EVENTS_MODULE_NAMESPACE} " +
"module.", nameof(sender));
}
if (!moduleManager.AssemblyLoaded)
{
Logger.Error($"Unable to load module, because dependency " +
$"{POSITION_EVENTS_MODULE_NAMESPACE} module" +
"could not be loaded.");
_thisModuleManager?.Disable();
return;
}
OnPositionEventsEnabled(moduleManager);
}
private void OnOtherModuleDisabled(object sender, EventArgs e)
{
if (!(sender is ModuleManager moduleManager))
{
throw new ArgumentException("OnOtherModuleEnabled must be called " +
"by a ModuleManager.", nameof(sender));
}
if (moduleManager.Manifest.Namespace != POSITION_EVENTS_MODULE_NAMESPACE)
{
throw new ArgumentException("OnOtherModuleEnabled must be called " +
$"by the ModuleManager of the {POSITION_EVENTS_MODULE_NAMESPACE} " +
"module.", nameof(sender));
}
OnPositionEventsDisabled(moduleManager);
}
protected override Task LoadAsync()
{
// set reference for this modules manager
_thisModuleManager = GameService.Module.Modules
.Where(moduleManager => moduleManager.Manifest.Namespace == Namespace)
.FirstOrDefault();
// Retrieve a reference to the Position Events Context
foreach (ModuleManager item in GameService.Module.Modules)
{
if (item.Manifest.Namespace == POSITION_EVENTS_MODULE_NAMESPACE)
{
// if the assembly is already loaded, call OnPositionEventsEnabled manually
if (item.AssemblyLoaded)
{
OnPositionEventsEnabled(item);
}
// make sure to retrieve the context only after the
// Position Events Module was enabled (and therefor the
// assembly was loaded)
item.ModuleEnabled += OnOtherModuleEnabled;
break;
}
}
return Task.CompletedTask;
}
private void AddTestAreas()
{
if (_positionEventsContext == null)
{
Logger.Error("Unable to add test areas, since the context was not retrieved.");
return;
}
// create the areas
IBoundingObject area = new BoundingObjectBox(new Vector3(50, 50, 10), new Vector3(60, 70, 40));
IBoundingObject prism = GetTestPrism();
IBoundingObject testLake = GetTestLake();
IBoundingObject testDifference = GetTestDifference();
// register the areas with the Position Events Context
// debug flags are true for this example. Never ship your module with
// those set to true!
_positionEventsContext.RegisterArea(this, 15, area, OnAreaJoinedOrLeft, debug: true);
_positionEventsContext.RegisterArea(this, 15, prism, OnAreaJoinedOrLeft, debug: true);
_positionEventsContext.RegisterArea(this, 15, testLake, OnAreaJoinedOrLeft, debug: true);
_positionEventsContext.RegisterArea(this, 15, testDifference, OnAreaJoinedOrLeft, debug: true);
}
private IBoundingObject GetTestPrism()
{
Vector2[] vertices = new Vector2[]
{
new Vector2(0),
new Vector2(15),
new Vector2(15, -3)
};
return new BoundingObjectPrism(10, 0, vertices);
}
private IBoundingObject GetTestLake()
{
Vector2[] vertices = new Vector2[]
{
new Vector2(-83.0f, 584.0f),
new Vector2(-81.0f, 626.0f),
new Vector2(-24.0f, 634.0f),
new Vector2(52.0f, 594.0f),
new Vector2(85.0f, 485.0f),
new Vector2(195.0f, 467.0f),
new Vector2(208.0f, 423.0f),
new Vector2(157.0f, 386.0f),
new Vector2(152.0f, 317.0f),
new Vector2(56.0f, 310.0f),
new Vector2(18.0f, 376.0f),
new Vector2(22.0f, 416.0f),
new Vector2(-20.0f, 438.0f),
new Vector2(-29.0f, 538.0f)
};
return new BoundingObjectPrism(50, 0, vertices);
}
private IBoundingObject GetTestDifference()
{
BoundingObjectSphere outerSphere = new BoundingObjectSphere(new Vector3(-20, -20, 20), 20);
BoundingObjectSphere innerSphere = new BoundingObjectSphere(new Vector3(-23, -26, 23), 8);
BoundingObjectBuilder builder = new BoundingObjectBuilder()
.Add(outerSphere)
.Subtract(innerSphere);
return builder.Build();
}
protected override void OnModuleLoaded(EventArgs e)
{
//if (!AreasAdded && _positionEventsContext != null)
//{
// AreasAdded = true;
// AddTestAreas();
//}
// Base handler must be called
base.OnModuleLoaded(e);
}
private void RetrieveContext()
{
_positionEventsContext = GameService.Contexts.GetContext<PositionEventsContext>();
}
protected override void Update(GameTime gameTime)
{
/** NOOP **/
}
/// <inheritdoc />
protected override void Unload()
{
// Unload here
// All static members must be manually unset
if (_positionEventsModuleManager != null)
{
_positionEventsModuleManager.ModuleEnabled -= OnOtherModuleEnabled;
_positionEventsModuleManager.ModuleDisabled -= OnOtherModuleDisabled;
}
// no need to remove the areas from the Position Events Module, since it
// takes care of that on it's own.
_positionEventsModuleManager = null;
_thisModuleManager = null;
_positionEventsContext = null;
}
}
}