Files
LihonT/scripts/quick_runtime.py
cheb0chik 1d5a9a1fad LihonT 2 Luna
Ghost mode for stories and chats, several photos in one comment, .ltp plugins (rights, defender, rollback, downloads
only with consent), custom chat bubbles with a text colour of your choice, the lihont-ltp SDK and the plugin docs,
streak flames as widgets with a keeper, avatar shapes, Material 3 surfaces.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 18:19:27 +03:00

107 lines
4.8 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, portmap # 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")
parser.add_argument("--java", default="src/lihont/two",
help="the runtime sources; give the tree of the commit an older APK was built from")
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")))
# with LIHONT_PORT_MAP set the runtime is compiled from a copy that names TikTok's classes and ids of that version
sources = portmap.stage_java(args.java, str(room / "java-src"))
runtime = Path(tools.compile_dex(sources, 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()