public bool WaitForState(GameState currentState, GameState nextState, Action action, int waitTimeInMs, Action actionForUnknown = null) { DateTime startTime = DateTime.Now; Console.WriteLine("WaitForState: {0} -> {1}", currentState, nextState); do { if (!MainWindowUtility.ExistsMainWindow() && !ScreenUtility.IsForegroundFullScreen()) { Console.WriteLine("No main window and no full screen foreground window"); return(false); } var activeState = Detector.Detect(); Console.WriteLine("{0} State: {1}", DateTime.Now, activeState); if (activeState == nextState) { return(true); } if (DialogHandler.IsDialogState(activeState)) { DialogHandler.Handle(activeState); } if (activeState == currentState && currentState != GameState.Unknown && action != null) { Console.WriteLine("Execute action {0}", action); action(); } if (activeState == GameState.Unknown && actionForUnknown != null) { Console.WriteLine("Execute action for unknown {0}", actionForUnknown); actionForUnknown(); } var elapsedTime = DateTime.Now - startTime; if (elapsedTime.TotalMilliseconds > waitTimeInMs) { return(false); } AutoIt.AutoItX.Sleep(1000); } while (true); }
static void Run(string target, JobType job) { bool existsMainWindow = MainWindowUtility.ExistsMainWindow(); if (!existsMainWindow && !ScreenUtility.IsForegroundFullScreen()) { if (!Launch(target)) { return; } } else { Console.WriteLine("Main windows had been opened"); } if (existsMainWindow) { MainWindowUtility.ActivateAndWait(); } var screenBounds = ScreenUtility.GetScreenBounds(null); Console.WriteLine("Screen: <{0}, {1}, {2}, {3}>", screenBounds.Top, screenBounds.Left, screenBounds.Height, screenBounds.Width); IntPtr hwnd = IntPtr.Zero; if (!ScreenUtility.IsForegroundFullScreen()) { hwnd = MainWindowUtility.GetMainWindowHandle(); var windowBounds = ScreenUtility.GetWindowBounds(hwnd); var clientRect = ScreenUtility.GetClientRect(hwnd); Console.WriteLine("Window: <{0}, {1}, {2}, {3}>", windowBounds.Top, windowBounds.Left, windowBounds.Height, windowBounds.Width); Console.WriteLine("Client: <{0}, {1}, {2}, {3}>", clientRect.Top, clientRect.Left, clientRect.Height, clientRect.Width); ScreenUtility.SetScreenAttribute(screenBounds, false); //int left, width, top, height; //if (windowBounds.Left < 0) //{ // width = windowBounds.Width - 2 * Math.Abs(windowBounds.Left); // left = 0; //} //else //{ // width = windowBounds.Width; // left = windowBounds.Left; //} //if (windowBounds.Top < 0) //{ // height = windowBounds.Height - 2 * Math.Abs(windowBounds.Top); // top = 0; //} //else //{ // height = windowBounds.Height; // top = windowBounds.Top; //} } else { ScreenUtility.SetScreenAttribute(screenBounds, true); } if (!_stateWaiter.WaitForState(GameState.Unknown, GameState.MainWindow, null, MaxSingleTaskTime)) { Console.WriteLine("Failed to show main window although main window was opened"); MainWindowUtility.Close(); return; } if (job == JobType.ElementChallenge) { //if (!CheatEngineUtility.EnableCheating()) //{ // Console.WriteLine("Cheating is not enabled, can't run element challenging job"); // return; //} } if (!ScreenUtility.IsForegroundFullScreen()) { MainWindowUtility.ActivateAndWait(); } Run(job); if (_stateWaiter.Detector.Detect() != GameState.MainWindow) { Console.WriteLine("Failed to go back main window after run"); MainWindowUtility.Close(); return; } }