#!/system/bin/sh

CONFIG_FILE="/data/injector_config.txt"
INJECT_TOOL="/data/Inject"
LOG_FILE="/data/injector_log.txt"
HIDE_CONFIG="/data/hide_config.txt"

# 颜色定义（使用echo -e需要Android支持）
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 添加日志功能
log_message() {
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    local message="$1"
    echo "[$timestamp] $message" >> "$LOG_FILE"
    # 对于不支持颜色的终端，只输出文字
    if echo -e test >/dev/null 2>&1; then
        echo -e "${CYAN}[$timestamp]${NC} $message"
    else
        echo "[$timestamp] $message"
    fi
}

# 显示横幅
show_banner() {
    clear 2>/dev/null || echo -e "\n\n\n"
    echo "╔══════════════════════════════════════════╗"
    echo "║     蔡徐坤注入器           ║"
    echo "║         Anti-Detection Injector           ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    echo "系统信息"
    echo "设备型号: $(getprop ro.product.model 2>/dev/null || echo '未知')"
    echo "Android版本: $(getprop ro.build.version.release 2>/dev/null || echo '未知')"
    echo "内核版本: $(uname -r 2>/dev/null || echo '未知')"
    echo "架构: $(uname -m 2>/dev/null || echo '未知')"
    echo "当前目录: $(pwd)"
    echo "日志文件: $LOG_FILE"
    echo ""
}

# 检查并设置Inject工具
find_and_setup_inject() {
    if [ -x "$INJECT_TOOL" ]; then
        log_message "找到Inject工具: $INJECT_TOOL"
        return 0
    fi
    
    # 使用空格分隔的路径列表，而不是数组
    local inject_paths="./Inject $(pwd)/Inject /sdcard/Inject /data/local/tmp/Inject /data/Inject /sdcard/Download/Inject /sdcard/DCIM/Inject /storage/emulated/0/Inject"
    local found_path=""
    
    for path in $inject_paths; do
        if [ -f "$path" ]; then
            found_path="$path"
            log_message "找到Inject文件: $found_path"
            break
        fi
    done
    
    if [ -z "$found_path" ]; then
        log_message "错误：未找到Inject文件"
        echo "请将Inject文件放置在以下位置之一："
        echo "当前目录"
        echo "/sdcard/"
        echo "/data/local/tmp/"
        echo "/data/"
        echo "/sdcard/Download/"
        echo "/sdcard/DCIM/"
        return 1
    fi
    
    # 复制到/data目录
    cp "$found_path" "$INJECT_TOOL" 2>/dev/null
    chmod 755 "$INJECT_TOOL" 2>/dev/null
    
    if [ -x "$INJECT_TOOL" ]; then
        log_message "Inject工具准备就绪"
        return 0
    else
        log_message "Inject工具权限设置失败"
        return 1
    fi
}

# 加载配置
load_config() {
    if [ -f "$CONFIG_FILE" ]; then
        SO_FILE=$(head -n 1 "$CONFIG_FILE" 2>/dev/null)
        GAME_PACKAGE=$(head -n 2 "$CONFIG_FILE" | tail -n 1 2>/dev/null)
        return 0
    fi
    return 1
}

# 保存配置
save_config() {
    echo "$SO_FILE" > "$CONFIG_FILE"
    echo "$GAME_PACKAGE" >> "$CONFIG_FILE"
    chmod 666 "$CONFIG_FILE" 2>/dev/null
}

# 加载隐藏配置
load_hide_config() {
    if [ -f "$HIDE_CONFIG" ]; then
        HIDE_OPTIONS=$(cat "$HIDE_CONFIG")
        return 0
    fi
    # 默认使用安全模式
    HIDE_OPTIONS="-hide_maps -hide_solist"
    return 1
}

# 保存隐藏配置
save_hide_config() {
    echo "$HIDE_OPTIONS" > "$HIDE_CONFIG"
    chmod 666 "$HIDE_CONFIG" 2>/dev/null
}

