/// <summary> /// Fast Capturing screen and return the image, uses WinAPI capture if Variables.Background is false. /// </summary> public static ScreenshotData ImageCapture([CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { if (!ScriptRun.Run) { return(null); } if (Variables.WinApiCapt && !Instance.captureerror) { if (Variables.ProchWnd != null) { return(ImageCapture(Variables.ProchWnd, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller)); } else { return(ImageCapture(Variables.Proc.MainWindowHandle, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller)); } } Instance.captureerror = false; try { Stopwatch s = Stopwatch.StartNew(); if (!Directory.Exists(Variables.SharedPath)) { Variables.ScriptLog("Warning, unable to find shared folder! Trying to use WinAPI!", Color.Red); Variables.WinApiCapt = true; return(ImageCapture(Variables.Proc.MainWindowHandle, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd, lineNumber, caller)); } if (AdbInstance.Instance.pcimagepath == "" || AdbInstance.Instance.androidimagepath == "") { var tempname = Encryption.SHA256(DateTime.Now.ToString()); AdbInstance.Instance.pcimagepath = (Variables.SharedPath + "\\" + tempname + ".rgba").Replace("\\\\", "\\"); if (Variables.AndroidSharedPath.Contains("|")) { foreach (var path in Variables.AndroidSharedPath.Split('|')) { if (EmulatorLoader.AndroidDirectoryExist(path)) { string temppath = path; if (temppath.Last() != '/') { temppath += "/"; } AdbInstance.Instance.androidimagepath = (temppath + tempname + ".rgba"); Variables.AdvanceLog("Multiple Android Path settes, selected " + AdbInstance.Instance.androidimagepath); break; } } } else { AdbInstance.Instance.androidimagepath = (Variables.AndroidSharedPath + tempname + ".rgba"); } } byte[] raw = null; if (Variables.Controlled_Device == null) { Variables.AdvanceLog("No device connected!", lineNumber, caller); EmulatorLoader.ConnectAndroidEmulator(); return(null); } if ((Variables.Controlled_Device as DeviceData).State == SharpAdbClient.DeviceState.Offline || !ScriptRun.Run) { return(null); } ConsoleOutputReceiver receiver = new ConsoleOutputReceiver(); AdbInstance.Instance.client.ExecuteRemoteCommand("screencap " + AdbInstance.Instance.androidimagepath, (Variables.Controlled_Device as DeviceData), receiver); if (Variables.NeedPull) { if (File.Exists(AdbInstance.Instance.pcimagepath)) { File.Delete(AdbInstance.Instance.pcimagepath); } BotCore.Pull(AdbInstance.Instance.androidimagepath, AdbInstance.Instance.pcimagepath); } if (!File.Exists(AdbInstance.Instance.pcimagepath)) { Variables.AdvanceLog("Unable to read rgba file because of file not exist!", lineNumber, caller); return(null); } raw = File.ReadAllBytes(AdbInstance.Instance.pcimagepath); int expectedsize = (Variables.EmulatorHeight * Variables.EmulatorWidth * 4) + 12; if (raw.Length != expectedsize || raw.Length > int.MaxValue || raw.Length < 1) { //Image is not in same size, resize emulator EmulatorLoader.ResizeEmulator(); return(null); } byte[] img = new byte[raw.Length - 12]; //remove header Array.Copy(raw, 12, img, 0, img.Length); Image <Rgba, byte> image = new Image <Rgba, byte>(Variables.EmulatorWidth, Variables.EmulatorHeight); image.Bytes = img; if (Variables.ImageDebug) { image.Save("Profiles\\Logs\\" + Encryption.SHA256(DateTime.Now.ToString()) + ".bmp"); } Variables.AdvanceLog("Screenshot saved to memory used " + s.ElapsedMilliseconds + " ms", lineNumber, caller); s.Stop(); return(Compress(image.Bitmap)); } catch (IOException) { } catch (ArgumentOutOfRangeException) { } return(null); }
private static void _RunScript(bool KeepRunning, ScriptInterface script) { if (script != null) { do { try { script.Script(); errornum = 0; } catch (Exception ex) { if (ex is SocketException || ex is DeviceNotFoundException || ex is AdbException) { Variables.ScriptLog("Exception found! Restarting emulator as emulator had closed!", Color.Red); Start: try { AdbInstance.Instance.server.RestartServer(); } catch { Variables.ScriptLog("Adb start failed! Retrying in 3 seconds!"); BotCore.Delay(3000); goto Start; } try { EmulatorLoader.RestartEmulator(); BotCore.Delay(10000); EmulatorLoader.ConnectAndroidEmulator(); script.ResetScript(); } catch (Exception ex2) { if (ex2 is SocketException || ex2 is DeviceNotFoundException || ex2 is AdbException) { goto Start; } else if (ex is ThreadAbortException) //Bot stopped { } else { ThrowException(ex2); } } continue; } else if (ex is ThreadAbortException) //Bot stopped { script.ResetScript(); } else { ThrowException(ex); continue; } } }while (KeepRunning && Run); } else { throw new DllNotFoundException("No script dll found!"); } Run = false; }