Repository navigation
Expand file tree
/
Copy pathInputAsync.cs
More file actions
171 lines (148 loc) · 4.76 KB
/
Copy pathInputAsync.cs
File metadata and controls
171 lines (148 loc) · 4.76 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
using System;
using System.Windows.Forms;
using ExileCore.Shared;
using SharpDX;
using InputHumanizer.Input;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ExileCore;
namespace MapDeviceActivator;
public class InputAsync : ExileCore.Input
{
private const int MouseEventWheel = 0x0800;
private const int WheelDelta = 120;
private static Random Random { get; } = new Random();
private static MapDeviceActivator Instance => MapDeviceActivator.Instance;
public static IInputController _inputController = null;
public static bool LOCK_CONTROLLER = false;
private static void IController()
{
LOCK_CONTROLLER = true;
if (_inputController != null)
return;
if (Instance == null)
{
return;
}
var tryGetInputController = Instance.GameController.PluginBridge.GetMethod<Func<string, IInputController>>("InputHumanizer.TryGetInputController");
if (tryGetInputController == null)
{
return;
}
try
{
_inputController = tryGetInputController("MapDeviceActivator");
if (_inputController == null)
{
throw new Exception("Failed to get InputHumanizer controller");
}
}
catch
{
throw;
}
}
public static void IControllerEnd()
{
if (LOCK_CONTROLLER)
return;
_inputController?.Dispose();
_inputController = null;
}
private static System.Numerics.Vector2 AnywhereInRectangle(RectangleF rect)
{
var topLeft = rect.TopLeft;
var bottomRight = rect.BottomRight;
var xTenPct = (int)((bottomRight.X - topLeft.X) * 0.2f);
var yTenPct = (int)((bottomRight.Y - topLeft.Y) * 0.2f);
var randomX = Random.Next((int)topLeft.X + xTenPct, (int)bottomRight.X - xTenPct);
var randomY = Random.Next((int)topLeft.Y + yTenPct, (int)bottomRight.Y - yTenPct);
return new System.Numerics.Vector2(randomX, randomY);
}
public static async SyncTask<bool> ClickElement(RectangleF pos, MouseButtons mouseButton = MouseButtons.Left)
{
IController();
ExileCore.Input.SetCursorPos(AnywhereInRectangle(pos));
await _inputController.Click(mouseButton);
IControllerEnd();
return true;
}
public static async SyncTask<bool> ClickElement(RectangleF pos)
{
return await ClickElement(pos, MouseButtons.Left);
}
public static new async SyncTask<bool> Click(MouseButtons mouseButton = MouseButtons.Left)
{
IController();
await _inputController.Click(mouseButton);
IControllerEnd();
return true;
}
public static async SyncTask<bool> HoldCtrl()
{
return await KeyDown(Keys.ControlKey);
}
public static async SyncTask<bool> ReleaseCtrl()
{
return await KeyUp(Keys.ControlKey);
}
public static new async SyncTask<bool> KeyDown(Keys key)
{
IController();
await _inputController.KeyDown(key);
IControllerEnd();
return true;
}
public static new async SyncTask<bool> KeyUp(Keys key)
{
IController();
await _inputController.KeyUp(key);
IControllerEnd();
return true;
}
public static new async SyncTask<bool> KeyPress(Keys key)
{
IController();
await _inputController.KeyDown(key);
await _inputController.KeyUp(key);
IControllerEnd();
return true;
}
public static new async SyncTask<bool> VerticalScroll(bool scrollUp, int clicks)
{
mouse_event(MouseEventWheel, 0, 0, (scrollUp ? clicks : -clicks) * WheelDelta, 0);
await TaskUtils.NextFrame();
return true;
}
public static async SyncTask<bool> Wait(int ms)
{
return await Wait(TimeSpan.FromMilliseconds(ms));
}
public static async SyncTask<bool> Wait(TimeSpan period)
{
var sw = Stopwatch.StartNew();
while (sw.Elapsed < period)
{
await TaskUtils.NextFrame();
}
return true;
}
public static async SyncTask<bool> Wait(Func<bool> fn, int ms = 100, string ErrorMessage = "")
{
return await Wait(fn, TimeSpan.FromMilliseconds(ms), ErrorMessage);
}
public static async SyncTask<bool> Wait(Func<bool> fn, TimeSpan period, string ErrorMessage = "")
{
var sw = Stopwatch.StartNew();
while (!fn() && sw.Elapsed < period)
await TaskUtils.NextFrame();
if (!fn())
{
return false;
}
return true;
}
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
}