def init_screencap_mode(cls, mode): consts = UtilConsts.ScreenCapMode cls.screencap_mode = mode if cls.screencap_mode == consts.ASCREENCAP: # Prepare for ascreencap, push the required libraries Adb.exec_out('rm /data/local/tmp/ascreencap') cpuArc = Adb.exec_out('getprop ro.product.cpu.abi').decode( 'utf-8').strip() sdkVer = int( Adb.exec_out('getprop ro.build.version.sdk').decode( 'utf-8').strip()) ascreencaplib = 'ascreencap_{}'.format(cpuArc) if sdkVer in range(21, 26) and os.path.isfile(ascreencaplib): Adb.cmd( 'push {} /data/local/tmp/ascreencap'.format(ascreencaplib)) else: Logger.log_warning( 'No suitable version of aScreenCap lib is available locally, using ascreencap_local...' ) if os.path.isfile('ascreencap_local'): Adb.cmd('push ascreencap_local /data/local/tmp/ascreencap') else: Logger.log_error( 'File "ascreencap_local" not found. Please download the appropriate version of aScreenCap for your device from github.com/ClnViewer/Android-fast-screen-capture and save it as "ascreencap_local"' ) Logger.log_warning( 'Since aScreenCap is not ready, falling back to normal adb screencap' ) Utils.useAScreenCap = False Adb.shell('chmod 0777 /data/local/tmp/ascreencap')
def screencapInit(cls, config): """Method that loads the required infos for screen_update and touch to work (screen resolution, emulator name, shared folder path), loads ascreencap in shared folder or through adb push depending on the device Args: config (Config): ALAuto Config instance. """ cls.resolution = config.resolution cls.emulator = config.network['emulator'] cls.sharedfolder = config.network['sharedfolder'] #loading acreencap in shared folder if not present if config.network['emulator'] == ('Memu' or 'BlueStacks'): if not os.path.exists(config.network['sharedfolder'] + 'ascreencap'): Logger.log_info('loading ascreencap in shared folder...') shutil.copy('ascreencap/x86/ascreencap', config.network['sharedfolder']) #loading ascreencap for other devices elif useAScreenCap == True: cpuArch = str(Adb.shell('getprop ro.product.cpu.abi')) #supported ascreencap cpu achitectures for arch in ['arm64-v8a', 'armeabi-v7a', 'x86_64', 'x86']: if cpuArch.find(arch) != -1: Logger.log_debug("CPU achitecture found: " + arch) Adb.push( 'ascreencap/{}/ascreencap /data/local/tmp/'.format( arch)) Adb.shell('chmod 0777 /data/local/tmp/ascreencap') return Logger.log_error("CPU architecture not supported: {}".format(arch)) sys.exit()
def touch(cls, coords): """Sends an input command to touch the device screen at the specified coordinates via ADB Args: coords (array): An array containing the x and y coordinate of where to touch the screen """ Adb.shell("input swipe {} {} {} {} {}".format(coords[0], coords[1], coords[0], coords[1], randint(50, 120))) cls.script_sleep()
def swipe(cls, x1, y1, x2, y2, ms): """Sends an input command to swipe the device screen between the specified coordinates via ADB Args: x1 (int): x-coordinate to begin the swipe at. y1 (int): x-coordinate to end the swipe at. x2 (int): y-coordinate to begin the swipe at. y2 (int): y-coordinate to begin the swipe at. ms (int): Duration in ms of swipe. """ Adb.shell("input swipe {} {} {} {} {}".format(x1, y1, x2, y2, ms))
def swipe(cls, x1, y1, x2, y2, ms): """Sends an input command to swipe the device screen between the specified coordinates via ADB Args: x1 (int): x-coordinate to begin the swipe at. y1 (int): y-coordinate to begin the swipe at. x2 (int): x-coordinate to end the swipe at. y2 (int): y-coordinate to end the swipe at. ms (int): Duration in ms of swipe. This value shouldn't be lower than 300, better if it is higher. """ Adb.shell("input swipe {} {} {} {} {}".format(x1, y1, x2, y2, ms)) cls.update_screen()
def update_screen(): """Uses ADB to pull a screenshot of the device and then read it via CV2 and then returns the read image. The image is in a grayscale format. Returns: image: A CV2 image object containing the current device screen. """ global screen screen = None while screen is None: if Adb.legacy: screen = cv2.imdecode(numpy.fromstring(Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"),dtype=numpy.uint8),0) else: screen = cv2.imdecode(numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), 0)
def get_color_screen(): """Uses ADB to pull a screenshot of the device and then read it via CV2 and then returns the read image. The image is in a BGR format. Returns: image: A CV2 image object containing the current device screen. """ color_screen = None while color_screen is None: if Adb.legacy: color_screen = cv2.imdecode(numpy.fromstring(Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"),dtype=numpy.uint8), 1) else: color_screen = cv2.imdecode(numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), 1) return color_screen
def find_siren_elites(cls): # XXX: This should be pulled into its own method at some point. color_screen = None while color_screen is None: if Adb.legacy: color_screen = cv2.imdecode( numpy.fromstring( Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"), dtype=numpy.uint8), 1) else: color_screen = cv2.imdecode( numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), 1) image = cv2.cvtColor(color_screen, cv2.COLOR_BGR2HSV) # We use this primarily to pick out elites from event maps. Depending on the event, this may need to be updated with additional masks. lower_red = numpy.array([170, 210, 180]) upper_red = numpy.array([180, 255, 255]) mask = cv2.inRange(image, lower_red, upper_red) ret, thresh = cv2.threshold(mask, 50, 255, cv2.THRESH_BINARY) # Build a structuring element to combine nearby contours together. rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25)) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, rect_kernel) im, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = list(filter(lambda x: cv2.contourArea(x) > 3000, contours)) locations = [] for contour in contours: hull = cv2.convexHull(contour) M = cv2.moments(hull) x = round(M['m10'] / M['m00']) y = round(M['m01'] / M['m00']) approx = cv2.approxPolyDP(hull, 0.04 * cv2.arcLength(contour, True), True) bound_x, bound_y, width, height = cv2.boundingRect(approx) aspect_ratio = width / float(height) # Avoid clicking on areas outside of the grid, filter out non-Siren matches (non-squares) if y > 160 and y < 938 and x > 180 and x < 1790 and len( approx) == 4 and aspect_ratio >= 1.5: locations.append([x, y]) return cls.filter_similar_coords(locations)
def touch(cls, coords): """Sends an input command to touch the device screen at the specified coordinates via ADB Args: coords (array): An array containing the x and y coordinate of where to touch the screen """ #scaling the touch coords depending on screen resolution if re.search('1920x1080|1080x1920', cls.resolution): coords = (int(coords[0]), int(coords[1])) else: #for 720p coords = (int(coords[0] / 1.5), int(coords[1] / 1.5)) Adb.shell("input swipe {} {} {} {} {}".format(coords[0], coords[1], coords[0], coords[1], randint(50, 120))) cls.script_sleep()
def update_screen(): """Uses ADB to pull a screenshot of the device and then read it via CV2 and then returns the read image. The image is in a grayscale format. Returns: image: A CV2 image object containing the current device screen. """ global screen screen = None while screen is None: if Adb.legacy: screen = cv2.imdecode( numpy.fromstring( Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"), dtype=numpy.uint8), 0) elif not Utils.useAScreenCap: screen = cv2.imdecode( numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), 0) else: start_time = time.perf_counter() raw_compressed_data = Utils.reposition_byte_pointer( Adb.exec_out( '/data/local/tmp/ascreencap --pack 2 --stdout')) compressed_data_header = numpy.frombuffer( raw_compressed_data[0:20], dtype=numpy.uint32) if compressed_data_header[0] != 828001602: compressed_data_header = compressed_data_header.byteswap() if compressed_data_header[0] != 828001602: Logger.log_error( 'If error persists, disable aScreenCap and report traceback' ) raise Exception( 'aScreenCap header verification failure, corrupted image received. HEADER IN HEX = {}' .format(compressed_data_header.tobytes().hex())) uncompressed_data_size = compressed_data_header[1].item() screen = cv2.imdecode( numpy.frombuffer(lz4.block.decompress( raw_compressed_data[20:], uncompressed_size=uncompressed_data_size), dtype=numpy.uint8), 0) elapsed_time = time.perf_counter() - start_time Logger.log_debug("aScreenCap took {} ms to complete.".format( '%.2f' % (elapsed_time * 1000)))
def swipe(cls, x1, y1, x2, y2, ms): """Sends an input command to swipe the device screen between the specified coordinates via ADB Args: x1 (int): x-coordinate to begin the swipe at. y1 (int): y-coordinate to begin the swipe at. x2 (int): x-coordinate to end the swipe at. y2 (int): y-coordinate to end the swipe at. ms (int): Duration in ms of swipe. This value shouldn't be lower than 300, better if it is higher. """ #scaling the swipe coords depending on screen resolution if re.search('1920x1080|1080x1920', cls.resolution): Adb.shell("input swipe {} {} {} {} {}".format(x1, y1, x2, y2, ms)) else: #for 720p Adb.shell("input swipe {} {} {} {} {}".format( x1 / 1.5, y1 / 1.5, x2 / 1.5, y2 / 1.5, ms)) cls.update_screen()
def update_screen(): """Uses ADB to pull a screenshot of the device and then read it via CV2 and then returns the read image. Returns: image: A CV2 image object containing the current device screen. """ decoded = None while decoded is None: decoded = cv2.imdecode( numpy.fromstring( Adb.exec_out('screencap -p'), dtype=numpy.uint8), 0) return decoded
def update_screen(cls, color=0): """Uses ascreencap or ADB depending on cls.emulator the to pull a screenshot of the device and then read it via CV2 and then returns the read image. The image is in a grayscale or BGR format. Args: color(boolean) by default 0 for grayscale, 1 outputs BGR image Returns: image: A CV2 image object containing the current device screen. """ start_time_total = time.time() global screen screen = None while screen is None: #ascreencap supported for Memu and BlueStacks only if cls.emulator == 'Memu': Adb.exec_out( '/storage/emulated/legacy/Download/ascreencap -f /storage/emulated/legacy/Download/screenshot.bmp' ) if re.search('1920x1080|1080x1920', cls.resolution): screen = cv2.imread(cls.sharedfolder + "screenshot.bmp", color) else: screen = cv2.resize( cv2.imread(cls.sharedfolder + "screenshot.bmp", color), (1920, 1080)) elif cls.emulator == 'BlueStacks': Adb.exec_out( '/storage/emulated/0/windows/BstSharedFolder/ascreencap -f /storage/emulated/0/windows/BstSharedFolder/screenshot.bmp' ) if re.search('1920x1080|1080x1920', cls.resolution): screen = cv2.imread(cls.sharedfolder + "screenshot.bmp", color) else: screen = cv2.resize( cv2.imread(cls.sharedfolder + "screenshot.bmp", color), (1920, 1080)) else: #defaults to adb screencap if Adb.legacy: screen = cv2.imdecode( numpy.fromstring( Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"), dtype=numpy.uint8), 0) else: screen = cv2.imdecode( numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), 0) end_time_total = time.time() Logger.log_debug("update_screen took: " + str(end_time_total - start_time_total)[:5])
parser.add_argument('-l', '--legacy', help='Enables sed usage.', action='store_true') args = parser.parse_args() # check args, and if none provided, load default config if args: if args.config: config = Config(args.config) else: config = Config('config.ini') if args.debug: Logger.log_info("Enabled debugging.") Logger.enable_debugging(Logger) if args.legacy: Logger.log_info("Enabled sed usage.") Adb.enable_legacy(Adb) script = ALAuto(config) script.run_update_check() Adb.service = config.network['service'] Adb.device = '-d' if (Adb.service == 'PHONE') else '-e' adb = Adb() if adb.init(): Logger.log_msg('Successfully connected to the service.') output = Adb.exec_out('wm size').decode('utf-8').strip() if not re.search('1920x1080|1080x1920', output): Logger.log_error("Resolution is not 1920x1080, please change it.") sys.exit()
import cv2 from util.adb import Adb from PIL import Image # A short script to take screen captures to contribute to the 720p bot version # Works with Memu by default, refer to update_screen in utils.py to make it work with Bluestacks # The output should be a 1080p upscaled version of the 720p screenshot Adb.service = '192.168.0.17:5555' Adb.tcp = False adb = Adb() adb.exec_out( '/storage/emulated/legacy/Download/ascreencap -f /storage/emulated/legacy/Download/screenshot.bmp' ) screen = cv2.resize( cv2.imread("C:\\Users\\Michel14\\Downloads\\MEmu Download\\screenshot.bmp", 1), (1920, 1080)) cv2.imwrite('screenshot.png', screen)
import sys from util.logger import Logger from util.adb import Adb from util.config import Config from modules.seal import Seal from modules.raise_dog import RaiseDog from util.utils import Utils from time import sleep config = Config("config.ini") Adb.service = config.network["service"] Adb.device = "-d" if (Adb.service == "PHONE") else "-e" adb = Adb() if adb.init(): Logger.log_msg('Successfully connected to the service.') output = Adb.exec_out('wm size').decode('utf-8').strip() Logger.log_info(output) if not re.search('1080x2340|2340x1080', output): Logger.log_error("Resolution is not 2340x1080, please change it.") sys.exit() # 开启妖气封印 # seal = Seal(config) # seal.start_logic() # 养狗粮
help='Enables sed usage.', action='store_true') args = parser.parse_args() # check args, and if none provided, load default config if args: if args.config: config = Config(args.config) else: config = Config('config.ini') if args.debug: Logger.log_info("Enabled debugging.") Logger.enable_debugging(Logger) if args.legacy: Logger.log_info("Enabled sed usage.") Adb.enable_legacy(Adb) script = ALAuto(config) script.run_update_check() Adb.service = config.network['service'] Adb.device = '-d' if (Adb.service == 'PHONE') else '-e' adb = Adb() if adb.init(): Logger.log_msg('Successfully connected to the service.') res = ['1920x1080', '1080x1920'] if Adb.exec_out('wm size').decode('utf-8').strip()[15:] not in res: Logger.log_error("Resolution is not 1920x1080, please change it.") sys.exit()
def update_screen(cls): """Uses ADB to pull a screenshot of the device and then read it via CV2 and then stores the images in grayscale and color to screen and color_screen, respectively. Returns: image: A CV2 image object containing the current device screen. """ consts = UtilConsts.ScreenCapMode global screen screen = None color_screen = None while color_screen is None: if Adb.legacy: color_screen = cv2.imdecode( numpy.fromstring(Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"), dtype=numpy.uint8), cv2.IMREAD_COLOR) else: if cls.screencap_mode == consts.SCREENCAP_PNG: start_time = time.perf_counter() color_screen = cv2.imdecode(numpy.frombuffer(Adb.exec_out('screencap -p'), dtype=numpy.uint8), cv2.IMREAD_COLOR) elapsed_time = time.perf_counter() - start_time Logger.log_debug("SCREENCAP_PNG took {} ms to complete.".format('%.2f' % (elapsed_time * 1000))) elif cls.screencap_mode == consts.SCREENCAP_RAW: start_time = time.perf_counter() pixel_size = 4 byte_arr = Adb.exec_out('screencap') header_format = 'III' header_size = struct.calcsize(header_format) if len(byte_arr) < header_size: continue header = struct.unpack(header_format, byte_arr[:header_size]) width = header[0] height = header[1] if len(byte_arr) != header_size + width * height * pixel_size: continue tmp = numpy.frombuffer(byte_arr, dtype=numpy.uint8, count=width * height * 4, offset=header_size) rgb_img = tmp.reshape((height, width, -1)) color_screen = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2BGR) elapsed_time = time.perf_counter() - start_time Logger.log_debug("SCREENCAP_RAW took {} ms to complete.".format('%.2f' % (elapsed_time * 1000))) elif cls.screencap_mode == consts.ASCREENCAP: start_time = time.perf_counter() raw_compressed_data = Utils.reposition_byte_pointer( Adb.exec_out('/data/local/tmp/ascreencap --pack 2 --stdout')) compressed_data_header = numpy.frombuffer(raw_compressed_data[0:20], dtype=numpy.uint32) if compressed_data_header[0] != 828001602: compressed_data_header = compressed_data_header.byteswap() if compressed_data_header[0] != 828001602: Logger.log_error('If error persists, disable aScreenCap and report traceback') raise Exception( 'aScreenCap header verification failure, corrupted image received. HEADER IN HEX = {}'.format( compressed_data_header.tobytes().hex())) uncompressed_data_size = compressed_data_header[1].item() color_screen = cv2.imdecode(numpy.frombuffer( lz4.block.decompress(raw_compressed_data[20:], uncompressed_size=uncompressed_data_size), dtype=numpy.uint8), cv2.IMREAD_COLOR) elapsed_time = time.perf_counter() - start_time Logger.log_debug("aScreenCap took {} ms to complete.".format('%.2f' % (elapsed_time * 1000))) else: raise Exception('Unknown screencap mode') screen = cv2.cvtColor(color_screen, cv2.COLOR_BGR2GRAY) cls.color_screen = color_screen cls.screen = screen
help='Enables sed usage.', action='store_true') args = parser.parse_args() config = Config('config.ini') # check args, and if none provided, load default config if args: if args.config: config = Config(args.config) if args.debug: Logger.log_info("Enabled debugging.") Logger.enable_debugging(Logger) if args.legacy: Logger.log_info("Enabled sed usage.") Adb.enable_legacy(Adb) script = ALAuto(config) script.run_update_check() Adb.service = config.network['service'] Adb.device = '-d' if (Adb.service == 'PHONE') else '-e' adb = Adb() if adb.init(): Logger.log_msg('Successfully connected to the service.') else: Logger.log_error('Unable to connect to the service.') sys.exit() while True: Utils.update_screen()
# exit() # check run-time args parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', metavar=('CONFIG_FILE'), help='Use the specified configuration file instead ' + 'of the default config.ini') parser.add_argument('-d', '--debug', nargs=2, metavar=('IMAGE_FILE', 'SIMILARITY'), help='Finds the specified image on the screen at ' + 'the specified similarity') parser.add_argument('--copyright',) args = parser.parse_args() # check args, and if none provided, load default config if args and args.config: config = Config(args.config) else: config = Config('config.ini') script = ALAuto(config) Adb.init() while True: script.run_test() script.run_commission_cycle() script.run_combat_cycle() script.run_mission_cycle() script.print_cycle_stats()
help='Enables sed usage.', action='store_true') args = parser.parse_args() # check args, and if none provided, load default config if args: if args.config: config = Config(args.config) else: config = Config('config.ini') if args.debug: Logger.log_info("Enabled debugging.") Logger.enable_debugging(Logger) if args.legacy: Logger.log_info("Enabled sed usage.") Adb.enable_legacy(Adb) script = ALAuto(config) script.run_update_check() Adb.service = config.network['service'] Adb.tcp = False if (Adb.service.find(':') == -1) else True adb = Adb() if adb.init(): Logger.log_msg( 'Successfully connected to the service with transport_id({}).'.format( Adb.transID)) config.resolution = Adb.exec_out('wm size').decode('utf-8').strip() Logger.log_debug('wm size request returned ' + str(config.resolution)) # check supported resolution and setting asset path accordingly
import re import sys from util.logger import Logger from util.adb import Adb from util.config import Config from modules.seal import Seal from util.utils import Utils, Region, globals_region from time import sleep config = Config("config.ini") Adb.service = config.network["service"] Adb.device = "-d" if (Adb.service == "PHONE") else "-e" adb = Adb() adb.init() # Utils.update_screen("tmp/2020-03-29 16:47:46.068835.png") # Utils.find("explore/22", 0.5, True) def swipeTest(): Utils.swipe(385, 1037, 1671, 1037, 1000) sleep(2) Utils.swipe(1689, 878, 1172, 483, 1000) sleep(2) Utils.swipe(1689, 878, 1828, 483, 1000) def find_max_levels(): Utils.update_screen("tmp/2020-03-29 17:29:48.594597.png") result = Utils.find_all("combat/level_max", 0.8)
help='Enables sed usage.', action='store_true') args = parser.parse_args() # check args, and if none provided, load default config if args: if args.config: config = Config(args.config) else: config = Config('config.ini') if args.debug: Logger.log_info("Enabled debugging.") Logger.enable_debugging(Logger) if args.legacy: Logger.log_info("Enabled sed usage.") Adb.enable_legacy(Adb) script = ALAuto(config) script.run_update_check() Adb.service = config.network['service'] Adb.tcp = False if (Adb.service.find(':') == -1) else True adb = Adb() if adb.init(): Logger.log_msg( 'Successfully connected to the service with transport_id({}).'.format( Adb.transID)) output = Adb.exec_out('wm size').decode('utf-8').strip() if not re.search('1920x1080|1080x1920', output):
def update_screen(cls, color=0): """Uses ascreencap or ADB depending on cls.emulator the to pull a screenshot of the device and then read it via CV2 and then returns the read image. The image is in a grayscale or BGR format. Args: color(boolean) by default 0 for grayscale, 1 outputs BGR image Returns: image: A CV2 image object containing the current device screen. """ start_time_total = time.time() global screen screen = None method = 'shared folder ' + cls.emulator while screen is None: #ascreencap supported for Memu and BlueStacks only if cls.emulator == 'Memu': Adb.exec_out( '/storage/emulated/legacy/Download/ascreencap -f /storage/emulated/legacy/Download/screenshot.bmp' ) if re.search('1920x1080|1080x1920', cls.resolution): screen = cv2.imread(cls.sharedfolder + "screenshot.bmp", color) else: screen = cv2.resize( cv2.imread(cls.sharedfolder + "screenshot.bmp", color), (1920, 1080)) elif cls.emulator == 'BlueStacks': Adb.exec_out( '/storage/emulated/0/windows/BstSharedFolder/ascreencap -f /storage/emulated/0/windows/BstSharedFolder/screenshot.bmp' ) if re.search('1920x1080|1080x1920', cls.resolution): screen = cv2.imread(cls.sharedfolder + "screenshot.bmp", color) else: screen = cv2.resize( cv2.imread(cls.sharedfolder + "screenshot.bmp", color), (1920, 1080)) #defaults to using stdout methods elif useAScreenCap == True: method = 'Ascreencap' raw_compressed_data = Adb.exec_out( '/data/local/tmp/ascreencap --pack 2 --stdout') compressed_data_header = numpy.frombuffer( raw_compressed_data[0:20], dtype=numpy.uint32) if compressed_data_header[0] != 828001602: compressed_data_header = compressed_data_header.byteswap() if compressed_data_header[0] != 828001602: raise Exception( 'aScreenCap header verification failure, corrupted image received' ) uncompressed_data_size = compressed_data_header[1].item() screen = cv2.imdecode( numpy.frombuffer(lz4.block.decompress( raw_compressed_data[20:], uncompressed_size=uncompressed_data_size), dtype=numpy.uint8), color) else: method = 'Adb screencap' if Adb.legacy: screen = cv2.imdecode( numpy.fromstring( Adb.exec_out(r"screencap -p | sed s/\r\n/\n/"), dtype=numpy.uint8), color) else: screen = cv2.imdecode( numpy.fromstring(Adb.exec_out('screencap -p'), dtype=numpy.uint8), color) end_time_total = time.time() Logger.log_debug("update_screen with method " + method + " took: " + str(end_time_total - start_time_total)[:5])