Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. using System.Runtime.InteropServices;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.IO;
  11.  
  12. namespace DeadByDaylightHelper
  13. {
  14. static class Program
  15. {
  16. private const int WH_KEYBOARD_LL = 13;
  17. private const int WM_KEYDOWN = 0x0100;
  18. private const int WH_MOUSE_LL = 14;
  19.  
  20. [StructLayout(LayoutKind.Sequential)]
  21. private struct POINT
  22. {
  23. public int x;
  24. public int y;
  25. }
  26.  
  27. [StructLayout(LayoutKind.Sequential)]
  28. private struct MSLLHOOKSTRUCT
  29. {adaDAdd
  30. public POINT pt;
  31. public uint mouseData;
  32. public uint flags;
  33. public uint time;
  34. public IntPtr dwExtraInfo;
  35. }
  36.  
  37. private delegate IntPtr LowLevelProc(int nCode, IntPtr wParam, IntPtr lParam);
  38.  
  39. private static IntPtr HookKeyboardCallback(int nCode, IntPtr wParam, IntPtr lParam)
  40. {
  41. return IntPtr.Zero;// CallNextHookEx(_hookID, nCode, wParam, lParam);
  42. }
  43.  
  44. private static LowLevelProc KeyboardProc = HookKeyboardCallback;
  45.  
  46. private static IntPtr HookMouseCallback(int nCode, IntPtr wParam, IntPtr lParam)
  47. {
  48. return IntPtr.Zero;// CallNextHookEx(_hookID, nCode, wParam, lParam);
  49. }
  50.  
  51. private static LowLevelProc MouseProc = HookMouseCallback;
  52.  
  53. private static IntPtr KeyboardHookID = IntPtr.Zero;
  54. private static IntPtr MouseHookID = IntPtr.Zero;
  55.  
  56. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  57. private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelProc lpfn, IntPtr hMod, uint dwThreadId);
  58.  
  59. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  60. [return: MarshalAs(UnmanagedType.Bool)]
  61. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  62.  
  63. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  64. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  65.  
  66. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  67. private static extern IntPtr GetModuleHandle(string lpModuleName);
  68.  
  69. private static IntPtr SetKeyboardHook(LowLevelProc proc)
  70. {
  71. using (Process curProcess = Process.GetCurrentProcess())
  72. using (ProcessModule curModule = curProcess.MainModule)
  73. {
  74. return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  75. }
  76. }
  77.  
  78. private static IntPtr SetMouseHook(LowLevelProc proc)
  79. {
  80. using (Process curProcess = Process.GetCurrentProcess())
  81. using (ProcessModule curModule = curProcess.MainModule)
  82. {
  83. return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  84. }
  85. }
  86.  
  87. static void SetHooks()
  88. {
  89. KeyboardHookID = SetKeyboardHook(KeyboardProc);
  90. MouseHookID = SetMouseHook(MouseProc);
  91. }
  92.  
  93. static void UnsetHooks()
  94. {
  95. UnhookWindowsHookEx(KeyboardHookID);
  96. UnhookWindowsHookEx(MouseHookID);
  97. }
  98.  
  99. public static string status_dbg = "";
  100.  
  101. static void Main(string[] args)
  102. {
  103. RegisterHotkeys();
  104.  
  105. ReferenceTimer.Start();
  106.  
  107. Tools.UpdateWindowHandle();
  108.  
  109. Overlay overlay = new Overlay();
  110. overlay.InitWindow();
  111.  
  112. Thread helper_thread = new Thread(HelperStart);
  113. helper_thread.Start();
  114.  
  115. Console.WriteLine("Dead by Daylight window handle: " + Tools.HWND);
  116.  
  117. Application.Run();
  118. }
  119.  
  120. public static bool WiggleActive = false;
  121. public static bool StruggleActive = false;
  122.  
  123. public static bool SkillCheckActive = false;
  124. public static volatile int PerfectZonePos = -1;
  125. public static volatile int ProgressPos = -1;
  126. public static float SpaceHitPos = -1;
  127. public static Int64 ScreenCaptureDuration = 0;
  128. public static Int64 ProcessingDuration = 0;
  129.  
  130. public static volatile int LastProgressPos = -1;
  131. public static volatile Stopwatch LastProgressTimer = new Stopwatch();
  132. public static Int64 LastProgressCorrection = 0;
  133.  
  134. // 1680
  135. //public static Vec3 CIRCLE_OFFSET = new Vec3(784, 468, 0);
  136. //public static int CIRCLE_TREMBLE_RANGE = 30;
  137. //public static Vec3 SKILL_CHECK_IMG_OFFSET = new Vec3(15, 45, 0);
  138. //public static int CIRCLE_DIAMETER = 113;
  139. //public static Rectangle SKILL_CHECK_RECT = new Rectangle(320, 200, 1100, 600);
  140. //public static string SKILL_CHECK_BITMAP = "img/skill_check.bmp";
  141. // 1920
  142. public static Vec3 CIRCLE_OFFSET = new Vec3(895, 473, 0);
  143. public static int CIRCLE_TREMBLE_RANGE = 50;
  144. public static Vec3 SKILL_CHECK_IMG_OFFSET = new Vec3(19, 52, 0);
  145. public static int CIRCLE_DIAMETER = 133;
  146. public static Rectangle SKILL_CHECK_RECT = new Rectangle(234, 140, 1500, 700);
  147. public static string SKILL_CHECK_BITMAP = "img/skill_check_1920.bmp";
  148.  
  149. public static Vec3 CIRCLE_CENTER = new Vec3(CIRCLE_DIAMETER * 0.5f, CIRCLE_DIAMETER * 0.5f, 0);
  150. public static Int64 FIXED_CORRECTION = 6;
  151.  
  152. public static Vec3 VEC_UP = new Vec3(0, 0, 1);
  153. public static Vec3 INITIAL_DIR = new Vec3(0, -CIRCLE_DIAMETER * 0.5f, 0);
  154. public static float PROGRESS_SPEED = 0.3273f;
  155.  
  156. public static Color PROGRESS_COLOR = Color.FromArgb(240, 30, 5);
  157. public static Color PROGRESS_COLOR_HEX = Color.FromArgb(250, 250, 250);
  158. public static Color PERFECT_ZONE_COLOR = Color.FromArgb(250, 250, 250);
  159. public static Color PERFECT_ZONE_COLOR_HEX = Color.FromArgb(180, 0, 0);
  160.  
  161. public static ReaderWriterLockSlim ProgressDataLock = new ReaderWriterLockSlim();
  162.  
  163. public static Stopwatch ReferenceTimer = new Stopwatch();
  164.  
  165. public static float GetEstimatedProgressIndex()
  166. {
  167. ProgressDataLock.EnterReadLock();
  168. float estimatedTimeSinceLastProgressChange = LastProgressTimer.ElapsedMilliseconds + LastProgressCorrection;
  169. float value = LastProgressPos + PROGRESS_SPEED * estimatedTimeSinceLastProgressChange;
  170. ProgressDataLock.ExitReadLock();
  171. return value;
  172. }
  173.  
  174. static void SpacerProc()
  175. {
  176. while (true)
  177. {
  178. AutoIt.Sleep(2);
  179.  
  180. float estimatedProgressIndex = GetEstimatedProgressIndex();
  181.  
  182. if (PerfectZonePos != -1 && LastProgressPos != -1 && SpaceHitPos == -1)
  183. {
  184. if (estimatedProgressIndex >= PerfectZonePos)
  185. {
  186. SpaceHitPos = estimatedProgressIndex;
  187. AutoIt.SendKey(Tools.HWND, Input.VirtualKeyCode.SPACE, 50);
  188. AutoIt.Sleep(600);
  189. }
  190. }
  191. else if (WiggleActive)
  192. {
  193. AutoIt.SendKey(Tools.HWND, Input.VirtualKeyCode.VK_A, 50);
  194. AutoIt.Sleep(50);
  195. AutoIt.SendKey(Tools.HWND, Input.VirtualKeyCode.VK_D, 50);
  196. AutoIt.Sleep(50);
  197. }
  198. else if (StruggleActive)
  199. {
  200. AutoIt.SendKey(Tools.HWND, Input.VirtualKeyCode.SPACE, 50);
  201. AutoIt.Sleep(50);
  202. }
  203. }
  204. }
  205.  
  206. static void HelperStart()
  207. {
  208. Thread spacer_thread = new Thread(SpacerProc);
  209. spacer_thread.Start();
  210.  
  211. Stopwatch processing_timer = new Stopwatch();
  212.  
  213. Bitmap skill_check_bmp = new Bitmap(SKILL_CHECK_BITMAP);
  214. IntPtr skill_check_bmp_hbitmap = skill_check_bmp.GetHbitmap();
  215. // capture using slow method once to create bitmap with proper dimensions
  216. Bitmap screen_bmp = AutoIt.CaptureWindow(Tools.HWND);
  217. // create GDI object from out bitmap so we can use it in C++ DLL functions
  218. IntPtr screen_hbitmap = screen_bmp.GetHbitmap();
  219.  
  220. AutoIt.RECT rct = new AutoIt.RECT();
  221. AutoIt.GetWindowRect(Tools.HWND, ref rct);
  222.  
  223. while (true)
  224. {
  225. processing_timer.Restart();
  226.  
  227. //AutoIt._CaptureWindowEx(Tools.HWND, screen_hbitmap); // this will work on any Windows
  228. AutoIt._CaptureWindow(Tools.HWND, screen_hbitmap); // this will work on Windows 8.1 and later
  229.  
  230. ScreenCaptureDuration = processing_timer.ElapsedMilliseconds;
  231. processing_timer.Restart();
  232.  
  233. bool progress_updated = false;
  234. int progress_pos = LastProgressPos;
  235. SkillCheckActive = false;
  236.  
  237. int x = 0; int y = 0;
  238.  
  239. if (AutoIt.ImageSearchArea(skill_check_bmp_hbitmap, 0, (int)(Program.CIRCLE_OFFSET.X + SKILL_CHECK_IMG_OFFSET.X), (int)(Program.CIRCLE_OFFSET.Y + SKILL_CHECK_IMG_OFFSET.Y), (int)Program.CIRCLE_OFFSET.X + Program.CIRCLE_DIAMETER, (int)Program.CIRCLE_OFFSET.Y + Program.CIRCLE_DIAMETER, ref x, ref y, 35, screen_hbitmap) ||
  240. AutoIt.ImageSearchArea(skill_check_bmp_hbitmap, 0, (int)(Program.CIRCLE_OFFSET.X + SKILL_CHECK_IMG_OFFSET.X - CIRCLE_TREMBLE_RANGE), (int)(Program.CIRCLE_OFFSET.Y + SKILL_CHECK_IMG_OFFSET.Y - CIRCLE_TREMBLE_RANGE), (int)Program.CIRCLE_OFFSET.X + Program.CIRCLE_DIAMETER, (int)Program.CIRCLE_OFFSET.Y + Program.CIRCLE_DIAMETER, ref x, ref y, 35, screen_hbitmap) ||
  241. AutoIt.ImageSearchArea(skill_check_bmp_hbitmap, 0, SKILL_CHECK_RECT.Left, SKILL_CHECK_RECT.Top, SKILL_CHECK_RECT.Right, SKILL_CHECK_RECT.Bottom, ref x, ref y, 35, screen_hbitmap))
  242. {
  243. CIRCLE_OFFSET.X = x - SKILL_CHECK_IMG_OFFSET.X;
  244. CIRCLE_OFFSET.Y = y - SKILL_CHECK_IMG_OFFSET.Y;
  245.  
  246. bool is_hex = false;
  247. int progress_pos_min = -1;
  248. int progress_pos_max = -1;
  249.  
  250. // find progress first
  251. for (int i = (ProgressPos != -1 ? ProgressPos : 0); i < 360; ++i)
  252. {
  253. Vec3 dir = Vec3.Rotate(INITIAL_DIR, i, VEC_UP);
  254.  
  255. // check progress pixel (check a little bit outside of the circle so w don't catch success zone)
  256. Vec3 pixel = CIRCLE_OFFSET + CIRCLE_CENTER + dir * 1.11f;
  257. Color pixel_color = AutoIt.PixelGetColor(screen_hbitmap, (int)pixel.X, (int)pixel.Y);
  258.  
  259. if (Tools.IsColor(pixel_color, PROGRESS_COLOR, 20) || Tools.IsColor(pixel_color, PROGRESS_COLOR_HEX, 20))
  260. {
  261. if (progress_pos_min == -1)
  262. {
  263. progress_pos_min = progress_pos_max = i;
  264. }
  265. else
  266. {
  267. progress_pos_max = i;
  268. }
  269.  
  270. is_hex = Tools.IsColor(pixel_color, PROGRESS_COLOR_HEX, 20);
  271. }
  272. else if (progress_pos_min != -1)
  273. {
  274. break;
  275. }
  276. }
  277.  
  278. if (progress_pos_min != -1)
  279. {
  280. ProgressPos = (progress_pos_max + progress_pos_min) / 2;
  281. SkillCheckActive = true;
  282.  
  283. if (ProgressPos > progress_pos)
  284. {
  285. progress_updated = true;
  286. progress_pos = ProgressPos;
  287. }
  288. }
  289.  
  290. // find perfect zone
  291. if (PerfectZonePos == -1)
  292. {
  293. for (int i = 0; i < 360; ++i)
  294. {
  295. Vec3 dir = Vec3.Rotate(INITIAL_DIR, i, VEC_UP);
  296.  
  297. // check perfect zone pixel (on circle)
  298. Vec3 pixel = CIRCLE_OFFSET + CIRCLE_CENTER + dir;
  299. Color pixel_color = AutoIt.PixelGetColor(screen_hbitmap, (int)pixel.X, (int)pixel.Y);
  300.  
  301. if (Tools.IsColor(pixel_color, is_hex ? PERFECT_ZONE_COLOR_HEX : PERFECT_ZONE_COLOR, 40))
  302. {
  303. PerfectZonePos = i;
  304. break;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. if (!SkillCheckActive)
  311. {
  312. PerfectZonePos = -1;
  313. ProgressPos = -1;
  314. LastProgressPos = -1;
  315. SpaceHitPos = -1;
  316. LastProgressTimer.Stop();
  317.  
  318. }
  319.  
  320. ProcessingDuration = processing_timer.ElapsedMilliseconds;
  321.  
  322. if (progress_updated)
  323. {
  324. ProgressDataLock.EnterWriteLock();
  325. LastProgressPos = progress_pos;
  326. LastProgressTimer.Restart();
  327. LastProgressCorrection = processing_timer.ElapsedMilliseconds + FIXED_CORRECTION + ScreenCaptureDuration;
  328. ProgressDataLock.ExitWriteLock();
  329. }
  330. }
  331. }
  332.  
  333. private static void RegisterHotkeys()
  334. {
  335. HotKeyManager.RegisterHotKey(Keys.F1, KeyModifiers.NoRepeat);
  336. HotKeyManager.RegisterHotKey(Keys.F2, KeyModifiers.NoRepeat);
  337. HotKeyManager.RegisterHotKey(Keys.F3, KeyModifiers.NoRepeat);
  338. HotKeyManager.RegisterHotKey(Keys.OemMinus, KeyModifiers.NoRepeat);
  339. HotKeyManager.RegisterHotKey(Keys.Oemplus, KeyModifiers.NoRepeat);
  340. HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(OnKeyPressed);
  341. }
  342.  
  343. private static void OnKeyPressed(object sender, HotKeyEventArgs e)
  344. {
  345. if (e.Key == Keys.F1)
  346. {
  347. WiggleActive = true;
  348. }
  349. else if (e.Key == Keys.F2)
  350. {
  351. StruggleActive = true;
  352. }
  353. else if (e.Key == Keys.F3)
  354. {
  355. WiggleActive = StruggleActive = false;
  356. }
  357. else if (e.Key == Keys.OemMinus)
  358. {
  359. --FIXED_CORRECTION;
  360. }
  361. else if (e.Key == Keys.Oemplus)
  362. {
  363. ++FIXED_CORRECTION;
  364. }
  365. }
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement