@echo off
chcp 65001 >nul
setlocal
cd /d "%~dp0"
title MatrixBox - 一键无损修复更新工具
if not exist "_internal" (
  echo [提示] 请把本脚本放到 MatrixBox 软件目录（与 MatrixBox.exe 同级）双击运行！
  pause
  exit /b 1
)
taskkill /F /IM MatrixBox.exe >nul 2>&1
timeout /t 1 /nobreak >nul
echo 正在从高速节点下载 MatrixBox 内核补丁并无损覆盖 _internal（100%%保留 data 账号与聊天数据）...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop';$root=(Get-Location).Path;$z=Join-Path $env:TEMP 'mb_fix.zip';$s=Join-Path $env:TEMP 'mb_fix_s';if(Test-Path $z){Remove-Item $z -Force};if(Test-Path $s){Remove-Item $s -Recurse -Force};if(Get-Command curl.exe -ErrorAction SilentlyContinue){& curl.exe -L --fail --retry 2 --connect-timeout 12 -o $z 'https://dc.jinno.app/matrixbox/ota?local=1';if($LASTEXITCODE -ne 0){& curl.exe -L --fail --retry 2 --connect-timeout 12 -o $z 'https://dc.jinno.app/matrixbox/ota?local=1'}};if((-not (Test-Path $z)) -or ((Get-Item $z).Length -lt 102400)){try{Invoke-WebRequest -Uri 'https://dc.jinno.app/matrixbox/ota?local=1' -OutFile $z -UseBasicParsing -TimeoutSec 35}catch{Invoke-WebRequest -Uri 'https://dc.jinno.app/matrixbox/ota?local=1' -OutFile $z -UseBasicParsing -TimeoutSec 35}};Expand-Archive -LiteralPath $z -DestinationPath $s -Force;Get-ChildItem -LiteralPath $s -Recurse -File|ForEach-Object{$r=$_.FullName.Substring($s.Length).TrimStart('\','/');if($r -notlike 'data\*' -and $r -ne 'boss_unlimited.key'){$d=Join-Path $root $r;$dd=Split-Path -Parent $d;if(-not(Test-Path $dd)){New-Item -ItemType Directory -Path $dd -Force|Out-Null};Copy-Item -LiteralPath $_.FullName -Destination $d -Force}};Remove-Item $s -Recurse -Force -ErrorAction SilentlyContinue;Remove-Item $z -Force -ErrorAction SilentlyContinue"
if errorlevel 1 ( echo 修复失败，请检查网络连接！ & pause & exit /b 1 )
echo 修复成功！正在启动 MatrixBox...
if exist "MatrixBox.exe" start "" "MatrixBox.exe"
timeout /t 2 /nobreak >nul