# 显示隐藏配置菜单
show_hide_menu() {
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         请选择隐藏模式                  ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    echo "1.  基础隐藏 (maps+solist隐藏) [推荐]"
    echo "2.  中级隐藏 (增加procfs隐藏)"
    echo "3.  高级隐藏 (全功能隐藏)"
    echo "4.  反检测模式 (针对游戏检测)"
    echo "5.  隐身模式 (最大程度隐藏)"
    echo "6.  自定义隐藏配置"
    echo "0.  不使用隐藏 [不推荐]"
    echo ""
    echo "注意：隐藏越强，稳定性可能越低"
    echo ""
}

# 根据选择生成隐藏参数
generate_hide_params() {
    local choice=$1
    case $choice in
        1)
            echo "-hide_maps -hide_solist"
            ;;
        2)
            echo "-hide_maps -hide_solist -hide_procfs -random_path"
            ;;
        3)
            echo "-hide_maps -hide_solist -hide_procfs -random_path -disguise_syslib -clean_maps"
            ;;
        4)
            echo "-antidetect -hide_all -clean_all -disable_ptrace -disguise_procname"
            ;;
        5)
            echo "-stealth_mode -hide_all -clean_all -no_trace -random_all -disguise_all"
            ;;
        6)
            echo "-custom"
            ;;
        *)
            echo ""
            ;;
    esac
}

# 自定义隐藏配置
apply_custom_hide() {
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         自定义隐藏配置                  ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    echo "可用的隐藏参数："
    echo "  -hide_maps          从maps隐藏SO"
    echo "  -hide_solist        从solist隐藏SO"
    echo "  -hide_procfs        从/proc隐藏SO"
    echo "  -random_path        随机化SO路径"
    echo "  -disguise_syslib    伪装为系统库"
    echo "  -clean_maps         清理maps痕迹"
    echo "  -clean_status       清理status调试痕迹"
    echo "  -disable_ptrace     禁用ptrace检测"
    echo "  -disguise_procname  伪装进程名"
    echo "  -antidetect         反检测保护"
    echo "  -stealth_mode       隐身模式"
    echo ""
    echo "示例: -hide_maps -hide_solist -antidetect"
    echo ""
    
    echo -n "请输入自定义隐藏参数: "
    read custom_hide
    
    if [ -z "$custom_hide" ]; then
        custom_hide="-hide_maps -hide_solist -antidetect"
    fi
    
    echo "$custom_hide"
}

# 检查系统状态
check_system_status() {
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         系统状态检查                    ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    
    # 检查root权限
    if [ "$(id -u)" = "0" ]; then
        echo "✓ ROOT权限: 已获得"
        ROOT_AVAILABLE=1
    else
        echo "⚠ ROOT权限: 未获得（部分功能受限）"
        ROOT_AVAILABLE=0
    fi
    
    # 检查SELinux状态
    if command -v getenforce >/dev/null 2>&1; then
        local selinux_status=$(getenforce 2>/dev/null)
        if [ "$selinux_status" = "Enforcing" ]; then
            echo "⚠ SELinux: Enforcing模式（可能限制注入）"
        else
            echo "✓ SELinux: $selinux_status模式"
        fi
    fi
    
    # 检查ptrace权限
    if [ -f "/proc/sys/kernel/yama/ptrace_scope" ]; then
        local ptrace_scope=$(cat /proc/sys/kernel/yama/ptrace_scope)
        if [ "$ptrace_scope" = "0" ]; then
            echo "✓ ptrace: 已启用"
        else
            echo "⚠ ptrace: 受限制 (scope=$ptrace_scope)"
        fi
    fi
    
    # 检查内核版本
    local kernel_version=$(uname -r)
    echo "✓ 内核版本: $kernel_version"
    
    # 检查系统内存
    if command -v free >/dev/null 2>&1; then
        local total_mem=$(free -m 2>/dev/null | awk '/^Mem:/{print $2}')
        if [ -n "$total_mem" ]; then
            echo "✓ 系统内存: ${total_mem}MB"
        fi
    fi
    
    echo ""
}

# 检查进程是否运行
check_process_running() {
    local package_name=$1
    pgrep -f "^.*$package_name" 2>/dev/null
    return $?
}

