Skip to content

Commit

Permalink
Merge pull request #25 from leojonathanoh/fix/fix-sourcequery-for-cou…
Browse files Browse the repository at this point in the history
…nter-strike-2

Fix: Fix `SourceQuery` for Counter-Strike 2
  • Loading branch information
leojonathanoh authored Nov 16, 2023
2 parents 547df6d + 6c42163 commit 40cc2c3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
20 changes: 4 additions & 16 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,25 @@
"label": "Test (pwsh)",
"type": "shell",
"command": "pwsh -Command test/test.ps1",
"group": {
"isDefault": true,
"kind": "test"
}
"group": "build"
},
{
"label": "Test (powershell)",
"type": "shell",
"command": "powershell -Command test/test.ps1",
"group": {
"isDefault": true,
"kind": "test"
}
"group": "build"
},
{
"label": "Build: Generate module manifest",
"type": "shell",
"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\"",
"group": {
"isDefault": true,
"kind": "build"
}
"group": "build"
},
{
"label": "Build: Test module manifest",
"type": "shell",
"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\"",
"group": {
"isDefault": true,
"kind": "build"
}
"group": "build"
},
{
"label": "Publish module (dry run)",
Expand Down
41 changes: 33 additions & 8 deletions src/PSSourceQuery/public/SourceQuery.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ Describe "SourceQuery" -Tag 'Unit' {

$gameservers = [ordered]@{
# Source
# left4dead2 = @{
# Address = 'l4d.startersclan.com'
# Port = 27016
# Engine = 'Source'
# }
cs2 = @{
Address = 'cs.startersclan.com'
Port = 27125
Engine = 'Source'
}
csgo = @{
Address = 'cs.startersclan.com'
Port = 27115
Engine = 'Source'
}
left4dead2 = @{
Address = 'l4d.startersclan.com'
Port = 27015
Engine = 'Source'
}
hl2mp = @{
Address = 'hl.startersclan.com'
Port = 27215
Expand Down Expand Up @@ -47,11 +52,26 @@ Describe "SourceQuery" -Tag 'Unit' {
. "$here\..\private\Resolve-DNS.ps1"

foreach ($game in $gameservers.Keys) {
"Testing $game" | Write-Host
$params = $gameservers[$game]
$result = SourceQuery @params -Type $type
$result['Protocol'] | Should -BeOfType [int]
$result['Name'] | Should -BeOfType [string]
$result['Map'] | Should -BeOfType [string]
$result['Folder'] | Should -BeOfType [string]
$result['Game'] | Should -BeOfType [string]
$result['ID'] | Should -BeOfType [int]
$result['Players'] | Should -BeOfType [int]
$result['Max_players'] | Should -BeOfType [int]
$result['Bots'] | Should -BeOfType [int]
$result['Server_type'] | Should -BeOfType [int]
$result['Environment'] | Should -BeOfType [string]
$result['Visibility'] | Should -BeOfType [string]
$result['VAC'] | Should -BeOfType [string]
$result['Version'] | Should -BeOfType [string]
$result['Port'] | Should -BeOfType [int]
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
}

}

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

foreach ($game in $gameservers.Keys) {
"Testing $game" | Write-Host
$params = $gameservers[$game]
$result = SourceQuery @params -Type $type
$result.Players_count | Should -BeOfType [int]
,$result.Players | Should -BeOfType [System.Collections.ArrayList]
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
}

}

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

foreach ($game in $gameservers.Keys) {
"Testing $game" | Write-Host
$params = $gameservers[$game]
$result = SourceQuery @params -Type $type
$result.Rules_count | Should -BeOfType [int]
,$result.Rules | Should -BeOfType [System.Collections.ArrayList]
$result | Should -BeOfType [System.Collections.Specialized.OrderedDictionary]
}

}

# Deprecated
# It 'Gets ping' {
# $type = 'ping'

# foreach ($game in $gameservers.Keys) {
# "Testing $game" | Write-Host
# $params = $gameservers[$game]
# Write-Host "game: $game"
# $result = SourceQuery @params -Type $type
Expand Down
19 changes: 10 additions & 9 deletions src/PSSourceQuery/public/SourceQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ function SourceQuery {
$requestBody = $A2A_PING
}


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

function GetQueryData ([byte[]]$rPack) {
if ($requestBody -eq $A2S_INFO) {

$pack = BuildPacket
$pack = @(255,255,255,255) + $requestBody + [System.Text.Encoding]::UTF8.GetBytes('Source Engine Query') + 0
SendPacket $pack
$rPack = ReceivePacket
if (!$rPack.Length) { return }

if ($rPack.length -eq 9 -and $rPack[4] -eq 0x41) {
# In Counter-Strike 2, if the client is not localhost, the reponse is always a challenge
# Got a challenge response. Send challenge request
$pack = @(255,255,255,255) + $requestBody + [System.Text.Encoding]::UTF8.GetBytes('Source Engine Query') + 0 + $rPack[5..8]
SendPacket $pack
$rpack = ReceivePacket
if (!$rPack.Length) { return }
}

$buffer = [SourceQueryBuffer]::New($rPack)
$Junk = $buffer.GetLong()
$Header = $buffer.GetByte()
Expand Down Expand Up @@ -306,7 +309,6 @@ function SourceQuery {
Banned = $true
}
}else {

# A2S_PLAYER request
$pack = @(255,255,255,255) + $requestBody + $rpack[5..8]
SendPacket $pack
Expand Down Expand Up @@ -356,7 +358,6 @@ function SourceQuery {
Banned = $true
}
}else {

# A2S_RULES request
$pack = @(255,255,255,255) + $requestBody + $rpack[5..8]
SendPacket $pack
Expand Down

0 comments on commit 40cc2c3

Please sign in to comment.