1
0
mirror of https://github.com/xPaw/PHP-Source-Query.git synced 2026-06-10 21:33:14 +02:00

PHPStan max level (improved error handling).

This commit is contained in:
Anthony Birkett
2021-05-31 12:24:57 +01:00
parent d9bab8aa25
commit efa37503bf
6 changed files with 184 additions and 82 deletions
+63 -63
View File
@@ -127,22 +127,6 @@ final class SourceQuery
$this->connected = true;
}
/**
* Forces GetChallenge to use old method for challenge retrieval because some games use outdated protocol (e.g Starbound)
*
* @param bool $value Set to true to force old method
*
* @return bool Previous value
*/
public function SetUseOldGetChallengeMethod(bool $value): bool
{
$previous = $this->useOldGetChallengeMethod;
$this->useOldGetChallengeMethod = $value === true;
return $previous;
}
/**
* Closes all open connections
*/
@@ -160,6 +144,22 @@ final class SourceQuery
}
}
/**
* Forces GetChallenge to use old method for challenge retrieval because some games use outdated protocol (e.g Starbound)
*
* @param bool $value Set to true to force old method
*
* @return bool Previous value
*/
public function SetUseOldGetChallengeMethod(bool $value): bool
{
$previous = $this->useOldGetChallengeMethod;
$this->useOldGetChallengeMethod = $value === true;
return $previous;
}
/**
* Sends ping packet to the server
* NOTE: This may not work on some games (TF2 for example)
@@ -279,7 +279,7 @@ final class SourceQuery
$server[ 'Version' ] = $buffer->getString();
// Extra Data Flags.
if (!$buffer->isEmpty()) {
if ($buffer->remaining() > 0) {
$server[ 'ExtraDataFlags' ] = $Flags = $buffer->getByte();
// S2A_EXTRA_DATA_HAS_GAME_PORT - Next 2 bytes include the game port.
@@ -421,52 +421,6 @@ final class SourceQuery
return $rules;
}
/**
* Get challenge (used for players/rules packets)
*
* @param int $header
* @param int $expectedResult
*
* @throws InvalidPacketException
*/
private function getChallenge(int $header, int $expectedResult): void
{
if ($this->challenge) {
return;
}
if ($this->useOldGetChallengeMethod) {
$header = self::A2S_SERVERQUERY_GETCHALLENGE;
}
$this->socket->write($header, "\xFF\xFF\xFF\xFF");
$buffer = $this->socket->read();
$type = $buffer->getByte();
switch ($type) {
case self::S2C_CHALLENGE:
{
$this->challenge = $buffer->get(4);
return;
}
case $expectedResult:
{
// Goldsource (HLTV).
return;
}
case 0:
{
throw new InvalidPacketException('GetChallenge: Failed to get challenge.');
}
default:
{
throw new InvalidPacketException('GetChallenge: Packet header mismatch. (0x' . dechex($type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
}
}
/**
* Sets rcon password, for future use in Rcon()
*
@@ -528,4 +482,50 @@ final class SourceQuery
return $this->rcon->command($command);
}
/**
* Get challenge (used for players/rules packets)
*
* @param int $header
* @param int $expectedResult
*
* @throws InvalidPacketException
*/
private function getChallenge(int $header, int $expectedResult): void
{
if ($this->challenge) {
return;
}
if ($this->useOldGetChallengeMethod) {
$header = self::A2S_SERVERQUERY_GETCHALLENGE;
}
$this->socket->write($header, "\xFF\xFF\xFF\xFF");
$buffer = $this->socket->read();
$type = $buffer->getByte();
switch ($type) {
case self::S2C_CHALLENGE:
{
$this->challenge = $buffer->get(4);
return;
}
case $expectedResult:
{
// Goldsource (HLTV).
return;
}
case 0:
{
throw new InvalidPacketException('GetChallenge: Failed to get challenge.');
}
default:
{
throw new InvalidPacketException('GetChallenge: Packet header mismatch. (0x' . dechex($type) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH);
}
}
}
}