|
| 1 | +import json |
| 2 | +import os |
| 3 | +import shlex |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def call(cmd: str, printOutput: bool = True) -> tuple[str, bool]: |
| 9 | + # print(f"{printOutput = }, {cmd = }") |
| 10 | + if sys.platform == "win32": |
| 11 | + args = cmd |
| 12 | + else: |
| 13 | + # linux must split arguments |
| 14 | + args = shlex.split(cmd) |
| 15 | + try: |
| 16 | + if printOutput: |
| 17 | + isOk = subprocess.call(args) == 0 |
| 18 | + return "", isOk |
| 19 | + |
| 20 | + data = subprocess.check_output(args) |
| 21 | + # python3 output is bytes |
| 22 | + output = data.decode("utf-8") |
| 23 | + return output, True |
| 24 | + except subprocess.CalledProcessError as callerr: |
| 25 | + print(f"cmd = {cmd}, callerr.output = {callerr.output}", file=sys.stderr) |
| 26 | + return (callerr.output, False) |
| 27 | + except IOError as ioerr: |
| 28 | + print(f"cmd = {cmd}, ioerr = {ioerr}", file=sys.stderr) |
| 29 | + return "", False |
| 30 | + |
| 31 | + |
| 32 | +def get_project_dir(): |
| 33 | + BASE_DIR = os.path.dirname(__file__) |
| 34 | + BASE_DIR = os.path.dirname(BASE_DIR) |
| 35 | + while BASE_DIR: |
| 36 | + BASE_DIR = os.path.dirname(BASE_DIR) |
| 37 | + if not BASE_DIR: |
| 38 | + return None |
| 39 | + assets_dir = os.path.join(BASE_DIR, "Assets") |
| 40 | + projectsettings_dir = os.path.join(BASE_DIR, "ProjectSettings") |
| 41 | + if os.path.exists(assets_dir) and os.path.exists(projectsettings_dir): |
| 42 | + return BASE_DIR |
| 43 | + return None |
| 44 | + |
| 45 | +def get_script_file(project_dir): |
| 46 | + setting_file = os.path.join(project_dir, "UserSettings/LuaInteractive.json") |
| 47 | + if os.path.exists(setting_file): |
| 48 | + with open(setting_file, mode='r', encoding='utf-8') as f: |
| 49 | + setting = json.loads(f.read()) |
| 50 | + script_file = setting.get('scriptPath', '') |
| 51 | + if script_file: |
| 52 | + script_file = os.path.join(project_dir, script_file) |
| 53 | + if os.path.exists(script_file): |
| 54 | + return script_file |
| 55 | + |
| 56 | + return None |
| 57 | + |
| 58 | +def get_package_name(project_dir): |
| 59 | + import yaml |
| 60 | + setting_file = os.path.join(project_dir, "ProjectSettings/ProjectSettings.asset") |
| 61 | + with open(setting_file, mode='r', encoding='utf-8') as f: |
| 62 | + f.readline() |
| 63 | + f.readline() |
| 64 | + f.readline() |
| 65 | + setting = yaml.load(f, Loader=yaml.CLoader) |
| 66 | + name = setting.get("PlayerSettings").get("applicationIdentifier").get("Android") |
| 67 | + return name.strip() |
| 68 | + return None |
| 69 | + |
| 70 | +def get_real_package_name(project_dir): |
| 71 | + if len(sys.argv) == 2: |
| 72 | + return sys.argv[1] |
| 73 | + |
| 74 | + return get_package_name(project_dir) |
| 75 | + |
| 76 | + |
| 77 | +project_dir = get_project_dir() |
| 78 | +assert project_dir is not None, 'Cannot found unity project folder' |
| 79 | +script_file = get_script_file(project_dir) |
| 80 | +assert script_file is not None, 'Cannot found lua script file' |
| 81 | +package_name = get_real_package_name(project_dir) |
| 82 | +assert package_name is not None, 'Cannot found package name' |
| 83 | + |
| 84 | +print(script_file) |
| 85 | + |
| 86 | +cmd = f'adb push "{script_file}" /sdcard/Android/data/{package_name}/files/_luarunner.lua' |
| 87 | +call(cmd) |
| 88 | +cmd = "adb shell input keyevent KEYCODE_F8" |
| 89 | +call(cmd) |
| 90 | +print("run success") |
0 commit comments