Last active
February 2, 2025 16:33
-
-
Save mrdaemon/6cfaddccad1c6fa52ebaf3781e0b258d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RunServer.ps1 | |
# | |
# Shitty wrapper around the MotorTown dedicated server binary | |
# Restarts the server if it crashes, or if it's not running. | |
# This is hot garbage, btw, I just wrote this at 3am in 5 minutes. | |
# One day I can wrap the server in a service and stop caring, | |
# but today is not that day. | |
# | |
# Latest version is always available at: | |
# https://gist.github.com/mrdaemon/6cfaddccad1c6fa52ebaf3781e0b258d | |
# | |
# Version 1.3 | |
# - Add parameters for server ports | |
# - Add version constant and changelog | |
# | |
# Version 1.2 | |
# - Add logging wrapper | |
# - Print timestamps in console logs | |
# | |
# Version 1.1 | |
# - Get full process object on shutdown, use WaitForExit() | |
# - Add garbage collection to the main loop | |
# | |
# Version 1.0 | |
# - Initial Version | |
New-Variable -Name ScriptVersion -Value "1.3" -Option Constant | |
# Configurable Values | |
$DedicatedServerPath = "$PSScriptRoot\dedicatedserver\" | |
$DedicatedServerBinary = "MotorTown\Binaries\Win64\MotorTownServer-Win64-Shipping.exe" | |
$ProgramArguments = @( | |
"Jeju_World?Listen?", | |
"-server", | |
"-log", | |
"-useperfthreads", | |
"-Port=7777", | |
"-QueryPort=27015" | |
) | |
# Current PID of the server process | |
$script:CurrentPid = $null | |
function Stop-MotortownServer { | |
if (-not (Test-MotortownServer)) { | |
Write-ConsoleLog "Server is not running." | |
return | |
} | |
Write-ConsoleLog "Stopping server with PID $script:CurrentPid" | |
$p = Stop-Process -Id $script:CurrentPid -PassThru -ErrorAction SilentlyContinue | |
Write-ConsoleLog "Waiting for server to stop..." | |
$p.WaitForExit() | |
$script:CurrentPid = $null | |
Write-ConsoleLog "Server stopped" | |
} | |
function Test-MotortownServer { | |
if ($null -ne $script:CurrentPid) { | |
if (Get-Process -Id $script:CurrentPid -ErrorAction SilentlyContinue) { | |
return $true | |
} else { | |
Write-ConsoleLog -Warning $true "Server process with PID $script:CurrentPid not found" | |
$script:CurrentPid = $null | |
} | |
} | |
return $false | |
} | |
function Start-MotortownServer { | |
if (Test-MotortownServer) { | |
Write-ConsoleLog "Server is already running with PID $script:CurrentPid" | |
return | |
} | |
Write-ConsoleLog "Starting server process" | |
$p = Start-Process -PassThru -FilePath "$DedicatedServerPath$DedicatedServerBinary" -WorkingDirectory $DedicatedServerPath -ArgumentList $ProgramArguments | |
$script:CurrentPid = $p.Id | |
Write-ConsoleLog "Server started with PID $script:CurrentPid" | |
} | |
function Write-ConsoleLog { | |
param ( | |
[string]$Message, | |
[bool]$Warning = $false | |
) | |
if ($Warning) { | |
Write-Warning "[$(Get-Date -Format u)] $Message" | |
} else { | |
Write-Host "[$(Get-Date -Format u)] $Message" | |
} | |
} | |
Write-Host "Motor Town Dedicated Server Wrapper v$ScriptVersion" | |
Write-Host "https://gist.github.com/mrdaemon/6cfaddccad1c6fa52ebaf3781e0b258d" | |
Write-Host "" | |
Write-Host "Press Ctrl+C to stop the server" | |
Write-Host "" | |
# Start server | |
Start-MotortownServer | |
# Monitor the server process | |
try { | |
while ($true) { | |
Start-Sleep -Seconds 10 | |
if (-not (Test-MotortownServer)) { | |
Write-ConsoleLog -Warning $true "Server process disappeared, restarting..." | |
Start-MotortownServer | |
} | |
# Collect garbage since this is a long running script | |
# and powershell just doesn't do that by itself. | |
[System.GC]::Collect() | |
} | |
} finally { | |
# Stop server on wrapper exit. | |
# In the absence of signal traps... | |
Stop-MotortownServer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment