@echo off
chcp 936 >nul
title 学习机自动回滚检查
set LOG=C:\StudyShell\自动回滚日志.txt

REM ★ 先看 Shell 还是不是 StudyShell。已经不是了就直接退出。
REM   否则每登录一次就等 15 分钟再重启一次，变成无限重启。
REM   2026-11-26 第二台真机实测：一条 Shell 判断缺失，让一次黑屏放大成 40 轮重启。
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Shell 2>nul | findstr /i "StudyShell.exe" >nul
if errorlevel 1 (
    echo [%date% %time%] 当前 Shell 已不是 StudyShell，无需回滚，直接退出 >>"%LOG%"
    exit /b 0
)

echo [%date% %time%] 保险启动：等 15 分钟看学习桌面在不在 >>"%LOG%"

REM 睡眠 15 分钟（用 ping 拖时间，计划任务里没有控制台也能用）
ping -n 901 127.0.0.1 >nul 2>&1

tasklist /fi "IMAGENAME eq StudyShell.exe" 2>nul | findstr /i "StudyShell.exe" >nul
if not errorlevel 1 goto :ALIVE

echo [%date% %time%] 学习桌面没在跑 → 自动还原成普通电脑 >>"%LOG%"
set W=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
reg add "%W%" /v Shell /t REG_SZ /d "explorer.exe" /f >nul 2>&1
reg add "%W%" /v Userinit /t REG_SZ /d "C:\Windows\system32\userinit.exe," /f >nul 2>&1
reg add "%W%" /v AutoAdminLogon /t REG_SZ /d 0 /f >nul 2>&1
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" /f >nul 2>&1
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f >nul 2>&1
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer" /f >nul 2>&1
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /f >nul 2>&1
reg delete "HKLM\SOFTWARE\Policies\Microsoft\Windows\Safer" /f >nul 2>&1
powercfg /change monitor-timeout-ac 10 >nul 2>&1
powercfg /change standby-timeout-ac 30 >nul 2>&1
echo [%date% %time%] 自动还原完成，即将重启 >>"%LOG%"
shutdown /r /t 5 /c "学习桌面没起来，已自动恢复成普通电脑"
exit /b 0

:ALIVE
echo [%date% %time%] 学习桌面正常，无需回滚 >>"%LOG%"
exit /b 0
