Complete C# server cutover

This commit is contained in:
Nomads_Reach
2026-08-16 02:36:32 -04:00
parent ddadf87423
commit 757e614cdd
79 changed files with 923 additions and 13129 deletions
+106 -122
View File
@@ -1,142 +1,126 @@
@echo off
setlocal enabledelayedexpansion
setlocal EnableExtensions DisableDelayedExpansion
cd /d "%~dp0"
echo.
echo ================================================================================
echo Commonwealth Online Server - Start Script
echo ================================================================================
echo.
set "CONFIG=%CD%\commonwealth-server.json"
set "UPDATE=0"
set "EXTRA_ARGS="
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
:parse
if "%~1"=="" goto parsed
if /I "%~1"=="--update-dependencies" (
set "UPDATE_DEPENDENCIES=1"
shift
goto parse_args
set "UPDATE=1"
shift
goto parse
)
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"=="--config" goto parse_config
if /I "%~1"=="-c" goto parse_config
if /I "%~1"=="--host" goto parse_host
if /I "%~1"=="-H" goto parse_host
if /I "%~1"=="--port" goto parse_port
if /I "%~1"=="-p" goto parse_port
if /I "%~1"=="--interactive" (
set "EXTRA_ARGS=%EXTRA_ARGS% --interactive"
shift
goto parse
)
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
if /I "%~1"=="-i" (
set "EXTRA_ARGS=%EXTRA_ARGS% --interactive"
shift
goto parse
)
echo %~1| findstr /I /R "\.json$" >nul
if not errorlevel 1 (
set "CONFIG_FILE=%~f1"
shift
goto parse_args
if /I "%~x1"==".json" (
set "CONFIG=%~f1"
shift
goto parse
)
set "SERVER_ARGS=!SERVER_ARGS! %1"
echo ERROR: Unknown option or argument: %~1 1>&2
exit /b 1
:parse_config
if "%~2"=="" (
echo ERROR: %~1 requires a config path. 1>&2
exit /b 1
)
set "CONFIG=%~f2"
shift
goto parse_args
:args_done
shift
goto parse
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
)
:parse_host
if "%~2"=="" (
echo ERROR: %~1 requires a host. 1>&2
exit /b 1
)
set "EXTRA_ARGS=%EXTRA_ARGS% --host %~2"
shift
shift
goto parse
:parse_port
if "%~2"=="" (
echo ERROR: %~1 requires a port. 1>&2
exit /b 1
)
set "EXTRA_ARGS=%EXTRA_ARGS% --port %~2"
shift
shift
goto parse
:parsed
set "MODE="
set "SERVER_EXE=%CD%\CommonwealthOnline.Server.exe"
set "SERVER_DLL=%CD%\CommonwealthOnline.Server.dll"
set "SERVER_PROJECT=%CD%\CommonwealthOnline.Server.csproj"
if exist "%SERVER_EXE%" set "MODE=apphost"
if defined MODE goto resolved
where dotnet >nul 2>nul
if errorlevel 1 goto missing
if exist "%SERVER_DLL%" (
set "MODE=dll"
goto resolved
)
if exist "%SERVER_PROJECT%" (
set "MODE=project"
goto resolved
)
set "VENV_PYTHON=.venv\Scripts\python.exe"
if not exist "%VENV_PYTHON%" (
echo ERROR: Virtual environment interpreter missing.
pause
:missing
echo ERROR: CommonwealthOnline.Server is not published and the .NET 8 SDK/runtime is unavailable. 1>&2
exit /b 1
:resolved
if "%UPDATE%"=="1" (
if not exist "%SERVER_PROJECT%" (
echo ERROR: --update-dependencies requires CommonwealthOnline.Server.csproj. 1>&2
exit /b 1
)
where dotnet >nul 2>nul || (
echo ERROR: --update-dependencies requires the .NET SDK. 1>&2
exit /b 1
)
dotnet restore "%SERVER_PROJECT%" || 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 not exist "%CONFIG%" (
echo Generating default configuration: %CONFIG%
call :run config init "%CONFIG%" || exit /b 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 Starting Commonwealth Online C# server
call :run serve --config "%CONFIG%" %EXTRA_ARGS%
exit /b %ERRORLEVEL%
:run
if /I "%MODE%"=="apphost" (
"%SERVER_EXE%" %*
exit /b %ERRORLEVEL%
)
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.
if /I "%MODE%"=="dll" (
dotnet "%SERVER_DLL%" %*
exit /b %ERRORLEVEL%
)
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!
dotnet run --project "%SERVER_PROJECT%" -c Release --no-launch-profile -- %*
exit /b %ERRORLEVEL%