-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUtils.cs
482 lines (430 loc) · 17.5 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
475
476
477
478
479
480
481
482
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Resources;
using System.Threading.Tasks;
using LeagueSharp;
using LeagueSharp.Common;
using SAwareness.Properties;
using SharpDX;
using SharpDX.Direct3D9;
using Color = System.Drawing.Color;
using Font = SharpDX.Direct3D9.Font;
using Rectangle = SharpDX.Rectangle;
namespace SAwareness
{
internal static class Log
{
public static String File = "C:\\SAwareness.log";
public static String Prefix = "Packet";
public static void LogString(String text, String file = null, String prefix = null)
{
switch (text)
{
case "missile":
case "DrawFX":
case "Mfx_pcm_mis.troy":
case "Mfx_bcm_tar.troy":
case "Mfx_bcm_mis.troy":
case "Mfx_pcm_tar.troy":
return;
}
LogWrite(text, file, prefix);
}
private static void LogGamePacket(GamePacket result, String file = null, String prefix = null)
{
byte[] b = new byte[result.Size()];
long size = result.Size();
int cur = 0;
while (cur < size - 1)
{
b[cur] = result.ReadByte(cur);
cur++;
}
LogPacket(b, file, prefix);
}
public static void LogPacket(byte[] data, String file = null, String prefix = null)
{
LogWrite(BitConverter.ToString(data), file, prefix);
}
private static void LogWrite(String text, String file = null, String prefix = null)
{
if (text == null)
return;
if (file == null)
file = File;
if (prefix == null)
prefix = Prefix;
using (var stream = new StreamWriter(file, true))
{
stream.WriteLine(prefix + "@" + Game.ClockTime + ": " + text);
}
}
}
internal static class Common
{
public static bool IsOnScreen(Vector3 vector)
{
Vector2 screen = Drawing.WorldToScreen(vector);
if (screen[0] < 0 || screen[0] > Drawing.Width || screen[1] < 0 || screen[1] > Drawing.Height)
return false;
return true;
}
public static bool IsOnScreen(Vector2 vector)
{
Vector2 screen = vector;
if (screen[0] < 0 || screen[0] > Drawing.Width || screen[1] < 0 || screen[1] > Drawing.Height)
return false;
return true;
}
public static Size ScaleSize(this Size size, float scale, Vector2 mainPos = default(Vector2))
{
size.Height = (int) (((size.Height - mainPos.Y)*scale) + mainPos.Y);
size.Width = (int) (((size.Width - mainPos.X)*scale) + mainPos.X);
return size;
}
public static bool IsInside(Vector2 mousePos, Size windowPos, int width, int height)
{
return Utils.IsUnderRectangle(mousePos, windowPos.Width, windowPos.Height, width, height);
}
}
internal class Downloader
{
public delegate void DownloadFinished(object sender, DlEventArgs args);
public static String Host = "https://github.com/Screeder/SAwareness/raw/master/Sprites/SAwareness/";
public static String Path = "CHAMP/";
private readonly List<Files> _downloadQueue = new List<Files>();
public event DownloadFinished DownloadFileFinished;
public void AddDownload(String hostFile, String localFile)
{
_downloadQueue.Add(new Files(hostFile, localFile));
}
public void StartDownload()
{
StartDownloadInternal();
}
private async Task StartDownloadInternal()
{
var webClient = new WebClient();
var tasks = new List<DlTask>();
foreach (Files files in _downloadQueue)
{
Task t = webClient.DownloadFileTaskAsync(new Uri(Host + Path + files.OnlineFile), files.OfflineFile);
tasks.Add(new DlTask(files, t));
}
foreach (DlTask task in tasks)
{
await task.Task;
tasks.Remove(task);
OnFinished(new DlEventArgs(task.Files));
}
}
protected virtual void OnFinished(DlEventArgs args)
{
if (DownloadFileFinished != null)
DownloadFileFinished(this, args);
}
public static void DownloadFile(String hostfile, String localfile)
{
var webClient = new WebClient();
webClient.DownloadFile(Host + Path + hostfile, localfile);
}
public class DlEventArgs : EventArgs
{
public Files DlFiles;
public DlEventArgs(Files files)
{
DlFiles = files;
}
}
private struct DlTask
{
public readonly Files Files;
public readonly Task Task;
public DlTask(Files files, Task task)
{
Files = files;
Task = task;
}
}
public struct Files
{
public String OfflineFile;
public String OnlineFile;
public Files(String onlineFile, String offlineFile)
{
OnlineFile = onlineFile;
OfflineFile = offlineFile;
}
}
}
internal static class SpriteHelper
{
public enum TextureType
{
Default,
Summoner,
Item
}
private static Downloader _downloader = new Downloader();
private static readonly Dictionary<String, byte[]> MyResources = new Dictionary<String, byte[]>();
//private static List<SpriteRef> Sprites = new List<SpriteRef>();
static SpriteHelper()
{
ResourceSet resourceSet = Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
MyResources.Add(entry.Key.ToString().ToLower(), (byte[]) entry.Value);
}
}
//struct SpriteRef
//{
// public String OnlineFile;
// public Texture Texture;
// public SpriteRef(String onlineFile, ref Texture texture)
// {
// OnlineFile = onlineFile;
// Texture = texture;
// }
//}
//public static void LoadTexture(String onlineFile, String subOnlinePath, String localPathFile,
// ref Texture texture, bool bForce = false)
//{
// if (!File.Exists(localPathFile))
// {
// try
// {
// Downloader.Path = subOnlinePath;
// Sprites.Add(new SpriteRef(onlineFile, ref texture));
// Downloader.DownloadFileFinished += Downloader_DownloadFileFinished;
// Downloader.AddDownload(onlineFile, localPathFile);
// Downloader.StartDownload();
// //Downloader.DownloadFile(onlineFile, localPathFile);
// }
// catch (Exception ex)
// {
// Console.WriteLine("SAwareness: Path: " + onlineFile + " \nException: " + ex);
// }
// }
// LoadTexture(localPathFile, ref texture, bForce);
//}
//private static void LoadTexture(String localPathFile, ref Texture texture, bool bForce = false)
//{
// if (File.Exists(localPathFile) && (bForce || texture == null))
// {
// try
// {
// texture = Texture.FromFile(Drawing.Direct3DDevice, localPathFile);
// }
// catch (Exception ex)
// {
// Console.WriteLine("SAwarness: Couldn't load texture: " + localPathFile + "\n Ex: " + ex);
// }
// if (texture == null)
// {
// return;
// }
// }
//}
public static void LoadTexture(String name, ref Texture texture, TextureType type)
{
if ((type == TextureType.Default || type == TextureType.Summoner) && MyResources.ContainsKey(name.ToLower()))
{
try
{
texture = Texture.FromMemory(Drawing.Direct3DDevice, MyResources[name.ToLower()]);
}
catch (Exception ex)
{
Console.WriteLine("SAwarness: Couldn't load texture: " + name + "\n Ex: " + ex);
}
}
else if (type == TextureType.Summoner && MyResources.ContainsKey(name.ToLower().Remove(name.Length - 1)))
{
try
{
texture = Texture.FromMemory(Drawing.Direct3DDevice,
MyResources[name.ToLower().Remove(name.Length - 1)]);
}
catch (Exception ex)
{
Console.WriteLine("SAwarness: Couldn't load texture: " + name + "\n Ex: " + ex);
}
}
else if (type == TextureType.Item && MyResources.ContainsKey(name.ToLower().Insert(0, "_")))
{
try
{
texture = Texture.FromMemory(Drawing.Direct3DDevice, MyResources[name.ToLower().Insert(0, "_")]);
}
catch (Exception ex)
{
Console.WriteLine("SAwarness: Couldn't load texture: " + name + "\n Ex: " + ex);
}
}
else
{
Console.WriteLine("SAwarness: " + name + " is missing. Please inform Screeder!");
}
}
//static void Downloader_DownloadFileFinished(object sender, Downloader.DlEventArgs args)
//{
// for (int i = 0; i < Sprites.Count - 1; i++)
// {
// var spriteRef = Sprites[i];
// if (spriteRef.OnlineFile.Contains(args.DlFiles.OnlineFile))
// {
// LoadTexture(args.DlFiles.OfflineFile, ref spriteRef.Texture);
// }
// }
// foreach (var spriteRef in Sprites.ToArray())
// {
// if (spriteRef.OnlineFile.Contains(args.DlFiles.OnlineFile))
// {
// Sprites.Remove(spriteRef);
// }
// }
//}
}
internal static class DirectXDrawer
{
private static void InternalRender(Vector3 target)
{
//Drawing.Direct3DDevice.SetTransform(TransformState.World, Matrix.Translation(target));
//Drawing.Direct3DDevice.SetTransform(TransformState.View, Drawing.View);
//Drawing.Direct3DDevice.SetTransform(TransformState.Projection, Drawing.Projection);
Drawing.Direct3DDevice.VertexShader = null;
Drawing.Direct3DDevice.PixelShader = null;
Drawing.Direct3DDevice.SetRenderState(RenderState.AlphaBlendEnable, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.BlendOperation, BlendOperation.Add);
Drawing.Direct3DDevice.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
Drawing.Direct3DDevice.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
Drawing.Direct3DDevice.SetRenderState(RenderState.Lighting, 0);
Drawing.Direct3DDevice.SetRenderState(RenderState.ZEnable, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.AntialiasedLineEnable, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.Clipping, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.EnableAdaptiveTessellation, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.MultisampleAntialias, true);
Drawing.Direct3DDevice.SetRenderState(RenderState.ShadeMode, ShadeMode.Gouraud);
Drawing.Direct3DDevice.SetTexture(0, null);
Drawing.Direct3DDevice.SetRenderState(RenderState.CullMode, Cull.None);
}
public static void DrawLine(Vector3 from, Vector3 to, Color color)
{
var vertices = new PositionColored[2];
vertices[0] = new PositionColored(Vector3.Zero, color.ToArgb());
from = from.SwitchYZ();
to = to.SwitchYZ();
vertices[1] = new PositionColored(to - from, color.ToArgb());
InternalRender(from);
Drawing.Direct3DDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices.Length/2, vertices);
}
public static void DrawLine(Line line, Vector3 from, Vector3 to, ColorBGRA color, Size size = default(Size),
float[] scale = null, float rotation = 0.0f)
{
if (line != null)
{
from = from.SwitchYZ();
to = to.SwitchYZ();
Matrix nMatrix = (scale != null ? Matrix.Scaling(scale[0], scale[1], 0) : Matrix.Scaling(1))*
Matrix.RotationZ(rotation)*Matrix.Translation(from);
Vector3[] vec = {from, to};
line.DrawTransform(vec, nMatrix, color);
}
}
public static void DrawText(Font font, String text, Size size, SharpDX.Color color)
{
DrawText(font, text, size.Width, size.Height, color);
}
//TODO: Too many drawtext for shadowtext, need another method fps issues
public static void DrawText(Font font, String text, int posX, int posY, SharpDX.Color color)
{
if (font == null || font.IsDisposed)
{
throw new SharpDXException("");
}
Rectangle rec = font.MeasureText(null, text, FontDrawFlags.Center);
//font.DrawText(null, text, posX + 1 + rec.X, posY, Color.Black);
font.DrawText(null, text, posX + 1 + rec.X, posY + 1, SharpDX.Color.Black);
font.DrawText(null, text, posX + rec.X, posY + 1, SharpDX.Color.Black);
//font.DrawText(null, text, posX - 1 + rec.X, posY, Color.Black);
font.DrawText(null, text, posX - 1 + rec.X, posY - 1, SharpDX.Color.Black);
font.DrawText(null, text, posX + rec.X, posY - 1, SharpDX.Color.Black);
font.DrawText(null, text, posX + rec.X, posY, color);
}
public static void DrawSprite(Sprite sprite, Texture texture, Size size, float[] scale = null,
float rotation = 0.0f)
{
DrawSprite(sprite, texture, size, SharpDX.Color.White, scale, rotation);
}
public static void DrawSprite(Sprite sprite, Texture texture, Size size, SharpDX.Color color,
float[] scale = null,
float rotation = 0.0f)
{
if (sprite != null && !sprite.IsDisposed && texture != null && !texture.IsDisposed)
{
Matrix matrix = sprite.Transform;
Matrix nMatrix = (scale != null ? Matrix.Scaling(scale[0], scale[1], 0) : Matrix.Scaling(1))*
Matrix.RotationZ(rotation)*Matrix.Translation(size.Width, size.Height, 0);
sprite.Transform = nMatrix;
Matrix mT = Drawing.Direct3DDevice.GetTransform(TransformState.World);
//InternalRender(mT.TranslationVector);
if (Common.IsOnScreen(new Vector2(size.Width, size.Height)))
sprite.Draw(texture, color);
sprite.Transform = matrix;
}
}
public static void DrawTransformSprite(Sprite sprite, Texture texture, SharpDX.Color color, Size size,
float[] scale,
float rotation, Rectangle? spriteResize)
{
if (sprite != null && texture != null)
{
Matrix matrix = sprite.Transform;
Matrix nMatrix = Matrix.Scaling(scale[0], scale[1], 0)*Matrix.RotationZ(rotation)*
Matrix.Translation(size.Width, size.Height, 0);
sprite.Transform = nMatrix;
sprite.Draw(texture, color);
sprite.Transform = matrix;
}
}
public static void DrawTransformedSprite(Sprite sprite, Texture texture, Rectangle spriteResize,
SharpDX.Color color)
{
if (sprite != null && texture != null)
{
sprite.Draw(texture, color);
}
}
public static void DrawSprite(Sprite sprite, Texture texture, Size size, SharpDX.Color color,
Rectangle? spriteResize)
{
if (sprite != null && texture != null)
{
sprite.Draw(texture, color, spriteResize, new Vector3(size.Width, size.Height, 0));
}
}
public static void DrawSprite(Sprite sprite, Texture texture, Size size, SharpDX.Color color)
{
if (sprite != null && texture != null)
{
DrawSprite(sprite, texture, size, color, null);
}
}
public struct PositionColored
{
public static readonly int Stride = Vector3.SizeInBytes + sizeof (int);
public int Color;
public Vector3 Position;
public PositionColored(Vector3 pos, int col)
{
Position = pos;
Color = col;
}
}
}
}