From 89581ccb51502f109c1d0ba138a36c6f92aef9da Mon Sep 17 00:00:00 2001 From: maelstrom Date: Sun, 18 Aug 2024 00:39:32 +0200 Subject: [PATCH] More stuff with dex --- build.bat | 1 + buildtool/task/compile_smali.py | 35 +++++++++++++++++++++++++++++++ buildtool/task/fileutil.py | 17 ++++++++++++++- buildtool/task/merge_resources.py | 10 ++++----- buildtool/task/patch_smali.py | 3 +++ buildtool/task/util.py | 1 + 6 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 build.bat create mode 100644 buildtool/task/compile_smali.py diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..d57244a --- /dev/null +++ b/build.bat @@ -0,0 +1 @@ +@python buildtool/build.py %* \ No newline at end of file diff --git a/buildtool/task/compile_smali.py b/buildtool/task/compile_smali.py new file mode 100644 index 0000000..b253447 --- /dev/null +++ b/buildtool/task/compile_smali.py @@ -0,0 +1,35 @@ +import subprocess +from .util import * +from . import fileutil + +import re + +from polly.patcher import process_all_patches + +# Task dependencies +from .patch_smali import patch_smali + +def to_dex_name(smali_name): + if smali_name == 'smali': + return 'classes.dex' + else: + num = re.match(r"smali_classes([0-9]+)", smali_name).group(1) + return f"classes{num}.dex" + +def compile_smali(): + _ = patch_smali() + + COMPILED_SMALI_DIR.mkdir(parents=True, exist_ok=True) + + print("Compiling smali...") + for smali_dir in PATCHED_SMALI_DIR.glob("*"): + dest_dex = COMPILED_SMALI_DIR / to_dex_name(smali_dir.name) + + # Don't compile if not necessary + # This depends on merge_into touching directories with copying + if dest_dex.exists() and smali_dir.stat().st_mtime < dest_dex.stat().st_mtime: + print(F"Skipping {dest_dex.name}") + continue + + print(f"Compiling {smali_dir.name} => {dest_dex.name}...") + _ = subprocess.run(["java", "-jar", "tools/smali-2.5.2.jar", "a", smali_dir, "-o", dest_dex]) \ No newline at end of file diff --git a/buildtool/task/fileutil.py b/buildtool/task/fileutil.py index 9e6b851..3654372 100644 --- a/buildtool/task/fileutil.py +++ b/buildtool/task/fileutil.py @@ -1,7 +1,18 @@ from pathlib import Path import shutil -def merge_into(src: Path | str, dest: Path | str, check_date: bool = True): +def merge_into(src: Path | str, dest: Path | str, check_date: bool = True, touch_directories: bool = True): + """Merges the contents of a directory into another + + Args: + src (Path | str): The source directory to merge from + dest (Path | str): The destination directory to merge into + check_date (bool, optional): If true, only copies files which are newer in src than in dest. Defaults to True. + touch_directories (bool, optional): Updates the modified time of parent directories. Defaults to True. + + Returns: + _type_: _description_ + """ src = Path(src) dest = Path(dest) @@ -20,4 +31,8 @@ def merge_into(src: Path | str, dest: Path | str, check_date: bool = True): _ = shutil.copyfile(src_file, dest_file) changed = True + if touch_directories: + for prnt in dest_file.relative_to(dest).parents: + (dest / prnt).touch(exist_ok=True) + return changed \ No newline at end of file diff --git a/buildtool/task/merge_resources.py b/buildtool/task/merge_resources.py index a049d6c..4703a7c 100644 --- a/buildtool/task/merge_resources.py +++ b/buildtool/task/merge_resources.py @@ -5,23 +5,23 @@ import shutil # Task dependencies from .patch_resources import patch_resources -from .patch_smali import patch_smali +from .compile_smali import compile_smali # Merges resources from all the previous steps def merge_resources(): _ = patch_resources() - _ = patch_smali() + _ = compile_smali() first_time = False # Copy original resources only the first time if not MERGED_RSC_DIR.exists(): print("Merging original resources...") - shutil.copytree(EXTRACTED_DIR, MERGED_RSC_DIR, ignore=shutil.ignore_patterns("smali*")) + shutil.copytree(EXTRACTED_DIR, MERGED_RSC_DIR, ignore=shutil.ignore_patterns("smali", "smali_classes*")) first_time = True print("Merging patched resources...") _ = fileutil.merge_into(PATCHED_RSC_DIR, MERGED_RSC_DIR, check_date=not first_time) - print("Merging patched smali...") - _ = fileutil.merge_into(PATCHED_SMALI_DIR, MERGED_RSC_DIR, check_date=not first_time) + print("Merging dex files...") + _ = fileutil.merge_into(COMPILED_SMALI_DIR, MERGED_RSC_DIR, check_date=not first_time) diff --git a/buildtool/task/patch_smali.py b/buildtool/task/patch_smali.py index 1bdea96..101ad92 100644 --- a/buildtool/task/patch_smali.py +++ b/buildtool/task/patch_smali.py @@ -17,6 +17,9 @@ def patch_smali(): if not PATCHED_SMALI_DIR.exists(): print("Copying original smali...") for smali_dir in EXTRACTED_DIR.glob("smali*"): + if smali_dir.parts[-1] == 'smali_assets': + continue # Not you, Seamus. + shutil.copytree(smali_dir, PATCHED_SMALI_DIR / smali_dir.relative_to(EXTRACTED_DIR)) first_time = True diff --git a/buildtool/task/util.py b/buildtool/task/util.py index 870d8d7..d5e2143 100644 --- a/buildtool/task/util.py +++ b/buildtool/task/util.py @@ -7,6 +7,7 @@ EXTRACTED_DIR = BUILD_DIR / "extracted" PATCHED_RSC_DIR = BUILD_DIR / "patched_resources" PATCHED_SMALI_DIR = BUILD_DIR / "patched_smali" MERGED_RSC_DIR = BUILD_DIR / "merged_resources" +COMPILED_SMALI_DIR = BUILD_DIR / "compiled_smali" SRC_DIR = Path("src") SRC_RESOURCES_DIR = SRC_DIR / "resources"