@echo off
setlocal enabledelayedexpansion
pushd "%~dp0"
chcp 936 >nul 2>&1

:osver
for /f "tokens=4-6 delims=. " %%I in ('ver') do (
  set /a OSVER=%%I*10+%%J
  set OSBLD=%%K
  set OSBLD=!OSBLD:]=!
)
if /i "%PROCESSOR_ARCHITECTURE%"=="AMD64" if %OSVER% geq 61 goto installlocation
echo \033[91m不支持的系统。| cmdcolor
echo.
echo \033[93m本补丁针对64位版Windows 7/8/8.1/10/11或Server 2008R2/2012/2016/2019/2022。| cmdcolor
echo.
pause
exit /b 10

:installlocation
set SYS=fancyrd.sys
set INSTALLLOCATION=
set RegKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{94B97E1E-9B67-4012-A126-6319E211A298}_is1
for /f "tokens=2,*" %%I in ('reg query %RegKey% /v InstallLocation') do set INSTALLLOCATION=%%J
if defined INSTALLLOCATION if exist "%INSTALLLOCATION%drv\%SYS%" goto displayversion
echo \033[91m系统中没有正常安装Primo Ramdisk。| cmdcolor
echo.
pause
exit /b 3

:displayversion
set DISPLAYVERSION=
for /f "tokens=3" %%I in ('reg query %RegKey% /v DisplayVersion') do set DISPLAYVERSION=%%I
set EDITION=
for /f "tokens=3" %%I in ('reg query "HKLM\SOFTWARE\Romex Software\FancyRd" /v Edition') do set EDITION=%%I
if "%DISPLAYVERSION%"=="6.6.0" if "%EDITION%"=="0x6" goto integrity
echo \033[91m不支持的Primo Ramdisk版本。| cmdcolor
echo.
echo \033[93m本补丁针对服务器版6.6.0，系统中安装的不是对应版本。| cmdcolor
echo.
pause
exit /b 10

:integrity
if %OSVER% geq 100 (
  set DRV=drv10
) else (
  set DRV=drv7
)
if not exist %DRV%\%SYS% (
  echo \033[91m补丁可能已被反病毒软件误报并破坏。请暂时禁用反病毒软件，重新解压缩补丁包后再试。| cmdcolor
  echo.
  pause
  exit /b 2
)

:privilege
net session >nul 2>&1
if %errorlevel% equ 0 goto cert
fltmc >nul 2>&1
if %errorlevel% equ 0 goto cert
reg query HKU\S-1-5-19 >nul 2>&1
if %errorlevel% equ 0 goto cert
echo \033[93m需要管理员权限。| cmdcolor
echo \033[93m请右键点击“%~nx0”选“以管理员身份运行”。| cmdcolor
echo.
pause
exit /b 5

:cert
echo.
echo \033[93;44m--- 导入驱动包和时间戳自签名根证书为受信证书 ---| cmdcolor
certutil.exe -f -addstore Root TestCertificate.cer >nul
certutil.exe -f -addstore TrustedPublisher TestCertificate.cer >nul
if %errorlevel% equ 0 (
  echo \033[92m证书导入成功。| cmdcolor
) else (
  echo \033[93m证书导入失败。| cmdcolor
)

:newdrv
echo.
echo \033[93;44m--- 替换和安装新驱动 ---| cmdcolor
attrib -r -h -s "%INSTALLLOCATION%drv\*.*" >nul 2>&1
copy /y %DRV%\*.* "%INSTALLLOCATION%drv\" >nul
if %errorlevel% equ 0 (
  echo \033[92m驱动文件替换成功。| cmdcolor
) else (
  echo \033[91m驱动文件替换失败。| cmdcolor
  pause
  exit /b %errorlevel%
)
"%INSTALLLOCATION%frsetup.exe" -i1 "%INSTALLLOCATION%drv\\"
fc /b %DRV%\%SYS% "%SystemRoot%\System32\drivers\%SYS%" >nul 2>&1
if %errorlevel% equ 0 (
  echo \033[92m新驱动安装成功。| cmdcolor
) else (
  echo \033[91m新驱动安装失败。| cmdcolor
  pause
  exit /b %errorlevel%
)

:reg
echo.
echo \033[93;44m--- 导入注册信息 ---| cmdcolor
reg import PrDSrv56crk.reg >nul 2>&1
if %errorlevel% equ 0 (
  echo \033[92m导入成功。| cmdcolor
) else (
  echo \033[93m导入失败。你可以稍后尝试手工导入。| cmdcolor
)

:end
echo.
echo \033[92m安装完成，重启电脑后生效。| cmdcolor
echo.
echo \033[93m强烈建议你自行操作，把“%SystemRoot%\System32\drivers\%SYS%”添加到所用反病毒软件的信任或排除项。| cmdcolor
echo.
echo \033[93m日后如果遇到系统月度更新后不能正常启动，请在PE下替换“%SystemRoot%\System32\drivers\%SYS%”为本补丁drv.bak文件夹中的官方原版。| cmdcolor
echo.
pause
exit /b 3010
