-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRitualHelperCore.cs
More file actions
1063 lines (939 loc) · 45.6 KB
/
Copy pathRitualHelperCore.cs
File metadata and controls
1063 lines (939 loc) · 45.6 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
// <copyright file="RitualHelperCore.cs" company="None">
// Copyright (c) None. All rights reserved.
// </copyright>
namespace RitualHelper
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Text;
using GameHelper;
using GameHelper.Plugin;
using GameHelper.Utils;
using GameHelper.RemoteEnums;
using GameHelper.RemoteObjects.Components;
using GameHelper.RemoteObjects.States;
using GameHelper.RemoteObjects.States.InGameStateObjects;
using GameOffsets.Objects.States.InGameState;
using GameOffsets.Objects.UiElement;
using GameOffsets.Natives;
using ImGuiNET;
using Newtonsoft.Json;
/// <summary>
/// <see cref="RitualHelperCore"/> plugin.
/// Reads item names from the Ritual shop (Favours window)
/// and displays them in an overlay.
/// </summary>
public sealed class RitualHelperCore : PCore<RitualHelperSettings>
{
// Rarity colors (ABGR format for ImGui)
private static readonly Vector4 ColorNormalV4 = new(1f, 1f, 1f, 1f);
private static readonly Vector4 ColorMagicV4 = new(0.52f, 0.52f, 1f, 1f);
private static readonly Vector4 ColorRareV4 = new(1f, 1f, 0.35f, 1f);
private static readonly Vector4 ColorUniqueV4 = new(1f, 0.45f, 0f, 1f);
private static readonly Vector4 ColorCurrencyV4 = new(0.67f, 1f, 0.5f, 1f);
private static readonly Vector4 ColorGemV4 = new(0.2f, 0.8f, 0.8f, 1f);
private static readonly Vector4 ColorHeaderV4 = new(1f, 0.84f, 0f, 1f);
private static int debugVisitedCount = 0;
private static string debugLastSigSearchStatus = "Not scanned yet";
private static readonly List<string> debugSampleTexts = new();
private string SettingPathname => Path.Join(this.DllDirectory, "config", "settings.txt");
private IntPtr scannedGridAddr = IntPtr.Zero;
private IntPtr lastUiRoot = IntPtr.Zero;
private DateTime nextScanUtc = DateTime.MinValue;
/// <inheritdoc/>
public override void DrawSettings()
{
ImGui.Checkbox(PluginText.T("ritualhelper.show_overlay", "Show Ritual Overlay"), ref this.Settings.ShowOverlay);
ImGui.Checkbox(PluginText.T("ritualhelper.debug_mode", "Debug Mode (Show All Inventories)"), ref this.Settings.DebugMode);
ImGui.Checkbox(PluginText.T("ritualhelper.force_bfs", "Force BFS window search (testing)"), ref this.Settings.ForceBfsFallback);
ImGui.Separator();
ImGui.Checkbox(PluginText.T("ritualhelper.draw_wisp_circle", "Draw Ritual Wisp Range Circle"), ref this.Settings.DrawWispCircle);
if (this.Settings.DrawWispCircle)
{
ImGui.Indent();
ImGui.Checkbox(PluginText.T("ritualhelper.hide_wisp_in_bg", "Hide Wisp Circle when game is in background or paused"), ref this.Settings.HideWispCircleInBackgroundOrPaused);
ImGui.DragFloat(PluginText.T("ritualhelper.wisp_radius", "Circle Radius (Meters)##WispRadius"), ref this.Settings.WispCircleRadiusMeters, 0.1f, 0.5f, 10f, "%.1f m");
ImGui.DragFloat(PluginText.T("ritualhelper.wisp_thickness", "Circle Thickness##WispThickness"), ref this.Settings.WispCircleThickness, 0.1f, 0.5f, 10f, "%.1f px");
var colInside = this.Settings.WispCircleColorInside;
if (ImGui.ColorEdit4(PluginText.T("ritualhelper.wisp_color_inside", "Color (Inside Range)##WispInsideCol"), ref colInside))
{
this.Settings.WispCircleColorInside = colInside;
}
var colOutside = this.Settings.WispCircleColorOutside;
if (ImGui.ColorEdit4(PluginText.T("ritualhelper.wisp_color_outside", "Color (Outside Range)##WispOutsideCol"), ref colOutside))
{
this.Settings.WispCircleColorOutside = colOutside;
}
ImGui.DragFloat(PluginText.T("ritualhelper.wisp_offset_x", "Offset Center X##WispOffsetX"), ref this.Settings.WispCircleOffsetX, 0.5f, -500f, 500f, "%.1f");
ImGui.DragFloat(PluginText.T("ritualhelper.wisp_offset_y", "Offset Center Y##WispOffsetY"), ref this.Settings.WispCircleOffsetY, 0.5f, -500f, 500f, "%.1f");
ImGui.DragFloat(PluginText.T("ritualhelper.wisp_offset_z", "Offset Height Z##WispOffsetZ"), ref this.Settings.WispCircleOffsetZ, 0.5f, -500f, 500f, "%.1f");
ImGui.Unindent();
}
ImGui.Separator();
ImGui.TextWrapped(PluginText.T("ritualhelper.description_text",
"This plugin uses a signature-based BFS scan to locate the post-ritual tribute shop " +
"and displays prices below each item. Open the Ritual window in-game for items to appear."));
if (this.Settings.DebugMode)
{
ImGui.Separator();
ImGui.Text(PluginText.F("ritualhelper.debug.ui_root", "UI Root Address: 0x{0:X}", Core.States.InGameStateObject.GameUi.Address.ToInt64()));
ImGui.Text(PluginText.F("ritualhelper.debug.scanned_grid", "Scanned Grid Address: 0x{0:X}", this.scannedGridAddr.ToInt64()));
ImGui.Text(PluginText.F("ritualhelper.debug.last_scan_status", "Last Scan Status: {0}", debugLastSigSearchStatus));
ImGui.Text(PluginText.F("ritualhelper.debug.visited_count", "Visited Elements Count: {0}", debugVisitedCount));
if (ImGui.TreeNode(PluginText.T("ritualhelper.debug.sample_texts_scanned", "Sample Text Values Scanned")))
{
lock (debugSampleTexts)
{
foreach (var s in debugSampleTexts)
{
ImGui.Text(s);
}
}
ImGui.TreePop();
}
}
}
/// <inheritdoc/>
public override void OnDisable()
{
Mem.Close();
}
/// <inheritdoc/>
public override void DrawUI()
{
if (Core.States.GameCurrentState is not (GameStateTypes.InGameState or GameStateTypes.EscapeState))
{
return;
}
var inGameState = Core.States.InGameStateObject;
var areaInstance = inGameState?.CurrentAreaInstance;
if (areaInstance != null && this.Settings.DrawWispCircle)
{
bool shouldDrawWisp = true;
if (this.Settings.HideWispCircleInBackgroundOrPaused)
{
if (!Core.Process.Foreground || Core.States.GameCurrentState != GameStateTypes.InGameState)
{
shouldDrawWisp = false;
}
}
if (shouldDrawWisp)
{
this.DrawWispOverlay(inGameState, areaInstance);
}
}
// General checks for pricing overlay / debug mode
if (Core.States.GameCurrentState != GameStateTypes.InGameState)
{
return;
}
if (!Core.Process.Foreground)
{
return;
}
if (!this.Settings.ShowOverlay && !this.Settings.DebugMode)
{
return;
}
try
{
string currentClipboard = "";
try { currentClipboard = ImGui.GetClipboardText() ?? ""; } catch { }
bool clipboardChanged = false;
if (currentClipboard != this.lastClipboardText && currentClipboard.StartsWith("Item Class:"))
{
clipboardChanged = true;
this.lastClipboardText = currentClipboard;
}
var uiRoot = Core.States.InGameStateObject.GameUi.Address;
if (uiRoot != IntPtr.Zero)
{
if (uiRoot != this.lastUiRoot)
{
this.scannedGridAddr = IntPtr.Zero;
this.lastUiRoot = uiRoot;
this.nextScanUtc = DateTime.MinValue;
}
// FAST PATH: fixed index chain root.children[76].children[13] = reward grid.
var fastGridAddr = IntPtr.Zero;
var fastChainValid = false;
if (ChildSpan(uiRoot, out var rootFirst, out var rootCount) && rootCount > 76)
{
var child76 = Ptr(rootFirst + 76 * 8);
if (child76 != IntPtr.Zero && ChildSpan(child76, out var child76First, out var child76Count) && child76Count > 13)
{
fastGridAddr = Ptr(child76First + 13 * 8);
fastChainValid = fastGridAddr != IntPtr.Zero;
}
}
var grid = IntPtr.Zero;
if (!this.Settings.ForceBfsFallback && fastChainValid)
{
var flags = Mem.Read<uint>(fastGridAddr + 0x180);
if (UiElementBaseFuncs.IsVisibleChecker(flags))
{
grid = fastGridAddr;
}
}
else
{
this.TryScanRitualWindowThrottled(uiRoot, out grid);
}
if (grid != IntPtr.Zero && ChildSpan(grid, out var gf, out var gn))
{
var fgDraw = ImGui.GetForegroundDrawList();
float winW = Core.Process.WindowArea.Width;
float winH = Core.Process.WindowArea.Height;
for (long i = 0; i < gn; i++)
{
var tile = Ptr(gf + (int)(i * 8));
var item = TileItem(tile);
if (item == IntPtr.Zero) continue;
var identity = ReadIdentityFromItem(item);
if (!TryUiElementRect(tile, winW, winH, out var tx, out var ty, out var tw, out var th)) continue;
PoeNinjaPrice priceInfo = null;
string itemName = "";
bool isMapped = false;
if (identity.Rarity == Rarity.Unique)
{
// Check for runic suffix (Runeforged / Runemastered)
var modsComp = ResolveComponent(item, "Mods");
var runicSuffix = GetRunicSuffix(modsComp);
if (!string.IsNullOrEmpty(runicSuffix))
{
if (!string.IsNullOrEmpty(identity.InternalNameOnly))
{
itemName = this.GetPrettyName(identity.InternalNameOnly, out isMapped);
if (!string.IsNullOrEmpty(itemName))
{
priceInfo = PoeNinjaPriceFetcher.GetPrice(itemName + " " + runicSuffix);
}
}
}
if (priceInfo == null && !string.IsNullOrEmpty(identity.Art))
{
priceInfo = PoeNinjaPriceFetcher.GetPriceByArt(identity.Art);
}
if (priceInfo == null && !string.IsNullOrEmpty(identity.InternalNameOnly))
{
itemName = this.GetPrettyName(identity.InternalNameOnly, out isMapped);
priceInfo = PoeNinjaPriceFetcher.GetPrice(itemName);
}
}
else
{
if (!string.IsNullOrEmpty(identity.InternalNameOnly))
{
itemName = this.GetPrettyName(identity.InternalNameOnly, out isMapped);
priceInfo = PoeNinjaPriceFetcher.GetPrice(itemName);
}
if (priceInfo == null && !string.IsNullOrEmpty(identity.Name))
{
priceInfo = PoeNinjaPriceFetcher.GetPrice(identity.Name);
}
}
// Clipboard mapping
if (clipboardChanged && !string.IsNullOrEmpty(identity.InternalNameOnly))
{
var mousePos = ImGui.GetMousePos();
if (mousePos.X >= tx && mousePos.X <= tx + tw &&
mousePos.Y >= ty && mousePos.Y <= ty + th)
{
var parsedName = this.ParseClipboardForItemName(currentClipboard);
if (!string.IsNullOrEmpty(parsedName))
{
this.UpdateCustomName(identity.InternalNameOnly, parsedName);
itemName = parsedName;
priceInfo = PoeNinjaPriceFetcher.GetPrice(itemName);
}
}
}
string label = "";
uint color = 0xFFFFFFFF; // White
if (priceInfo != null)
{
var (displayValue, displayCurrency) = PoeNinjaPriceFetcher.GetDisplayPrice(priceInfo);
if (displayCurrency == "divine")
{
label = PluginText.F("ritualhelper.price.divine", "{0:0.##} Div", displayValue);
color = 0xFFFFFFFF;
}
else if (displayCurrency == "exalted" || displayCurrency == "ex")
{
label = PluginText.F("ritualhelper.price.exalted", "{0:0.##} Ex", displayValue);
color = 0xFF00D7FF; // Gold
}
else if (displayCurrency == "chaos")
{
label = PluginText.F("ritualhelper.price.chaos", "{0:0.##} C", displayValue);
color = 0xFF00A2FF; // Cyan
}
else
{
label = PluginText.F("ritualhelper.price.chaos", "{0:0.##} C", displayValue);
color = 0xFF00D7FF; // Gold/yellow
}
}
var rectMin = new Vector2(tx, ty);
var rectMax = new Vector2(tx + tw, ty + th);
// Border color by rarity
uint borderCol = 0xFFFFFFFF;
if (identity.Rarity == Rarity.Unique) borderCol = 0xFF007CFF; // Unique (Orange)
else if (identity.Rarity == Rarity.Rare) borderCol = 0xFF00FFFF; // Rare (Yellow)
else if (identity.Rarity == Rarity.Magic) borderCol = 0xFFFF8F00; // Magic (Blue)
fgDraw.AddRect(rectMin, rectMax, borderCol, 0, ImDrawFlags.None, 1.5f);
if (!string.IsNullOrEmpty(label))
{
var pSize = ImGui.CalcTextSize(label);
float chipH = pSize.Y + 4f;
float chipW = pSize.X + 8f;
float chipX = tx + (tw - chipW) / 2f;
float chipY = ty + th - chipH / 2f;
var chipMin = new Vector2(chipX, chipY);
var chipMax = new Vector2(chipX + chipW, chipY + chipH);
fgDraw.AddRectFilled(chipMin, chipMax, 0xDD151515, 3f);
fgDraw.AddRect(chipMin, chipMax, borderCol, 3f, ImDrawFlags.None, 1f);
fgDraw.AddText(chipMin + new Vector2(4f, 2f), color, label);
}
}
}
}
}
catch { }
if (this.Settings.DebugMode)
{
this.DrawDebugOverlay();
}
}
private struct ItemIdentity
{
public Rarity Rarity;
public string Art;
public string Name;
public bool Identified;
public string Path;
public string InternalNameOnly;
}
private static IntPtr Ptr(IntPtr addr)
{
var p = Mem.Read<IntPtr>(addr);
if (p == IntPtr.Zero) return IntPtr.Zero;
var u = (ulong)p;
return (u < 0x10000 || u > 0x7FFFFFFFFFFF) ? IntPtr.Zero : p;
}
private static string ReadStdWString(IntPtr addr)
{
return Mem.ReadStdWString(addr);
}
private static string ReadMetadata(IntPtr entity)
{
var details = Ptr(entity + 0x08);
if (details == IntPtr.Zero) return string.Empty;
return ReadStdWString(details + 0x08);
}
private static string ArtBasename(string path)
{
if (string.IsNullOrEmpty(path)) return null;
var lastSlash = path.LastIndexOf('/');
var name = lastSlash >= 0 ? path[(lastSlash + 1)..] : path;
var dot = name.IndexOf('.');
return dot >= 0 ? name[..dot] : name;
}
private static IntPtr ResolveComponent(IntPtr entity, string name)
{
var details = Ptr(entity + 0x08);
if (details == IntPtr.Zero) return IntPtr.Zero;
var lookup = Ptr(details + 0x28);
if (lookup == IntPtr.Zero) return IntPtr.Zero;
var compList = Mem.Read<StdVector>(entity + 0x10);
var compCount = ((long)compList.Last - (long)compList.First) / 8;
if (compCount <= 0 || compCount > 256) return IntPtr.Zero;
var bFirst = Ptr(lookup + 0x28);
var bLast = Mem.Read<IntPtr>(lookup + 0x30);
if (bLast == IntPtr.Zero) return IntPtr.Zero;
var entries = ((long)bLast - (long)bFirst) / 16;
if (bFirst == IntPtr.Zero || entries <= 0 || entries > 256) return IntPtr.Zero;
for (long i = 0; i < entries; i++)
{
var e = bFirst + (int)(i * 16);
var namePtr = Ptr(e);
var index = Mem.Read<int>(e + 8);
if (index < 0 || index >= compCount) continue;
// Read the ASCII component name
var bytes = Mem.ReadBytes(namePtr, 64);
string compName = "";
if (bytes.Length > 0)
{
int z = Array.IndexOf(bytes, (byte)0);
compName = z >= 0 ? Encoding.ASCII.GetString(bytes, 0, z) : Encoding.ASCII.GetString(bytes);
}
if (compName != name) continue;
return Ptr(compList.First + (int)(index * 8));
}
return IntPtr.Zero;
}
private static ItemIdentity ReadIdentityFromItem(IntPtr item)
{
var identity = new ItemIdentity
{
Rarity = Rarity.Normal,
Art = null,
Name = null,
Identified = true,
Path = "",
InternalNameOnly = ""
};
if (item == IntPtr.Zero) return identity;
identity.Path = ReadMetadata(item);
if (!string.IsNullOrEmpty(identity.Path))
{
var parts = identity.Path.Split('/');
if (parts.Length > 0)
{
identity.InternalNameOnly = parts[parts.Length - 1];
}
}
var modsCompAddr = ResolveComponent(item, "Mods");
if (modsCompAddr != IntPtr.Zero)
{
var modsComp = new Mods(modsCompAddr);
identity.Rarity = modsComp.Rarity;
var idf = Mem.Read<byte>(modsComp.Address + 0x90);
identity.Identified = idf != 0;
}
var renderItemAddr = ResolveComponent(item, "RenderItem");
if (renderItemAddr != IntPtr.Zero)
{
var renderComp = new RenderItem(renderItemAddr);
identity.Art = ArtBasename(renderComp.ResourcePath);
}
var baseCompAddr = ResolveComponent(item, "Base");
if (baseCompAddr != IntPtr.Zero)
{
var baseComp = new Base(baseCompAddr);
if (!string.IsNullOrWhiteSpace(baseComp.BaseItemName))
{
identity.Name = baseComp.BaseItemName.Trim();
}
}
return identity;
}
private static bool ChildSpan(IntPtr el, out IntPtr first, out long n)
{
first = Ptr(el + 0x10);
n = 0;
if (first == IntPtr.Zero) return false;
var last = Mem.Read<IntPtr>(el + 0x18);
if (last == IntPtr.Zero) return false;
n = ((long)last - (long)first) / 8;
return n > 0 && n <= 4000;
}
private static IntPtr TileItem(IntPtr tile)
{
if (tile == IntPtr.Zero) return IntPtr.Zero;
var item = Ptr(tile + 0x4F8);
return item != IntPtr.Zero && ResolveComponent(item, "RenderItem") != IntPtr.Zero ? item : IntPtr.Zero;
}
private static IntPtr FindRewardGrid(IntPtr parent)
{
if (!ChildSpan(parent, out var first, out var n)) return IntPtr.Zero;
IntPtr best = IntPtr.Zero;
var bestItems = 0;
for (long i = 0; i < n; i++)
{
var c = Ptr(first + (int)(i * 8));
if (!ChildSpan(c, out var cf, out var cn) || cn < 1 || cn > 120) continue;
var items = 0;
for (long k = 0; k < cn; k++)
{
if (TileItem(Ptr(cf + (int)(k * 8))) != IntPtr.Zero) items++;
}
if (items >= 2 && items > bestItems)
{
best = c;
bestItems = items;
}
}
return best;
}
private bool IsValidRewardGrid(IntPtr gridAddr)
{
if (gridAddr == IntPtr.Zero) return false;
try
{
var flags = Mem.Read<uint>(gridAddr + 0x180);
if (!UiElementBaseFuncs.IsVisibleChecker(flags))
{
return false;
}
if (!ChildSpan(gridAddr, out var gf, out var gn) || gn < 1 || gn > 16)
{
return false;
}
for (long i = 0; i < gn; i++)
{
var tile = Ptr(gf + (int)(i * 8));
if (TileItem(tile) != IntPtr.Zero)
{
return true;
}
}
return false;
}
catch
{
return false;
}
}
private bool TryScanRitualWindowThrottled(IntPtr gameUiRoot, out IntPtr gridAddr)
{
if (this.scannedGridAddr != IntPtr.Zero && this.IsValidRewardGrid(this.scannedGridAddr))
{
gridAddr = this.scannedGridAddr;
return true;
}
this.scannedGridAddr = IntPtr.Zero;
var now = DateTime.UtcNow;
if (now >= this.nextScanUtc)
{
this.nextScanUtc = now.AddMilliseconds(750);
this.scannedGridAddr = this.FindRitualRewardGrid(gameUiRoot);
}
gridAddr = this.scannedGridAddr;
return gridAddr != IntPtr.Zero;
}
private IntPtr FindRitualRewardGrid(IntPtr gameUiRoot)
{
if (gameUiRoot == IntPtr.Zero)
{
debugLastSigSearchStatus = "uiRoot is Zero";
return IntPtr.Zero;
}
var queue = new Queue<IntPtr>();
var visited = new HashSet<IntPtr>();
queue.Enqueue(gameUiRoot);
var sigEl = IntPtr.Zero;
lock (debugSampleTexts)
{
debugSampleTexts.Clear();
}
while (queue.Count > 0 && visited.Count < 20000)
{
var el = queue.Dequeue();
if (el == IntPtr.Zero || !visited.Add(el)) continue;
var flags = Mem.Read<uint>(el + 0x180);
var visible = (flags & (1u << 0x0B)) != 0;
if (!visible && el != gameUiRoot) continue; // prune invisible subtrees
if (ChildSpan(el, out var f, out var nn))
{
for (long k = 0; k < nn; k++)
{
queue.Enqueue(Ptr(f + (int)(k * 8)));
}
}
var textAddr = el + 0x390;
var t = Mem.ReadStdWString(textAddr);
if (!string.IsNullOrEmpty(t))
{
lock (debugSampleTexts)
{
if (debugSampleTexts.Count < 15)
{
debugSampleTexts.Add(t);
}
}
if (t.Length >= 6 && (t.Contains("Ritual Remaining", StringComparison.OrdinalIgnoreCase) ||
t.Contains("tribute to the king", StringComparison.OrdinalIgnoreCase)))
{
sigEl = el;
debugLastSigSearchStatus = $"Found sig at 0x{el.ToInt64():X} with text '{t}'";
break;
}
}
}
debugVisitedCount = visited.Count;
if (sigEl == IntPtr.Zero)
{
debugLastSigSearchStatus = $"Finished scan, visited {visited.Count} elements, sig not found";
return IntPtr.Zero;
}
// Walk up from the signature element; at each ancestor look for the reward grid.
var cur = sigEl;
for (var up = 0; up < 8; up++)
{
var grid = FindRewardGrid(cur);
if (grid != IntPtr.Zero) return grid;
var parent = Ptr(cur + 0xB8); // Parent offset is 0xB8
if (parent == IntPtr.Zero) break;
cur = parent;
}
return IntPtr.Zero;
}
private static bool TryUiElementRect(IntPtr el, float winW, float winH, out float x, out float y, out float w, out float h)
{
x = y = w = h = 0f;
if (el == IntPtr.Zero) return false;
var flags = Mem.Read<uint>(el + 0x180);
if ((flags & (1u << 0x0B)) == 0) return false;
var idx = Mem.Read<byte>(el + 0x18A);
var mul = Mem.Read<float>(el + 0x130);
var sz = Mem.Read<Vector2>(el + 0x288);
var (sw, sh) = UiScaleValue(idx, mul, winW, winH);
if (sw <= 0f || sh <= 0f) return false;
var (px, py) = UiUnscaledPos(el, 0, winW, winH);
if (!float.IsFinite(px) || !float.IsFinite(py)) return false;
x = px * sw; y = py * sh; w = sz.X * sw; h = sz.Y * sh;
return w > 1f && h > 1f;
}
private static (float w, float h) UiScaleValue(byte idx, float mul, float winW, float winH)
{
if (mul == 0f) mul = 1f;
var v1 = winW / 2560.0f;
var v2 = winH / 1600.0f;
float w = mul, h = mul;
switch (idx)
{
case 1: w *= v1; h *= v1; break;
case 2: w *= v2; h *= v2; break;
case 3: w *= v1; h *= v2; break;
}
return (w, h);
}
private static (float x, float y) UiUnscaledPos(IntPtr el, int depth, float winW, float winH)
{
var rel = Mem.Read<Vector2>(el + 0x118);
var parent = Ptr(el + 0xB8);
if (parent == IntPtr.Zero || depth >= 64) return (rel.X, rel.Y);
var (ppx, ppy) = UiUnscaledPos(parent, depth + 1, winW, winH);
var flags = Mem.Read<uint>(el + 0x180);
if ((flags & (1u << 0x0A)) != 0)
{
var mod = Mem.Read<Vector2>(el + 0xF0);
ppx += mod.X; ppy += mod.Y;
}
return (ppx + rel.X, ppy + rel.Y);
}
private string lastClipboardText = "";
private string ParseClipboardForItemName(string text)
{
if (string.IsNullOrEmpty(text) || !text.StartsWith("Item Class:")) return null;
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length > 2)
{
return lines[2].Trim();
}
return null;
}
private void UpdateCustomName(string internalName, string newName)
{
if (this.customNamesCache == null) return;
string baseInternalName = internalName;
if (internalName.EndsWith("Runeforged", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "Runeforged".Length);
}
else if (internalName.EndsWith("Runemastered", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "Runemastered".Length);
}
else if (internalName.EndsWith("reforged", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "reforged".Length);
}
this.customNamesCache[baseInternalName] = newName;
var dictionaryPath = System.IO.Path.Combine(this.DllDirectory, "item_names.json");
try
{
System.IO.File.WriteAllText(dictionaryPath, Newtonsoft.Json.JsonConvert.SerializeObject(this.customNamesCache, Newtonsoft.Json.Formatting.Indented));
} catch { }
}
private System.Collections.Generic.Dictionary<string, string> customNamesCache = null;
private static string GetRunicSuffix(IntPtr modsCompAddr)
{
if (modsCompAddr == IntPtr.Zero) return "";
var modsComp = new Mods(modsCompAddr);
foreach (var mod in modsComp.ImplicitMods)
{
if (mod.name.Contains("RuneForged", StringComparison.OrdinalIgnoreCase))
{
return "Runeforged";
}
if (mod.name.Contains("RuneMastered", StringComparison.OrdinalIgnoreCase))
{
return "Runemastered";
}
}
return "";
}
private string GetPrettyName(string internalName, out bool isMapped)
{
isMapped = false;
var dictionaryPath = System.IO.Path.Combine(this.DllDirectory, "item_names.json");
if (this.customNamesCache == null)
{
this.customNamesCache = new System.Collections.Generic.Dictionary<string, string>();
if (System.IO.File.Exists(dictionaryPath))
{
try
{
var content = System.IO.File.ReadAllText(dictionaryPath);
var loaded = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, string>>(content);
if (loaded != null)
{
foreach (var kvp in loaded)
{
this.customNamesCache[kvp.Key] = kvp.Value;
}
}
}
catch { }
}
var defaults = new System.Collections.Generic.Dictionary<string, string>
{
{ "FourBelt3", "Wide Belt" },
{ "FourShieldStrDex4", "AdicioneONomeDoShieldAqui" },
{ "OmenGambleNoGoldCost", "Omen of Gambling" },
{ "HiddenItem1x1Ritual", "Hidden Ritual Item (1x1)" },
{ "HiddenItem1x2Ritual", "Hidden Ritual Item (1x2)" },
{ "HiddenItem1x3Ritual", "Hidden Ritual Item (1x3)" },
{ "HiddenItem1x4Ritual", "Hidden Ritual Item (1x4)" },
{ "HiddenItem2x2Ritual", "Hidden Ritual Item (2x2)" },
{ "HiddenItem2x3Ritual", "Hidden Ritual Item (2x3)" },
{ "HiddenItem2x4Ritual", "Hidden Ritual Item (2x4)" },
{ "FourCharm3", "Charm" },
{ "FourBodyStr6", "Strength Body Armour" },
{ "FourWand3", "Wand" },
{ "FourQuarterstaff5", "Quarterstaff" },
{ "FourStaff10", "Staff" },
{ "DistilledEmotion6", "Distilled Emotion" },
{ "CurrencyUpgradeToMagic3", "Orb of Transmutation" },
{ "MapKeyTier15", "Tier 15 Map" },
{ "FourRing3", "Ring" },
{ "ReservationGemUncut18", "Uncut Reservation Gem" },
{ "CurrencyArmourQuality", "Armourer's Scrap" },
{ "FourGlovesStr6", "Strength Gloves" },
{ "RitualCorruptedIdol2", "Idol of the Martyr" },
{ "RitualCorruptedIdol1", "Idol of the Sycophant" },
{ "RitualCorruptedIdol3", "Idol of the Pharisee" },
{ "AzmeriSocketableCat", "Cat Idol" },
{ "AzmeriSocketableBear", "Bear Idol" },
{ "AzmeriSocketableBoar", "Boar Idol" },
{ "IdoloAlira", "Idol of Alira" },
{ "AzmeriSocketableOwlSpecial", "Idol of Eeshta" },
{ "AzmeriSocketableCatSpecial", "Idol of Egrin" },
{ "IdolofEramir", "Idol of Eramir" },
{ "IdolofGreust", "Idol of Greust" },
{ "AzmeriSocketableStagSpecial", "Idol of Maxarius" },
{ "IdolofOak", "Idol of Oak" },
{ "AzmeriSocketableMonkeySpecial", "Idol of Ralakesh" },
{ "IdolofYeena", "Idol of Yeena" },
{ "AzmeriSocketableOwl", "Owl Idol" },
{ "AzmeriSocketableMonkey", "Primate Idol" },
{ "AzmeriSocketableStag", "Stag Idol" },
{ "AzmeriSocketableWolf", "Wolf Idol" },
{ "AzmeriSocketableFox", "Fox Idol" },
{ "AzmeriSocketableBearSpecial", "Idol of Grold" },
{ "IdoloKraityn", "Idol of Kraityn" },
{ "IdolofSilk", "Idol of Silk" },
{ "AzmeriSocketableWolfSpecial", "Idol of Sirrius" },
{ "AzmeriSocketableSnakeSpecial", "Idol of Thruldana" },
{ "AzmeriSocketableOx", "Ox Idol" },
{ "AzmeriSocketableRabbit", "Rabbit Idol" },
{ "AzmeriSocketableSnake", "Snake Idol" }
};
bool needsSave = false;
foreach (var kvp in defaults)
{
if (!this.customNamesCache.ContainsKey(kvp.Key))
{
this.customNamesCache[kvp.Key] = kvp.Value;
needsSave = true;
}
}
if (needsSave || !System.IO.File.Exists(dictionaryPath))
{
try
{
System.IO.File.WriteAllText(dictionaryPath, Newtonsoft.Json.JsonConvert.SerializeObject(this.customNamesCache, Newtonsoft.Json.Formatting.Indented));
}
catch { }
}
}
string baseInternalName = internalName;
string suffix = "";
if (internalName.EndsWith("Runeforged", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "Runeforged".Length);
suffix = "Runeforged";
}
else if (internalName.EndsWith("Runemastered", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "Runemastered".Length);
suffix = "Runemastered";
}
else if (internalName.EndsWith("reforged", StringComparison.OrdinalIgnoreCase))
{
baseInternalName = internalName.Substring(0, internalName.Length - "reforged".Length);
suffix = "Runeforged";
}
if (this.customNamesCache.TryGetValue(baseInternalName, out var pretty))
{
isMapped = true;
return string.IsNullOrEmpty(suffix) ? pretty : $"{pretty} {suffix}";
}
var clean = System.Text.RegularExpressions.Regex.Replace(internalName, "([A-Z])", " $1").Trim();
clean = clean.Replace("Four ", "");
clean = System.Text.RegularExpressions.Regex.Replace(clean, @"\d+", "").Trim();
return clean;
}
/// <inheritdoc/>
public override void OnEnable(bool isGameOpened)
{
this.scannedGridAddr = IntPtr.Zero;
this.lastUiRoot = IntPtr.Zero;
this.nextScanUtc = DateTime.MinValue;
if (File.Exists(this.SettingPathname))
{
try
{
var content = File.ReadAllText(this.SettingPathname);
this.Settings = JsonConvert.DeserializeObject<RitualHelperSettings>(content)
?? new RitualHelperSettings();
}
catch (Exception ex)
{
Console.WriteLine($"[RitualHelper] Failed to load settings: {ex.Message}");
this.Settings = new RitualHelperSettings();
}
}
// Always fetch/initialize PoeNinja prices when plugin is loaded/enabled
PoeNinjaPriceFetcher.Initialize(this.DllDirectory);
}
/// <inheritdoc/>
public override void SaveSettings()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(this.SettingPathname) ?? string.Empty);
var settingsData = JsonConvert.SerializeObject(this.Settings, Formatting.Indented);
File.WriteAllText(this.SettingPathname, settingsData);
}
catch (Exception ex)
{
Console.WriteLine($"[RitualHelper] Failed to save settings: {ex.Message}");
}
}
private void DrawDebugOverlay()
{
ImGui.SetNextWindowSize(new Vector2(400, 300), ImGuiCond.FirstUseEver);
var flags = ImGuiWindowFlags.NoFocusOnAppearing;
if (ImGui.Begin(PluginText.T("ritualhelper.debug.window_title", "Ritual Debug##RitualHelperDebug"), ref this.Settings.DebugMode, flags))
{
var uiManagerPtr = Core.States.InGameStateObject.GameUi.Address;
ImGui.Text(PluginText.F("ritualhelper.debug.ui_manager_addr", "UiManager Address: 0x{0:X}", uiManagerPtr.ToInt64()));
var uiRoot = uiManagerPtr;
ImGui.Text(PluginText.F("ritualhelper.debug.ui_root_addr", "UiRoot Address: 0x{0:X}", uiRoot.ToInt64()));
if (uiRoot != IntPtr.Zero)
{
var grid = this.scannedGridAddr;
ImGui.Text(PluginText.F("ritualhelper.debug.reward_grid_addr", "Reward Grid Element: 0x{0:X}", grid.ToInt64()));
if (grid != IntPtr.Zero && ChildSpan(grid, out var gf, out var gn))
{
ImGui.Text(PluginText.F("ritualhelper.debug.grid_children_count", "Grid Children Count: {0}", gn));
var itemsFound = 0;
for (long i = 0; i < gn; i++)
{
var tile = Ptr(gf + (int)(i * 8));
var item = TileItem(tile);
if (item != IntPtr.Zero) itemsFound++;
}
ImGui.Text(PluginText.F("ritualhelper.debug.grid_items_found", "Grid Items Found: {0}", itemsFound));
}
}
}
ImGui.End();
}
private void DrawWispOverlay(InGameState inGameState, AreaInstance areaInstance)
{
var player = areaInstance.Player;
if (player == null) return;
var drawList = ImGui.GetBackgroundDrawList();
var worldInstance = inGameState.CurrentWorldInstance;
if (worldInstance == null) return;
foreach (var kvp in areaInstance.AwakeEntities)
{
var ent = kvp.Value;
if (ent == null || !ent.IsValid) continue;
if (!string.IsNullOrEmpty(ent.Path) && ent.Path.Contains("Metadata/Monsters/LeagueRitual/RitualWispDaemon", StringComparison.OrdinalIgnoreCase))
{
if (ent.TryGetComponent<Render>(out var rComp))
{
var wispWorldPos = rComp.WorldPosition;
var distanceGrid = player.DistanceFrom(ent);
float radiusGrid = this.Settings.WispCircleRadiusMeters * 10f;
float worldRadius = radiusGrid * (250f / 23f);
bool isInside = distanceGrid <= radiusGrid;
var colorV4 = isInside ? this.Settings.WispCircleColorInside : this.Settings.WispCircleColorOutside;
uint color = ImGuiHelper.Color(colorV4);
var centerPos = new Vector3(
wispWorldPos.X + this.Settings.WispCircleOffsetX,