private void PlayAndVerify(MacroClickStep macroClickStep, int stepNumber) { Debug.WriteLine("PlayAndVerify for #{0} {1}", macroClickStep.StepNumber, macroClickStep.Description); Bitmap beforeClickSmallImage = null; if (WaitHandle.WaitOne(1)) return; int shortDelay = 500; // move cursor to position SetCursorPos(macroClickStep.X, macroClickStep.Y); // slight delay to allow hover/ui effects to show up if (WaitHandle.WaitOne(1)) return; Debug.WriteLine("Waiting {0}ms", shortDelay); Thread.Sleep(shortDelay); if (WaitHandle.WaitOne(1)) return; // take picture around cursor Debug.WriteLine("Collecting before image at {0} {1}", macroClickStep.X, macroClickStep.Y); beforeClickSmallImage = ExtractCursorBitmap(macroClickStep.X, macroClickStep.Y); Debug.WriteLine("Collecting before screen image for {0} {1}", macroClickStep.X, macroClickStep.Y); var beforeClickScreenImage = ExtractScreenBitmap(); bool retrying = false; while (true) { if (retrying) Debug.WriteLine("Retrying"); // send mouse click Debug.WriteLine("Sending click"); SetCursorPos(macroClickStep.X, macroClickStep.Y); mouse_event(MOUSEEVENTF_LEFTDOWN, macroClickStep.X, macroClickStep.Y, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, macroClickStep.X, macroClickStep.Y, 0, 0); // another short delay if (WaitHandle.WaitOne(1)) return; Debug.WriteLine("Waiting {0}ms", shortDelay); Thread.Sleep(shortDelay); if (WaitHandle.WaitOne(1)) return; // take 'after' picture Debug.WriteLine("Collecting after click image at {0} {1}", macroClickStep.X, macroClickStep.Y); var afterClickSmallImage = ExtractCursorBitmap(macroClickStep.X, macroClickStep.Y); Debug.WriteLine("Collecting after screen image for {0} {1}", macroClickStep.X, macroClickStep.Y); var afterClickScreenImage = ExtractScreenBitmap(); // compare, if too similar then UI did not respond in time or there // was interference (f**k playdom and their snow) if (AreTooSimilar(beforeClickSmallImage, afterClickSmallImage) == true) { Debug.WriteLine("Before-click and after-click images are too similar for {0},{1}", macroClickStep.X, macroClickStep.Y); #if DEBUG beforeClickSmallImage.Save(string.Format("{0}_{1}_click_before.bmp", macroClickStep.X, macroClickStep.Y), ImageFormat.Bmp); afterClickSmallImage.Save(string.Format("{0}_{1}_click_after.bmp", macroClickStep.X, macroClickStep.Y), ImageFormat.Bmp); #endif if (AreTooSimilar(beforeClickScreenImage, afterClickScreenImage) == true) { Debug.WriteLine("Before-screen and after-screen images were too similar for {0},{1}", macroClickStep.X, macroClickStep.Y); #if DEBUG beforeClickScreenImage.Save(string.Format("{0}_{1}_screen_before.bmp", macroClickStep.X, macroClickStep.Y), ImageFormat.Bmp); afterClickScreenImage.Save(string.Format("{0}_{1}_screen_after.bmp", macroClickStep.X, macroClickStep.Y), ImageFormat.Bmp); #endif afterClickSmallImage.Dispose(); retrying = true; continue; } else { Debug.WriteLine("Before-screen and after-screen images were different enough for {0},{1}", macroClickStep.X, macroClickStep.Y); beforeClickSmallImage.Dispose(); afterClickSmallImage.Dispose(); beforeClickSmallImage = null; beforeClickScreenImage.Dispose(); beforeClickScreenImage = null; retrying = false; break; } } else { Debug.WriteLine("Before-click and after-click images are different enough for {0},{1}", macroClickStep.X, macroClickStep.Y); // they were not too similar - UI click worked so break out of // verification loop beforeClickSmallImage.Dispose(); afterClickSmallImage.Dispose(); beforeClickSmallImage = null; beforeClickScreenImage.Dispose(); beforeClickScreenImage = null; retrying = false; break; } } }
private void Play(MacroClickStep macroClickStep, int stepNumber, bool verify, int uiDelay) { if (WaitHandle.WaitOne(1)) return; var elementLower = macroClickStep.ScreenElement.ToString().ToLower(); if (verify == true && ( elementLower.StartsWith("jetbay") || elementLower.StartsWith("collect") )) { PlayAndVerify(macroClickStep, stepNumber); } else { _stepStartedSubject.OnNext(new StepStartedInfo(stepNumber, 1)); _stepProgressUpdatedSubject.OnNext(1); LeftMouseClick(macroClickStep.X, macroClickStep.Y); _stepCompletedSubject.OnNext(stepNumber); } }