# 获取游戏Activity
get_game_activity() {
    local package_name=$1
    local activity=""
    
    # 尝试多种方式获取Activity
    if command -v dumpsys >/dev/null 2>&1; then
        activity=$(dumpsys package "$package_name" 2>/dev/null | grep -E "\.UnityPlayerActivity|\.MainActivity" | head -1 | awk '{print $2}')
    fi
    
    if [ -z "$activity" ]; then
        # 尝试从launcher中获取
        if command -v cmd >/dev/null 2>&1; then
            activity=$(cmd package resolve-activity --brief "$package_name" 2>/dev/null | head -1)
        fi
    fi
    
    if [ -z "$activity" ]; then
        # 默认Activity
        activity="$package_name/.UnityPlayerActivity"
    fi
    
    echo "$activity"
}

# 等待进程稳定
wait_for_process_stable() {
    local package_name=$1
    local max_wait=$2
    local wait_time=0
    
    while [ $wait_time -lt $max_wait ]; do
        if check_process_running "$package_name" >/dev/null; then
            log_message "进程已稳定运行"
            return 0
        fi
        sleep 0.1
        wait_time=$((wait_time + 1))
    done
    return 1
}

# 增强的进程检查
enhanced_process_check() {
    local package_name=$1
    
    # 方法1: 通过包名查找
    local pid=$(pgrep -f "^.*$package_name" 2>/dev/null)
    
    # 方法2: 通过ps查找
    if [ -z "$pid" ]; then
        if command -v ps >/dev/null 2>&1; then
            pid=$(ps -ef 2>/dev/null | grep "$package_name" | grep -v grep | head -1 | awk '{print $2}')
        fi
    fi
    
    # 方法3: 通过dumpsys查找
    if [ -z "$pid" ] && [ -x "/system/bin/dumpsys" ]; then
        pid=$(dumpsys activity processes 2>/dev/null | grep "$package_name" | grep "pid=" | sed 's/.*pid=\([0-9]*\).*/\1/')
    fi
    
    echo "$pid"
}

# 清理MT管理器后台
clean_mt_manager() {
    log_message "开始清理MT管理器后台..."
    
    # MT管理器的常见包名
    local mt_packages="bin.mt.plus bin.mt.plus.canary bin.mt.plus.easy"
    local mt_found=0
    
    for package in $mt_packages; do
        # 检查MT管理器是否在运行
        local mt_pid=$(pgrep -f "$package" 2>/dev/null)
        if [ -n "$mt_pid" ]; then
            mt_found=1
            log_message "检测到MT管理器 ($package) 正在运行，PID: $mt_pid"
            
            # 强制停止MT管理器
            am force-stop "$package" 2>/dev/null
            if [ $? -eq 0 ]; then
                log_message "✓ MT管理器 ($package) 已强制停止"
            else
                # 如果am命令失败，尝试kill
                kill -9 $mt_pid 2>/dev/null
                log_message "✓ MT管理器 ($package) 进程已终止"
            fi
        fi
    done
    
    # 再次检查是否还有MT进程
    sleep 1
    for package in $mt_packages; do
        if pgrep -f "$package" >/dev/null 2>&1; then
            pkill -9 -f "$package" 2>/dev/null
            log_message "✓ MT管理器残留进程已清理"
        fi
    done
    
    if [ $mt_found -eq 1 ]; then
        log_message "MT管理器后台清理完成"
        echo "✓ MT管理器后台已清理"
    else
        log_message "未检测到MT管理器运行"
        echo "ℹ MT管理器未在运行"
    fi
}

