1
0
Fork 0
mirror of https://github.com/Radiquum/anixart-patcher.git synced 2025-09-04 02:05:33 +05:00

feat: add patch init arg

This commit is contained in:
Kentai Radiquum 2025-09-03 16:40:15 +05:00
parent 2fef0a32b7
commit 2c8af07a67
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
3 changed files with 69 additions and 7 deletions

View file

@ -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("--config", help="path to config.json file", default="config.json")
parser.add_argument("--no-decompile", action="store_true") parser.add_argument("--no-decompile", action="store_true")
parser.add_argument("--no-compile", action="store_true") parser.add_argument("--no-compile", action="store_true")
parser.add_argument("--patch", action="store_true") parser.add_argument("--patch", action="store_true", help="only patch decompiled")
parser.add_argument("--sign", action="store_true") 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() args = parser.parse_args()

View file

@ -1,7 +1,7 @@
from scripts.download_tools import check_and_download_all_tools from scripts.download_tools import check_and_download_all_tools
from scripts.select_apk import get_apks, select_apk from scripts.select_apk import get_apks, select_apk
from scripts.select_patches import apply_patches, get_patches, select_patches 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 config import args, config, log, console
from time import time from time import time
@ -29,6 +29,10 @@ def patch():
if __name__ == "__main__": if __name__ == "__main__":
check_and_download_all_tools() check_and_download_all_tools()
check_java_version() check_java_version()
if args.init:
init_patch()
exit(0)
if not args.patch and not args.sign: if not args.patch and not args.sign:
apks = get_apks() apks = get_apks()

View file

@ -2,6 +2,7 @@ import os
import shutil import shutil
import subprocess import subprocess
from config import log, config from config import log, config
from beaupy import prompt
def check_java_version(): def check_java_version():
@ -80,10 +81,16 @@ def compile_apk(apk: str):
def sign_apk(apk: str): def sign_apk(apk: str):
log.info(f"sign and align apk: `{apk}`") 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") 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"): if os.path.exists(
os.remove(f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk") f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk"
):
os.remove(
f"{config['folders']['dist']}/{apk.removesuffix('.apk')}-aligned-signed.apk"
)
command = "" command = ""
try: try:
@ -111,5 +118,55 @@ def sign_apk(apk: str):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) )
except subprocess.CalledProcessError as e: 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) 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")