forked from exApiTools/AutoOpen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoOpen.cs
More file actions
349 lines (310 loc) · 12.7 KB
/
AutoOpen.cs
File metadata and controls
349 lines (310 loc) · 12.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using AutoOpen.Utils;
using ExileCore;
using ExileCore.PoEMemory.Components;
using ExileCore.PoEMemory.MemoryObjects;
using ExileCore.Shared.Enums;
using ExileCore.Shared.Helpers;
using SharpDX;
using Vector2 = System.Numerics.Vector2;
namespace AutoOpen;
public class AutoOpen : BaseSettingsPlugin<Settings>
{
private IngameState IngameState => GameController.Game.IngameState;
private Vector2 WindowOffset
{
get
{
var topLeft = GameController.Window.GetWindowRectangleTimeCache.TopLeft;
return new Vector2(topLeft.X, topLeft.Y);
}
}
private readonly Dictionary<long, int> _clickedEntities = [];
private List<string> _doorBlacklist;
private List<string> _switchBlacklist;
private List<string> _chestWhitelist;
public override bool Initialise()
{
_doorBlacklist = LoadFile("doorBlacklist.txt");
_switchBlacklist = LoadFile("switchBlacklist.txt");
_chestWhitelist = LoadFile("chestWhitelist.txt");
return true;
}
public override void Render()
{
var toggleHotkeyPressed = Settings.ToggleEntityKey.PressedOnce();
var camera = IngameState.Camera;
var playerPos = GameController.Player.PosNum;
var prevMousePosition = Input.ForceMousePositionNum;
if (!new[] { Settings.DoorSettings, Settings.ChestSettings, Settings.ShrineSettings, Settings.SwitchSettings }.Any(x => x.Open))
{
return;
}
var entities = GameController.EntityListWrapper.OnlyValidEntities
.Where(entity => entity.HasComponent<Render>() &&
entity.Address != GameController.Player.Address &&
entity.IsValid &&
entity.IsTargetable &&
(entity.HasComponent<TriggerableBlockage>() ||
entity.HasComponent<Transitionable>() ||
entity.HasComponent<Chest>() ||
entity.HasComponent<Shrine>() ||
entity.Path.Contains("darkshrine", StringComparison.OrdinalIgnoreCase)));
foreach (var entity in entities)
{
var entityPos = entity.PosNum;
var entityDistanceToPlayer = (playerPos - entityPos).Length();
if (!entity.TryGetComponent<Targetable>(out var targetableComp))
{
_clickedEntities.Remove(entity.Address);
continue;
}
var entityScreenPos = camera.WorldToScreen(entityPos);
var isTargeted = targetableComp.isTargeted;
if (Settings.DoorSettings.Open &&
entity.HasComponent<TriggerableBlockage>() &&
entity.Path.Contains("door", StringComparison.OrdinalIgnoreCase) &&
!entity.Path.Contains("door_npc", StringComparison.OrdinalIgnoreCase))
{
var isClosed = entity.GetComponent<TriggerableBlockage>().IsClosed;
var isBlacklisted = _doorBlacklist != null && _doorBlacklist.Contains(entity.Path);
if (!isBlacklisted)
Graphics.DrawText(isClosed ? "closed" : "opened",
entityScreenPos,
isClosed ? Color.Red : Color.Green,
FontAlign.Center);
if (isTargeted && toggleHotkeyPressed)
ToggleDoorBlacklistItem(entity.Path);
if (!isBlacklisted && isClosed && Control.MouseButtons == MouseButtons.Left)
{
var labelPos = FindDoorLabel(entity);
if (labelPos.HasValue)
{
OpenLabel(entity, entityDistanceToPlayer, labelPos.Value, prevMousePosition, Settings.DoorSettings.MaxDistance);
}
else
{
Open(entity, entityDistanceToPlayer, entityScreenPos, prevMousePosition, Settings.DoorSettings.MaxDistance);
}
}
}
if (Settings.SwitchSettings.Open &&
entity.HasComponent<Transitionable>() &&
!entity.HasComponent<TriggerableBlockage>() &&
entity.Path.Contains("switch", StringComparison.OrdinalIgnoreCase))
{
var isBlacklisted = _switchBlacklist != null && _switchBlacklist.Contains(entity.Path);
var switchState = entity.GetComponent<Transitionable>().Flag1;
var switched = switchState != 1;
if (!isBlacklisted)
{
var count = 1;
Graphics.DrawText(isTargeted ? "targeted" : "not targeted",
entityScreenPos.Translate(0, count * 16),
isTargeted ? Color.Green : Color.Red,
FontAlign.Center);
count++;
Graphics.DrawText($"{(switched ? "switched" : "not switched")}:{switchState}",
entityScreenPos.Translate(0, count * 16),
switched ? Color.Green : Color.Red,
FontAlign.Center);
count++;
}
if (isTargeted && toggleHotkeyPressed)
ToggleSwitchBlacklistItem(entity.Path);
if (!isBlacklisted && !switched && Control.MouseButtons == MouseButtons.Left)
{
Open(entity, entityDistanceToPlayer, entityScreenPos, prevMousePosition, Settings.SwitchSettings.MaxDistance);
}
}
if (Settings.ChestSettings.Open &&
(entity.HasComponent<Chest>() ||
entity.Path.Contains("chest", StringComparison.OrdinalIgnoreCase)))
{
var isOpened = entity.GetComponent<Chest>().IsOpened;
var whitelisted = _chestWhitelist != null && _chestWhitelist.Contains(entity.Path);
if (isTargeted && toggleHotkeyPressed)
ToggleChestWhitelistItem(entity.Path);
if (whitelisted && !isOpened)
{
Graphics.DrawText("Open me!", entityScreenPos, Color.LimeGreen, FontAlign.Center);
if (Control.MouseButtons == MouseButtons.Left)
{
Open(entity, entityDistanceToPlayer, entityScreenPos, prevMousePosition, Settings.ChestSettings.MaxDistance);
}
}
}
if (Settings.ShrineSettings.Open &&
(entity.HasComponent<Shrine>() ||
entity.Path.Contains("darkshrine", StringComparison.OrdinalIgnoreCase)))
{
Graphics.DrawText("Get me!", entityScreenPos, Color.LimeGreen, FontAlign.Center);
if (Control.MouseButtons == MouseButtons.Left)
{
Open(entity, entityDistanceToPlayer, entityScreenPos, prevMousePosition, Settings.ShrineSettings.MaxDistance);
}
}
}
}
private void Open(Entity entity, float entityDistanceToPlayer, Vector2 entityScreenPos, Vector2 prevMousePosition, double maxDistance)
{
if (entityDistanceToPlayer <= maxDistance)
{
if (GetEntityClickedCount(entity) <= 15)
{
if (Settings.BlockInputWhenClicking) Mouse.blockInput(true);
try
{
Mouse.MoveMouse(entityScreenPos + WindowOffset);
Mouse.LeftUp(0);
Mouse.LeftDown(0);
Mouse.LeftUp(0);
Mouse.MoveMouse(prevMousePosition);
Mouse.LeftDown(0);
Thread.Sleep(Settings.ClickDelay);
_clickedEntities[entity.Address] = _clickedEntities.GetValueOrDefault(entity.Address) + 1;
}
finally
{
if (Settings.BlockInputWhenClicking) Mouse.blockInput(false);
}
}
}
else
{
_clickedEntities.Remove(entity.Address);
}
}
private int GetEntityClickedCount(Entity entity)
{
if (!_clickedEntities.TryGetValue(entity.Address, out var clickCount))
_clickedEntities.Add(entity.Address, clickCount);
if (clickCount >= 15) LogMessage(entity.Path + " clicked too often!", 3);
return clickCount;
}
private List<string> LoadFile(string path)
{
try
{
var customPath = Path.Join(ConfigDirectory, path);
if (File.Exists(customPath))
{
return File.ReadAllLines(customPath).ToList();
}
else
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
using var reader = new StreamReader(stream);
return reader.ReadToEnd().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
catch (Exception ex)
{
DebugWindow.LogError($"Cannot load {path}: {ex}");
}
return [];
}
private void ToggleDoorBlacklistItem(string name)
{
Toggle(_doorBlacklist, name, "doorBlacklist.txt", true);
}
private void ToggleSwitchBlacklistItem(string name)
{
Toggle(_switchBlacklist, name, "switchBlacklist.txt", true);
}
private void ToggleChestWhitelistItem(string name)
{
Toggle(_chestWhitelist, name, "chestWhitelist.txt", false);
}
private void Toggle(List<string> collection, string name, string path, bool isBlacklist)
{
if (collection.Remove(name))
{
LogMessage($"{name} will now be {(isBlacklist ? "opened" : "ignored")}", 5, Color.Red);
}
else
{
collection.Add(name);
LogMessage($"{name} will now be {(isBlacklist ? "ignored" : "opened")}", 5, Color.Green);
}
File.WriteAllLines(Path.Join(ConfigDirectory, path), _chestWhitelist);
}
public override void AreaChange(AreaInstance area)
{
_clickedEntities.Clear();
}
private Vector2? FindDoorLabel(Entity entity)
{
try
{
var itemsOnGroundLabels = IngameState.IngameUi.ItemsOnGroundLabels;
if (itemsOnGroundLabels == null || itemsOnGroundLabels.Count == 0)
return null;
dynamic closestLabel = null;
float closestDistance = float.MaxValue;
var entityPos = entity.PosNum;
foreach (var labelElement in itemsOnGroundLabels)
{
if (labelElement.IsVisible)
{
dynamic miscLabel = labelElement;
var labelEntity = miscLabel.ItemOnGround;
if (labelEntity != null && labelEntity.Path == entity.Path)
{
var labelEntityPos = labelEntity.PosNum;
var distance = (entityPos - labelEntityPos).Length();
if (distance < closestDistance)
{
closestDistance = distance;
closestLabel = miscLabel;
}
}
}
}
if (closestLabel != null && closestDistance < 10)
{
var center = closestLabel.Label.GetClientRectCache.Center;
return new Vector2(center.X, center.Y);
}
}
catch (Exception ex)
{
LogError($"Error finding door label: {ex.Message}");
}
return null;
}
private bool IsLabelVisible(dynamic label)
{
try
{
return label.IsVisible && label.Label.IsVisible;
}
catch
{
return false;
}
}
private void OpenLabel(Entity entity, float entityDistanceToPlayer, Vector2 labelPos, Vector2 prevMousePosition, double maxDistance)
{
if (entityDistanceToPlayer <= maxDistance)
{
if (GetEntityClickedCount(entity) <= 3)
{
Mouse.LeftUp(0);
GameController.PluginBridge.GetMethod<Action<Entity, uint>>("MagicInput.CastSkillWithTarget")(entity, 0x400);
Mouse.LeftDown(0);
_clickedEntities[entity.Address] = _clickedEntities.GetValueOrDefault(entity.Address) + 1;
}
}
else
{
_clickedEntities.Remove(entity.Address);
}
}
}