# 智能注入
inject_smart() {
    log_message "使用智能注入模式..."
    
    local hide_params=""
    if [ -n "$HIDE_OPTIONS" ] && [ "$HIDE_OPTIONS" != "-custom" ]; then
        hide_params="$HIDE_OPTIONS"
    fi
    
    # 等待用户手动启动游戏
    log_message "等待用户手动启动游戏..."
    echo ""
    echo "请手动启动游戏..."
    echo "正在检测游戏进程..."
    
    local timeout=60
    local start_time=$(date +%s)
    
    while true; do
        if check_process_running "$GAME_PACKAGE"; then
            log_message "检测到游戏已启动"
            break
        fi
        
        local current_time=$(date +%s)
        local elapsed=$((current_time - start_time))
        
        if [ $elapsed -ge $timeout ]; then
            log_message "等待超时，游戏未启动"
            return 1
        fi
        
        # 显示等待提示
        if [ $((elapsed % 5)) -eq 0 ]; then
            echo "已等待 ${elapsed} 秒，请启动游戏..."
        fi
        sleep 1
    done
    
    # 等待游戏稳定（8秒）
    log_message "等待8秒确保游戏稳定..."
    echo ""
    echo "正在等待8秒确保游戏稳定..."
    for i in 1 2 3 4 5 6 7 8; do
        echo -n "$i "
        sleep 1
    done
    echo ""
    log_message "8秒等待完成"
    
    # 尝试不同注入策略
    log_message "尝试策略1: dl_memfd + 隐藏"
    "$INJECT_TOOL" -pkg "$GAME_PACKAGE" -lib "$SO_FILE" -dl_memfd $hide_params
    if [ $? -eq 0 ]; then
        log_message "注入成功 - 策略1"
        sleep 1
        # 验证进程是否存活
        if check_process_running "$GAME_PACKAGE"; then
            log_message "游戏运行正常"
            
            # 注入成功后自动清理MT管理器后台
            echo ""
            echo "╔══════════════════════════════════════════╗"
            echo "║         正在清理MT管理器后台            ║"
            echo "╚══════════════════════════════════════════╝"
            clean_mt_manager
            sleep 1
            
            return 0
        fi
    fi
    
    log_message "策略1失败，尝试策略2: 基础注入 + 隐藏"
    sleep 1
    "$INJECT_TOOL" -pkg "$GAME_PACKAGE" -lib "$SO_FILE" $hide_params
    if [ $? -eq 0 ]; then
        log_message "注入成功 - 策略2"
        sleep 1
        if check_process_running "$GAME_PACKAGE"; then
            log_message "游戏运行正常"
            
            # 注入成功后自动清理MT管理器后台
            echo ""
            echo "╔══════════════════════════════════════════╗"
            echo "║         正在清理MT管理器后台            ║"
            echo "╚══════════════════════════════════════════╝"
            clean_mt_manager
            sleep 1
            
            return 0
        fi
    fi
    
    log_message "所有智能注入策略都失败"
    return 1
}

# 验证注入
verify_injection() {
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         验证注入结果                    ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    
    if ! check_process_running "$GAME_PACKAGE"; then
        log_message "游戏已退出"
        return 1
    fi
    
    log_message "游戏正在运行"
    
    # 给游戏一些时间恢复
    sleep 2
    
    local so_name=$(basename "$SO_FILE")
    local found=0
    
    # 获取进程ID
    local pid=$(enhanced_process_check "$GAME_PACKAGE")
    
    if [ -n "$pid" ]; then
        # 检查maps
        log_message "检查进程 $pid 的内存映射..."
        if cat /proc/$pid/maps 2>/dev/null | grep -i "$so_name" >/dev/null; then
            log_message "✓ SO文件在maps中可见"
            found=1
        else
            log_message "⚠ SO文件未在maps中找到（可能已隐藏）"
        fi
    fi
    
    if [ $found -eq 1 ]; then
        log_message "注入验证成功"
        return 0
    else
        log_message "注入可能成功但被隐藏"
        return 2
    fi
}

# 检查游戏稳定性
check_game_stability() {
    echo ""
    echo "监控游戏稳定性(15秒)..."
    
    local max_wait=15
    local i=1
    
    while [ $i -le $max_wait ]; do
        if ! check_process_running "$GAME_PACKAGE"; then
            log_message "游戏在第 $i 秒闪退"
            return 1
        fi
        
        # 显示进度
        if [ $((i % 5)) -eq 0 ]; then
            echo -n "$i "
        else
            echo -n "."
        fi
        
        sleep 1
        i=$((i + 1))
    done
    
    echo ""
    log_message "✓ 游戏稳定运行了 $max_wait 秒"
    return 0
}

