-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
432 lines (353 loc) · 15.5 KB
/
MainWindow.xaml.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
using System;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using WindowsInput;
using Microsoft.Win32;
using SharpDX.XInput;
using TCD.System.TouchInjection;
using System.Windows.Media;
using System.Windows.Ink;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Diagnostics;
using System.Windows.Media.Animation;
using System.Threading;
using System.Text;
using System.Windows.Input;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
namespace d3gamepad
{
public partial class MainWindow : Window
{
private static GameController _gameController;
private static InputSimulator _inputSimulator;
private static ControllerSettings _settings;
private static bool IsAlive;
[DllImport("User32.Dll")]
public static extern long SetCursorPos(int x, int y);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static Bitmap CaptureFromScreen(System.Drawing.Point location, int width, int height)
{
Bitmap bmpScreenCapture = new Bitmap(width, height);
Graphics p = Graphics.FromImage(bmpScreenCapture);
p.CopyFromScreen(location.X,location.Y, 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy);
p.Dispose();
return bmpScreenCapture;
}
public static System.Drawing.Color GetColorFromScreen(System.Drawing.Point location)
{
Bitmap map = CaptureFromScreen(location,1,1);
System.Drawing.Color c = map.GetPixel(0, 0);
map.Dispose();
return c;
}
static double default_w_ratio = 2560;
static double default_h_ratio = 1080;
static double default_padding = 11;
static double default_cell = 56;
static double default_bottom = 22;
static double default_checkui = 17;
static double default_check_inventory_w = 17;
static double default_check_inventory_h = 26;
static double default_life_check_high = 920; // from top
static double default_life_check_diff = 142; // from check_high
static double default_life_check_left = 420; // from middle
static double current_h_ratio, current_w_ratio;
static double compute_h_ratio, compute_w_ratio;
static double current_padding,current_cell,current_bottom,current_checkui;
static double current_check_inventory_w, current_check_inventory_h;
static double current_life_check_diff, current_life_check_high, current_life_check_left;
static double draw_pos_x, draw_pos_y;
private static void ComputeScreenValues()
{
current_h_ratio = _settings.UIHeight;
current_w_ratio = _settings.UIWidth;
compute_h_ratio = current_h_ratio / default_h_ratio;
compute_w_ratio = current_w_ratio / default_w_ratio;
current_padding = default_padding * compute_h_ratio;
current_cell = default_cell * compute_h_ratio;
current_bottom = default_bottom * compute_h_ratio;
current_checkui = default_checkui * compute_h_ratio;
current_check_inventory_w = default_check_inventory_w * compute_h_ratio;
current_check_inventory_h = default_check_inventory_h * compute_h_ratio;
current_life_check_diff = default_life_check_diff * compute_h_ratio;
current_life_check_high = default_life_check_high * compute_h_ratio;
current_life_check_left = default_life_check_left * compute_h_ratio;
}
private static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
private static bool IsInGame()
{
return GetActiveWindowTitle() == "Diablo III";
}
private static bool IsInMap()
{
double cursor_x = _settings.d3_Rect.Left + _settings.c_d3Width - (34 * compute_h_ratio);
double cursor_y = _settings.d3_Rect.Bottom - current_checkui;
System.Drawing.Point cursor = new System.Drawing.Point((int)cursor_x, (int)cursor_y);
System.Drawing.Color c = GetColorFromScreen(cursor);
if (c.G > c.R && c.G > c.B && c.R <= 50 && c.G >= 100 && c.B <= 50)
return true;
return false;
}
private bool IsInInventory()
{
double cursor_x = _settings.d3_Rect.Right - 2 - current_check_inventory_w;
double cursor_y = _settings.d3_Rect.Top + current_check_inventory_h;
System.Drawing.Point cursor = new System.Drawing.Point((int)cursor_x, (int)cursor_y);
System.Drawing.Color c = GetColorFromScreen(cursor);
if (c.R > c.G && c.G > c.B && c.R >= 60 && c.B <= 20)
return true;
return false;
}
private static int CheckHealth()
{
double cursor_x = _settings.d3_Rect.Left + _settings.c_d3Width;
cursor_x -= current_life_check_left;
double health_max_y = _settings.d3_Rect.Top + current_life_check_high;
double health_min_y = health_max_y + current_life_check_diff;
double life_percent = 100;
double life_cursor = current_life_check_diff;
double table_cursor = 0;
double R, G, B = 255;
Bitmap map = CaptureFromScreen(new System.Drawing.Point((int)cursor_x, (int)health_max_y), 1, (int)current_life_check_diff);
for (double y = health_max_y; y < health_min_y; y++)
{
life_cursor--;
System.Drawing.Color c = map.GetPixel(0, (int)table_cursor);
R = c.R > 0 ? c.R : 1;
G = c.G > 0 ? c.G : 1;
B = c.B > 0 ? c.B : 1;
if (R / G > 2 && R / B > 2 && R > 50 && R < 140)
break;
else
life_percent = life_cursor / current_life_check_diff * 100;
if(table_cursor + 1 < map.Height)
table_cursor++;
}
map.Dispose();
return (int)life_percent;
}
public static void ThreadHealth()
{
int health_known = 100;
while (IsAlive)
{
if (_gameController.IsConnected())
{
if (IsInGame() && IsInMap())
{
int health_current = CheckHealth();
int damage = health_known - health_current;
int vibra = Math.Min(Math.Abs(damage) * 5000, 60000);
if (damage >= _settings.rumble_min_dmg && health_current != 0)
_gameController.SendVibration(vibra, vibra);
health_known = health_current;
}
}
Thread.Sleep(_settings.refresh_rate);
}
}
public static void ThreadGamepad()
{
while (IsAlive)
{
if (_gameController.IsConnected())
_gameController.Poll();
Thread.Sleep(_settings.refresh_rate);
}
}
public static void ThreadUI()
{
while (IsAlive)
{
// avoid crash on closing
if (Application.Current == null)
return;
_settings.UpdateScreenValues();
if (_settings.hasChanged)
{
ComputeScreenValues();
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
myForm.Height = _settings.d3Height;
myForm.Width = _settings.d3Width;
myForm.Top = _settings.d3_Rect.Top;
myForm.Left = _settings.d3_Rect.Left;
myGamepad.Width = (int)current_cell;
myGamepad.Height = (int)current_cell;
Canvas.SetTop(myGamepad, 40 * compute_h_ratio); // temp
Canvas.SetLeft(myGamepad, _settings.d3Width - (90 * compute_h_ratio)); // temp
draw_pos_x = _settings.c_d3Width - (current_padding / 2) - current_cell;
draw_pos_y = _settings.d3Height - current_cell - current_bottom;
for (int i = 0; i < 4; i++)
draw_pos_x -= (current_cell + current_padding);
foreach(UIElement item in myCanvas.Children)
{
if (item is System.Windows.Shapes.Rectangle)
{
((System.Windows.Shapes.Rectangle)item).Width = (int)current_cell / 2;
((System.Windows.Shapes.Rectangle)item).Height = (int)current_cell / 2;
Canvas.SetTop(item, draw_pos_y);
Canvas.SetLeft(item, draw_pos_x);
draw_pos_x += current_cell;
draw_pos_x += current_padding;
}
}
draw_pos_x += current_cell / 2;
}));
}
Application.Current.Dispatcher.BeginInvoke((Action)(() => {
if (IsInGame())
{
myCanvas.Visibility = Visibility.Visible;
if (_gameController.IsConnected())
myGamepad.Visibility = Visibility.Visible;
else
myGamepad.Visibility = Visibility.Hidden;
if (IsInMap())
{
foreach (UIElement item in myCanvas.Children)
if (item is System.Windows.Shapes.Rectangle)
item.Visibility = Visibility.Visible;
}
else
{
foreach (UIElement item in myCanvas.Children)
if (item is System.Windows.Shapes.Rectangle)
item.Visibility = Visibility.Hidden;
}
}
else
myCanvas.Visibility = Visibility.Hidden;
}));
Thread.Sleep(100);
}
}
public static Bitmap GetImageByName(string imageName)
{
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resourceName = asm.GetName().Name + ".Properties.Resources";
var rm = new System.Resources.ResourceManager(resourceName, asm);
return (Bitmap)rm.GetObject(imageName);
}
public ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally { DeleteObject(handle); }
}
static d3gamepad.MainWindow myForm;
static Canvas myCanvas;
static System.Windows.Controls.Image myGamepad;
public MainWindow()
{
InitializeComponent();
IsAlive = true;
// TOUCH SETTINGS
TouchInjector.InitializeTouchInjection();
_inputSimulator = new InputSimulator();
// CONTROLLER SETTINGS
_settings = new ControllerSettings();
_gameController = new GameController(new Controller(UserIndex.One), _settings, _inputSimulator);
// STATICS
myForm = Form1;
myCanvas = Canvas1;
myGamepad = GamepadIco;
string ico = "Menu";
for (int i = 0; i < 7; i ++)
{
switch (i)
{
case 0: ico = "B"; break;
case 1: ico = "Y"; break;
case 2: ico = "RB"; break;
case 3: ico = "RT"; break;
case 4: ico = "X"; break;
case 5: ico = "A"; break;
case 6: ico = "LB"; break;
}
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle
{
Fill = new ImageBrush
{
ImageSource = ImageSourceFromBitmap(GetImageByName("XBOne_" + ico))
},
Stretch = Stretch.Uniform,
};
myCanvas.Children.Add(rect);
}
Thread myThread = new Thread(new ThreadStart(ThreadHealth));
myThread.Start();
Thread myThread2 = new Thread(new ThreadStart(ThreadGamepad));
myThread2.Start();
Thread myThread3 = new Thread(new ThreadStart(ThreadUI));
myThread3.Start();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
IsAlive = false;
Application.Current.Shutdown();
}
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public MouseKeybdhardwareInputUnion mkhi;
}
[StructLayout(LayoutKind.Explicit)]
private struct MouseKeybdhardwareInputUnion
{
[FieldOffset(0)] public MouseInputData mi;
[FieldOffset(0)] public readonly KEYBDINPUT ki;
[FieldOffset(0)] public readonly HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
private struct KEYBDINPUT
{
public readonly ushort wVk;
public readonly ushort wScan;
public readonly uint dwFlags;
public readonly uint time;
public readonly IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct HARDWAREINPUT
{
public readonly int uMsg;
public readonly short wParamL;
public readonly short wParamH;
}
private struct MouseInputData
{
public int dx;
public int dy;
public uint mouseData;
public uint time;
public IntPtr dwExtraInfo;
}
}
}