-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlips.cpp
More file actions
1254 lines (1118 loc) · 49.3 KB
/
Blips.cpp
File metadata and controls
1254 lines (1118 loc) · 49.3 KB
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of VanillaMinimapTracking.
//
// VanillaMinimapTracking is free software: you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// VanillaMinimapTracking is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with
// VanillaMinimapTracking. If not, see <https://www.gnu.org/licenses/>.
#include "Blips.h"
#include "Common.h"
#include "Game.h"
#include "MinHook.h"
#include "Offsets.h"
#include "event/Custom.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <windows.h>
namespace Blips {
static Game::RenderObjectBlips_t RenderObjectBlips_o = nullptr;
static Game::ObjectEnumProc_t ObjectEnumProc_o = nullptr;
static Game::ClntObjMgrEnumVisibleObjects_t ClntObjMgrEnumVisibleObjects_o = nullptr;
static void *MinimapRender_PartyListing_o = nullptr;
static void *OnLayerTrackUpdate_ChangedGate_o = nullptr;
static void *OnLayerTrackUpdate_PreShowGate_o = nullptr;
static void *OnLayerTrackUpdate_AppendToTooltipBuffer_o = nullptr;
// Registered-texture record. `gxTex` is resolved eagerly at load time so
// per-frame draws skip the `TextureGetGxTex` + `CStatus` round-trip we'd
// otherwise pay for every blip. `texture` is kept so we can re-resolve if
// `gxTex` ever comes back null (e.g. engine not yet ready at load time).
struct TextureCacheEntry {
Game::HTEXTURE__ *texture;
mutable Game::CGxTex *gxTex;
};
struct Blip {
// Pointer into `g_textureCache` — stable across map inserts (unordered_map
// pointer-stability guarantee) and never erased.
const TextureCacheEntry *cacheEntry;
float scale;
};
// One per Lua-registered icon. Holds the data addons need to read back via
// `GetIconList` (label, original icon path) plus the engine handles needed
// at draw time. Kept off the hot per-tracked-object `Blip` struct so we
// don't copy the strings into every `TrackedObjectData` each enum pass.
struct IconRegistration {
std::string label;
std::string iconPath;
Blip blip;
};
struct TrackedObjectData {
uint64_t guid;
Game::C2Vector minimapPos;
bool isInDifferentArea;
std::string name;
std::string subName;
std::string typeName; // matches kBlipTypes[i].typeName (e.g. "vendor", "target")
Blip blip;
};
struct BlipHoverEntry {
uint64_t guid;
std::string name;
std::string subName;
bool gray;
};
struct BlipHoverState {
bool changed = false;
bool nonEmpty = false;
uint64_t hash = 0;
std::vector<BlipHoverEntry> hits;
};
volatile bool g_inLogout = false;
static bool g_hooksInstalled = false;
static std::unordered_map<std::string, TextureCacheEntry> g_textureCache;
static std::unordered_map<std::string, IconRegistration> g_registeredIcons;
// Registration order, for `GetIconList` to return entries in the same order
// the addon passed them to `RegisterIcons` — addons rely on this to drive
// menu layout and the late-wins priority of `GetBestTrackingTexture`.
static std::vector<std::string> g_iconOrder;
static bool g_targetTracking = false;
static uint64_t g_currentTargetGUID = 0;
static bool g_focusTracking = false;
static uint64_t g_focusGUID = 0;
static uint64_t g_playerGUID = 0;
// Resolved once per enum pass alongside g_playerGUID — every CheckObject call
// in NpcFlag-tracked types needs it for the reaction filter, and resolving
// per-call would be a `ClntObjMgrObjectPtr` round trip for every candidate.
static Game::CGUnit_C *g_playerUnit = nullptr;
static std::unordered_set<std::string> g_enabledTypes;
static std::string g_configPath;
static bool g_configLoaded = false;
// Tracked-NPC flag list, sorted by flag value descending. The minimap
// "highest flag wins" rule lets us break on the first match instead of
// scanning every entry. N is tiny (≤ ~10), so linear insert/erase in
// ApplyTrack is cheaper than maintaining a sorted+hash dual structure.
static std::vector<std::pair<uint32_t, Blip>> g_trackedUnitFlagsBlips;
static std::unordered_map<uint32_t, Blip> g_trackedGameObjectTypesBlips;
// OR of every flag in g_trackedUnitFlagsBlips; (unit.m_npcFlags & this) == 0
// is the fast-path bail in CheckObject. Bitmask of (1 << type) for every type
// in g_trackedGameObjectTypesBlips; GO types are 0–30 so fit in 32 bits.
static uint32_t g_combinedNpcFlagMask = 0;
static uint32_t g_combinedGameObjectTypeBits = 0;
static std::vector<TrackedObjectData> g_trackedObjectsData;
static BlipHoverState g_blipHoverState;
// Single source of truth for every tracking type. Drives:
// 1. ApplyTrack (lookup by `typeName`, dispatch on `kind`/`engineValue`)
// 2. The Enum.MinimapBlip Lua table (each row's `enumKey` → `typeName`)
// Adding a new type is one row here.
enum class BlipKind {
Special, // "target" / "focus" — own per-type boolean flag
NpcFlag, // unit's m_npcFlags & engineValue
GameObject, // gameobject's m_type == engineValue
};
struct BlipTypeDef {
const char *enumKey; // PascalCase, exposed as Enum.MinimapBlip.<key>
const char *typeName; // lowercase, the value addons actually pass to C_Minimap.*
BlipKind kind;
uint32_t engineValue; // unused for Special
};
static constexpr BlipTypeDef kBlipTypes[] = {
{"Target", "target", BlipKind::Special, 0},
{"Focus", "focus", BlipKind::Special, 0},
{"Auctioneer", "auctioneer", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_AUCTIONEER},
{"Banker", "banker", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_BANKER},
{"Battlemaster", "battlemaster", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_BATTLEMASTER},
{"FlightMaster", "flight master", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_FLIGHTMASTER},
{"Innkeeper", "innkeeper", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_INNKEEPER},
{"Repair", "repair", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_REPAIR},
{"StableMaster", "stable master", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_STABLEMASTER},
{"SummoningRitualUnit", "summoning ritual unit", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_SUMMONING_RITUAL},
{"Trainer", "trainer", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_TRAINER},
{"Vendor", "vendor", BlipKind::NpcFlag, Game::UNIT_NPC_FLAG_VENDOR},
{"Mailbox", "mailbox", BlipKind::GameObject, Game::GAMEOBJECT_TYPE_MAILBOX},
{"SummoningRitualObject", "summoning ritual object", BlipKind::GameObject, Game::GAMEOBJECT_TYPE_SUMMONING_RITUAL},
};
static const BlipTypeDef *FindBlipType(const std::string &typeName) {
for (const auto &d : kBlipTypes) {
if (typeName == d.typeName)
return &d;
}
return nullptr;
}
static const char *FindBlipTypeNameByEngine(BlipKind kind, uint32_t engineValue) {
for (const auto &d : kBlipTypes) {
if (d.kind == kind && d.engineValue == engineValue)
return d.typeName;
}
return nullptr;
}
static void TrackObject(Game::MINIMAPINFO *info, Game::CGObject_C *objectptr, uint64_t guid,
Blip blip, const char *typeName) {
Game::C2Vector minimapPos;
Game::C3Vector unitPos;
uint32_t wmoID = 0;
uint32_t mapObjID = 0;
uint32_t groupNum = 0;
Game::CWorld_QueryMapObjIDs(objectptr->m_worldData, &wmoID, &mapObjID, &groupNum);
// Hide outside blip when inside, replicating original function
if (info->wmoID && (wmoID != info->wmoID || mapObjID != info->mapObjID))
return;
objectptr->vftable->GetPosition(objectptr, &unitPos);
float unkScale = info->minimapFrame->FrameScriptPart.vftable->GetUnkScale(
&info->minimapFrame->FrameScriptPart);
Game::WorldPosToMinimapFrameCoords(&minimapPos, nullptr, info->currentPos, info->radius,
unitPos.x, unitPos.y, info->layoutScale, unkScale);
std::string subName;
if (objectptr->m_objectType == Game::OBJECT_TYPE::UNIT) {
// Walk unit → creature-cache row → subname string. Either hop can be
// NULL (uncached / unnamed); SafeDeref short-circuits if so.
const auto *unitBytes = reinterpret_cast<const uint8_t *>(objectptr);
const auto *cacheRow = Game::SafeDeref(unitBytes, 0xB30);
const auto *raw = reinterpret_cast<const char *>(Game::SafeDeref(cacheRow, 0x10));
if (raw != nullptr && raw[0] != '\0')
subName = raw;
}
g_trackedObjectsData.push_back(
{guid, minimapPos, wmoID != info->wmoID, objectptr->vftable->GetName(objectptr), subName,
typeName ? typeName : "", blip});
}
static bool CheckObject(Game::MINIMAPINFO *info, uint64_t guid) {
if (g_targetTracking && g_currentTargetGUID != 0 && guid == g_currentTargetGUID &&
guid != g_playerGUID) {
const auto iconIt = g_registeredIcons.find("target");
if (iconIt != g_registeredIcons.end()) {
auto *unitptr = reinterpret_cast<Game::CGUnit_C *>(
Game::ClntObjMgrObjectPtr(Game::TYPE_MASK::TYPEMASK_UNIT, nullptr, guid, 0));
if (unitptr != nullptr) {
TrackObject(info, reinterpret_cast<Game::CGObject_C *>(unitptr), guid,
iconIt->second.blip, "target");
return true;
}
}
return false;
}
if (g_focusTracking && g_focusGUID != 0 && guid == g_focusGUID && guid != g_playerGUID) {
const auto iconIt = g_registeredIcons.find("focus");
if (iconIt != g_registeredIcons.end()) {
auto *unitptr = reinterpret_cast<Game::CGUnit_C *>(
Game::ClntObjMgrObjectPtr(Game::TYPE_MASK::TYPEMASK_UNIT, nullptr, guid, 0));
if (unitptr != nullptr) {
TrackObject(info, reinterpret_cast<Game::CGObject_C *>(unitptr), guid,
iconIt->second.blip, "focus");
return true;
}
}
return false;
}
uint32_t typemask = 0;
if (!g_trackedUnitFlagsBlips.empty()) {
typemask = typemask | Game::TYPE_MASK::TYPEMASK_UNIT;
}
if (!g_trackedGameObjectTypesBlips.empty()) {
typemask = typemask | Game::TYPE_MASK::TYPEMASK_GAMEOBJECT;
}
if (typemask == 0)
return false;
Game::CGObject_C *objptr = Game::ClntObjMgrObjectPtr(typemask, nullptr, guid, 0);
if (objptr == nullptr)
return false;
if (objptr->m_objectType == Game::OBJECT_TYPE::UNIT) {
const auto *unitData = reinterpret_cast<Game::CGUnitData *>(objptr->m_data);
// Fast bail before scanning the per-flag list. Skips the vast
// majority of city NPCs (anyone not flagged for something tracked).
if ((unitData->m_npcFlags & g_combinedNpcFlagMask) == 0)
return false;
// Hide NPCs the player can't interact with (Unfriendly/Hostile/Hated —
// reaction < 4). E.g. a Horde player walking past an Alliance repair
// vendor: still flagged repair, but useless to them. Neutral (4) is
// kept because vendors like the Booty Bay/Gadgetzan goblin AHes are
// genuinely neutral-faction and still tradeable. We skip the check
// when g_playerUnit is null (pre-login / load screen) rather than
// hiding everything — fail open.
if (g_playerUnit != nullptr) {
auto *npcUnit = reinterpret_cast<Game::CGUnit_C *>(objptr);
if (Game::CGUnit_C_UnitReaction(g_playerUnit, npcUnit) < 4)
return false;
}
// Vector is sorted by flag-value descending — first match wins,
// matching the engine's "stable master (0x2000) beats vendor (0x4)" rule.
for (const auto &[flag, tracked] : g_trackedUnitFlagsBlips) {
if (unitData->m_npcFlags & flag) {
TrackObject(info, objptr, guid, tracked,
FindBlipTypeNameByEngine(BlipKind::NpcFlag, flag));
return true;
}
}
} else if (objptr->m_objectType == Game::OBJECT_TYPE::GAMEOBJECT) {
const auto *gameObjectData = reinterpret_cast<Game::CGGameObjectData *>(objptr->m_data);
// 1.12 GO types go up to AURA_GENERATOR (30); the > 31 guard keeps
// the shift defined in case a future patch widens the enum.
if (gameObjectData->m_type > 31 ||
(g_combinedGameObjectTypeBits & (1u << gameObjectData->m_type)) == 0)
return false;
const auto it = g_trackedGameObjectTypesBlips.find(gameObjectData->m_type);
if (it != g_trackedGameObjectTypesBlips.end()) {
TrackObject(info, objptr, guid, it->second,
FindBlipTypeNameByEngine(BlipKind::GameObject, gameObjectData->m_type));
return true;
}
}
return false;
}
static void DrawTrackedBlips(Game::CGMinimapFrame *minimapPtr, Game::DNInfo *dnInfo) {
// We are gathering the position in ObjectEnumProc because it is rate limited to avoid spamming
// expensive calls. To do it in RenderObjectBlips, we can use DNInfo for current position,
// MinimapGetWorldRadius() for world radius and minimapPtr +0x7C for layout scale.
for (const auto &objData : g_trackedObjectsData) {
if (objData.blip.cacheEntry == nullptr)
continue;
Game::DrawMinimapTexture(objData.blip.cacheEntry->gxTex, objData.minimapPos,
objData.blip.scale, objData.isInDifferentArea);
}
}
static bool IsUnitTracked(uint64_t guid) {
if (guid == 0)
return false;
if (g_targetTracking && guid == g_currentTargetGUID)
return true;
if (g_focusTracking && guid == g_focusGUID)
return true;
return false;
}
static void UpdateCustomHover(Game::C2Vector mouse, Game::C2Vector offset) {
std::vector<BlipHoverEntry> now;
now.reserve(g_trackedObjectsData.size());
for (const auto &objData : g_trackedObjectsData) {
const float half = Game::BLIP_HALF * objData.blip.scale;
const float px = objData.minimapPos.x + offset.x;
const float py = objData.minimapPos.y + offset.y;
if (fabsf(mouse.x - px) <= half && fabsf(mouse.y - py) <= half) {
bool dup = false;
for (const auto &existing : now) {
if (existing.name == objData.name && existing.subName == objData.subName) {
dup = true;
break;
}
}
if (!dup) {
now.push_back(
{objData.guid, objData.name, objData.subName, objData.isInDifferentArea});
}
}
}
std::sort(now.begin(), now.end(), [](const auto &a, const auto &b) { return a.guid < b.guid; });
// FNV-1a hash
uint64_t h = 14695981039346656037ULL;
auto mix = [&](uint64_t v) {
h ^= v;
h *= 1099511628211ULL;
};
for (const auto &hit : now) {
mix(hit.guid);
mix(hit.gray ? 1ULL : 0ULL);
mix(std::hash<std::string_view>{}(hit.name));
mix(std::hash<std::string_view>{}(hit.subName));
}
g_blipHoverState.nonEmpty = !now.empty();
const bool changed = (h != g_blipHoverState.hash);
g_blipHoverState.changed = changed;
if (changed) {
g_blipHoverState.hash = h;
g_blipHoverState.hits.swap(now);
}
}
static void WriteToMinimapTooltip(char *tooltipText) {
if (g_blipHoverState.hits.empty())
return;
for (const auto &hit : g_blipHoverState.hits) {
if (hit.gray)
Game::SStrPack(tooltipText, "|cffb0b0b0", 0x400);
Game::SStrPack(tooltipText, hit.name.c_str(), 0x400);
if (!hit.subName.empty()) {
Game::SStrPack(tooltipText, "\n<", 0x400);
Game::SStrPack(tooltipText, hit.subName.c_str(), 0x400);
Game::SStrPack(tooltipText, ">", 0x400);
}
if (hit.gray)
Game::SStrPack(tooltipText, "|r", 0x400);
Game::SStrPack(tooltipText, "\n", 0x400);
}
}
static int __fastcall
ClntObjMgrEnumVisibleObjects_h(Game::ClntObjMgrEnumVisibleObjectsCallback_t callback,
void *context) {
// Once `CGGameUI_Shutdown` has begun, the engine is in mid-teardown and
// any path through the trampoline risks crashing inside the
// object-manager deref. Bail without invoking the trampoline.
if (g_inLogout) {
return 0;
}
// The trampoline's very first instruction is `MOV EAX,[VAR_OBJECT_MANAGER_PTR]`
// followed by a `[EAX + 0xAC]` deref. On the glue/character-select screen
// that pointer is NULL and the engine itself never calls this function
// there — but our DLL (and anything that drives our Lua API) might.
if (*reinterpret_cast<void *volatile *>(Offsets::VAR_OBJECT_MANAGER_PTR) == nullptr) {
return 0;
}
if (reinterpret_cast<uintptr_t>(callback) == Offsets::FUN_OBJECT_ENUM_PROC) {
g_trackedObjectsData.clear();
g_playerGUID = Game::GetGUIDFromName("player");
g_playerUnit = (g_playerGUID != 0)
? reinterpret_cast<Game::CGUnit_C *>(Game::ClntObjMgrObjectPtr(
Game::TYPE_MASK::TYPEMASK_UNIT | Game::TYPE_MASK::TYPEMASK_PLAYER,
nullptr, g_playerGUID, 0))
: nullptr;
if (g_targetTracking) {
g_currentTargetGUID = Game::GetGUIDFromName("target");
}
}
return ClntObjMgrEnumVisibleObjects_o(callback, context);
}
static int __fastcall ObjectEnumProc_h(Game::MINIMAPINFO *info, uint64_t guid) {
if (g_inLogout)
return 1;
if (!CheckObject(info, guid))
ObjectEnumProc_o(info, guid);
return 1; // The original function always seems to return 1
}
static void __fastcall RenderObjectBlips_h(Game::CGMinimapFrame *thisptr, void * /*edx*/,
Game::DNInfo *dnInfo) {
if (g_inLogout)
return;
RenderObjectBlips_o(thisptr, dnInfo);
DrawTrackedBlips(thisptr, dnInfo);
}
constexpr uintptr_t minimapSkipPartyUnitAddress = 0x4ed79e;
static void __declspec(naked) MinimapRender_PartyListing_h() {
__asm {
pushad
mov eax, [edi + 0xbc7660] // GUID low
mov edx, [edi + 0xbc7664] // GUID high
push edx
push eax
call IsUnitTracked
add esp, 8
test al, al
jnz skip
popad
jmp dword ptr [MinimapRender_PartyListing_o]
skip:
popad
jmp minimapSkipPartyUnitAddress // skip processing this unit
}
}
// Right before the early-out check uses ECX to decide if anything changed
static void __declspec(naked) OnLayerTrackUpdate_ChangedGate_h() {
__asm {
pushad
mov eax, [ebx+0x8] // param pointer
push dword ptr [ebp-0x40] // offsetY
push dword ptr [ebp-0x3C] // offsetX
push dword ptr [eax+0x28] // mouseY
push dword ptr [eax+0x24] // mouseX
call UpdateCustomHover
add esp, 16
popad
// If our set changed, force ECX=1 (stock "changed" flag)
cmp byte ptr [g_blipHoverState.changed], 0
je short no_custom_change
mov ecx, 1
no_custom_change:
jmp dword ptr [OnLayerTrackUpdate_ChangedGate_o]
}
}
// Right before the branch that decides whether to build/update the tooltip text
static void __declspec(naked) OnLayerTrackUpdate_PreShowGate_h() {
__asm {
// If we have any changes, force EDX=1 so the stock code builds the tooltip
cmp byte ptr [g_blipHoverState.changed], 0
je short no_set_edx
mov edx, 1
no_set_edx:
jmp dword ptr [OnLayerTrackUpdate_PreShowGate_o]
}
}
// After the scratch tooltip buffer is initialized, before original blips text are appended
static void __declspec(naked) OnLayerTrackUpdate_AppendToTooltipBuffer_h() {
__asm {
// Append into stock scratch buffer [EBP-0x468]
pushad
lea eax, [ebp-0x468]
push eax
call WriteToMinimapTooltip
add esp, 4
popad
jmp dword ptr [OnLayerTrackUpdate_AppendToTooltipBuffer_o]
}
}
static bool InstallHooks() {
if (g_hooksInstalled)
return TRUE;
HOOK_FUNCTION(Offsets::FUN_CLNT_OBJ_MGR_ENUM_VISIBLE_OBJECTS, ClntObjMgrEnumVisibleObjects_h,
ClntObjMgrEnumVisibleObjects_o);
HOOK_FUNCTION(Offsets::FUN_OBJECT_ENUM_PROC, ObjectEnumProc_h, ObjectEnumProc_o);
HOOK_FUNCTION(Offsets::FUN_RENDER_OBJECT_BLIP, RenderObjectBlips_h, RenderObjectBlips_o);
HOOK_FUNCTION(Offsets::PATCH_MINIMAP_RENDER_PARTY_LISTING, MinimapRender_PartyListing_h,
MinimapRender_PartyListing_o);
HOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_CHANGED_GATE,
OnLayerTrackUpdate_ChangedGate_h, OnLayerTrackUpdate_ChangedGate_o);
HOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_PRE_SHOW_GATE,
OnLayerTrackUpdate_PreShowGate_h, OnLayerTrackUpdate_PreShowGate_o);
HOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_APPEND_TO_TOOLTIP_BUFFER,
OnLayerTrackUpdate_AppendToTooltipBuffer_h,
OnLayerTrackUpdate_AppendToTooltipBuffer_o);
g_hooksInstalled = true;
return TRUE;
}
static bool UninstallHooks() {
if (!g_hooksInstalled)
return TRUE;
// The render hook is about to go away; drop the last enum pass's blip
// list so a future re-install can't draw from stale data before the
// first refill of `g_trackedObjectsData` lands. Same for hover hits —
// they cache whichever blips the cursor was last over.
g_trackedObjectsData.clear();
g_blipHoverState = BlipHoverState();
UNHOOK_FUNCTION(Offsets::FUN_CLNT_OBJ_MGR_ENUM_VISIBLE_OBJECTS, ClntObjMgrEnumVisibleObjects_o);
UNHOOK_FUNCTION(Offsets::FUN_OBJECT_ENUM_PROC, ObjectEnumProc_o);
UNHOOK_FUNCTION(Offsets::FUN_RENDER_OBJECT_BLIP, RenderObjectBlips_o);
UNHOOK_FUNCTION(Offsets::PATCH_MINIMAP_RENDER_PARTY_LISTING, MinimapRender_PartyListing_o);
UNHOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_CHANGED_GATE,
OnLayerTrackUpdate_ChangedGate_o);
UNHOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_PRE_SHOW_GATE,
OnLayerTrackUpdate_PreShowGate_o);
UNHOOK_FUNCTION(Offsets::PATCH_MINIMAP_TRACK_UPDATE_APPEND_TO_TOOLTIP_BUFFER,
OnLayerTrackUpdate_AppendToTooltipBuffer_o);
g_hooksInstalled = false;
return TRUE;
}
// Resolves and caches the engine-side `CGxTex *` for an entry. Called once
// at load and again only if the eager resolve returned null (engine wasn't
// ready). Returning the same null is fine — the per-frame draw path bails
// silently when `gxTex` is null.
static void ResolveGxTex(const TextureCacheEntry &entry) {
if (entry.gxTex != nullptr || entry.texture == nullptr)
return;
Game::CStatus status;
Game::CGxTex *gxTex = Game::TextureGetGxTex(entry.texture, 1, &status);
if (status.ok())
entry.gxTex = gxTex;
}
static const TextureCacheEntry *LoadTextureCached(const std::string &texturePathLower) {
if (const auto it = g_textureCache.find(texturePathLower); it != g_textureCache.end()) {
// Retry the GxTex resolve in case the first attempt happened before
// the engine was ready. Cheap when already resolved (early return).
ResolveGxTex(it->second);
return &it->second;
}
Game::CGxTexFlags texFlags(Game::EGxTexFilter::GxTex_Nearest, 0, 0, 0, 0, 0, 0, 1);
Game::CStatus status;
Game::HTEXTURE__ *texture =
Game::TextureCreate(texturePathLower.c_str(), &status, texFlags, 0, 1);
if (!status.ok()) {
return nullptr;
}
auto [it, inserted] = g_textureCache.emplace(texturePathLower, TextureCacheEntry{texture, nullptr});
ResolveGxTex(it->second);
return &it->second;
}
static bool EnsureConfigLoaded(); // defined below; forward-declared so the
// public Script_* entry points can call it
static void SyncRuntimeFromIntent();
// Inserts or updates a registration entry. First registration of a given
// type name appends to `g_iconOrder`; re-registration updates fields in
// place and leaves the order untouched. Returns false if the texture
// failed to load (caller decides whether that's fatal).
static bool RegisterIconInternal(const std::string &typeName, const std::string &iconPath,
const std::string &label, float scale) {
const TextureCacheEntry *entry = LoadTextureCached(iconPath);
if (entry == nullptr)
return false;
auto [it, inserted] = g_registeredIcons.emplace(
typeName, IconRegistration{label, iconPath, {entry, scale}});
if (inserted) {
g_iconOrder.push_back(typeName);
} else {
it->second = IconRegistration{label, iconPath, {entry, scale}};
}
return true;
}
static int __fastcall Script_MinimapBlip_RegisterIcon(void *L) {
if (!Game::Lua::IsString(L, 1) || !Game::Lua::IsString(L, 2)) {
Game::Lua::Error(L, "Usage: C_Minimap.RegisterIcon(trackingType, icon [, scale [, label]])");
return 0;
}
const std::string typeName = Game::Lua::ToString(L, 1);
std::string texturePath = Game::Lua::ToString(L, 2);
std::transform(texturePath.begin(), texturePath.end(), texturePath.begin(), ::tolower);
float scale = 1.0F;
if (Game::Lua::IsNumber(L, 3)) {
scale = static_cast<float>(Game::Lua::ToNumber(L, 3));
}
std::string label;
if (Game::Lua::IsString(L, 4)) {
if (const char *s = Game::Lua::ToString(L, 4)) {
label = s;
}
}
if (!RegisterIconInternal(typeName, texturePath, label, scale)) {
Game::Lua::Error(L, "Couldn't load texture.");
return 0;
}
// Pull saved intent from disk (if not already), then bring this type's
// runtime state up if the addon previously had it tracked. Order matters:
// the icon must be in g_registeredIcons before SyncRuntimeFromIntent so
// ApplyTrack can find it.
EnsureConfigLoaded();
SyncRuntimeFromIntent();
return 0;
}
// Bulk-register icons from a Lua array of `{ type, icon, scale }`
// entries. Equivalent to a loop of C_Minimap.RegisterIcon
static int __fastcall Script_MinimapBlip_RegisterIcons(void *L) {
if (!Game::Lua::IsTable(L, 1)) {
Game::Lua::Error(L, "Usage: C_Minimap.RegisterIcons({ {type, icon, scale}, ... })");
return 0;
}
// Helpers that read a field off the entry-table at the given relative index.
// Pushing the key shifts negative indices by one, so we adjust before
// calling lua_gettable. The string variant copies into `out` BEFORE the
// pop so we don't hand back a pointer into Lua-managed memory that GC
// could free.
auto adjust = [](int idx) { return idx < 0 ? idx - 1 : idx; };
auto readField = [&](int tableStackIdx, const char *field, std::string &out) -> bool {
Game::Lua::PushString(L, field);
Game::Lua::GetTable(L, adjust(tableStackIdx));
bool ok = false;
if (Game::Lua::IsString(L, -1)) {
if (const char *s = Game::Lua::ToString(L, -1)) {
out = s;
ok = true;
}
}
Game::Lua::SetTop(L, -2); // pop looked-up value
return ok;
};
auto readScale = [&](int tableStackIdx) -> float {
Game::Lua::PushString(L, "scale");
Game::Lua::GetTable(L, adjust(tableStackIdx));
const float v = Game::Lua::IsNumber(L, -1)
? static_cast<float>(Game::Lua::ToNumber(L, -1))
: 1.0F;
Game::Lua::SetTop(L, -2);
return v;
};
Game::Lua::PushNil(L);
while (Game::Lua::Next(L, 1) != 0) {
// Stack: [array, key, entry]
if (Game::Lua::IsTable(L, -1)) {
std::string typeName, iconPath, label;
const bool hasType = readField(-1, "type", typeName);
const bool hasIcon = readField(-1, "icon", iconPath);
readField(-1, "label", label); // optional; empty string if absent
const float scale = readScale(-1);
if (hasType && hasIcon) {
std::transform(iconPath.begin(), iconPath.end(), iconPath.begin(),
::tolower);
RegisterIconInternal(typeName, iconPath, label, scale);
}
}
Game::Lua::SetTop(L, -2); // pop value, keep key for next iteration
}
// Now that every icon in this batch is in g_registeredIcons, load the
// saved intent (if not yet) and bring up runtime state for any tracked
// types whose icons were just supplied.
EnsureConfigLoaded();
SyncRuntimeFromIntent();
return 0;
}
enum class ApplyResult { Applied, NoChange, UnknownType, IconMissing };
// Toggles tracking for a single type. `typeName` must be lowercase — this is
// the canonical key form used throughout (`g_stringToFlag`, the
// `"target"`/`"focus"` literal compares below, and `g_enabledTypes`).
static ApplyResult ApplyTrack(const std::string &typeName, bool enabled) {
auto recordEnabled = [&](bool on) {
if (on)
g_enabledTypes.insert(typeName);
else
g_enabledTypes.erase(typeName);
};
const BlipTypeDef *def = FindBlipType(typeName);
if (def == nullptr)
return ApplyResult::UnknownType;
auto needIcon = [&]() -> const Blip * {
const auto it = g_registeredIcons.find(typeName);
return (it == g_registeredIcons.end()) ? nullptr : &it->second.blip;
};
if (def->kind == BlipKind::Special) {
bool *flag = (typeName == "target") ? &g_targetTracking : &g_focusTracking;
if (enabled == *flag)
return ApplyResult::NoChange;
if (enabled && needIcon() == nullptr)
return ApplyResult::IconMissing;
*flag = enabled;
recordEnabled(enabled);
return ApplyResult::Applied;
}
if (def->kind == BlipKind::NpcFlag) {
const auto it = std::find_if(
g_trackedUnitFlagsBlips.begin(), g_trackedUnitFlagsBlips.end(),
[&](const auto &p) { return p.first == def->engineValue; });
const bool currently = it != g_trackedUnitFlagsBlips.end();
if (enabled == currently)
return ApplyResult::NoChange;
if (enabled) {
const Blip *icon = needIcon();
if (icon == nullptr)
return ApplyResult::IconMissing;
// Keep descending order so the CheckObject loop breaks on the
// highest-priority flag (the "stable master beats vendor" rule).
const auto pos = std::lower_bound(
g_trackedUnitFlagsBlips.begin(), g_trackedUnitFlagsBlips.end(), def->engineValue,
[](const auto &p, uint32_t f) { return p.first > f; });
g_trackedUnitFlagsBlips.insert(pos, {def->engineValue, *icon});
g_combinedNpcFlagMask |= def->engineValue;
} else {
g_trackedUnitFlagsBlips.erase(it);
g_combinedNpcFlagMask &= ~def->engineValue;
}
} else {
// BlipKind::GameObject
const bool currently =
g_trackedGameObjectTypesBlips.find(def->engineValue) !=
g_trackedGameObjectTypesBlips.end();
if (enabled == currently)
return ApplyResult::NoChange;
if (enabled) {
const Blip *icon = needIcon();
if (icon == nullptr)
return ApplyResult::IconMissing;
g_trackedGameObjectTypesBlips[def->engineValue] = *icon;
g_combinedGameObjectTypeBits |= (1u << def->engineValue);
} else {
g_trackedGameObjectTypesBlips.erase(def->engineValue);
g_combinedGameObjectTypeBits &= ~(1u << def->engineValue);
}
}
recordEnabled(enabled);
return ApplyResult::Applied;
}
static void EnsureParentDir(const std::string &filePath) {
const size_t lastSlash = filePath.find_last_of("/\\");
if (lastSlash == std::string::npos)
return;
const std::string dir = filePath.substr(0, lastSlash);
size_t pos = 0;
while (true) {
pos = dir.find_first_of("/\\", pos + 1);
const std::string sub = (pos == std::string::npos) ? dir : dir.substr(0, pos);
if (!sub.empty())
CreateDirectoryA(sub.c_str(), nullptr);
if (pos == std::string::npos)
break;
}
}
// Flushes the in-memory `g_enabledTypes` set to disk. Mirrors how WoW writes
// AddOns.txt — only on UI shutdown (clean logout / `/reload`), not on every
// toggle. Called from `CGGameUI_Shutdown_h` before `Reset()`.
void Save() {
if (g_configPath.empty())
return;
EnsureParentDir(g_configPath);
std::ofstream file(g_configPath, std::ios::trunc);
if (!file.is_open())
return;
file << "# VanillaMinimapTracking — enabled tracking categories (one per line)\n";
for (const auto &name : g_enabledTypes)
file << name << "\n";
}
static const char *const kTrackingChangedEvent = "MINIMAP_UPDATE_TRACKING";
static const Event::Custom::AutoReserve _reserveTrackingChanged{kTrackingChangedEvent};
// Notifies listeners that the tracked-set changed. `arg1` is the type
// name that changed (so a row keyed on its own type can fast-path past
// other rows' updates), or `""` for `ClearAllTracking` — a sentinel for
// "everything changed, re-query whatever you care about." Listeners
// still call back into `C_Minimap.IsTracked` for the on/off state;
// `arg1` exists for dispatch, not as the source of truth.
static void FireTrackingChanged(const char *typeName) {
const int eventID = Event::Custom::Lookup(kTrackingChangedEvent);
Event::Custom::Fire_S(eventID, typeName != nullptr ? typeName : "");
}
static const char *ReadActiveAccountName();
static const char *ReadActiveRealmName();
static const char *ReadActiveCharacterName();
static int __fastcall Script_MinimapBlip_Track(void *L) {
if (!Game::Lua::IsString(L, 1)) {
Game::Lua::Error(L, "Usage: C_Minimap.Track(trackingType [, enabled])");
return 0;
}
const std::string typeName = Game::Lua::ToString(L, 1);
bool enabled = true;
if (Game::Lua::IsNumber(L, 2))
enabled = (Game::Lua::ToNumber(L, 2) != 0.0);
EnsureConfigLoaded();
const ApplyResult r = ApplyTrack(typeName, enabled);
if (r == ApplyResult::UnknownType) {
Game::Lua::Error(
L, "Unknown tracking type. Supported types: target, focus, auctioneer, banker, "
"battlemaster, flight master, innkeeper, item restore, mailbox, outdoor pvp, "
"repair, stable master, trainer, transmog, vendor.");
return 0;
}
if (r == ApplyResult::IconMissing) {
Game::Lua::Error(L, "No icon registered for this type. Call "
"C_Minimap.RegisterIcon first.");
return 0;
}
if (r == ApplyResult::Applied) {
FireTrackingChanged(typeName.c_str());
}
return 0;
}
// Flips the tracked state of `type` without the caller having to know its
// current value — UI handlers just call `C_Minimap.Toggle(t)`.
static int __fastcall Script_MinimapBlip_Toggle(void *L) {
if (!Game::Lua::IsString(L, 1)) {
Game::Lua::Error(L, "Usage: C_Minimap.Toggle(trackingType)");
return 0;
}
const std::string typeName = Game::Lua::ToString(L, 1);
EnsureConfigLoaded();
const bool nextState = (g_enabledTypes.count(typeName) == 0);
const ApplyResult r = ApplyTrack(typeName, nextState);
if (r == ApplyResult::UnknownType) {
Game::Lua::Error(L, "Unknown tracking type.");
return 0;
}
if (r == ApplyResult::IconMissing) {
Game::Lua::Error(L, "No icon registered for this type. Call "
"C_Minimap.RegisterIcon first.");
return 0;
}
if (r == ApplyResult::Applied) {
FireTrackingChanged(typeName.c_str());
}
return 0;
}
// Drops every currently-tracked category in one shot. Fires the event a
// single time at the end with `arg1=""` (the "everything changed"
// sentinel) so the menu can do one refresh instead of N.
static int __fastcall Script_MinimapBlip_ClearAllTracking(void * /*L*/) {
EnsureConfigLoaded();
// ApplyTrack mutates g_enabledTypes, so iterate a snapshot.
std::vector<std::string> snapshot(g_enabledTypes.begin(), g_enabledTypes.end());
bool anyChanged = false;
for (const auto &name : snapshot) {
if (ApplyTrack(name, false) == ApplyResult::Applied)
anyChanged = true;
}
if (anyChanged)
FireTrackingChanged("");
return 0;
}
static int __fastcall Script_MinimapBlip_IsTracked(void *L) {
if (!Game::Lua::IsString(L, 1)) {
Game::Lua::Error(L, "Usage: C_Minimap.IsTracked(trackingType)");
return 0;
}
const std::string typeName = Game::Lua::ToString(L, 1);
EnsureConfigLoaded();
if (g_enabledTypes.count(typeName) == 0)
return 0; // nil in Lua → falsy
Game::Lua::PushNumber(L, 1.0);
return 1;
}
// Returns a Lua array of GUID hex strings for every tracked object currently
// rendered on the minimap. Optional first arg filters by type (e.g.
// `C_Minimap.ListVisibleGUIDs("vendor")`).
static int __fastcall Script_MinimapBlip_ListVisibleGUIDs(void *L) {
std::string filter;
bool hasFilter = false;
if (Game::Lua::IsString(L, 1)) {
filter = Game::Lua::ToString(L, 1);
hasFilter = true;
}
Game::Lua::SetTop(L, 0);
Game::Lua::NewTable(L);
int idx = 1;
char buf[24];
for (const auto &obj : g_trackedObjectsData) {
if (hasFilter && obj.typeName != filter)
continue;
snprintf(buf, sizeof(buf), "0x%016llX", static_cast<unsigned long long>(obj.guid));
Game::Lua::PushNumber(L, static_cast<double>(idx));
Game::Lua::PushString(L, buf);
Game::Lua::SetTable(L, -3);
idx++;
}
return 1;
}
// Returns a Lua array of `{type, label, icon}` tables in registration order
// — same shape addons originally passed to `RegisterIcons`. Lets the addon
// stop carrying its own copy of the icon list and read it back from the
// engine instead, so the source of truth lives in one place.
static int __fastcall Script_MinimapBlip_GetIconList(void *L) {
Game::Lua::SetTop(L, 0);
Game::Lua::NewTable(L);
int idx = 1;
for (const auto &typeName : g_iconOrder) {
const auto it = g_registeredIcons.find(typeName);
if (it == g_registeredIcons.end())
continue;
const auto ® = it->second;
Game::Lua::PushNumber(L, static_cast<double>(idx));
Game::Lua::NewTable(L);
Game::Lua::PushString(L, "type");
Game::Lua::PushString(L, typeName.c_str());
Game::Lua::SetTable(L, -3);
Game::Lua::PushString(L, "label");
Game::Lua::PushString(L, reg.label.c_str());
Game::Lua::SetTable(L, -3);
Game::Lua::PushString(L, "icon");
Game::Lua::PushString(L, reg.iconPath.c_str());
Game::Lua::SetTable(L, -3);
Game::Lua::SetTable(L, -3);
idx++;
}
return 1;
}
// Returns a Lua set keyed by lowercase type name (`{ target = 1, vendor = 1 }`).
// Lets callers do a direct `tracked[name]` lookup without building their own
// set from a list.
static int __fastcall Script_MinimapBlip_GetTracked(void *L) {