# -*- 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:
    for dex_name in zf.namelist():
        if not dex_name.endswith(".dex"):
            continue
        data = zf.read(dex_name)
        if b"loadSoLibrary" not in data:
            continue
        tmp = Path(__file__).resolve().parent.parent / "tmp" / "tmp_loadso.dex"
        tmp.write_bytes(data)
        out = subprocess.check_output(
            [str(DEXDUMP), "-d", str(tmp)], universal_newlines=True, errors="replace"
        )
        current_class = ""
        for line in out.splitlines():
            m = re.search(r"Class descriptor\s+:\s+'([^']+)'", line)
            if m:
                current_class = m.group(1)
            if "loadSoLibrary" not in line:
                continue
            cls = current_class.replace("L", "").replace(";", "").replace("/", ".")
            print(f"{dex_name}\t{cls}\t{line.strip()}")
