Skip to content

Commit 40cc2c3

Browse files
Merge pull request #25 from leojonathanoh/fix/fix-sourcequery-for-counter-strike-2
Fix: Fix `SourceQuery` for Counter-Strike 2
2 parents 547df6d + 6c42163 commit 40cc2c3

3 files changed

Lines changed: 47 additions & 33 deletions

File tree

.vscode/tasks.json

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,25 @@
2121
"label": "Test (pwsh)",
2222
"type": "shell",
2323
"command": "pwsh -Command test/test.ps1",
24-
"group": {
25-
"isDefault": true,
26-
"kind": "test"
27-
}
24+
"group": "build"
2825
},
2926
{
3027
"label": "Test (powershell)",
3128
"type": "shell",
3229
"command": "powershell -Command test/test.ps1",
33-
"group": {
34-
"isDefault": true,
35-
"kind": "test"
36-
}
30+
"group": "build"
3731
},
3832
{
3933
"label": "Build: Generate module manifest",
4034
"type": "shell",
4135
"command": "MODULE_NAME=$(basename $(pwd)); MODULE_VERSION=${input:MODULE_VERSION} pwsh -Command \"build/PSModulePublisher/src/module/Generate-ModuleManifest.ps1 -DefinitionFile build/definitions/modulemanifest/definition.ps1 -Path src/$MODULE_NAME/$MODULE_NAME.psd1\"",
42-
"group": {
43-
"isDefault": true,
44-
"kind": "build"
45-
}
36+
"group": "build"
4637
},
4738
{
4839
"label": "Build: Test module manifest",
4940
"type": "shell",
5041
"command": "MODULE_NAME=$(basename $(pwd)); MODULE_VERSION=${input:MODULE_VERSION} pwsh -Command \"build/PSModulePublisher/src/module/Test-ModuleManifest.ps1 -Path src/$MODULE_NAME/$MODULE_NAME.psd1\"",
51-
"group": {
52-
"isDefault": true,
53-
"kind": "build"
54-
}
42+
"group": "build"
5543
},
5644
{
5745
"label": "Publish module (dry run)",

src/PSSourceQuery/public/SourceQuery.Tests.ps1

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ Describe "SourceQuery" -Tag 'Unit' {
88

99
$gameservers = [ordered]@{
1010
# Source
11-
# left4dead2 = @{
12-
# Address = 'l4d.startersclan.com'
13-
# Port = 27016
14-
# Engine = 'Source'
15-
# }
11+
cs2 = @{
12+
Address = 'cs.startersclan.com'
13+
Port = 27125
14+
Engine = 'Source'
15+
}
1616
csgo = @{
1717
Address = 'cs.startersclan.com'
1818
Port = 27115
1919
Engine = 'Source'
2020
}
21+
left4dead2 = @{
22+
Address = 'l4d.startersclan.com'
23+
Port = 27015
24+
Engine = 'Source'
25+
}
2126
hl2mp = @{
2227
Address = 'hl.startersclan.com'
2328
Port = 27215
@@ -47,11 +52,26 @@ Describe "SourceQuery" -Tag 'Unit' {
4752
. "$here\..\private\Resolve-DNS.ps1"
4853

4954
foreach ($game in $gameservers.Keys) {
55+
"Testing $game" | Write-Host
5056
$params = $gameservers[$game]
5157
$result = SourceQuery @params -Type $type
58+
$result['Protocol'] | Should -BeOfType [int]
59+
$result['Name'] | Should -BeOfType [string]
60+
$result['Map'] | Should -BeOfType [string]
61+
$result['Folder'] | Should -BeOfType [string]
62+
$result['Game'] | Should -BeOfType [string]
63+
$result['ID'] | Should -BeOfType [int]
64+
$result['Players'] | Should -BeOfType [int]
65+
$result['Max_players'] | Should -BeOfType [int]
66+
$result['Bots'] | Should -BeOfType [int]
67+
$result['Server_type'] | Should -BeOfType [int]
68+
$result['Environment'] | Should -BeOfType [string]
69+
$result['Visibility'] | Should -BeOfType [string]
70+
$result['VAC'] | Should -BeOfType [string]
71+
$result['Version'] | Should -BeOfType [string]
72+
$result['Port'] | Should -BeOfType [int]
5273
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
5374
}
54-
5575
}
5676

5777
It 'Gets players' {
@@ -60,11 +80,13 @@ Describe "SourceQuery" -Tag 'Unit' {
6080
. "$here\..\private\Resolve-DNS.ps1"
6181

6282
foreach ($game in $gameservers.Keys) {
83+
"Testing $game" | Write-Host
6384
$params = $gameservers[$game]
6485
$result = SourceQuery @params -Type $type
86+
$result.Players_count | Should -BeOfType [int]
87+
,$result.Players | Should -BeOfType [System.Collections.ArrayList]
6588
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
6689
}
67-
6890
}
6991

7092
It 'Gets rules' {
@@ -73,18 +95,21 @@ Describe "SourceQuery" -Tag 'Unit' {
7395
. "$here\..\private\Resolve-DNS.ps1"
7496

7597
foreach ($game in $gameservers.Keys) {
98+
"Testing $game" | Write-Host
7699
$params = $gameservers[$game]
77100
$result = SourceQuery @params -Type $type
101+
$result.Rules_count | Should -BeOfType [int]
102+
,$result.Rules | Should -BeOfType [System.Collections.ArrayList]
78103
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
79104
}
80-
81105
}
82106

83107
# Deprecated
84108
# It 'Gets ping' {
85109
# $type = 'ping'
86110

87111
# foreach ($game in $gameservers.Keys) {
112+
# "Testing $game" | Write-Host
88113
# $params = $gameservers[$game]
89114
# Write-Host "game: $game"
90115
# $result = SourceQuery @params -Type $type

src/PSSourceQuery/public/SourceQuery.ps1

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ function SourceQuery {
193193
$requestBody = $A2A_PING
194194
}
195195

196-
197-
function BuildPacket () {
198-
$pack = @(255,255,255,255) + $requestBody + [System.Text.Encoding]::UTF8.GetBytes('Source Engine Query') + 0
199-
$pack
200-
}
201196
function SendPacket ($pack) {
202197
Debug-Packet $MyInvocation.MyCommand.Name $pack
203198
$udpClient.Send($pack, $pack.Length) > $null
@@ -210,12 +205,20 @@ function SourceQuery {
210205

211206
function GetQueryData ([byte[]]$rPack) {
212207
if ($requestBody -eq $A2S_INFO) {
213-
214-
$pack = BuildPacket
208+
$pack = @(255,255,255,255) + $requestBody + [System.Text.Encoding]::UTF8.GetBytes('Source Engine Query') + 0
215209
SendPacket $pack
216210
$rPack = ReceivePacket
217211
if (!$rPack.Length) { return }
218212

213+
if ($rPack.length -eq 9 -and $rPack[4] -eq 0x41) {
214+
# In Counter-Strike 2, if the client is not localhost, the reponse is always a challenge
215+
# Got a challenge response. Send challenge request
216+
$pack = @(255,255,255,255) + $requestBody + [System.Text.Encoding]::UTF8.GetBytes('Source Engine Query') + 0 + $rPack[5..8]
217+
SendPacket $pack
218+
$rpack = ReceivePacket
219+
if (!$rPack.Length) { return }
220+
}
221+
219222
$buffer = [SourceQueryBuffer]::New($rPack)
220223
$Junk = $buffer.GetLong()
221224
$Header = $buffer.GetByte()
@@ -306,7 +309,6 @@ function SourceQuery {
306309
Banned = $true
307310
}
308311
}else {
309-
310312
# A2S_PLAYER request
311313
$pack = @(255,255,255,255) + $requestBody + $rpack[5..8]
312314
SendPacket $pack
@@ -356,7 +358,6 @@ function SourceQuery {
356358
Banned = $true
357359
}
358360
}else {
359-
360361
# A2S_RULES request
361362
$pack = @(255,255,255,255) + $requestBody + $rpack[5..8]
362363
SendPacket $pack

0 commit comments

Comments
 (0)