#!/system/bin/sh

# 自动获取脚本所在的绝对路径
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SO_DIR="$SCRIPT_DIR/arm64-v8a"

# 固定注入无畏契约
PKG_NAME="com.tencent.tmgp.codev"
GAME_NAME="无畏契约"

# 检查 Root 权限
if [ "$(id -u)" != "0" ]; then
    printf "\n请使用root权限执行 !\n"
    exit 1
fi

# 自动取 arm64-v8a 目录下的 so (直接用, 不移动)
SO_FILE=""
for f in "$SO_DIR"/*.so; do
    [ -f "$f" ] && SO_FILE="$f" && break
done

if [ -z "$SO_FILE" ]; then
    echo "[-] 未找到 arm64-v8a/*.so 动态库文件"
    exit 1
fi

if [ ! -f "$SO_DIR/inject" ]; then
    echo "[-] 未找到 arm64-v8a/inject 劫持文件"
    exit 1
fi

chmod 755 "$SO_FILE" 2>/dev/null
chmod 777 "$SO_DIR/inject" 2>/dev/null

echo "[+] 动态库文件正常: $(basename "$SO_FILE")"
echo "[+] 劫持文件正常"
printf "\n"
printf "\033[41m准备完毕! 正在启动 %s (%s)...\033[0m\n" "$GAME_NAME" "$PKG_NAME"
printf "\n"

monkey -p "$PKG_NAME" -c android.intent.category.LAUNCHER 1 > /dev/null 2>&1

printf "\n"
printf "\033[44m开始对 %s 执行注入...\033[0m\n" "$GAME_NAME"

"$SO_DIR/inject" -n "$PKG_NAME" -so "$SO_FILE"

echo "[+] 注入命令发送完毕，请留意游戏画面与文件夹产出！"