@echo off
chcp 936 >nul 2>&1
title 图灵点击器 - 环境检测

echo ========================================
echo       图灵点击器 - 环境检测
echo ========================================
echo.

:: 获取 Windows 版本号
set win_ok=0
set win_version=未知

:: 获取详细版本名称（从注册表）
set win_fullname=
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName 2^>nul') do set win_fullname=%%b

:: 从 ver 命令提取
for /f "tokens=2 delims=[]" %%a in ('ver') do set ver_raw=%%a
for /f "tokens=2" %%b in ("%ver_raw%") do set ver_num=%%b
if "%ver_num%"=="" set ver_num=%ver_raw%

:: 如果失败，用 wmic
if "%ver_num%"=="" (
    for /f "tokens=2 delims==" %%a in ('wmic os get version /value 2^>nul ^| find "="') do set ver_num=%%a
)

echo 检测到内核版本: %ver_num%
echo.

:: 提取主版本、次版本、Build号
for /f "tokens=1,2,3 delims=." %%a in ("%ver_num%") do (
    set major_ver=%%a
    set minor_ver=%%b
    set build_num=%%c
)

:: 防止变量为空
if "%major_ver%"=="" set major_ver=0
if "%minor_ver%"=="" set minor_ver=0
if "%build_num%"=="" set build_num=0

:: 去除可能的前导空格
set build_num=%build_num: =%

:: ========== 操作系统检测 ==========
:: Windows 11: Build >= 22000
set win_ok=0
if %major_ver%==10 (
    if %build_num% GEQ 22000 (
        set win_ok=1
        set win_version=Windows 11
        goto :check_net
    )
)

:: Windows 10: Build 10240-21999
if %major_ver%==10 (
    if %build_num% GEQ 10240 (
        set win_ok=1
        set win_version=Windows 10
        goto :check_net
    )
)

:: Windows 8.1
if %major_ver%==6 (
    if %minor_ver%==3 (
        set win_ok=1
        set win_version=Windows 8.1
        goto :check_net
    )
)

:: Windows 8
if %major_ver%==6 (
    if %minor_ver%==2 (
        set win_ok=1
        set win_version=Windows 8
        goto :check_net
    )
)

:: Windows 7 SP1 检测
if %major_ver%==6 (
    if %minor_ver%==1 (
        reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CSDVersion 2>nul | find "Service Pack 1" >nul
        if %errorlevel% equ 0 (
            set win_ok=1
            set win_version=Windows 7 SP1
        ) else (
            set win_ok=0
            set win_version=Windows 7 (无 SP1)
        )
        goto :check_net
    )
)

:: Windows Vista
if %major_ver%==6 (
    if %minor_ver%==0 (
        set win_ok=0
        set win_version=Windows Vista (不支持)
        goto :check_net
    )
)

:: Windows XP
if %major_ver%==5 (
    set win_ok=0
    set win_version=Windows XP (不支持)
    goto :check_net
)

:: 其他情况
if %major_ver% GTR 10 (
    set win_ok=1
    set win_version=Windows 更高版本
)

:check_net
:: ========== .NET Framework 检测 ==========
set release_hex=
for /f "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Release 2^>nul ^| find "Release"') do set release_hex=%%a

if "%release_hex%"=="" (
    set net_version=未安装
    set net_ok=0
    goto :check_vc
)

set release_hex=%release_hex:0x=%
set /a release_dec=0x%release_hex% 2>nul

set net_version=未知
set net_ok=0

if %release_dec% GEQ 528040 set net_version=4.8 & set net_ok=1
if %release_dec% GEQ 461808 if %release_dec% LSS 528040 set net_version=4.7.2 & set net_ok=1
if %release_dec% GEQ 461308 if %release_dec% LSS 461808 set net_version=4.7.1 & set net_ok=0
if %release_dec% GEQ 460798 if %release_dec% LSS 461308 set net_version=4.7 & set net_ok=0
if %release_dec% GEQ 394802 if %release_dec% LSS 460798 set net_version=4.6.2 & set net_ok=0
if %release_dec% LSS 394802 set net_version=低于4.6.2 & set net_ok=0

:check_vc
:: ========== VC++ 运行时检测 ==========
set vc_found=0
if exist "%SystemRoot%\SysWOW64\vcruntime140.dll" set vc_found=1
if exist "%SystemRoot%\System32\vcruntime140.dll" set vc_found=1

:: ========== 系统架构检测 ==========
set is64=0
if defined ProgramFiles(x86) set is64=1

:: 设置架构文字
set arch_text=32位
if %is64%==1 set arch_text=64位

:: ========== 输出结果 ==========
echo.
echo ========================================
echo           检测结果
echo ========================================
echo.

:: 操作系统（合并系统架构）
if not "%win_fullname%"=="" (
    echo   [√] 操作系统: %win_fullname% ^(%arch_text%^)
) else (
    if %win_ok%==1 (
        echo   [√] 操作系统: %win_version% ^(%arch_text%^)
    ) else (
        echo   [X] 操作系统: %win_version% ^(%arch_text%^)
    )
)

:: .NET Framework
if %net_ok%==1 (
    echo   [√] .NET Framework: %net_version%
) else (
    echo   [X] .NET Framework: %net_version%
    if "%net_version%"=="未安装" echo       下载: https://dotnet.microsoft.com/download/dotnet-framework/net472
)

:: VC++ 运行时
if %vc_found%==1 (
    echo   [√] VC++ 运行时: 已安装
) else (
    echo   [X] VC++ 运行时: 未安装
    echo       下载: https://aka.ms/vs/17/release/vc_redist.x86.exe
)

:: ========== 最终结论 ==========
echo.
echo ========================================

if %win_ok%==1 (
    if %net_ok%==1 (
        if %vc_found%==1 (
            echo   环境正常，可以运行程序
        ) else (
            echo   ? VC++ 运行时缺失，部分功能可能受限
            echo   建议安装后运行
        )
    ) else (
        echo   ? .NET Framework 版本过低，请升级到 4.7.2+
    )
) else (
    echo   ? 操作系统不符合要求
    echo   需要 Windows 7 SP1 或更高版本
)

echo ========================================
echo.

pause