#!/bin/sh
COLOR_RED="31"
COLOR_GREEN="32"
COLOR_YELLOW="33"
COLOR_BLUE="34"
COLOR_CYAN="36"
COLOR_WHITE="37"

PKG_NAME="com.tencent.igce"
LAUNCH_ACTIVITY="com.epicgames.ue4.SplashActivity"
APP_DATA_DIR="/data/data/${PKG_NAME}"
TARGET_SO="${APP_DATA_DIR}/libXG.so"
INJECT_BIN="${APP_DATA_DIR}/inject"
PERM_STD="777"
START_DELAY=1.5
INJECT_DELAY=10
GAME_DL_URL="https://web.gpubgm.com/m/AOS_Beta4.6.0-b.3_No6_4.6.3.21435_Shipping_Google_CE.shell.signed.apk"

print_log() {
    local c="$1"
    local tag="$2"
    local msg="$3"
    printf "\033[${c}m[${tag}] %s\033[0m\n" "$msg"
}

print_banner() {
    local title="$1"
    local bar="========================================================"
    printf "\033[36m%s\n        %s\n%s\033[0m\n\n" "$bar" "$title" "$bar"
}

print_line() {
    printf "\033[36m--------------------------------------------------------\033[0m\n"
}

show_header() {
clear
cat << "EOF"
 ██████╗ ██╗███╗   ██╗ █████╗ ████████╗███████╗██████╗ 
 ██╔════╝ ██║████╗  ██║██╔══██╗╚══██╔══╝██╔════╝██╔══██╗
 ██║  ███╗██║██╔██╗ ██║███████║   ██║   █████╗  ██████╔╝
 ██║   ██║██║██║╚██╗██║██╔══██║   ██║   ██╔══╝  ██╔══██╗
 ╚██████╔╝██║██║ ╚████║██║  ██║   ██║   ███████╗██║  ██║
  ╚═════╝ ╚═╝╚═╝  ╚═══╝╚═╝  ╚═╝   ╚═╝   ╚══════╝╚═╝  ╚═╝
        Premium Android SO Injection Utility v2.2
                Target SO: libXG.so
EOF
echo ""
print_log $COLOR_CYAN "INFO" "套件加载完成，开始前置校验流程"
print_line
}

check_root() {
    if [ "$(id -u)" -ne 0 ]; then
        print_log $COLOR_RED "FATAL" "权限不足！必须使用ROOT权限执行本工具"
        exit 1
    fi
}

check_game_exist(){
if ! pm list packages | grep -q "${PKG_NAME}";then
    print_log $COLOR_RED "FATAL" "未检测游戏，唤起浏览器下载"
    am start -a android.intent.action.VIEW -d "${GAME_DL_URL}" >/dev/null 2>&1
    exit 1
fi
}

launch_game() {
    print_banner "应用进程管理模块"
    game_pid=$(pgrep -f "^${PKG_NAME}$")
    if [ -z "$game_pid" ]; then
        print_log $COLOR_GREEN "PROC" "目标应用未运行，正在拉起游戏程序"
        am start -n "${PKG_NAME}/${LAUNCH_ACTIVITY}" >/dev/null 2>&1
        sleep $START_DELAY
        game_pid=$(pgrep -f "^${PKG_NAME}$")
        if [ -z "$game_pid" ]; then
            print_log $COLOR_RED "FATAL" "游戏启动失败，请检查应用是否正常安装"
            exit 1
        fi
        print_log $COLOR_GREEN "PROC" "游戏启动成功，PID: $game_pid"
    else
        print_log $COLOR_GREEN "PROC" "检测到游戏正在运行，PID: $game_pid"
    fi
    print_line
}

deploy_assets() {
    print_banner "注入文件部署模块"
    rm -rf "$TARGET_SO" "$INJECT_BIN"
    print_log $COLOR_GREEN "FILE" "旧版注入文件清理完毕"
    print_log $COLOR_BLUE "FILE" "正在推送注入程序与libXG.so库至应用私有目录"
    cp -f libXG.so "$TARGET_SO"
    chmod $PERM_STD "$TARGET_SO"
    cp -f inject "$INJECT_BIN"
    chmod $PERM_STD "$INJECT_BIN"

    so_ok=0
    bin_ok=0
    [ -f "$TARGET_SO" ] && so_ok=1
    [ -f "$INJECT_BIN" ] && bin_ok=1
    perm_so=$(stat -c '%a' "$TARGET_SO")
    perm_bin=$(stat -c '%a' "$INJECT_BIN")

    if [ $so_ok -eq 1 ] && [ $bin_ok -eq 1 ] && [ "$perm_so" = "$PERM_STD" ] && [ "$perm_bin" = "$PERM_STD" ]; then
        print_log $COLOR_GREEN "FILE" "libXG.so部署完整性校验通过，权限配置正常"
    else
        print_log $COLOR_RED "FATAL" "文件部署校验失败，请确认目录存在libXG.so文件"
        exit 1
    fi
    print_line
}

inject_so_delay() {
    print_banner "SO延迟注入核心模块"
    print_log $COLOR_YELLOW "WAIT" "注入程序就绪，将延迟 ${INJECT_DELAY} 秒后注入 libXG.so"
    print_log $COLOR_YELLOW "TIPS" "请在倒计时期间切换至游戏主界面，避免注入失效"
    print_line

    sec=$INJECT_DELAY
    while [ $sec -gt 0 ]; do
        printf "\033[33m[COUNT] 剩余等待时间：%2d 秒\033[0m\r" "$sec"
        sleep 1
        # 使用awk做减法，避开mksh $(( )) / expr 的bug
        sec=$(echo "$sec" | awk '{print $1 - 1}')
    done
    echo ""
    print_line
    print_log $COLOR_GREEN "INJECT" "倒计时结束，启动 libXG.so 注入流程"
    print_line
    "${INJECT_BIN}" -n "$PKG_NAME" -so "$TARGET_SO" | while read -r log_line; do
        print_log $COLOR_WHITE "OUT" "$log_line"
    done
    print_line
    print_log $COLOR_GREEN "INJECT" "libXG.so 注入流程执行完成"
    print_line
}

clean_residual() {
    print_banner "后置残留清理模块"
    rm -rf "$TARGET_SO" "$INJECT_BIN"
    print_log $COLOR_GREEN "CLEAN" "libXG.so与注入器临时文件已全部清除，无残留"
    print_line
}

main() {
    show_header
    check_root
    check_game_exist
    launch_game
    deploy_assets
    inject_so_delay
    clean_residual
    print_banner "全部任务执行完毕"
    print_log $COLOR_CYAN "FINISH" "libXG.so注入套件完整流程结束，可正常游玩"
}

main
