From 2c8af07a67ed682948b384fd2f01f88c57750b0d Mon Sep 17 00:00:00 2001 From: Radiquum Date: Wed, 3 Sep 2025 16:40:15 +0500 Subject: [PATCH] feat: add patch init arg --- config.py | 5 ++-- main.py | 6 ++++- scripts/utils.py | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/config.py b/config.py index 0adb060..8b1d7cb 100644 --- a/config.py +++ b/config.py @@ -47,8 +47,9 @@ parser = argparse.ArgumentParser(prog="anixart patcher") parser.add_argument("--config", help="path to config.json file", default="config.json") parser.add_argument("--no-decompile", action="store_true") parser.add_argument("--no-compile", action="store_true") -parser.add_argument("--patch", action="store_true") -parser.add_argument("--sign", action="store_true") +parser.add_argument("--patch", action="store_true", help="only patch decompiled") +parser.add_argument("--sign", action="store_true", help="only sign compiled apk") +parser.add_argument("--init", action="store_true", help="init a new patch") args = parser.parse_args() diff --git a/main.py b/main.py index 03c1588..75e0efd 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ from scripts.download_tools import check_and_download_all_tools from scripts.select_apk import get_apks, select_apk from scripts.select_patches import apply_patches, get_patches, select_patches -from scripts.utils import check_java_version, compile_apk, decompile_apk, sign_apk +from scripts.utils import check_java_version, compile_apk, decompile_apk, sign_apk, init_patch from config import args, config, log, console from time import time @@ -29,6 +29,10 @@ def patch(): if __name__ == "__main__": check_and_download_all_tools() check_java_version() + + if args.init: + init_patch() + exit(0) if not args.patch and not args.sign: apks = get_apks() diff --git a/scripts/utils.py b/scripts/utils.py index d72319e..310e5d4 100644 --- a/scripts/utils.py +++ b/scripts/utils.py @@ -2,6 +2,7 @@ import os import shutil import subprocess from config import log, config +from beaupy import prompt def check_java_version(): @@ -80,10 +81,16 @@ def compile_apk(apk: str): def sign_apk(apk: str): log.info(f"sign and align apk: `{apk}`") - if os.path.exists(f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned.apk"): + if os.path.exists( + f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned.apk" + ): os.remove(f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned.apk") - if os.path.exists(f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk"): - os.remove(f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk") + if os.path.exists( + f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk" + ): + os.remove( + f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk" + ) command = "" try: @@ -111,5 +118,55 @@ def sign_apk(apk: str): stderr=subprocess.PIPE, ) except subprocess.CalledProcessError as e: - log.fatal(f"error of running a command: %s :: %s", command, e.stderr, exc_info=True) + log.fatal( + f"error of running a command: %s :: %s", command, e.stderr, exc_info=True + ) exit(1) + +def patch_config_name(patch_name: str) -> str: + components = patch_name.split("_") + return "PatchConfig_" + "".join(x.title() for x in components) + +def init_patch(): + if not os.path.exists(config["folders"]["patches"]): + log.info(f"creating `patches` folder: {config['folders']['patches']}") + os.mkdir(config["folders"]["patches"]) + + if not os.path.exists(f"{config['folders']['patches']}/__init__.py"): + with open(f"{config['folders']['patches']}/__init__.py", "w") as f: + f.write("") + + name = prompt("Patch name: ", lambda x: x.strip().lower().replace(" ", "_")) + description = prompt("Patch description: ", lambda x: x.strip()) + priority = prompt("Patch priority: ", target_type=int, initial_value="0") + + patch_content = f"""\"\"\"{description}\"\"\" + +# patch settings +# priority, default: {priority} +priority = {priority} + +# imports +## bundled +from typing import TypedDict + +## custom +from config import config, log + + +# Patch +class {patch_config_name(name)}(TypedDict): + pass + + +def apply(patch_conf: {patch_config_name(name)}) -> bool: + log.info("patch `{name}` applied, nothing changed") + return True +""" + + with open(f"{config['folders']['patches']}/{name}.py", "w", encoding="utf-8") as f: + f.write(patch_content) + with open(f"{config['folders']['patches']}/{name}.config.json", "w", encoding="utf-8") as f: + f.write("{}") + + log.info(f"patch `{name}` created") \ No newline at end of file