-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTimelineManager.cs
499 lines (414 loc) · 16.4 KB
/
TimelineManager.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Hooking;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Game;
using FFXIVClientStructs.FFXIV.Client.UI;
using ImGuiNET;
using Lumina.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using LuminaAction = Lumina.Excel.Sheets.Action;
using LuminaItem = Lumina.Excel.Sheets.Item;
namespace ActionTimeline.Helpers
{
public enum TimelineItemType
{
Action = 0,
CastStart = 1,
CastCancel = 2,
OffGCD = 3,
AutoAttack = 4,
Item = 5
}
public class TimelineItem
{
public uint ActionID { get; }
public uint IconID { get; }
public TimelineItemType Type { get; }
public double Time { get; }
public float GCDDuration { get; }
public float CastTime { get; }
public GCDClipData? GCDClipData = null;
public TimelineItem(uint actionID, uint iconID, TimelineItemType type, double time)
{
ActionID = actionID;
IconID = iconID;
Type = type;
Time = time;
GCDDuration = 0;
CastTime = 0;
}
public TimelineItem(uint actionID, uint iconID, TimelineItemType type, double time, float gcdDuration, float castTime) : this(actionID, iconID, type, time)
{
GCDDuration = gcdDuration;
CastTime = castTime;
}
}
public struct GCDClipData
{
public bool IsClipped { get; }
public double StartTime { get; }
public double? EndTime { get; }
public bool IsFakeEndTime { get; }
public bool ShouldDraw => IsClipped && !Utils.UnderThreshold(StartTime, EndTime.HasValue ? EndTime.Value : ImGui.GetTime());
public GCDClipData(bool isClipped, double startTime, double? endTime, bool isFakeEndTime)
{
IsClipped = isClipped;
StartTime = startTime;
EndTime = endTime;
IsFakeEndTime = isFakeEndTime;
}
}
public class TimelineManager
{
#region singleton
public static void Initialize() { Instance = new TimelineManager(); }
public static TimelineManager Instance { get; private set; } = null!;
private TimelineManager()
{
_actionSheet = Plugin.DataManager.GetExcelSheet<LuminaAction>();
_itemSheet = Plugin.DataManager.GetExcelSheet<LuminaItem>();
try
{
_onActionUsedHook = Plugin.GameInteropProvider.HookFromSignature<OnActionUsedDelegate>(
"40 ?? 56 57 41 ?? 41 ?? 41 ?? 48 ?? ?? ?? ?? ?? ?? ?? 48",
OnActionUsed
);
_onActionUsedHook?.Enable();
_onActorControlHook = Plugin.GameInteropProvider.HookFromSignature<OnActorControlDelegate>(
"E8 ?? ?? ?? ?? 0F B7 0B 83 E9 64",
OnActorControl
);
_onActorControlHook?.Enable();
_onCastHook = Plugin.GameInteropProvider.HookFromSignature<OnCastDelegate>(
"40 56 41 56 48 81 EC ?? ?? ?? ?? 48 8B F2",
OnCast
);
_onCastHook?.Enable();
}
catch (Exception e)
{
Plugin.Logger.Error("Error initiating hooks: " + e.Message);
}
Plugin.Framework.Update += Update;
}
~TimelineManager()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
Plugin.Framework.Update -= Update;
_items.Clear();
_onActionUsedHook?.Disable();
_onActionUsedHook?.Dispose();
_onActorControlHook?.Disable();
_onActorControlHook?.Dispose();
_onCastHook?.Disable();
_onCastHook?.Dispose();
}
#endregion
private delegate void OnActionUsedDelegate(uint sourceId, IntPtr sourceCharacter, IntPtr pos, IntPtr effectHeader, IntPtr effectArray, IntPtr effectTrail);
private Hook<OnActionUsedDelegate>? _onActionUsedHook;
private delegate void OnActorControlDelegate(uint entityId, uint id, uint unk1, uint type, uint unk2, uint unk3, uint unk4, uint unk5, UInt64 targetId, byte unk6);
private Hook<OnActorControlDelegate>? _onActorControlHook;
private delegate void OnCastDelegate(uint sourceId, IntPtr sourceCharacter);
private Hook<OnCastDelegate>? _onCastHook;
private ExcelSheet<LuminaAction>? _actionSheet;
private ExcelSheet<LuminaItem>? _itemSheet;
private Dictionary<uint, uint> _specialCasesMap = new()
{
// MNK
[16475] = 53, // anatman
// SAM
[16484] = 7477, // kaeshi higanbana
[16485] = 7477, // kaeshi goken
[16486] = 7477, // keashi setsugekka
[25782] = 7477, // kaeshi namikiri
// RDM
[25858] = 7504 // resolution
};
private Dictionary<uint, float> _hardcodedCasesMap = new()
{
// NIN
[2259] = 0.5f, // ten
[2261] = 0.5f, // chi
[2263] = 0.5f, // jin
[18805] = 0.5f, // ten
[18806] = 0.5f, // chi
[18807] = 0.5f, // jin
[2265] = 1.5f, // fuma shuriken
[18873] = 1.5f, // fuma shuriken
[18874] = 1.5f, // fuma shuriken
[18875] = 1.5f, // fuma shuriken
[2266] = 1.5f, // katon
[18876] = 1.5f, // katon
[2267] = 1.5f, // raiton
[18877] = 1.5f, // raiton
[2268] = 1.5f, // hyoton
[18878] = 1.5f, // hyoton
[2269] = 1.5f, // huton
[18879] = 1.5f, // huton
[2270] = 1.5f, // doton
[10892] = 1.5f, // doton
[18880] = 1.5f, // doton
[2271] = 1.5f, // suiton
[18881] = 1.5f, // suiton
[2272] = 1.5f, // rabbit medium
[16491] = 1.5f, // goka mekkyaku
[16492] = 1.5f, // hyosho ranryu
};
private static int kMaxItemCount = 50;
private List<TimelineItem> _items = new List<TimelineItem>(kMaxItemCount);
public IReadOnlyCollection<TimelineItem> Items => _items.AsReadOnly();
private Settings Settings => Plugin.Settings;
private double _outOfCombatStartTime = -1;
private bool _hadSwiftcast = false;
private unsafe void Update(IFramework framework)
{
double now = ImGui.GetTime();
CheckOutOfCombat(now);
CheckSwiftcast();
// gcd clipping logic
for (int i = 0; i < _items.Count; i++)
{
TimelineItem item = _items[i];
if (item.Type != TimelineItemType.Action) { continue; }
if (item.GCDDuration == 0) { continue; } // does this ever happen???
if (item.GCDClipData.HasValue && item.GCDClipData.Value.EndTime.HasValue && !item.GCDClipData.Value.IsFakeEndTime) { continue; }
double gcdClipStart = item.Time + Math.Max(0, item.GCDDuration - item.CastTime);
// cast threshold
if (item.CastTime > 0)
{
gcdClipStart += Settings.GCDClippingCastsThreshold;
}
// check if clipped
if (now >= gcdClipStart)
{
var (gcdClipEnd, isFakeEnd) = FindGCDClipEndTime(item, i);
// make sure threshold doesn't break the math
if (gcdClipEnd.HasValue && gcdClipStart > gcdClipEnd.Value)
{
gcdClipStart = gcdClipEnd.Value;
}
// check max time
if (!gcdClipEnd.HasValue && now - gcdClipStart > Settings.GCDClippingMaxTime)
{
gcdClipEnd = now;
isFakeEnd = false;
}
// not clipped?
if (!isFakeEnd && gcdClipEnd.HasValue && Utils.UnderThreshold(gcdClipStart, gcdClipEnd.Value))
{
item.GCDClipData = new GCDClipData(false, 0, 0, false);
}
// clipped :(
else
{
item.GCDClipData = new GCDClipData(true, gcdClipStart, gcdClipEnd, isFakeEnd);
}
}
}
}
private void CheckOutOfCombat(double now)
{
if (!Plugin.Condition[ConditionFlag.InCombat] && _outOfCombatStartTime != -2)
{
if (_outOfCombatStartTime == -1)
{
_outOfCombatStartTime = now;
}
else if (now - _outOfCombatStartTime >= Settings.OutOfCombatClearTime)
{
_items.Clear();
_outOfCombatStartTime = -2;
}
}
else
{
_outOfCombatStartTime = -1;
}
}
private void CheckSwiftcast()
{
IPlayerCharacter? player = Plugin.ClientState.LocalPlayer;
if (player != null)
{
_hadSwiftcast = player.StatusList.Any(s => s.StatusId == 167);
}
}
private (double?, bool) FindGCDClipEndTime(TimelineItem item, int index)
{
if (index >= _items.Count - 1) { return (null, false); }
TimelineItem? prevItem = null;
for (int i = index + 1; i < _items.Count; i++)
{
TimelineItem nextItem = _items[i];
if (nextItem.Type == TimelineItemType.Action)
{
double time = prevItem != null && prevItem.Type == TimelineItemType.CastStart ? prevItem.Time : nextItem.Time;
return (time, false);
}
else if (nextItem.Type == TimelineItemType.CastStart && i == _items.Count - 1)
{
return (nextItem.Time, true);
}
prevItem = nextItem;
}
return (null, false);
}
private unsafe float GetGCDTime(uint actionId)
{
ActionManager* actionManager = ActionManager.Instance();
uint adjustedId = actionManager->GetAdjustedActionId(actionId);
return actionManager->GetRecastTime(ActionType.Action, adjustedId);
}
private unsafe float GetCastTime(uint actionId)
{
ActionManager* actionManager = ActionManager.Instance();
uint adjustedId = actionManager->GetAdjustedActionId(actionId);
return (float)ActionManager.GetAdjustedCastTime(ActionType.Action, adjustedId) / 1000f;
}
private void Add(uint id, TimelineItemType type)
{
if (type == TimelineItemType.Item)
{
AddItem(id);
return;
}
LuminaAction? action = _actionSheet?.GetRowOrDefault(id);
if (action == null || !action.HasValue) { return; }
// only cache the last kMaxItemCount items
if (_items.Count >= kMaxItemCount)
{
_items.RemoveAt(0);
}
double now = ImGui.GetTime();
float gcdDuration = 0;
float castTime = 0;
// handle sprint and auto attack icons
int iconId = id == 3 ? 104 : (id == 1 ? 101 : action.Value.Icon);
// handle weird cases
uint _id = id;
if (_specialCasesMap.TryGetValue(id, out uint replacedId))
{
type = TimelineItemType.Action;
_id = replacedId;
}
// calculate gcd and cast time
if (type == TimelineItemType.CastStart)
{
gcdDuration = GetGCDTime(_id);
castTime = GetCastTime(_id);
}
else if (type == TimelineItemType.Action)
{
TimelineItem? lastItem = _items.LastOrDefault();
if (lastItem != null && lastItem.Type == TimelineItemType.CastStart)
{
gcdDuration = lastItem.GCDDuration;
castTime = lastItem.CastTime;
}
else
{
gcdDuration = GetGCDTime(_id);
castTime = _hadSwiftcast ? 0 : GetCastTime(_id);
}
}
// handle more weird cases
if (_hardcodedCasesMap.TryGetValue(id, out float gcd))
{
type = TimelineItemType.Action;
gcdDuration = gcd;
}
TimelineItem item = new TimelineItem(id, (uint)iconId, type, now, gcdDuration, castTime);
_items.Add(item);
}
private void AddItem(uint id)
{
LuminaItem? item = _itemSheet?.GetRowOrDefault(id) ?? _itemSheet?.GetRowOrDefault(id - 1000000);
if (item == null || !item.HasValue) { return; }
// only cache the last kMaxItemCount items
if (_items.Count >= kMaxItemCount)
{
_items.RemoveAt(0);
}
TimelineItem i = new TimelineItem(
id,
item.Value.Icon,
TimelineItemType.Item,
ImGui.GetTime(),
0,
0
);
_items.Add(i);
}
private TimelineItemType? TypeForID(uint id)
{
LuminaAction? action = _actionSheet?.GetRowOrDefault(id);
if (action != null)
{
return TypeForAction(action.Value);
}
LuminaItem? item = _itemSheet?.GetRowOrDefault(id) ?? _itemSheet?.GetRowOrDefault(id - 1000000);
if (item != null)
{
return TimelineItemType.Item;
}
return TimelineItemType.Action;
}
private TimelineItemType TypeForAction(LuminaAction action)
{
// off gcd or sprint
if (action.ActionCategory.RowId is 4 || action.RowId == 3)
{
return TimelineItemType.OffGCD;
}
if (action.ActionCategory.RowId is 1)
{
return TimelineItemType.AutoAttack;
}
return TimelineItemType.Action;
}
private void OnActionUsed(uint sourceId, IntPtr sourceCharacter, IntPtr pos, IntPtr effectHeader,
IntPtr effectArray, IntPtr effectTrail)
{
_onActionUsedHook?.Original(sourceId, sourceCharacter, pos, effectHeader, effectArray, effectTrail);
IPlayerCharacter ? player = Plugin.ClientState.LocalPlayer;
if (player == null || sourceId != player.GameObjectId) { return; }
int actionId = Marshal.ReadInt32(effectHeader, 0x8);
TimelineItemType? type = TypeForID((uint)actionId);
if (!type.HasValue) { return; }
Add((uint)actionId, type.Value);
}
private void OnActorControl(uint entityId, uint type, uint buffID, uint direct, uint actionId, uint sourceId, uint arg4, uint arg5, ulong targetId, byte a10)
{
_onActorControlHook?.Original(entityId, type, buffID, direct, actionId, sourceId, arg4, arg5, targetId, a10);
if (type != 15) { return; }
IPlayerCharacter? player = Plugin.ClientState.LocalPlayer;
if (player == null || entityId != player.GameObjectId) { return; }
Add(actionId, TimelineItemType.CastCancel);
}
private void OnCast(uint sourceId, IntPtr ptr)
{
_onCastHook?.Original(sourceId, ptr);
IPlayerCharacter? player = Plugin.ClientState.LocalPlayer;
if (player == null || sourceId != player.GameObjectId) { return; }
int value = Marshal.ReadInt16(ptr);
uint actionId = value < 0 ? (uint)(value + 65536) : (uint)value;
Add(actionId, TimelineItemType.CastStart);
}
}
}