# 隐藏注入验证
verify_hide_injection() {
    local pid=$1
    local so_name=$2
    
    if [ -z "$pid" ]; then
        return 1
    fi
    
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         隐藏注入验证                    ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    
    log_message "验证进程 $pid 的隐藏状态..."
    
    # 检查maps（如果启用了hide_maps，这里应该找不到）
    if echo "$HIDE_OPTIONS" | grep -q "hide_maps"; then
        local maps_result=$(cat /proc/$pid/maps 2>/dev/null | grep -i "$so_name" | wc -l)
        if [ "$maps_result" -eq 0 ]; then
            log_message "✓ SO在maps中已隐藏"
        else
            log_message "⚠ SO在maps中仍然可见"
        fi
    fi
    
    # 检查进程状态
    if [ -f "/proc/$pid/status" ]; then
        local tracer_pid=$(grep "TracerPid" /proc/$pid/status | awk '{print $2}')
        if [ "$tracer_pid" = "0" ]; then
            log_message "✓ 进程未被调试 (TracerPid: 0)"
        else
            log_message "⚠ 进程可能被调试 (TracerPid: $tracer_pid)"
        fi
    fi
    
    # 检查进程名伪装
    if echo "$HIDE_OPTIONS" | grep -q "disguise_procname"; then
        local cmdline=$(cat /proc/$pid/cmdline 2>/dev/null)
        if [ -n "$cmdline" ] && echo "$cmdline" | grep -qE "(system|service|daemon)"; then
            log_message "✓ 进程名已伪装"
        fi
    fi
    
    return 0
}

