@echo off chcp 65001 >nul setlocal set "PROJECT_DIR=%~dp0" set "RUN_DIR=%PROJECT_DIR%.run" set "PID_FILE=%RUN_DIR%\spine-connector-windows.pid" set "OUT_LOG=%RUN_DIR%\spine-connector-windows.log" set "ERROR_LOG=%RUN_DIR%\spine-connector-windows-error.log" set "HEALTH_URL=http://127.0.0.1:27843/v1/health" set "CONNECTOR_ENTRY=%PROJECT_DIR%connector\spine-connector.mjs" set "SPINE_EXECUTABLE=C:\Spine\Spine.com" if not exist "%RUN_DIR%" mkdir "%RUN_DIR%" if not exist "%CONNECTOR_ENTRY%" ( echo 未找到连接器入口:%CONNECTOR_ENTRY% timeout /t 1 /nobreak >nul exit /b 1 ) cd /d "%PROJECT_DIR%" set "CONNECTOR_PID=" if exist "%PID_FILE%" set /p CONNECTOR_PID=<"%PID_FILE%" if defined CONNECTOR_PID ( powershell -NoProfile -Command ^ "$pidValue=0; [void][int]::TryParse($env:CONNECTOR_PID,[ref]$pidValue);" ^ "$process=if($pidValue -gt 0){Get-CimInstance Win32_Process -Filter ('ProcessId = ' + $pidValue) -ErrorAction SilentlyContinue};" ^ "$expected=$env:CONNECTOR_ENTRY;" ^ "if($process -and $process.CommandLine -and $process.CommandLine.IndexOf($expected,[StringComparison]::OrdinalIgnoreCase) -ge 0){exit 0}else{exit 1}" if not errorlevel 1 ( powershell -NoProfile -Command "try{$response=Invoke-WebRequest -UseBasicParsing -Uri $env:HEALTH_URL -TimeoutSec 2; if($response.StatusCode -eq 200){exit 0}}catch{}; exit 1" if not errorlevel 1 ( echo Spine粒子连接器已经在运行。 timeout /t 1 /nobreak >nul exit /b 0 ) powershell -NoProfile -Command "Stop-Process -Id $env:CONNECTOR_PID -Force -ErrorAction SilentlyContinue" ) del /q "%PID_FILE%" >nul 2>&1 ) rem PID 文件可能因异常退出或脚本升级而缺失,启动前检查固定端口的实际占用者。 powershell -NoProfile -Command ^ "$listener=Get-NetTCPConnection -LocalPort 27843 -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1;" ^ "if(-not $listener){exit 2};" ^ "$process=Get-CimInstance Win32_Process -Filter ('ProcessId = ' + $listener.OwningProcess) -ErrorAction SilentlyContinue;" ^ "$expected=$env:CONNECTOR_ENTRY;" ^ "if($process -and $process.CommandLine -and $process.CommandLine.IndexOf($expected,[StringComparison]::OrdinalIgnoreCase) -ge 0){Set-Content -Path $env:PID_FILE -Value $process.ProcessId -Encoding ascii; exit 0};" ^ "exit 3" set "PORT_RESULT=%ERRORLEVEL%" if "%PORT_RESULT%"=="0" ( powershell -NoProfile -Command "try{$response=Invoke-WebRequest -UseBasicParsing -Uri $env:HEALTH_URL -TimeoutSec 2; if($response.StatusCode -eq 200){exit 0}}catch{}; exit 1" if not errorlevel 1 ( echo Spine粒子连接器已经在运行。 timeout /t 1 /nobreak >nul exit /b 0 ) echo 已发现本项目的连接器进程,但健康检查失败。请先运行停止脚本后再试。 timeout /t 1 /nobreak >nul exit /b 1 ) if "%PORT_RESULT%"=="3" ( echo 27843 端口已被其他程序占用,无法启动 Spine粒子连接器。 timeout /t 1 /nobreak >nul exit /b 1 ) where node >nul 2>&1 if errorlevel 1 ( echo 未找到 Node.js,请先安装 Node.js 18 或更高版本。 timeout /t 1 /nobreak >nul exit /b 1 ) echo 正在启动 Spine粒子连接器... powershell -NoProfile -Command ^ "$node=(Get-Command node -ErrorAction Stop).Source;" ^ "$quotedEntry=[char]34 + $env:CONNECTOR_ENTRY + [char]34;" ^ "$process=Start-Process -FilePath $node -ArgumentList $quotedEntry -WorkingDirectory $env:PROJECT_DIR -RedirectStandardOutput $env:OUT_LOG -RedirectStandardError $env:ERROR_LOG -WindowStyle Hidden -PassThru;" ^ "Set-Content -Path $env:PID_FILE -Value $process.Id -Encoding ascii" if errorlevel 1 ( echo 连接器进程启动失败。 timeout /t 1 /nobreak >nul exit /b 1 ) set "CONNECTOR_PID=" if exist "%PID_FILE%" set /p CONNECTOR_PID=<"%PID_FILE%" powershell -NoProfile -Command ^ "$deadline=(Get-Date).AddSeconds(10);" ^ "do{" ^ "$process=Get-Process -Id $env:CONNECTOR_PID -ErrorAction SilentlyContinue; if(-not $process){exit 2};" ^ "try{$response=Invoke-WebRequest -UseBasicParsing -Uri $env:HEALTH_URL -TimeoutSec 1; if($response.StatusCode -eq 200){exit 0}}catch{};" ^ "Start-Sleep -Milliseconds 250" ^ "}while((Get-Date) -lt $deadline); exit 1" set "START_RESULT=%ERRORLEVEL%" if "%START_RESULT%"=="0" ( echo Spine粒子连接器启动成功,可以回到网页直接导出 .spine。 timeout /t 1 /nobreak >nul exit /b 0 ) echo 连接器未能通过健康检查。 if exist "%ERROR_LOG%" ( echo. echo 错误日志: powershell -NoProfile -Command "Get-Content -Path $env:ERROR_LOG -Tail 30 -ErrorAction SilentlyContinue" ) if exist "%OUT_LOG%" ( echo. echo 运行日志: powershell -NoProfile -Command "Get-Content -Path $env:OUT_LOG -Tail 30 -ErrorAction SilentlyContinue" ) powershell -NoProfile -Command "Stop-Process -Id $env:CONNECTOR_PID -Force -ErrorAction SilentlyContinue" del /q "%PID_FILE%" >nul 2>&1 timeout /t 1 /nobreak >nul exit /b 1