-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathUtils.cs
474 lines (406 loc) · 15.3 KB
/
Utils.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
#region LICENSE
/*
Copyright 2014 - 2014 LeagueSharp
Utils.cs is part of LeagueSharp.Common.
LeagueSharp.Common is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LeagueSharp.Common is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LeagueSharp.Common. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Input;
using SharpDX;
#endregion
namespace LeagueSharp.Common
{
public enum WindowsMessages
{
WM_LBUTTONDBLCLCK = 0x203,
WM_RBUTTONDBLCLCK = 0x206,
WM_MBUTTONDBLCLCK = 0x209,
WM_MBUTTONDOWN = 0x207,
WM_MBUTTONUP = 0x208,
WM_MOUSEMOVE = 0x200,
WM_LBUTTONDOWN = 0x201,
WM_LBUTTONUP = 0x202,
WM_RBUTTONDOWN = 0x204,
WM_RBUTTONUP = 0x205,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x101
}
public enum MouseEvents
{
MOUSEEVENTF_RIGHTDOWN = 0x0008,
MOUSEEVENTF_RIGHTUP = 0x0010,
}
public enum KeyboardEvents
{
KEYBDEVENTF_SHIFTVIRTUAL = 0x10,
KEYBDEVENTF_SHIFTSCANCODE = 0x2A,
KEYBDEVENTF_KEYDOWN = 0,
KEYBDEVENTF_KEYUP = 2
}
/// <summary>
/// This class offers real mouse clicks.
/// </summary>
public static class VirtualMouse
{
public static int clickdelay;
public static int attkdelay;
public static bool disableOrbClick = false; //if set to true, orbwalker won't send right clicks - for other scripts
public static int coordX;
public static int coordY;
// mouse event
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
// keyboard event
[DllImport("user32.dll", EntryPoint = "keybd_event", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void keybd_event(byte vk, byte scan, int flags, int extrainfo);
// simulates a click-and-release action of the right mouse button at its current position
public static void RightClick()
{
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTDOWN, coordX, coordY, 0, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTUP, coordX, coordY, 0, 0);
}
public static void RightClick(Vector2 position)
{
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTDOWN, (int)position.X, (int)position.Y, 0, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTUP, (int)position.X, (int)position.Y, 0, 0);
}
public static void RightClick(Vector3 gamePosition)
{
RightClick(Drawing.WorldToScreen(gamePosition));
}
public static void ShiftClick()
{
keybd_event((int)KeyboardEvents.KEYBDEVENTF_SHIFTVIRTUAL, (int)KeyboardEvents.KEYBDEVENTF_SHIFTSCANCODE, (int)KeyboardEvents.KEYBDEVENTF_KEYDOWN, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTDOWN, coordX, coordY, 0, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTUP, coordX, coordY, 0, 0);
Utility.DelayAction.Add(200, () => { keybd_event((int)KeyboardEvents.KEYBDEVENTF_SHIFTVIRTUAL, (int)KeyboardEvents.KEYBDEVENTF_SHIFTSCANCODE, (int)KeyboardEvents.KEYBDEVENTF_KEYUP, 0); });
}
public static void ShiftClick(Vector2 position)
{
keybd_event((int)KeyboardEvents.KEYBDEVENTF_SHIFTVIRTUAL, (int)KeyboardEvents.KEYBDEVENTF_SHIFTSCANCODE, (int)KeyboardEvents.KEYBDEVENTF_KEYDOWN, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTDOWN, (int)position.X, (int)position.Y, 0, 0);
mouse_event((int)MouseEvents.MOUSEEVENTF_RIGHTUP, (int)position.X, (int)position.Y, 0, 0);
Utility.DelayAction.Add(200, () => { keybd_event((int)KeyboardEvents.KEYBDEVENTF_SHIFTVIRTUAL, (int)KeyboardEvents.KEYBDEVENTF_SHIFTSCANCODE, (int)KeyboardEvents.KEYBDEVENTF_KEYUP, 0); });
}
public static void ShiftClick(Vector3 gamePosition)
{
ShiftClick(Drawing.WorldToScreen(gamePosition));
}
}
/// <summary>
/// Non game related utilities.
/// </summary>
public static class Utils
{
private const int STD_INPUT_HANDLE = -10;
private const int ENABLE_QUICK_EDIT_MODE = 0x40 | 0x80;
// Convert an object to a byte array
internal static byte[] Serialize(Object obj)
{
if (obj == null)
{
return null;
}
var bf = new BinaryFormatter();
var ms = new System.IO.MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
// Convert a byte array to an Object
internal static T Deserialize<T>(byte[] arrBytes)
{
var memStream = new System.IO.MemoryStream();
var binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, System.IO.SeekOrigin.Begin);
return (T)binForm.Deserialize(memStream);
}
public static byte FixVirtualKey(byte key)
{
switch (key)
{
case 160:
return 0x10;
case 161:
return 0x10;
case 162:
return 0x11;
case 163:
return 0x11;
}
return key;
}
public static int GameTimeTickCount
{
get { return (int)(Game.Time * 1000); }
}
public static int TickCount
{
get { return Environment.TickCount & int.MaxValue; }
}
/// <summary>
/// Returns the cursor position on the screen.
/// </summary>
public static Vector2 GetCursorPos()
{
return CursorPosT.GetCursorPos();
}
public static string KeyToText(uint vKey)
{
/*A-Z */
if (vKey >= 65 && vKey <= 90)
{
return ((char)vKey).ToString();
}
/*F1-F12*/
if (vKey >= 112 && vKey <= 123)
{
return ("F" + (vKey - 111));
}
switch (vKey)
{
case 9:
return "Tab";
case 16:
return "Shift";
case 17:
return "Ctrl";
case 20:
return "CAPS";
case 27:
return "ESC";
case 32:
return "Space";
case 45:
return "Insert";
case 220:
return "º";
default:
return vKey.ToString();
}
}
/// <summary>
/// Returns the md5 hash from a string.
/// </summary>
public static string Md5Hash(string s)
{
var sb = new StringBuilder();
HashAlgorithm algorithm = MD5.Create();
var h = algorithm.ComputeHash(Encoding.UTF8.GetBytes(s));
foreach (var b in h)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
/// <summary>
/// Returns true if the point is under the rectangle
/// </summary>
public static bool IsUnderRectangle(Vector2 point, float x, float y, float width, float height)
{
return (point.X > x && point.X < x + width && point.Y > y && point.Y < y + height);
}
public static string FormatTime(double time)
{
var t = TimeSpan.FromSeconds(time);
return string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds);
}
public static byte[] GetBytes(string str)
{
var bytes = new byte[str.Length * sizeof(char)];
Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string GetString(byte[] bytes)
{
var chars = new char[bytes.Length / sizeof(char)];
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
/// <summary>
/// Searches in the haystack array for the given needle using the default equality operator and returns the index at
/// which the needle starts.
/// </summary>
/// <typeparam name="T">Type of the arrays.</typeparam>
/// <param name="haystack">Sequence to operate on.</param>
/// <param name="needle">Sequence to search for.</param>
/// <returns>Index of the needle within the haystack or -1 if the needle isn't contained.</returns>
public static IEnumerable<int> IndexOf<T>(this T[] haystack, T[] needle)
{
if ((needle == null) || (haystack.Length < needle.Length))
{
yield break;
}
for (var l = 0; l < haystack.Length - needle.Length + 1; l++)
{
if (!needle.Where((data, index) => !haystack[l + index].Equals(data)).Any())
{
yield return l;
}
}
}
public static void ClearConsole()
{
try
{
var window_height = Console.WindowHeight;
Console.Clear();
}
catch { }
}
/// <summary>
/// Returns the directory where the assembly is located
/// </summary>
public static string GetLocation()
{
var FileLoc = Assembly.GetExecutingAssembly().Location;
return FileLoc.Remove(FileLoc.LastIndexOf("\\", StringComparison.Ordinal));
}
public static string ToHexString(this byte bit)
{
return BitConverter.ToString(new[] { bit });
}
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int mode);
[DllImport("kernel32.dll")]
private static extern IntPtr GetStdHandle(int handle);
/// <summary>
/// Allows text in the console to be selected and copied.
/// </summary>
public static void EnableConsoleEditMode()
{
/*
int mode;
var handle = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(handle, out mode);
mode |= ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(handle, mode);*/
}
public static double NextDouble(this Random rng, double min, double max)
{
return min + (rng.NextDouble() * (max - min));
}
internal static class CursorPosT
{
private static int _posX;
private static int _posY;
static CursorPosT()
{
Game.OnWndProc += Game_OnWndProc;
}
private static void Game_OnWndProc(WndEventArgs args)
{
if (args.Msg == (uint)WindowsMessages.WM_MOUSEMOVE)
{
_posX = unchecked((short)args.LParam);
_posY = unchecked((short)((long)args.LParam >> 16));
}
}
internal static Vector2 GetCursorPos()
{
return new Vector2(_posX, _posY);
}
}
}
public static class EnumerableExtensions
{
/// <summary>
/// Searches for an element that matches the conditions defined by the specified predicate, and returns the first
/// occurrence within the entire IEnumerable.
/// </summary>
public static TSource Find<TSource>(this IEnumerable<TSource> source, Predicate<TSource> match)
{
return (source as List<TSource> ?? source.ToList()).Find(match);
}
/// <summary>
/// Retrieves all the elements that match the conditions defined by the specified predicate.
/// </summary>
[Obsolete("Use IEnumerable<TSource>.Where() instead", false)]
public static List<TSource> FindAll<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
return source.Where(predicate).ToList();
}
public static T MaxOrDefault<T, R>(this IEnumerable<T> container, Func<T, R> valuingFoo) where R : IComparable
{
var enumerator = container.GetEnumerator();
if (!enumerator.MoveNext())
{
return default(T);
}
var maxElem = enumerator.Current;
var maxVal = valuingFoo(maxElem);
while (enumerator.MoveNext())
{
var currVal = valuingFoo(enumerator.Current);
if (currVal.CompareTo(maxVal) > 0)
{
maxVal = currVal;
maxElem = enumerator.Current;
}
}
return maxElem;
}
public static T MinOrDefault<T, R>(this IEnumerable<T> container, Func<T, R> valuingFoo) where R : IComparable
{
var enumerator = container.GetEnumerator();
if (!enumerator.MoveNext())
{
return default(T);
}
var minElem = enumerator.Current;
var minVal = valuingFoo(minElem);
while (enumerator.MoveNext())
{
var currVal = valuingFoo(enumerator.Current);
if (currVal.CompareTo(minVal) < 0)
{
minVal = currVal;
minElem = enumerator.Current;
}
}
return minElem;
}
}
public static class WeightedRandom
{
public static Random Random = new Random(Utils.TickCount);
public static int Next(int min, int max)
{
var list = new List<int>();
list.AddRange(Enumerable.Range(min, max));
var mean = list.Average();
var stdDev = list.StandardDeviation();
var v1 = Random.NextDouble();
var v2 = Random.NextDouble();
var randStdNormal = Math.Sqrt(-2.0 * Math.Log(v1)) *
Math.Sin(2.0 * Math.PI * v2);
return (int)(mean + stdDev * randStdNormal);
}
public static double StandardDeviation(this IEnumerable<int> values)
{
var enumerable = values as int[] ?? values.ToArray();
var avg = enumerable.Average();
return Math.Sqrt(enumerable.Average(v => Math.Pow(v - avg, 2)));
}
}
}