# 主函数
main() {
    # 初始化日志
    echo "童话故事注入器 - 增强隐藏版" > "$LOG_FILE"
    echo "开始时间: $(date '+%Y-%m-%d %H:%M:%S')" >> "$LOG_FILE"
    echo "======================================" >> "$LOG_FILE"
    chmod 666 "$LOG_FILE" 2>/dev/null
    
    show_banner
    
    # 检查基本命令
    if ! command -v am >/dev/null 2>&1; then
        log_message "错误：未找到am命令"
        exit 1
    fi
    
    if ! command -v pm >/dev/null 2>&1; then
        log_message "错误：未找到pm命令"
        exit 1
    fi
    
    # 检查并设置Inject工具
    if ! find_and_setup_inject; then
        log_message "Inject工具初始化失败"
        exit 1
    fi
    
    # 检查系统状态
    check_system_status
    
    echo ""
    
    # 加载配置
    if load_config; then
        echo "上次配置:"
        echo "SO文件: $SO_FILE"
        echo "游戏包名: $GAME_PACKAGE"
        echo ""
        
        echo -n "是否使用上次配置？ (y/n): "
        read use_last
        
        if [ "$use_last" != "y" ] && [ "$use_last" != "Y" ]; then
            SO_FILE=""
            GAME_PACKAGE=""
        fi
    fi
    
    # 输入SO文件
    if [ -z "$SO_FILE" ]; then
        echo "当前目录文件:"
        ls *.so 2>/dev/null || echo "未找到.so文件"
        echo ""
        
        echo -n "请输入.so文件路径: "
        read SO_FILE
        
        if [ -z "$SO_FILE" ]; then
            log_message "错误：SO文件路径不能为空"
            exit 1
        fi
        
        # 尝试多个路径
        local found=0
        # 使用空格分隔的路径列表
        local possible_paths="$SO_FILE ./$SO_FILE $(pwd)/$SO_FILE /sdcard/$SO_FILE /storage/emulated/0/$SO_FILE /data/local/tmp/$SO_FILE /data/$SO_FILE /sdcard/Download/$SO_FILE"
        
        for path in $possible_paths; do
            if [ -f "$path" ]; then
                SO_FILE="$path"
                found=1
                break
            fi
        done
        
        if [ $found -eq 0 ]; then
            log_message "错误：SO文件不存在"
            echo "请在以下位置放置SO文件："
            echo "当前目录"
            echo "/sdcard/"
            echo "/data/local/tmp/"
            echo "/data/"
            echo "/sdcard/Download/"
            exit 1
        fi
    fi
    
    # 输入游戏包名
    if [ -z "$GAME_PACKAGE" ]; then
        echo ""
        echo -n "请输入游戏包名: "
        read GAME_PACKAGE
        
        if [ -z "$GAME_PACKAGE" ]; then
            log_message "错误：游戏包名不能为空"
            exit 1
        fi
    fi
    
    # 保存配置
    save_config
    
    # 设置权限
    chmod 777 "$SO_FILE" 2>/dev/null
    
    # 加载隐藏配置
    load_hide_config
    
    # 选择隐藏模式
    echo ""
    show_hide_menu
    echo -n "请选择隐藏模式 (0-6): "
    read hide_choice
    
    case $hide_choice in
        6)
            HIDE_OPTIONS=$(apply_custom_hide)
            ;;
        0)
            HIDE_OPTIONS=""
            ;;
        [1-5])
            HIDE_OPTIONS=$(generate_hide_params "$hide_choice")
            ;;
        *)
            HIDE_OPTIONS="-hide_maps -hide_solist"
            ;;
    esac
    
    save_hide_config
    
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         注入配置汇总                    ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    echo "Inject工具: $INJECT_TOOL"
    echo "SO文件: $SO_FILE"
    echo "游戏包名: $GAME_PACKAGE"
    echo "隐藏参数: $HIDE_OPTIONS"
    echo "日志文件: $LOG_FILE"
    echo ""
    
    echo "注意：请确保在注入前手动启动游戏"
    echo "脚本会自动等待游戏启动后8秒进行注入"
    echo ""
    
    # 检查游戏是否安装
    if ! pm path "$GAME_PACKAGE" >/dev/null 2>&1; then
        log_message "警告：未找到游戏 $GAME_PACKAGE"
        echo -n "是否继续？(y/n): "
        read answer
        if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
            exit 1
        fi
    fi
    
    # 执行智能注入
    inject_smart
    local inject_result=$?
    
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         注入完成                        ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    echo "使用方式: 智能注入"
    echo "隐藏参数: $HIDE_OPTIONS"
    
    if [ $inject_result -eq 0 ]; then
        log_message "✓ 注入命令执行成功"
    else
        log_message "✗ 注入命令执行失败"
    fi
    
    # 获取进程ID进行隐藏验证
    local pid=$(enhanced_process_check "$GAME_PACKAGE")
    local so_name=$(basename "$SO_FILE")
    
    if [ -n "$pid" ]; then
        # 验证隐藏效果
        verify_hide_injection "$pid" "$so_name"
    fi
    
    # 验证注入
    verify_injection
    local verify_result=$?
    
    # 稳定性测试
    if [ $verify_result -eq 0 ] || [ $verify_result -eq 2 ]; then
        check_game_stability
        if [ $? -eq 0 ]; then
            echo ""
            echo "╔══════════════════════════════════════════╗"
            echo "║         注入成功且游戏稳定！            ║"
            echo "╚══════════════════════════════════════════╝"
            log_message "✓ 注入成功且游戏稳定"
            
            # 显示隐藏状态总结
            echo ""
            echo "隐藏状态总结:"
            if [ -n "$HIDE_OPTIONS" ] && [ "$HIDE_OPTIONS" != "-custom" ]; then
                echo "✓ 隐藏功能: 已启用"
                echo "✓ 隐藏参数: $HIDE_OPTIONS"
                echo "✓ 建议: 游戏检测风险较低"
            elif [ "$HIDE_OPTIONS" = "-custom" ]; then
                echo "✓ 隐藏功能: 自定义模式"
                echo "✓ 建议: 根据自定义配置评估风险"
            else
                echo "⚠ 隐藏功能: 未启用"
                echo "⚠ 建议: 建议启用隐藏功能以降低检测风险"
            fi
        else
            echo ""
            echo "⚠ 注入成功但游戏不稳定"
            log_message "⚠ 注入成功但游戏不稳定"
        fi
    else
        echo ""
        echo "✗ 注入验证失败"
        log_message "✗ 注入验证失败"
    fi
    
    # 显示日志路径
    echo ""
    echo "详细日志请查看: $LOG_FILE"
    
    # 提供重试选项
    echo ""
    echo -n "是否重新注入？(y/n): "
    read retry
    
    if [ "$retry" = "y" ] || [ "$retry" = "Y" ]; then
        echo ""
        echo "重新启动注入流程..."
        echo ""
        sleep 1
        main
        return
    fi
    
    echo ""
    echo "╔══════════════════════════════════════════╗"
    echo "║         感谢使用童话故事注入器          ║"
    echo "║         Anti-Detection Injector          ║"
    echo "║           作者QQ: 3523762866             ║"
    echo "╚══════════════════════════════════════════╝"
    echo ""
    
    # 保持终端打开
    echo -n "按回车键退出..."
    read dummy
}

# 启动主函数
main