Files
Commonwealth-Online-Server/server/start.bat
T
andrew dab76146cf
Linux Compatibility / Ubuntu dedicated server (push) Canceled after 0s
Linux Compatibility / Arch Linux container (push) Canceled after 0s
Handle config path args in server startup
Updated server launch behavior to reliably treat JSON file arguments as config paths, including file-manager “Open with” cases. The CLI `serve` command now accepts an optional positional config path (equivalent to `--config`) and errors on conflicting values. `start.sh` and `start.bat` now parse config-related arguments more explicitly, validate required values, and always pass `--config` to avoid accidental positional forwarding. Added tests covering both positional and `--config` forms, plus README documentation for the new startup behavior.
2026-08-01 21:49:48 +12:00

143 lines
3.8 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
echo.
echo ================================================================================
echo Commonwealth Online Server - Start Script
echo ================================================================================
echo.
python --version >nul 2>&1
if errorlevel 1 (
echo ERROR: Python is not installed or not in PATH.
echo Please install Python 3.9+ from https://www.python.org
pause
exit /b 1
)
if not exist "requirements-server.txt" (
echo ERROR: Missing requirements-server.txt
pause
exit /b 1
)
set "UPDATE_DEPENDENCIES=0"
set "CONFIG_FILE=%cd%\commonwealth-server.json"
set "SERVER_ARGS="
:parse_args
if "%~1"=="" goto args_done
if /I "%~1"=="--update-dependencies" (
set "UPDATE_DEPENDENCIES=1"
shift
goto parse_args
)
if /I "%~1"=="--config" (
if "%~2"=="" (
echo ERROR: --config requires a config file path.
pause
exit /b 1
)
set "CONFIG_FILE=%~f2"
shift
shift
goto parse_args
)
if /I "%~1"=="-c" (
if "%~2"=="" (
echo ERROR: -c requires a config file path.
pause
exit /b 1
)
set "CONFIG_FILE=%~f2"
shift
shift
goto parse_args
)
echo %~1| findstr /I /R "\.json$" >nul
if not errorlevel 1 (
set "CONFIG_FILE=%~f1"
shift
goto parse_args
)
set "SERVER_ARGS=!SERVER_ARGS! %1"
shift
goto parse_args
:args_done
if not exist ".venv\Scripts\python.exe" (
echo Creating virtual environment at .venv...
python -m venv .venv
if errorlevel 1 (
echo ERROR: Failed to create virtual environment.
pause
exit /b 1
)
)
set "VENV_PYTHON=.venv\Scripts\python.exe"
if not exist "%VENV_PYTHON%" (
echo ERROR: Virtual environment interpreter missing.
pause
exit /b 1
)
set "NEED_INSTALL=0"
if not exist ".venv\.requirements-server.sha256" set "NEED_INSTALL=1"
if "%UPDATE_DEPENDENCIES%"=="1" set "NEED_INSTALL=1"
if "%NEED_INSTALL%"=="0" (
"%VENV_PYTHON%" -c "from hashlib import sha256; from pathlib import Path; expected=Path('.venv/.requirements-server.sha256').read_text(encoding='utf-8').strip(); actual=sha256(Path('requirements-server.txt').read_bytes()).hexdigest(); raise SystemExit(0 if expected==actual else 1)"
if errorlevel 1 set "NEED_INSTALL=1"
)
if "%NEED_INSTALL%"=="1" (
echo Installing dedicated-server dependencies into .venv...
"%VENV_PYTHON%" -m pip install --upgrade pip >nul 2>&1
"%VENV_PYTHON%" -m pip install -r requirements-server.txt
if errorlevel 1 (
echo ERROR: Failed to install required packages.
pause
exit /b 1
)
"%VENV_PYTHON%" -c "from hashlib import sha256; from pathlib import Path; Path('.venv/.requirements-server.sha256').write_text(sha256(Path('requirements-server.txt').read_bytes()).hexdigest() + chr(10), encoding='utf-8')"
echo Dependencies installed.
) else (
echo Dependencies are up to date.
)
echo.
if not exist "!CONFIG_FILE!" (
echo Generating default configuration file...
"%VENV_PYTHON%" -u consumer_server_cli.py config init "!CONFIG_FILE!"
if errorlevel 1 (
echo ERROR: Failed to generate config file.
pause
exit /b 1
)
echo.
)
echo Starting Commonwealth Online Server...
echo Type help for commands. Type quit or press Ctrl+C to stop.
echo.
"%VENV_PYTHON%" -u consumer_server_cli.py serve --config "!CONFIG_FILE!" --interactive !SERVER_ARGS!
set "EXIT_CODE=!ERRORLEVEL!"
if not "!EXIT_CODE!"=="0" (
echo.
echo Server failed to start. If you see "Only one usage of each socket address"
echo error, the port may already be in use.
echo.
choice /C YN /N /M "Would you like to fix the port issue? (Y/N): "
if !errorlevel!==1 (
echo.
call fix-port.bat
)
)
pause
exit /b !EXIT_CODE!