forked from Caraxi/RemindMe
/
RemindMe.cs
398 lines (329 loc) · 17.5 KB
/
RemindMe.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using Dalamud.Game.ClientState.Actors.Types;
using Dalamud.Game.ClientState.Actors.Types.NonPlayer;
using Dalamud.Game.Internal;
using Dalamud.Plugin;
using ImGuiNET;
using Lumina.Excel.GeneratedSheets;
using RemindMe.Config;
namespace RemindMe {
public partial class RemindMe : IDalamudPlugin {
public string Name => "Remind Me";
public DalamudPluginInterface PluginInterface { get; private set; }
public RemindMeConfig PluginConfig { get; private set; }
public bool InPvP { get; private set; } = false;
private IntPtr actionManagerStatic;
public ActionManager ActionManager;
private bool drawConfigWindow;
public IconManager IconManager;
private readonly Stopwatch generalStopwatch = new Stopwatch();
internal Stopwatch OutOfCombatTimer = new Stopwatch();
internal Dictionary<uint, List<Actor>> ActorsWithStatus = new Dictionary<uint, List<Actor>>();
private readonly Stopwatch cacheTimer = new Stopwatch();
private Exception configLoadException;
public void Dispose() {
PluginInterface.UiBuilder.OnOpenConfigUi -= OnOpenConfigUi;
PluginInterface.UiBuilder.OnBuildUi -= this.BuildUI;
PluginInterface.Framework.OnUpdateEvent -= FrameworkOnOnUpdateEvent;
ActionManager?.Dispose();
IconManager?.Dispose();
generalStopwatch.Stop();
OutOfCombatTimer.Stop();
cacheTimer.Stop();
RemoveCommands();
PluginInterface.Dispose();
}
public void LoadConfig(bool clearConfig = false) {
try {
if (clearConfig) {
this.PluginConfig = new RemindMeConfig();
} else {
this.PluginConfig = (RemindMeConfig)PluginInterface.GetPluginConfig() ?? new RemindMeConfig();
}
this.PluginConfig.Init(this, PluginInterface);
} catch (Exception ex) {
PluginLog.LogError("Failed to load config.");
PluginLog.LogError(ex.ToString());
PluginConfig = new RemindMeConfig();
PluginConfig.Init(this, PluginInterface);
configLoadException = ex;
}
}
public void Initialize(DalamudPluginInterface pluginInterface) {
generalStopwatch.Start();
cacheTimer.Start();
#if DEBUG
drawConfigWindow = true;
#endif
this.PluginInterface = pluginInterface;
LoadConfig();
PluginInterface.Framework.OnUpdateEvent += FrameworkOnOnUpdateEvent;
IconManager = new IconManager(pluginInterface);
actionManagerStatic = pluginInterface.TargetModuleScanner.GetStaticAddressFromSig("48 89 05 ?? ?? ?? ?? C3 CC C2 00 00 CC CC CC CC CC CC CC CC CC CC CC CC CC 48 8D 0D ?? ?? ?? ?? E9 ?? ?? ?? ??");
ActionManager = new ActionManager(this, actionManagerStatic);
pluginInterface.UiBuilder.OnOpenConfigUi += OnOpenConfigUi;
PluginInterface.UiBuilder.OnBuildUi += this.BuildUI;
pluginInterface.ClientState.TerritoryChanged += TerritoryChanged;
TerritoryChanged(this, pluginInterface.ClientState.TerritoryType);
SetupCommands();
}
private void TerritoryChanged(object sender, ushort e) {
InPvP = PluginInterface.Data.GetExcelSheet<TerritoryType>().GetRow(e)?.IsPvpZone ?? false;
}
private void FrameworkOnOnUpdateEvent(Framework framework) {
if (PluginInterface.ClientState?.LocalPlayer == null) return;
var inCombat = PluginInterface.ClientState.LocalPlayer.IsStatus(StatusFlags.InCombat);
if (OutOfCombatTimer.IsRunning && inCombat) {
generalStopwatch.Restart();
ActionManager.ResetTimers();
OutOfCombatTimer.Stop();
OutOfCombatTimer.Reset();
} else if (!OutOfCombatTimer.IsRunning && !inCombat) {
OutOfCombatTimer.Start();
}
if (cacheTimer.ElapsedMilliseconds >= PluginConfig.PollingRate) {
cacheTimer.Restart();
ActorsWithStatus.Clear();
foreach (var a in PluginInterface.ClientState.Actors) {
if (!(a is PlayerCharacter || a is BattleNpc)) continue;
unsafe {
if (a is BattleNpc bNpc && bNpc.NameId != 541 &&*(ulong*) (a.Address + 0xF0) == 0
|| ((*(uint*) (a.Address + 0x104)) & 0x10000) == 0x10000) {
continue;
}
}
foreach (var s in a.StatusEffects) {
if (s.EffectId == 0) continue;
var eid = (uint) s.EffectId;
if (!ActorsWithStatus.ContainsKey(eid)) ActorsWithStatus.Add(eid, new List<Actor>());
if (ActorsWithStatus[eid].Contains(a)) continue;
ActorsWithStatus[eid].Add(a);
}
}
}
}
private void OnOpenConfigUi(object sender, EventArgs e) {
drawConfigWindow = true;
}
public void SetupCommands() {
PluginInterface.CommandManager.AddHandler("/remindme", new Dalamud.Game.Command.CommandInfo(OnConfigCommandHandler) {
HelpMessage = $"Open config window for {this.Name}",
ShowInHelp = true
});
}
public void OnConfigCommandHandler(string command, string args) {
drawConfigWindow = !drawConfigWindow;
}
public void RemoveCommands() {
PluginInterface.CommandManager.RemoveHandler("/remindme");
}
private void TextShadowed(string text, Vector4 foregroundColor, Vector4 shadowColor, byte shadowWidth = 1) {
var x = ImGui.GetCursorPosX();
var y = ImGui.GetCursorPosY();
for (var i = -shadowWidth; i < shadowWidth; i++) {
for (var j = -shadowWidth; j < shadowWidth; j++) {
ImGui.SetCursorPosX(x + i);
ImGui.SetCursorPosY(y + j);
ImGui.TextColored(shadowColor, text);
}
}
ImGui.SetCursorPosX(x);
ImGui.SetCursorPosY(y);
ImGui.TextColored(foregroundColor, text);
}
private List<DisplayTimer> GetTimerList(MonitorDisplay display) {
var timerList = new List<DisplayTimer>();
if (InPvP) return timerList;
try {
if (display.Cooldowns.Count > 0) {
foreach (var cd in display.Cooldowns.Where(cd => {
if (cd.ClassJob != PluginInterface.ClientState.LocalPlayer.ClassJob.Id) return false;
var action = ActionManager.GetAction(cd.ActionId);
if (action == null || !action.ClassJobCategory.Value.HasClass(PluginInterface.ClientState.LocalPlayer.ClassJob.Id)) return false;
if (action.ClassJobLevel > PluginInterface.ClientState.LocalPlayer.Level) return false;
var cooldown = ActionManager.GetActionCooldown(action);
if (display.OnlyShowReady && cooldown.IsOnCooldown) return false;
if (display.OnlyShowCooldown && !cooldown.IsOnCooldown) return false;
if (display.LimitDisplayTime && cooldown.Countdown > display.LimitDisplayTimeSeconds) return false;
if (display.LimitDisplayReadyTime && cooldown.CompleteFor > display.LimitDisplayReadyTimeSeconds) return false;
return true;
})) {
var action = ActionManager.GetAction(cd.ActionId);
if (action != null) {
var cooldown = ActionManager.GetActionCooldown(action);
timerList.Add(new DisplayTimer {
TimerMax = cooldown.CooldownTotal,
TimerCurrent = cooldown.CooldownElapsed + cooldown.CompleteFor,
FinishedColor = display.AbilityReadyColor,
ProgressColor = display.AbilityCooldownColor,
IconId = IconManager.GetActionIconId(action),
Name = action.Name
});
}
}
}
} catch (Exception ex) {
PluginLog.LogError("Error parsing cooldowns.");
PluginLog.Log(ex.ToString());
}
try {
if (display.StatusMonitors.Count > 0) {
var localPlayerAsList = new List<Actor>() { PluginInterface.ClientState.LocalPlayer };
foreach (var sd in display.StatusMonitors.Where(sm => sm.ClassJob == PluginInterface.ClientState.LocalPlayer.ClassJob.Id)) {
foreach (var sid in sd.StatusIDs) {
var status = PluginInterface.Data.Excel.GetSheet<Status>().GetRow(sid);
if (status == null) continue;
if (!ActorsWithStatus.ContainsKey(status.RowId)) continue;
foreach (var a in sd.SelfOnly ? localPlayerAsList : ActorsWithStatus[status.RowId]) {
if (a != null) {
foreach (var se in a.StatusEffects) {
if (sd.IsRaid == false && se.OwnerId != PluginInterface.ClientState.LocalPlayer.ActorId) continue;
if (sd.LimitedZone > 0 && sd.LimitedZone != PluginInterface.ClientState.TerritoryType) continue;
if (display.LimitDisplayTime && se.Duration > display.LimitDisplayTimeSeconds) continue;
if (se.EffectId == (short)status.RowId) {
var t = new DisplayTimer {
TimerMax = sd.MaxDuration,
TimerCurrent = sd.MaxDuration - se.Duration,
FinishedColor = display.AbilityReadyColor,
ProgressColor = display.StatusEffectColor,
IconId = status.Icon,
Name = status.Name
};
if (!sd.SelfOnly) {
t.TargetName = a.Name;
t.ClickAction = sd.ClickHandler;
t.ClickParam = a;
}
timerList.Add(t);
}
}
}
}
}
}
}
} catch (Exception ex) {
PluginLog.LogError("Error parsing statuses.");
PluginLog.Log(ex.ToString());
}
timerList.Sort((a, b) => {
var diff = a.TimerRemaining - b.TimerRemaining;
if (Math.Abs(diff) < 0.1) return string.CompareOrdinal(a.Name, b.Name); // Equal
if (diff < 0) return -1;
return 1;
});
foreach (var reminder in display.GeneralReminders) {
if (reminder.ShouldShow(PluginInterface, this, display)) {
timerList.Insert(0, new DisplayTimer {
TimerMax = 1,
TimerCurrent = 1 + generalStopwatch.ElapsedMilliseconds / 1000f,
FinishedColor = display.AbilityReadyColor,
ProgressColor = display.StatusEffectColor,
IconId = reminder.GetIconID(PluginInterface, this, display),
Name = reminder.GetText(PluginInterface, this, display),
AllowCountdown = false
});
}
}
return timerList;
}
private void DrawDisplays() {
if (PluginInterface.ClientState.LocalPlayer == null) return;
if (PluginConfig.MonitorDisplays.Count == 0) return;
foreach (var display in PluginConfig.MonitorDisplays.Values) {
if (display.Locked && display.OnlyInCombat) {
var inCombat = PluginInterface.ClientState.LocalPlayer.IsStatus(StatusFlags.InCombat);
if (!inCombat && !display.KeepVisibleOutsideCombat) continue;
if (!inCombat && display.KeepVisibleOutsideCombat) {
if (OutOfCombatTimer.Elapsed.TotalSeconds > display.KeepVisibleOutsideCombatSeconds) {
continue;
}
}
}
var flags = ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoTitleBar;
if (display.Locked) {
flags |= ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoFocusOnAppearing | ImGuiWindowFlags.NoBackground;
if (!display.AllowClicking || !display.IsClickableHovered) {
flags |= ImGuiWindowFlags.NoMouseInputs;
}
}
var timerList = display.TimerList ??= GetTimerList(display);
if (timerList.Count > 0 || !display.Locked) {
ImGui.SetNextWindowSize(new Vector2(250, 250), ImGuiCond.FirstUseEver);
ImGui.SetNextWindowPos(new Vector2(250, 250), ImGuiCond.FirstUseEver);
if (display.IsClickableHovered || !display.Locked) {
ImGui.PushStyleColor(ImGuiCol.Border, new Vector4(1, 0, 0, 1));
ImGui.PushStyleVar(ImGuiStyleVar.WindowBorderSize, 1);
}
ImGui.Begin($"Display##{display.Guid}", flags);
if (display.IsClickableHovered || !display.Locked) {
ImGui.PopStyleColor();
ImGui.PopStyleVar();
}
display.IsClickableHovered = false;
switch (display.DisplayType) {
case 0: {
DrawDisplayHorizontal(display, timerList);
break;
}
case 1: {
DrawDisplayVertical(display, timerList);
break;
}
case 2: {
DrawDisplayIcons(display, timerList);
break;
}
default: {
display.DisplayType = 0;
DrawDisplayHorizontal(display, timerList);
break;
}
}
ImGui.End();
}
}
}
private void BuildUI() {
if (PluginInterface.ClientState.LocalPlayer == null) return;
if (configLoadException != null || PluginConfig == null) {
ImGui.PushStyleColor(ImGuiCol.TitleBg, 0x880000AA);
ImGui.PushStyleColor(ImGuiCol.TitleBgActive, 0x880000FF);
ImGui.Begin($"{Name} - Config Load Error", ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.AlwaysAutoResize);
ImGui.PopStyleColor(2);
ImGui.Text($"{Name} failed to load the config file.");
ImGui.Text($"Continuing will result in a loss of any configs you have setup for {Name}.");
ImGui.Text("Please report this error.");
if (configLoadException != null) {
var str = configLoadException.ToString();
ImGui.InputTextMultiline("###exceptionText", ref str, uint.MaxValue, new Vector2(-1, 80), ImGuiInputTextFlags.ReadOnly | ImGuiInputTextFlags.AutoSelectAll);
}
ImGui.Dummy(new Vector2(5));
if (ImGui.Button("Retry Load")) {
PluginConfig = null;
configLoadException = null;
LoadConfig();
}
ImGui.SameLine();
ImGui.Dummy(new Vector2(15));
ImGui.SameLine();
ImGui.PushStyleColor(ImGuiCol.Button, 0x880000FF);
ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0x88000088);
ImGui.PushStyleColor(ImGuiCol.ButtonHovered, 0x880000AA);
if (ImGui.Button("Clear Config")) {
LoadConfig(true);
configLoadException = null;
}
ImGui.PopStyleColor(3);
ImGui.End();
} else {
drawConfigWindow = drawConfigWindow && PluginConfig.DrawConfigUI();
if (!InPvP) DrawDisplays();
}
}
}
}