|
| 1 | +# Ping the local DreamDaemon server until it responds |
| 2 | +# Autodetects port by finding what dreamdaemon.exe is listening on |
| 3 | + |
| 4 | +function Find-DreamDaemonPort { |
| 5 | + # check running processes, this is slow as shit because it looks at everything so erroraction should be faster |
| 6 | + $proc = Get-Process -Name 'dd','dreamdaemon' -ErrorAction SilentlyContinue | Select-Object -First 1 |
| 7 | + if (-not $proc) { |
| 8 | + return $null |
| 9 | + } |
| 10 | + |
| 11 | + # found the process, get the port |
| 12 | + $conn = Get-NetTCPConnection -OwningProcess $proc.Id -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1 |
| 13 | + if ($conn) { |
| 14 | + return $conn.LocalPort |
| 15 | + } |
| 16 | + return $null |
| 17 | +} |
| 18 | + |
| 19 | +function Test-Server { |
| 20 | + param([string]$Server = "localhost", [int]$Port) |
| 21 | + try { |
| 22 | + $client = New-Object System.Net.Sockets.TcpClient |
| 23 | + $asyncResult = $client.BeginConnect($Server, $Port, $null, $null) |
| 24 | + if (-not $asyncResult.AsyncWaitHandle.WaitOne(1000)) { |
| 25 | + $client.Close() |
| 26 | + return $false |
| 27 | + } |
| 28 | + $client.EndConnect($asyncResult) |
| 29 | + $stream = $client.GetStream() |
| 30 | + |
| 31 | + $query = "?ping" |
| 32 | + $queryBytes = [System.Text.Encoding]::ASCII.GetBytes($query) |
| 33 | + $length = $queryBytes.Length + 6 |
| 34 | + |
| 35 | + # Byond topic packet construction |
| 36 | + $packet = [byte[]]@( |
| 37 | + 0x00, # Header byte |
| 38 | + 0x83, # Header byte |
| 39 | + [byte](($length -shr 8) -band 0xFF), # Message body size in big endian |
| 40 | + [byte]($length -band 0xFF), # and lower byte, its an int16 |
| 41 | + 0x00, 0x00, 0x00, 0x00, 0x00 # flag, port (unused here so 0) |
| 42 | + ) + $queryBytes + [byte]0x00 # mesasge + null terminator |
| 43 | + |
| 44 | + $stream.Write($packet, 0, $packet.Length) |
| 45 | + $stream.Flush() |
| 46 | + $stream.ReadTimeout = 1000 |
| 47 | + |
| 48 | + $buffer = New-Object byte[] 1024 |
| 49 | + $read = $stream.Read($buffer, 0, 1024) |
| 50 | + |
| 51 | + $client.Close() |
| 52 | + return $read -gt 0 |
| 53 | + } catch { |
| 54 | + return $false |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +$detectedPort = $null |
| 59 | + |
| 60 | +while ($true) { |
| 61 | + # get whatever port dreamdaemon is on |
| 62 | + $port = Find-DreamDaemonPort |
| 63 | + if ($port) { |
| 64 | + Write-Host "Found DreamDaemon on port $port" |
| 65 | + # ping the server to make sure we can actually connect yet |
| 66 | + if (Test-Server -Port $port) { |
| 67 | + $detectedPort = $port |
| 68 | + break |
| 69 | + } |
| 70 | + Write-Host "Server is still starting and not responding yet, retrying" |
| 71 | + } else { |
| 72 | + Write-Host "DreamDaemon has not yet launched, retrying" |
| 73 | + } |
| 74 | + Start-Sleep -Seconds 2 |
| 75 | +} |
| 76 | + |
| 77 | +if ($detectedPort) { |
| 78 | + $dsPath = "C:\Program Files (x86)\BYOND\bin\dreamseeker.exe" |
| 79 | + if (-not (Test-Path $dsPath)) { |
| 80 | + $dsPath = "C:\Program Files\BYOND\bin\dreamseeker.exe" |
| 81 | + } |
| 82 | + Start-Process $dsPath -ArgumentList "byond://localhost:$detectedPort" |
| 83 | +} |
0 commit comments