Skip to content

Commit

Permalink
Update screen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Leviaria authored Jun 24, 2024
1 parent 7a080eb commit 813b12d
Showing 1 changed file with 17 additions and 35 deletions.
52 changes: 17 additions & 35 deletions clashroyalebuildabot/screen.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,60 @@
"""
This script is only for configuration testing and does not have any additional functionality.
"""

import os
import subprocess
import tempfile

import yaml
from adb_shell.adb_device import AdbDeviceTcp
from PIL import Image
import yaml

import tempfile

# Load configuration from config.yaml
def load_config(config_path=r"clashroyalebuildabot\config.yaml"):
with open(config_path, "r") as file:
def load_config(config_path=r'clashroyalebuildabot\config.yaml'):
with open(config_path, 'r', encoding='utf-8') as file: # Specify encoding here
config = yaml.safe_load(file)
return config


# Initialize ADB connection
def init_adb_connection(host, port):
device = AdbDeviceTcp(host, port, default_transport_timeout_s=9.0)
device.connect()
return device


# Get emulator resolution
def get_emulator_resolution(device):
result = device.shell("wm size")
result = device.shell('wm size')
return result.strip()


# Get emulator density
def get_emulator_density(device):
result = device.shell("wm density")
result = device.shell('wm density')
return result.strip()


# Take a screenshot
def take_screenshot(device):
screenshot_path = "/sdcard/screen.png"
device.shell(f"screencap -p {screenshot_path}")
screenshot_path = '/sdcard/screen.png'
device.shell(f'screencap -p {screenshot_path}')
return screenshot_path


# Pull the screenshot to a temporary file
def pull_screenshot(device, remote_path):
local_fd, local_path = tempfile.mkstemp(suffix=".png")
local_fd, local_path = tempfile.mkstemp(suffix='.png')
os.close(local_fd) # Close the file descriptor immediately
device.pull(remote_path, local_path)
return local_path


# Open the screenshot
def open_screenshot(local_path):
img = Image.open(local_path)
img.show()


# Delete the remote and local screenshot files
def delete_screenshot(device, remote_path, local_path):
device.shell(f"rm {remote_path}")
device.shell(f'rm {remote_path}')
os.remove(local_path)


# Check emulator properties
def check_emulator_properties():
config = load_config()
adb_config = config["adb"]

device = init_adb_connection(adb_config["ip"], adb_config["port"])
adb_config = config['adb']
device = init_adb_connection(adb_config['ip'], adb_config['port'])

resolution = get_emulator_resolution(device)
density = get_emulator_density(device)
Expand All @@ -78,16 +63,14 @@ def check_emulator_properties():
density_correct = "240" in density

if resolution_correct and density_correct:
print(
"The emulator has the correct resolution (720x1280) and density (240 dpi)."
)

print("The emulator has the correct resolution (720x1280) and density (240 dpi).")

screenshot_path = take_screenshot(device)
local_screenshot_path = pull_screenshot(device, screenshot_path)
open_screenshot(local_screenshot_path)

delete_screenshot(device, screenshot_path, local_screenshot_path)

print("ClashRoyaleBuildABot can now be started with `python main.py`.")
else:
print("The emulator does not have the correct properties.")
Expand All @@ -96,6 +79,5 @@ def check_emulator_properties():
if not density_correct:
print(f"Current density: {density}")


if __name__ == "__main__":
check_emulator_properties()

0 comments on commit 813b12d

Please sign in to comment.