More stuff with dex

This commit is contained in:
maelstrom 2024-08-18 00:39:32 +02:00
parent 7229e518d3
commit 89581ccb51
6 changed files with 61 additions and 6 deletions

1
build.bat Normal file
View file

@ -0,0 +1 @@
@python buildtool/build.py %*

View file

@ -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])

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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"