Files
LihonT/scripts/quick_runtime.py
cheb0chik eb4ae1cf25 LihonT 2 Luna pre-release 2
Streak widget from 1x1 to 4x4 with an animated flame (orange while lit, grey when not), a streak keeper that writes
the chosen message once a day to the chosen chats (off until switched on), avatar shapes, widgets and the keeper kept
per account, Material 3 tonal surfaces instead of black (AMOLED keeps pure black), launcher previews of our own, and
the mod runtime in a dex of its own.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 15:09:30 +03:00

103 lines
4.4 KiB
Python

"""Quick rebuild after a change in the runtime Java only (src/lihont/two).
Takes an APK that py -m lihont.upgrade built earlier, swaps the mod's runtime for a fresh compile of
src/lihont/two (the dex of its own that holds only the runtime, or, in an APK built before that dex existed, the
runtime classes inside classes35.dex), and signs the result. The smali patches of TikTok's
own classes are not touched, so this is only right while lihont/*_patch.py and lihont/upgrade.py are
unchanged since that APK was built; after any such change run the full build.
usage (from the repository root):
py -B scripts/quick_runtime.py ComplectAPK/lihont-2.0-luna-beta5.apk -o ComplectAPK/lihont-2.0-luna-beta6.apk
"""
import argparse
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
os.chdir(ROOT)
from lihont import dexpatch, manifest as manifest_module # noqa: E402
from lihont.apkzip import Apk # noqa: E402
from lihont.axml import Axml # noqa: E402
from lihont.toolchain import Toolchain # noqa: E402
RUNTIME = "Llihont/two/"
DEX = "classes35.dex"
def runtime_dex_of(apk: Apk):
"""The dex that defines the mod's runtime and nothing else, or None when it rides inside a TikTok dex."""
for name in sorted(apk.names()):
if not name.endswith(".dex"):
continue
data = apk.read(name)
if b"Llihont/two/LihonTRegion;" not in data:
continue
defined = dexpatch.defined_methods(data)
if defined and all(m.startswith(RUNTIME) for m in defined):
return name
return None
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("built", help="an APK built by lihont.upgrade")
parser.add_argument("-o", "--out", required=True)
parser.add_argument("--work", default="work/quick")
args = parser.parse_args()
source, output, room = Path(args.built).resolve(), Path(args.out).resolve(), Path(args.work).resolve()
if source == output:
raise SystemExit("Write to a new file: the built APK stays as it is")
room.mkdir(parents=True, exist_ok=True)
tools = Toolchain("tools")
apk = Apk(str(source))
try:
api = manifest_module.min_sdk(Axml.parse(apk.read("AndroidManifest.xml")))
runtime = Path(tools.compile_dex("src/lihont/two", str(room / "runtime"), api)).read_bytes()
target = runtime_dex_of(apk)
if target is not None:
# a dex that holds nothing but the runtime is simply replaced
replacement = runtime
else:
# an APK built before the runtime had a dex of its own: its classes ride inside classes35.dex
target = DEX
compiler = room / "compiler"
compiler.mkdir(parents=True, exist_ok=True)
subprocess.run(["javac", "--release", "17", "-cp", tools.smali, "-d", str(compiler),
"src/RefreshClasses.java"], check=True)
host_dex = apk.read(DEX)
host, fresh, merged = room / "host.dex", room / "fresh.dex", room / "merged.dex"
host.write_bytes(host_dex)
fresh.write_bytes(runtime)
subprocess.run(["java", "-Xmx1g", "-cp", os.pathsep.join([str(compiler), tools.smali]),
"RefreshClasses", str(host), str(fresh), str(merged), str(api)], check=True)
replacement = merged.read_bytes()
if dexpatch.dex_format(host_dex) != dexpatch.dex_format(replacement):
raise SystemExit("The refresh changed the DEX format")
before = {m for m in dexpatch.defined_methods(host_dex) if not m.startswith(RUNTIME)}
after = dexpatch.defined_methods(replacement)
if not before.issubset(after):
raise SystemExit("The refresh lost classes that are not part of the runtime")
if not any(m.startswith("Llihont/two/LihonTRegion;->") for m in after):
raise SystemExit("LihonTRegion is missing after the refresh")
apk.replace(target, replacement)
apk.drop_signature()
output.parent.mkdir(parents=True, exist_ok=True)
apk.write(str(output))
finally:
apk.close()
tools.sign(str(output))
print("%s %d bytes (runtime in %s)" % (output.name, output.stat().st_size, target))
if __name__ == "__main__":
main()