@@ -21,11 +21,10 @@ class SourceQuery
21
21
const SOURCE = 1 ;
22
22
23
23
// 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 ;
29
28
30
29
// Packets Receiving
31
30
const S2A_PING = 0x6A ;
@@ -218,19 +217,22 @@ public function GetInfo( )
218
217
219
218
public function GetPlayers ( )
220
219
{
221
- if ( !$ this ->Connected || ! $ this -> GetChallenge ( ) )
220
+ if ( !$ this ->Connected )
222
221
{
223
222
return false ;
224
223
}
225
224
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 ) )
232
226
{
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
+ }
234
236
}
235
237
236
238
$ Players = Array ( );
@@ -252,19 +254,22 @@ public function GetPlayers( )
252
254
253
255
public function GetRules ( )
254
256
{
255
- if ( !$ this ->Connected || ! $ this -> GetChallenge ( ) )
257
+ if ( !$ this ->Connected )
256
258
{
257
259
return false ;
258
260
}
259
261
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 ) )
266
263
{
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
+ }
268
273
}
269
274
270
275
$ Rules = Array ( );
@@ -284,29 +289,41 @@ public function GetRules( )
284
289
return $ Rules ;
285
290
}
286
291
287
- private function GetChallenge ( )
292
+ private function GetChallenge ( $ Header , $ ExpectedResult )
288
293
{
289
294
if ( $ this ->Challenge )
290
295
{
291
- return $ this -> Challenge ;
296
+ return true ;
292
297
}
293
298
294
- // A2S_GETCHALLENGE is broken
295
-
296
- $ this ->Socket ->Write ( self :: A2S_PLAYER , 0xFFFFFFFF );
299
+ $ this ->Socket ->Write ( $ Header , 0xFFFFFFFF );
297
300
$ this ->Socket ->Read ( );
298
301
299
302
$ Type = $ this ->Buffer ->GetByte ( );
300
303
301
- if ( $ Type != self :: S2A_CHALLENGE )
304
+ switch ( $ Type )
302
305
{
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
+ }
304
324
}
305
325
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 ;
310
327
}
311
328
312
329
public function SetRconPassword ( $ Password )
0 commit comments