forked from LeagueSharp/LeagueSharp.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
318 lines (270 loc) · 9.68 KB
/
Utils.cs
File metadata and controls
318 lines (270 loc) · 9.68 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
#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.Security.Cryptography;
using System.Text;
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
}
/// <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;
/// <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>
public static List<TSource> FindAll<TSource>(this IEnumerable<TSource> source, Predicate<TSource> match)
{
return (source as List<TSource> ?? source.ToList()).FindAll(match);
}
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;
}
}
}