Skip to content

Commit 3293f9a

Browse files
committed
Fixed HLTV GetPlayers/GetInfo
1 parent 011696a commit 3293f9a

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

SourceQuery.class.php

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ class SourceQuery
2121
const SOURCE = 1;
2222

2323
// Packets Ending
24-
const A2S_PING = 0x69;
25-
const A2S_GETCHALLENGE = 0x57; // Doesn't work
26-
const A2S_INFO = 0x54;
27-
const A2S_PLAYER = 0x55;
28-
const A2S_RULES = 0x56;
24+
const A2S_PING = 0x69;
25+
const A2S_INFO = 0x54;
26+
const A2S_PLAYER = 0x55;
27+
const A2S_RULES = 0x56;
2928

3029
// Packets Receiving
3130
const S2A_PING = 0x6A;
@@ -218,19 +217,22 @@ public function GetInfo( )
218217

219218
public function GetPlayers( )
220219
{
221-
if( !$this->Connected || !$this->GetChallenge( ) )
220+
if( !$this->Connected )
222221
{
223222
return false;
224223
}
225224

226-
$this->Socket->Write( self :: A2S_PLAYER, $this->GetChallenge( ) );
227-
$this->Socket->Read( );
228-
229-
$Type = $this->Buffer->GetByte( );
230-
231-
if( $Type != self :: S2A_PLAYER )
225+
if( $this->GetChallenge( self :: A2S_PLAYER, self :: S2A_PLAYER ) )
232226
{
233-
throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (' . $Type . ')' );
227+
$this->Socket->Write( self :: A2S_PLAYER, $this->Challenge );
228+
$this->Socket->Read( );
229+
230+
$Type = $this->Buffer->GetByte( );
231+
232+
if( $Type != self :: S2A_PLAYER )
233+
{
234+
throw new SourceQueryException( 'GetPlayers: Packet header mismatch. (' . $Type . ')' );
235+
}
234236
}
235237

236238
$Players = Array( );
@@ -252,19 +254,22 @@ public function GetPlayers( )
252254

253255
public function GetRules( )
254256
{
255-
if( !$this->Connected || !$this->GetChallenge( ) )
257+
if( !$this->Connected )
256258
{
257259
return false;
258260
}
259261

260-
$this->Socket->Write( self :: A2S_RULES, $this->GetChallenge( ) );
261-
$this->Socket->Read( );
262-
263-
$Type = $this->Buffer->GetByte( );
264-
265-
if( $Type != self :: S2A_RULES )
262+
if( $this->GetChallenge( self :: A2S_RULES, self :: S2A_RULES ) )
266263
{
267-
throw new SourceQueryException( 'GetRules: Packet header mismatch. (' . $Type . ')' );
264+
$this->Socket->Write( self :: A2S_RULES, $this->Challenge );
265+
$this->Socket->Read( );
266+
267+
$Type = $this->Buffer->GetByte( );
268+
269+
if( $Type != self :: S2A_RULES )
270+
{
271+
throw new SourceQueryException( 'GetRules: Packet header mismatch. (' . $Type . ')' );
272+
}
268273
}
269274

270275
$Rules = Array( );
@@ -284,29 +289,41 @@ public function GetRules( )
284289
return $Rules;
285290
}
286291

287-
private function GetChallenge( )
292+
private function GetChallenge( $Header, $ExpectedResult )
288293
{
289294
if( $this->Challenge )
290295
{
291-
return $this->Challenge;
296+
return true;
292297
}
293298

294-
// A2S_GETCHALLENGE is broken
295-
296-
$this->Socket->Write( self :: A2S_PLAYER, 0xFFFFFFFF );
299+
$this->Socket->Write( $Header, 0xFFFFFFFF );
297300
$this->Socket->Read( );
298301

299302
$Type = $this->Buffer->GetByte( );
300303

301-
if( $Type != self :: S2A_CHALLENGE )
304+
switch( $Type )
302305
{
303-
throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (' . $Type . ')' );
306+
case self :: S2A_CHALLENGE:
307+
{
308+
// All clear
309+
310+
$this->Challenge = $this->Buffer->Get( 4 );
311+
312+
return true;
313+
}
314+
case $ExpectedResult:
315+
{
316+
// Goldsource (HLTV)
317+
318+
return false;
319+
}
320+
default:
321+
{
322+
throw new SourceQueryException( 'GetChallenge: Packet header mismatch. (' . $Type . ')' );
323+
}
304324
}
305325

306-
// Let's keep it raw, instead of reading as long
307-
$this->Challenge = $this->Buffer->Get( 4 );
308-
309-
return $this->Challenge;
326+
return true;
310327
}
311328

312329
public function SetRconPassword( $Password )

0 commit comments

Comments
 (0)