# -*- coding: utf-8 -*-
import re
import subprocess
import zipfile
from pathlib import Path

APK = Path(__file__).resolve().parent.parent / "apks" / "seabank_ph_base.apk"
DEXDUMP = Path(r"C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\37.0.0\dexdump.exe")

with zipfile.ZipFile(str(APK)) as zf:
    data = b"".join(zf.read(n) for n in zf.namelist() if n.endswith(".dex"))
    for m in re.finditer(rb"Lcom/shopee/bke/[^;]{0,80}RegisterRequest[^;]{0,20};", data):
        print(m.group().decode()[1:-1].replace("/", "."))

    for name in zf.namelist():
        if not name.endswith(".dex"):
            continue
        dex = zf.read(name)
        if b"RegisterRequest" not in dex:
            continue
        tmp = Path(__file__).resolve().parent.parent / "tmp" / "tmp_reg.dex"
        tmp.write_bytes(dex)
        out = subprocess.check_output(
            [str(DEXDUMP), "-d", str(tmp)], universal_newlines=True, errors="replace"
        )
        for target in re.findall(r"Lcom/shopee/bke/[^;]*RegisterRequest[^;]*;", data.decode("latin1", errors="ignore")):
            cap = False
            print("\n" + "=" * 60, name, target)
            for line in out.splitlines():
                if f"Class descriptor  : '{target}'" in line:
                    cap = True
                elif cap and line.startswith("  Class descriptor") and target not in line:
                    break
                if cap:
                    print(line)
