autool/all_pc_exec.bat
2026-06-17 19:44:18 +08:00

310 lines
8.8 KiB
Batchfile

@echo off
setlocal EnableDelayedExpansion
:: --------------------------------------------------
:: 1. 读取配置文件 (config.ini)
:: --------------------------------------------------
echo Reading configuration from config.ini...
echo.
set "ConfigFile=%~dp0config.ini"
:: 检查配置文件是否存在
if not exist "%ConfigFile%" (
echo [ERROR] Configuration file not found: %ConfigFile%
echo Please create the config.ini file in the same directory.
pause
exit /b 1
)
:: 从INI文件中读取设置 (注意: ini文件里请使用 Key=Value 格式,不要有空格)
for /f "usebackq tokens=1,* delims==" %%a in (`findstr /v /c:";" /c:"#" /c:"[" "%ConfigFile%"`) do (
for /f "tokens=*" %%v in ("%%b") do set "%%a=%%v"
)
echo
:: --------------------------------------------------
:: 2. 关闭旧的Python任务
:: --------------------------------------------------
taskkill /f /t /im python.exe
taskkill /f /t /im pythonw.exe
:: --------------------------------------------------
:: 重启电脑
:: --------------------------------------------------
if /i "%RestartComputer%"=="true" (
echo [EXEC] RESTARTING COMPUTER IN 10 SECONDS...
echo Press Ctrl+C to cancel.
shutdown /r /f /t 10 /c "Restart initiated by automated script."
:: 如果执行到这里,脚本会因为电脑重启而终止
exit /b 0
) else (
echo [SKIP] Skipping: Restart computer.
)
echo.
:: --------------------------------------------------
:: 5. Git Clone
:: --------------------------------------------------
if /i "%RunGitClone%"=="true" (
:: 检查目标文件夹是否已存在
if exist "%TargetDir%\.git" (
echo [SKIP] Target directory already exists: %TargetDir%
echo [SKIP] Skipping git clone, proceeding to next step.
) else (
echo [EXEC] Changing to %TargetDrive% drive...
%TargetDrive%
echo [EXEC] Cloning repository from %GitRepoUrl%...
git clone %GitRepoUrl%
if errorlevel 1 (
echo [ERROR] Git clone failed!
pause
exit /b 1
)
echo [EXEC] Pulling LFS files...
echo [EXEC] Changing to %TargetDir% directory...
cd /d %TargetDir%
git lfs fetch --all
)
) else (
echo [SKIP] Skipping: Git clone.
)
echo.
:: --------------------------------------------------
:: 6. Git Pull
:: --------------------------------------------------
if /i "%RunGitPull%"=="true" (
echo [EXEC] Changing to %TargetDrive% drive...
%TargetDrive%
echo [EXEC] Changing to %TargetDir% directory...
cd /d %TargetDir%
if errorlevel 1 (
echo [ERROR] Failed to enter %TargetDir% directory!
pause
exit /b 1
)
echo [EXEC] Running git pull...
git pull
if errorlevel 1 (
echo [ERROR] Git pull failed!
pause
exit /b 1
)
echo [EXEC] Pulling LFS files...
git lfs fetch --all
) else (
echo [SKIP] Skipping: Git pull.
)
echo.
:: --------------------------------------------------
:: 7. Setup
:: --------------------------------------------------
if /i "%RunSetup%"=="true" (
echo [EXEC] Changing to %TargetDrive% drive...
%TargetDrive%
echo [EXEC] Changing to %TargetDir% directory...
cd /d %TargetDir%
if errorlevel 1 (
echo [ERROR] Failed to enter %TargetDir% directory!
pause
exit /b 1
)
echo [EXEC] Configuring pip to use Aliyun mirror...
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set install.trusted-host mirrors.aliyun.com
echo [EXEC] Running %SetupScript%...
python %SetupScript%
if errorlevel 1 (
echo [ERROR] %SetupScript% execution failed!
pause
exit /b 1
)
) else (
echo [SKIP] Skipping: Setup.
)
echo.
:: --------------------------------------------------
:: 8. 恢复MuMu模拟器镜像
:: --------------------------------------------------
if /i "%RunRecoverMuMu%"=="true" (
echo [EXEC] Recovering MuMu emulator...
:: 准备本地导入目录
if not exist "%LocalImportDir%" (
mkdir "%LocalImportDir%"
)
:: 拷贝备份文件到本地
set "ImportFile=%LocalImportDir%\%BackupFileName%"
set "BackupFullPath=%CleanBackupPath%\%BackupFileName%"
echo Copying backup file to local directory, please wait...
echo Source: !BackupFullPath!
echo Target: !ImportFile!
copy /y "!BackupFullPath!" "!ImportFile!" >nul
echo Copy completed.
if errorlevel 1 (
echo [ERROR] Failed to copy backup file!
pause
exit /b 1
)
:: 关闭模拟器
echo Shutting down MuMu emulator...
"%MuMuManagerPath%" api shutdown_player -v %MuMuIndex%
timeout /t 15 /nobreak
:: 删除现有虚拟机
echo Deleting existing VM...
"%MuMuManagerPath%" delete -v %MuMuIndex%
timeout /t 15 /nobreak
:: 删除其他虚拟机索引
for /l %%i in (2,1,%MaxVmIndex%) do (
"%MuMuManagerPath%" delete -v %%i
)
timeout /t 15 /nobreak
:: 导入备份
echo Importing backup...
echo Import file path: !ImportFile!
"%MuMuManagerPath%" import -v %MuMuIndex% -p "!ImportFile!"
if errorlevel 1 (
echo [ERROR] Failed to import backup!
pause
exit /b 1
)
timeout /t 15 /nobreak
:: 设置网桥 - 自动获取第一个有网的网卡
echo Detecting network adapter...
set "NetAdapterName="
for /f "tokens=* skip=1" %%a in ('wmic nic where "NetConnectionStatus=2" get Name /value 2^>nul') do (
if "!NetAdapterName!"=="" (
for /f "tokens=2 delims==" %%b in ("%%a") do set "NetAdapterName=%%b"
)
)
:: 去除可能存在的空格
if not "!NetAdapterName!"=="" (
for /f "tokens=*" %%a in ("!NetAdapterName!") do set "NetAdapterName=%%a"
)
if "!NetAdapterName!"=="" (
echo [WARNING] No active network adapter found, using default: %NetBridgeCard%
set "NetAdapterName=%NetBridgeCard%"
)
echo Setting network bridge to: !NetAdapterName!
"%MuMuManagerPath%" setting -v %MuMuIndex% -k "net_bridge_card" -val "!NetAdapterName!"
timeout /t 15 /nobreak
echo MuMu emulator recovery completed.
) else (
echo [SKIP] Skipping: Recover MuMu emulator.
)
echo.
:: --------------------------------------------------
:: 9. 重启MuMu模拟器
:: --------------------------------------------------
if /i "%RunRestartMuMu%"=="true" (
echo Starting ADB server...
adb devices
timeout /t 3 /nobreak
echo Restarting MuMu emulator...
"%MuMuManagerPath%" api shutdown_player -v %MuMuIndex%
timeout /t 10 /nobreak
"%MuMuManagerPath%" api launch_player -v %MuMuIndex%
timeout /t 10 /nobreak
echo MuMu emulator restarted.
) else (
echo [SKIP] Skipping: Restart MuMu emulator.
)
echo.
:: --------------------------------------------------
:: 10. Wait for ADB Device and Connect
:: --------------------------------------------------
if /i "%RunBatch%"=="true" (
echo [EXEC] Starting ADB server...
adb start-server
timeout /t 2 /nobreak >nul
echo [EXEC] Connecting to MuMu emulator ADB...
adb connect 127.0.0.1:5555
timeout /t 2 /nobreak >nul
echo [EXEC] Waiting for ADB device to be ready...
set "DeviceFound=false"
set "RetryCount=0"
set "MaxRetries=30"
:WaitForDevice
adb devices | findstr /C:"device" | findstr /V /C:"List" >nul 2>&1
if errorlevel 1 (
set /a RetryCount+=1
if !RetryCount! gtr !MaxRetries! (
echo [ERROR] ADB device not found after !MaxRetries! retries!
echo [INFO] Current ADB devices:
adb devices
pause
exit /b 1
)
echo [INFO] Waiting for device... Attempt !RetryCount!/!MaxRetries!
timeout /t 2 /nobreak >nul
goto WaitForDevice
) else (
echo [SUCCESS] ADB device is ready!
echo [INFO] Connected devices:
adb devices
)
echo.
:: --------------------------------------------------
:: 11. Activate Virtual Environment and Run Batch
:: --------------------------------------------------
echo [EXEC] Changing to %TargetDrive% drive...
%TargetDrive%
echo [EXEC] Changing to %TargetDir% directory...
cd /d %TargetDir%
if errorlevel 1 (
echo [ERROR] Failed to enter %TargetDir% directory!
pause
exit /b 1
)
echo [EXEC] Activating virtual environment from %VenvPath%...
call %VenvPath%
if errorlevel 1 (
echo [ERROR] Failed to activate virtual environment!
pause
exit /b 1
)
echo [EXEC] Running %BatchScript%...
python %BatchScript%
if errorlevel 1 (
echo [ERROR] %BatchScript% execution failed!
pause
exit /b 1
)
) else (
echo [SKIP] Skipping: Run batch script.
)
echo.
echo All tasks completed successfully!
pause
exit 0