update_xhshook_db.sh 2.47 KB
#!/bin/bash
# update_xhshook_db.sh — 重装 xhshook APK 后更新 LSPosed modules_config.db
# 必须在 adb install --no-incremental 之后运行
set -e

ADB="${ADB:-$(dirname "$0")/../platform-tools/adb}"
if [ ! -f "$ADB" ]; then ADB=adb; fi

TERMUX_PY=/data/data/com.termux/files/usr/bin/python3
NEW_PATH=$($ADB shell 'pm path com.hermes.xhshook' | tr -d '\r' | sed 's/package://')
echo "New APK path: $NEW_PATH"

# --- Phase 1: Python in-place UPDATE (preserves inode, works while lspd is running) ---
# commit() writes to WAL so lspd can see it immediately via its DB connection.
# Checkpoint may fail if lspd holds a write lock — that's OK, we skip it.
$ADB shell "su -c \"$TERMUX_PY -c \\\"
import sqlite3
db = sqlite3.connect('/data/adb/lspd/config/modules_config.db')
db.execute(\\\\\\\"UPDATE modules SET apk_path='${NEW_PATH}' WHERE module_pkg_name='com.hermes.xhshook'\\\\\\\")
db.commit()
try:
    db.execute('PRAGMA wal_checkpoint(TRUNCATE)')
except Exception as e:
    pass  # lspd holds write lock; skip checkpoint, WAL commit is enough
db.close()
print('phase1 done')
\\\"\"" 2>&1

# --- Phase 2: pull + overwrite main file (ensures correct path survives reboot) ---
# After phase 1, WAL has the correct path. Pull the DB+WAL, checkpoint locally,
# then push clean main file back and remove WAL/SHM on device.
$ADB shell 'su -c "cp /data/adb/lspd/config/modules_config.db /data/local/tmp/mc_upd.db && cp /data/adb/lspd/config/modules_config.db-wal /data/local/tmp/mc_upd.db-wal 2>/dev/null; chmod 644 /data/local/tmp/mc_upd.db /data/local/tmp/mc_upd.db-wal 2>/dev/null; true"'
$ADB pull /data/local/tmp/mc_upd.db /tmp/mc_upd.db 2>/dev/null
$ADB pull /data/local/tmp/mc_upd.db-wal /tmp/mc_upd.db-wal 2>/dev/null || true

# Checkpoint locally (Mac sqlite3 has no lock contention)
sqlite3 /tmp/mc_upd.db "PRAGMA wal_checkpoint(TRUNCATE);" 2>/dev/null || true
# Verify the path is there
ACTUAL=$(sqlite3 /tmp/mc_upd.db "SELECT apk_path FROM modules WHERE module_pkg_name='com.hermes.xhshook';")
echo "Verified path in DB: $ACTUAL"

# Push clean DB and remove WAL/SHM on device
$ADB push /tmp/mc_upd.db /data/local/tmp/mc_upd_clean.db
$ADB shell 'su -c "cp /data/local/tmp/mc_upd_clean.db /data/adb/lspd/config/modules_config.db && rm -f /data/adb/lspd/config/modules_config.db-wal /data/adb/lspd/config/modules_config.db-shm && echo phase2 done"'

echo "DB updated. Now: am force-stop com.xingin.xhs → open XHS manually."
echo "(If lspd cache doesn't auto-refresh, reboot is needed for changes to take effect.)"