Skip to content

Commit c39c03e

Browse files
authored
Add a Run/Debug config to launch DreamDaemon and DreamSeeker together (#38757)
* add rundebug config for launching both * wait for dreamdaemon * im going to bed
1 parent 22b4b29 commit c39c03e

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
"preLaunchTask": "Build All",
1616
"dmb": "${workspaceFolder}/${command:CurrentDMB}",
1717
"dreamDaemon": true
18+
},
19+
{
20+
"type": "byond",
21+
"request": "launch",
22+
"name": "Launch Both",
23+
"preLaunchTask": "Build and Launch Server",
24+
"dmb": "${workspaceFolder}/${command:CurrentDMB}",
25+
"dreamDaemon": true
1826
}
1927
]
2028
}

.vscode/tasks.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@
100100
},
101101
"group": "build",
102102
"label": "tgui: clean",
103+
},
104+
{
105+
"type": "shell",
106+
"label": "Background: Wait for DreamDaemon",
107+
"command": "start",
108+
"args": ["\"Waiting for DreamDaemon\"", "powershell", "-ExecutionPolicy", "Bypass", "-File", "${workspaceFolder}\\bin\\connect-to-local-wait.ps1"],
109+
"options": {
110+
"shell": {
111+
"executable": "cmd.exe", // it needs to detach so vscode can actually launch dreamdaemon before we start waiting for it
112+
"args": ["/c"]
113+
}
114+
},
115+
"presentation": {
116+
"reveal": "silent"
117+
},
118+
"problemMatcher": []
119+
},
120+
{
121+
"type": "shell",
122+
"label": "Build and Launch Server",
123+
"dependsOn": ["Build All", "Background: Wait for DreamDaemon"],
124+
"dependsOrder": "parallel",
125+
"problemMatcher": []
103126
}
104127
]
105128
}

bin/connect-to-local-wait.ps1

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)