# -*- 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 needle in [b"sdkutils", b"SoUtils", b"CharacterCrypto", b"vvuwuuvuu", b"vuwuuwvw", b"wwvwvwuvv", b"dfp is empty"]:
        print(needle.decode(), data.count(needle))

    print("\n--- classes referencing sdkutils ---")
    for m in re.finditer(rb"Lcom/[^;]{0,120};", data):
        s = m.group()
        if b"sdkutils" in s.lower() or b"SoUtils" in s or b"Crypto" in s:
            print(s.decode()[1:-1].replace("/", "."))

    # dump vvuuuuvvv.wwvuwuwvu (getRiskToken core)
    target = "Lcom/shopee/shpssdkbank/uwuvuvvww/vvuuuuvvv;"
    for dex_name in zf.namelist():
        if not dex_name.endswith(".dex"):
            continue
        dex = zf.read(dex_name)
        if target.encode() not in dex:
            continue
        tmp = Path(__file__).resolve().parent.parent / "tmp" / "tmp_vv.dex"
        tmp.write_bytes(dex)
        out = subprocess.check_output([str(DEXDUMP), "-d", str(tmp)], text=True, errors="replace")
        print("\n===", dex_name, "vvuuuuvvv methods (native only) ===")
        cap = False
        for line in out.splitlines():
            if f"Class descriptor  : '{target}'" in line:
                cap = True
            elif cap and line.startswith("  Class descriptor"):
                break
            if cap and ("NATIVE" in line or "name          :" in line):
                print(line.encode("ascii", "replace").decode())
