-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathStashieCore.cs
1022 lines (834 loc) · 36.7 KB
/
StashieCore.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
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
#region Header
//-----------------------------------------------------------------
// Class: StashieLogic
// Description: PoeHUD plugin. Main plugin logic.
// Author: Stridemann, nymann Date: 08.26.2017
//-----------------------------------------------------------------
#endregion
#define DebugMode
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Newtonsoft.Json;
using PoeHUD.Hud.Menu;
using PoeHUD.Hud.Settings;
using PoeHUD.Models.Enums;
using PoeHUD.Plugins;
using PoeHUD.Poe.Components;
using PoeHUD.Poe.Elements;
using PoeHUD.Poe.EntityComponents;
using PoeHUD.Poe.RemoteMemoryObjects;
using SharpDX;
using Stashie.Filters;
using Stashie.Settings;
using Stashie.Utils;
using MenuItem = PoeHUD.Hud.Menu.MenuItem;
namespace Stashie
{
public class StashieCore : BaseSettingsPlugin<StashieSettings>
{
public StashieCore()
{
PluginName = "Stashie (BETA)";
}
private const string FITERS_CONFIG_FILE = "FitersConfig.txt";
private bool _bDropOnce;
private Vector2 _clickWindowOffset;
private List<FilterResult> _dropItems;
private int[,] _ignoredCells;
private List<ListIndexNode> _settingsListNodes;
private Thread _tabNamesUpdaterThread;
private const int WHILE_DELAY = 5;
private const int INPUT_DELAY = 15;
private List<CustomFilter> _customFilters;
private List<RefillProcessor> _customRefills;
public override void Initialise()
{
Settings.Enable.OnValueChanged += SetupOrClose;
SetupOrClose();
}
private void CreateFileAndAppendTextIfItDoesNotExitst(string path, string content)
{
if (File.Exists(path))
{
return;
}
using (var streamWriter = new StreamWriter(path, true))
{
streamWriter.Write(content);
streamWriter.Close();
}
}
private void SaveDefaultConfigsToDisk()
{
var path = $"{PluginDirectory}\\GitUpdateConfig.txt";
const string gitUpdateConfig = "Owner:nymann\r\n" +
"Name:Stashie\r\n" +
"Release\r\n";
CreateFileAndAppendTextIfItDoesNotExitst(path, gitUpdateConfig);
path = $"{PluginDirectory}\\RefillCurrency.txt";
const string refillCurrency =
"//MenuName:\t\t\tClassName,\t\t\tStackSize,\tInventoryX,\tInventoryY\r\n" +
"Portal Scrolls:\t\tPortal Scroll,\t\t40,\t\t\t12,\t\t\t1\r\n" +
"Scrolls of Wisdom:\tScroll of Wisdom,\t40,\t\t\t12,\t\t\t2\r\n" +
"//Chances:\t\t\tOrb of Chance,\t\t20,\t\t\t12,\t\t\t3";
CreateFileAndAppendTextIfItDoesNotExitst(path, refillCurrency);
path = $"{PluginDirectory}\\FitersConfig.txt";
const string filtersConfig =
"//FilterName(menu name):\tfilters\t\t:ParentMenu(optionaly, will be created automatially for grouping)\r\n" +
"//Filter parts should divided by coma or | (for OR operation(any filter part can pass))\r\n" +
"\r\n" +
"////////////\tAvailable properties:\t/////////////////////\r\n" +
"/////////\tString (name) properties:\r\n" +
"//classname\r\n" +
"//basename\r\n" +
"//path\r\n" +
"/////////\tNumerical properties:\r\n" +
"//itemquality\r\n//rarity\r\n//ilvl\r\n" +
"/////////\tBoolean properties:\r\n" +
"//identified\r\n" +
"/////////////////////////////////////////////////////////////\r\n" +
"////////////\tAvailable operations:\t/////////////////////\r\n" +
"/////////\tString (name) operations:\r\n" +
"//!=\t(not equal)\r\n" +
"//=\t\t(equal)\r\n" +
"//^\t\t(contains)\r\n" +
"//!^\t(not contains)\r\n" +
"/////////\tNumerical operations:\r\n" +
"//!=\t(not equal)\r\n" +
"//=\t\t(equal)\r\n" +
"//>\t\t(bigger)\r\n" +
"//<\t\t(less)\r\n//<=\t(less or qual)\r\n//>=\t(bigger or qual)\r\n/////////\tBoolean operations:\r\n//!\t\t(not/invert)\r\n/////////////////////////////////////////////////////////////\r\n\r\n//Default Tabs\r\nDivination Cards:\tClassName=DivinationCard\t\t\t\t\t:Default Tabs\r\nGems:\t\t\t\tClassName^Skill Gem,ItemQuality=0\t\t\t:Default Tabs\r\nCurrency:\t\t\tClassName=StackableCurrency,path!^Essence\t:Default Tabs\r\nLeaguestones:\t\tClassName=Leaguestone\t\t\t\t\t\t:Default Tabs\r\nEssences:\t\t\tBaseName^Essence,ClassName=StackableCurrency:Default Tabs\r\nJewels:\t\t\t\tClassName=Jewel\t\t\t\t\t\t\t\t:Default Tabs\r\nFlasks:\t\t\t\tClassName^Flask,ItemQuality=0\t\t\t\t:Default Tabs\r\nTalisman:\t\t\tClassName=Amulet,BaseName^Talisman\t\t\t:Default Tabs\r\nJewelery:\t\t\tClassName=Amulet|ClassName=Ring\t\t\t\t:Default Tabs\r\n//White Items:\t\tRarity=Normal\t\t\t\t\t\t\t\t:Default Tabs\r\n\r\n//Chance Items\r\nSorcerer Boots:\tBaseName=Sorcerer Boots,Rarity=Normal\t:Chance Items\r\nLeather Belt:\tBaseName=Leather Belt,Rarity=Normal\t\t:Chance Items\r\n\r\n//Vendor Recipes\r\nChisel Recipe:\t\tBaseName=Stone Hammer|BaseName=Rock Breaker,ItemQuality=20\t:Vendor Recipes\r\nQuality Gems:\t\tClassName^Skill Gem,ItemQuality>0\t\t\t\t\t\t\t:Vendor Recipes\r\nQuality Flasks:\t\tClassName^Flask,ItemQuality>0\t\t\t\t\t\t\t\t:Vendor Recipes\r\n\r\n//Maps\r\nShore Shaped:\tClassName=Map,BaseName=Shaped Shore Map\t:Maps\r\nStrand Shaped:\tClassName=Map,BaseName=Shaped Strand Map:Maps\r\nShaped Maps:\tClassName=Map,BaseName^Shaped\t\t\t:Maps\r\nUniq Maps:\t\tClassName=Map,Rarity=Unique\t\t\t\t:Maps\r\nOther Maps:\t\tClassName=Map\t\t\t\t\t\t\t:Maps\r\n\r\n//Chaos Recipe LVL 2 (unindentified and ilvl 60 or above)\r\nWeapons:\t\t!identified,Rarity=Rare,ilvl>=60,ClassName^Two Hand|ClassName^One Hand|ClassName=Bow|ClassName=Staff|ClassName=Sceptre|ClassName=Wand|ClassName=Dagger|ClassName=Claw|ClassName=Shield :Chaos Recipe\r\nJewelry:\t\t!identified,Rarity=Rare,ilvl>=60,ClassName=Ring|ClassName=Amulet \t:Chaos Recipe\r\nBelts:\t\t\t!identified,Rarity=Rare,ilvl>=60,ClassName=Belt \t\t\t\t\t:Chaos Recipe\r\nHelms:\t\t\t!identified,Rarity=Rare,ilvl>=60,ClassName=Helmet \t\t\t\t\t:Chaos Recipe\r\nBody Armours:\t!identified,Rarity=Rare,ilvl>=60,ClassName=Body Armour \t\t\t\t:Chaos Recipe\r\nBoots:\t\t\t!identified,Rarity=Rare,ilvl>=60,ClassName=Boots \t\t\t\t\t:Chaos Recipe\r\n" +
"Gloves:\t\t\t!identified,Rarity=Rare,ilvl>=60,ClassName=Gloves \t\t\t\t\t:Chaos Recipe";
CreateFileAndAppendTextIfItDoesNotExitst(path, filtersConfig);
}
private void LoadCustomFilters()
{
var filterPath = Path.Combine(PluginDirectory, FITERS_CONFIG_FILE);
var filtersLines = File.ReadAllLines(filterPath);
var unused = new FilterParser();
_customFilters = FilterParser.Parse(filtersLines);
var submenu = new Dictionary<string, MenuItem>();
foreach (var customFilter in _customFilters)
{
ListIndexNode indexNode;
if (!Settings.CustomFilterOptions.TryGetValue(customFilter.Name, out indexNode))
{
indexNode = new ListIndexNode
{
Value = "Ignore",
Index = -1
};
Settings.CustomFilterOptions.Add(customFilter.Name, indexNode);
}
var parentMenu = PluginSettingsRootMenu;
if (!string.IsNullOrEmpty(customFilter.SubmenuName))
{
if (!submenu.TryGetValue(customFilter.SubmenuName, out parentMenu))
{
parentMenu = MenuPlugin.AddChild(PluginSettingsRootMenu, customFilter.SubmenuName);
submenu.Add(customFilter.SubmenuName, parentMenu);
}
}
MenuPlugin.AddChild(parentMenu, customFilter.Name, indexNode);
customFilter.StashIndexNode = indexNode;
_settingsListNodes.Add(indexNode);
}
}
private void LoadCustomRefills()
{
_customRefills = RefillParser.Parse(PluginDirectory);
if (_customRefills.Count == 0)
{
return;
}
var refillMenu = MenuPlugin.AddChild(PluginSettingsRootMenu, "Refill Currency", Settings.RefillCurrency);
MenuPlugin.AddChild(refillMenu, "Currency Tab", Settings.CurrencyStashTab);
MenuPlugin.AddChild(refillMenu, "Allow Have More", Settings.AllowHaveMore);
foreach (var refill in _customRefills)
{
RangeNode<int> amountOption;
if (!Settings.CustomRefillOptions.TryGetValue(refill.MenuName, out amountOption))
{
amountOption = new RangeNode<int>(0, 0, refill.StackSize);
Settings.CustomRefillOptions.Add(refill.MenuName, amountOption);
}
amountOption.Max = refill.StackSize;
refill.AmountOption = amountOption;
MenuPlugin.AddChild(refillMenu, refill.MenuName, amountOption);
}
_settingsListNodes.Add(Settings.CurrencyStashTab);
}
private void LoadIgnoredCells()
{
const string fileName = @"/IgnoredCells.json";
var filePath = PluginDirectory + fileName;
if (File.Exists(filePath))
{
var json = File.ReadAllText(filePath);
try
{
_ignoredCells = JsonConvert.DeserializeObject<int[,]>(json);
var ignoredHeight = _ignoredCells.GetLength(0);
var ignoredWidth = _ignoredCells.GetLength(1);
if (ignoredHeight != 5 || ignoredWidth != 12)
{
LogError("Stashie: Wrong IgnoredCells size! Should be 12x5. Reseting to default..", 5);
}
else
{
return;
}
}
catch (Exception ex)
{
LogError(
"Stashie: Can't decode IgnoredCells settings in " + fileName +
". Reseting to default. Error: " + ex.Message, 5);
}
}
_ignoredCells = new[,]
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
};
var defaultSettings = JsonConvert.SerializeObject(_ignoredCells);
defaultSettings = defaultSettings.Replace("[[", "[\n[");
defaultSettings = defaultSettings.Replace("],[", "],\n[");
defaultSettings = defaultSettings.Replace("]]", "]\n]");
File.WriteAllText(filePath, defaultSettings);
}
public override void Render()
{
if (!Settings.Enable)
{
return;
}
var uiTabsOpened = GameController.Game.IngameState.IngameUi.InventoryPanel.IsVisible &&
GameController.Game.IngameState.ServerData.StashPanel.IsVisible;
if (!uiTabsOpened)
{
_bDropOnce = false;
return;
}
if (Settings.RequireHotkey.Value && !Keyboard.IsKeyDown((int) Settings.DropHotkey.Value))
{
_bDropOnce = false;
return;
}
if (_bDropOnce)
{
return;
}
_bDropOnce = true;
ProcessInventoryItems();
}
private void ProcessInventoryItems()
{
var inventory =
GameController.Game.IngameState.IngameUi.InventoryPanel[
InventoryIndex.PlayerInventory];
var invItems = inventory.VisibleInventoryItems;
if (invItems == null)
{
LogMessage("Player inventory->VisibleInventoryItems is null!", 5);
}
else
{
_dropItems = new List<FilterResult>();
_clickWindowOffset = GameController.Window.GetWindowRectangle().TopLeft;
foreach (var invItem in invItems)
{
if (invItem.Item == null)
{
continue;
}
if (CheckIgnoreCells(invItem))
{
continue;
}
var baseItemType = GameController.Files.BaseItemTypes.Translate(invItem.Item.Path);
var testItem = new ItemData(invItem, baseItemType);
var result = CheckFilters(testItem);
if (result != null)
{
_dropItems.Add(result);
}
}
DropToStash();
}
}
private bool CheckIgnoreCells(NormalInventoryItem inventItem)
{
var inventPosX = inventItem.InventPosX;
var inventPosY = inventItem.InventPosY;
if (_customRefills.Any(x => x.InventPos.X == inventPosX && x.InventPos.Y == inventPosY))
{
return true;
}
if (inventPosX < 0 || inventPosX >= 12)
{
return true;
}
if (inventPosY < 0 || inventPosY >= 5)
{
return true;
}
return _ignoredCells[inventPosY, inventPosX] != 0; //No need to check all item size
}
private FilterResult CheckFilters(ItemData itemData)
{
foreach (var filter in _customFilters)
{
if (!filter.AllowProcess)
{
continue;
}
if (filter.CompareItem(itemData))
{
return new FilterResult(filter.StashIndexNode, itemData);
}
}
return null;
}
private void DropToStash()
{
var cursorPosPreMoving = Mouse.GetCursorPosition();
if (Settings.BlockInput.Value)
{
WinApi.BlockInput(true);
}
if (_dropItems.Count > 0)
{
var sortedByStash = (from itemResult in _dropItems
group itemResult by itemResult.StashIndex
into groupedDemoClass
select groupedDemoClass).ToDictionary(gdc => gdc.Key, gdc => gdc.ToList());
var latency = (int) GameController.Game.IngameState.CurLatency + Settings.ExtraDelay;
Keyboard.KeyDown(Keys.LControlKey);
Thread.Sleep(INPUT_DELAY);
foreach (var stashResults in sortedByStash)
{
if (!SwitchToTab(stashResults.Key))
{
continue;
}
foreach (var stashResult in stashResults.Value)
{
Mouse.SetCursorPosAndLeftClick(stashResult.ClickPos + _clickWindowOffset,
Settings.ExtraDelay);
Thread.Sleep(latency);
}
}
Keyboard.KeyUp(Keys.LControlKey);
}
ProcessRefills();
Mouse.SetCursorPos(cursorPosPreMoving.X, cursorPosPreMoving.Y);
if (Settings.BlockInput.Value)
{
WinApi.BlockInput(false);
Keyboard.KeyUp(Settings.DropHotkey.Value);
Thread.Sleep(INPUT_DELAY);
}
}
#region Refill
private void ProcessRefills()
{
if (!Settings.RefillCurrency.Value || _customRefills.Count == 0)
{
return;
}
if (Settings.CurrencyStashTab.Index == -1)
{
LogError("Can't process refill: CurrencyStashTab is not set.", 5);
return;
}
var delay = (int) GameController.Game.IngameState.CurLatency + Settings.ExtraDelay.Value;
var currencyTabVisible = false;
var inventory = GameController.Game.IngameState.IngameUi.InventoryPanel[InventoryIndex.PlayerInventory];
var stashItems = inventory.VisibleInventoryItems;
if (stashItems == null)
{
LogError("Can't process refill: VisibleInventoryItems is null!", 5);
return;
}
_customRefills.ForEach(x => x.Clear());
var filledCells = new int[5, 12];
foreach (var inventItem in stashItems)
{
var item = inventItem.Item;
if (item == null)
{
continue;
}
if (!Settings.AllowHaveMore.Value)
{
var iPosX = inventItem.InventPosX;
var iPosY = inventItem.InventPosY;
var iBase = item.GetComponent<Base>();
for (var x = iPosX; x <= iPosX + iBase.ItemCellsSizeX - 1; x++)
{
for (var y = iPosY; y <= iPosY + iBase.ItemCellsSizeY - 1; y++)
{
if (x >= 0 && x <= 11 && y >= 0 && y <= 4)
{
filledCells[y, x] = 1;
}
else
{
LogMessage($"Out of range: {x} {y}", 10);
}
}
}
}
if (!item.HasComponent<Stack>())
{
continue;
}
foreach (var refill in _customRefills)
{
//if (refill.AmountOption.Value == 0) continue;
var bit = GameController.Files.BaseItemTypes.Translate(item.Path);
if (bit.BaseName != refill.CurrencyClass)
{
continue;
}
var stack = item.GetComponent<Stack>();
refill.OwnedCount = stack.Size;
refill.ClickPos = inventItem.GetClientRect().Center;
if (refill.OwnedCount < 0 || refill.OwnedCount > 40)
{
LogError(
$"Ignoring refill: {refill.CurrencyClass}: Stacksize {refill.OwnedCount} not in range 0-40 ",
5);
refill.OwnedCount = -1;
}
break;
}
}
var inventoryRec = inventory.InventoryUiElement.GetClientRect();
var cellSize = inventoryRec.Width / 12;
var freeCellFound = false;
var freeCelPos = new Point();
if (!Settings.AllowHaveMore.Value)
{
for (var x = 0; x <= 11; x++)
{
for (var y = 0; y <= 4; y++)
{
if (filledCells[y, x] != 0)
{
continue;
}
freeCellFound = true;
freeCelPos = new Point(x, y);
break;
}
if (freeCellFound)
{
break;
}
}
}
foreach (var refill in _customRefills)
{
if (refill.OwnedCount == -1)
{
continue;
}
if (refill.OwnedCount == refill.AmountOption.Value)
{
continue;
}
if (refill.OwnedCount < refill.AmountOption.Value)
#region Refill
{
if (!currencyTabVisible)
{
if (!SwitchToTab(Settings.CurrencyStashTab.Index))
{
continue;
}
currencyTabVisible = true;
Thread.Sleep(delay);
}
var moveCount = refill.AmountOption.Value - refill.OwnedCount;
var currStashItems = GameController.Game.IngameState.ServerData.StashPanel.VisibleStash
.VisibleInventoryItems;
var foundSourceOfRefill = currStashItems
.Where(x => GameController.Files.BaseItemTypes.Translate(x.Item.Path).BaseName ==
refill.CurrencyClass).ToList();
foreach (var sourceOfRefill in foundSourceOfRefill)
{
var stackSize = sourceOfRefill.Item.GetComponent<Stack>().Size;
var getCurCount = moveCount > stackSize ? stackSize : moveCount;
var destination = refill.ClickPos;
if (refill.OwnedCount == 0)
{
destination = GetInventoryClickPosByCellIndex(inventory, refill.InventPos.X,
refill.InventPos.Y, cellSize);
// If cells is not free then continue.
if (GameController.Game.IngameState.IngameUi.InventoryPanel[InventoryIndex.PlayerInventory]
[refill.InventPos.X, refill.InventPos.Y, 12] != null)
{
moveCount--;
LogMessage(
$"Inventoy ({refill.InventPos.X}, {refill.InventPos.Y}) is occupied by the wrong item!",
5);
continue;
}
}
SplitStack(moveCount, sourceOfRefill.GetClientRect().Center, destination);
moveCount -= getCurCount;
if (moveCount == 0)
{
break;
}
}
if (moveCount > 0)
{
LogMessage(
$"Not enough currency (need {moveCount} more) to fill {refill.CurrencyClass} stack", 5);
}
}
#endregion
else if (!Settings.AllowHaveMore.Value && refill.OwnedCount > refill.AmountOption.Value)
#region Devastate
{
if (!freeCellFound)
{
LogMessage("Can\'t find free cell in player inventory to move excess currency.", 5);
continue;
}
if (!currencyTabVisible)
{
if (!SwitchToTab(Settings.CurrencyStashTab.Index))
{
continue;
}
currencyTabVisible = true;
Thread.Sleep(delay);
}
var destination = GetInventoryClickPosByCellIndex(inventory, freeCelPos.X, freeCelPos.Y, cellSize) +
_clickWindowOffset;
var moveCount = refill.OwnedCount - refill.AmountOption.Value;
Thread.Sleep(delay);
SplitStack(moveCount, refill.ClickPos, destination);
Thread.Sleep(delay);
Keyboard.KeyDown(Keys.LControlKey);
Mouse.SetCursorPosAndLeftClick(destination + _clickWindowOffset, Settings.ExtraDelay.Value);
Keyboard.KeyUp(Keys.LControlKey);
Thread.Sleep(delay);
}
#endregion
}
}
private Vector2 GetInventoryClickPosByCellIndex(Inventory inventory, int indexX, int indexY, float cellSize)
{
return inventory.InventoryUiElement.GetClientRect().TopLeft +
new Vector2(cellSize * (indexX + 0.5f), cellSize * (indexY + 0.5f));
}
private void SplitStack(int amount, Vector2 from, Vector2 to)
{
var delay = (int) GameController.Game.IngameState.CurLatency * 2 + Settings.ExtraDelay;
Keyboard.KeyDown(Keys.ShiftKey);
while (!Keyboard.IsKeyDown((int) Keys.ShiftKey))
{
Thread.Sleep(WHILE_DELAY);
}
Mouse.SetCursorPosAndLeftClick(from + _clickWindowOffset, Settings.ExtraDelay.Value);
Thread.Sleep(INPUT_DELAY);
Keyboard.KeyUp(Keys.ShiftKey);
Thread.Sleep(delay + 50);
if (amount > 40)
{
LogMessage("Can't select amount more than 40, current value: " + amount, 5);
amount = 40;
}
if (amount < 10)
{
var keyToPress = (int) Keys.D0 + amount;
Keyboard.KeyPress((Keys) keyToPress);
}
else
{
var keyToPress = (int) Keys.D0 + amount / 10;
Keyboard.KeyPress((Keys) keyToPress);
Thread.Sleep(delay);
keyToPress = (int) Keys.D0 + amount % 10;
Keyboard.KeyPress((Keys) keyToPress);
}
Thread.Sleep(delay);
Keyboard.KeyPress(Keys.Enter);
Thread.Sleep(delay + 50);
Mouse.SetCursorPosAndLeftClick(to + _clickWindowOffset, Settings.ExtraDelay.Value);
Thread.Sleep(delay + 50);
}
#endregion
#region Switching between StashTabs
public bool SwitchToTab(int tabIndex)
{
var latency = (int) GameController.Game.IngameState.CurLatency;
// We don't want to Switch to a tab that we are already on
var openLeftPanel = GameController.Game.IngameState.IngameUi.OpenLeftPanel;
try
{
var stashTabToGoTo =
GameController.Game.IngameState.ServerData.StashPanel.GetStashInventoryByIndex(tabIndex)
.InventoryUiElement;
if (stashTabToGoTo.IsVisible)
{
return true;
}
}
catch
{
// Nothing to see here officer.
}
// We want to maximum wait 20 times the Current Latency before giving up in our while loops.
var maxNumberOfTries = latency * 20 > 2000 ? latency * 20 / WHILE_DELAY : 2000 / WHILE_DELAY;
if (tabIndex > 30)
{
return SwitchToTabViaArrowKeys(tabIndex);
}
var stashPanel = GameController.Game.IngameState.ServerData.StashPanel;
try
{
var viewAllTabsButton = GameController.Game.IngameState.ServerData.StashPanel.ViewAllStashButton;
if (stashPanel.IsVisible && !viewAllTabsButton.IsVisible)
{
// The user doesn't have a view all tabs button, eg. 4 tabs.
return SwitchToTabViaArrowKeys(tabIndex);
}
var dropDownTabElements = GameController.Game.IngameState.ServerData.StashPanel.ViewAllStashPanel;
if (!dropDownTabElements.IsVisible)
{
var pos = viewAllTabsButton.GetClientRect();
Mouse.SetCursorPosAndLeftClick(pos.Center + _clickWindowOffset, Settings.ExtraDelay);
var brCounter = 0;
while (!dropDownTabElements.IsVisible)
{
Thread.Sleep(WHILE_DELAY);
if (brCounter++ <= maxNumberOfTries)
{
continue;
}
LogMessage($"1. Error in SwitchToTab: {tabIndex}.", 5);
return false;
}
if (GameController.Game.IngameState.ServerData.StashPanel.TotalStashes > 30)
{
// TODO:Zafaar implemented something that allows us to get in contact with the ScrollBar.
Mouse.VerticalScroll(true, 5);
Thread.Sleep(latency + 50);
}
}
var tabPos = dropDownTabElements.Children[tabIndex].GetClientRect();
Mouse.SetCursorPosAndLeftClick(tabPos.Center + _clickWindowOffset, Settings.ExtraDelay);
Thread.Sleep(latency);
}
catch (Exception e)
{
LogError($"Error in GoToTab {tabIndex}: {e.Message}", 5);
return false;
}
Inventory stash;
var counter = 0;
do
{
Thread.Sleep(WHILE_DELAY);
stash = stashPanel.VisibleStash;
if (counter++ <= maxNumberOfTries)
{
continue;
}
LogMessage("2. Error opening stash: " + tabIndex, 5);
return false;
} while (stash?.VisibleInventoryItems == null);
return true;
}
private bool SwitchToTabViaArrowKeys(int tabIndex)
{
var latency = (int) GameController.Game.IngameState.CurLatency;
var indexOfCurrentVisibleTab = GetIndexOfCurrentVisibleTab();
var difference = tabIndex - indexOfCurrentVisibleTab;
var negative = difference < 0;
for (var i = 0; i < Math.Abs(difference); i++)
{
Keyboard.KeyPress(negative ? Keys.Left : Keys.Right);
Thread.Sleep(latency);
}
return true;
}
private int GetIndexOfCurrentVisibleTab()
{
var openLeftPanel = GameController.Game.IngameState.IngameUi.OpenLeftPanel;
var totalStashes = GameController.Game.IngameState.ServerData.StashPanel.TotalStashes;
for (var i = 0; i < totalStashes; i++)
{
var stashTabToGoTo = openLeftPanel
.Children[2]
.Children[0]
.Children[1]
.Children[1]
.Children[i]
.Children[0];
if (stashTabToGoTo.IsVisible)
{
return i;
}
}
return -1;
}
#endregion
#region Stashes update
private void OnSettingsStashNameChanged(ListIndexNode node, string newValue)
{
node.Index = GetInventIndexByStashName(newValue);
}
public override void OnClose()
{
CloseThreads();
}
private void SetupOrClose()
{
if (!Settings.Enable.Value)
{
CloseThreads();
return;
}
SaveDefaultConfigsToDisk();
_settingsListNodes = new List<ListIndexNode>();
LoadCustomRefills();
LoadCustomFilters();
var names = GameController.Game.IngameState.ServerData.StashPanel.AllStashNames;
UpdateStashNames(names);
foreach (var lOption in _settingsListNodes)
{
var option = lOption; //Enumerator delegate fix
option.OnValueSelected += delegate(string newValue) { OnSettingsStashNameChanged(option, newValue); };
}
LoadIgnoredCells();
_tabNamesUpdaterThread = new Thread(StashTabNamesUpdater_Thread);
_tabNamesUpdaterThread.Start();
}
private int GetInventIndexByStashName(string name)
{
var index = _renamedAllStashNames.IndexOf(name);
if (index != -1)
{
index--;
}
return index;
}
private List<string> _renamedAllStashNames;
private void UpdateStashNames(List<string> newNames)
{
Settings.AllStashNames = newNames;
_renamedAllStashNames = new List<string> {"Ignore"};
for (var i = 0; i < Settings.AllStashNames.Count; i++)
{
var realStashName = Settings.AllStashNames[i];
if (_renamedAllStashNames.Contains(realStashName))
{
realStashName += " (" + i + ")";
#if DebugMode
LogMessage("Stashie: fixed same stash name to: " + realStashName, 3);
#endif
}
_renamedAllStashNames.Add(realStashName);
}
Settings.AllStashNames.Insert(0, "Ignore");
foreach (var lOption in _settingsListNodes)
{
lOption.SetListValues(_renamedAllStashNames);
var inventoryIndex = GetInventIndexByStashName(lOption.Value);
if (inventoryIndex == -1) //If the value doesn't exist in list (renamed)
{
if (lOption.Index != -1) //If the value doesn't exist in list and the value was not Ignore
{
#if DebugMode
LogMessage(
"Tab renamed : " + lOption.Value + " to " + _renamedAllStashNames[lOption.Index + 1], 5);
#endif
if (lOption.Index >= _renamedAllStashNames.Count)
{
lOption.Index = -1;
lOption.Value = _renamedAllStashNames[0];
}
else
{
lOption.Value = _renamedAllStashNames[lOption.Index + 1]; // Just update it's name
}
}
else
{
lOption.Value =
_renamedAllStashNames[0]; //Actually it was "Ignore", we just update it (can be removed)
}
}
else //tab just change it's index
{
#if DebugMode
if (lOption.Index != inventoryIndex)
{
LogMessage("Tab moved: " + lOption.Index + " to " + inventoryIndex, 5);
}
#endif
lOption.Index = inventoryIndex;
lOption.Value = _renamedAllStashNames[inventoryIndex + 1];
}
}
}
private void CloseThreads()
{
if (_tabNamesUpdaterThread != null && _tabNamesUpdaterThread.IsAlive)
{
_tabNamesUpdaterThread.IsBackground = true;
}
}
public void StashTabNamesUpdater_Thread()
{
while (!_tabNamesUpdaterThread.IsBackground)
{
if (!GameController.Game.IngameState.InGame)
{
Thread.Sleep(500);
continue;
}
var stashPanel = GameController.Game.IngameState.ServerData.StashPanel;
if (!stashPanel.IsVisible)
{
Thread.Sleep(500);
continue;
}
var cachedNames = Settings.AllStashNames;
var realNames = stashPanel.AllStashNames;
if (realNames.Count + 1 != cachedNames.Count)
{
UpdateStashNames(realNames);
continue;
}