diff --git a/third_party/gameq/GameQ/Filters/Base.php b/third_party/gameq/GameQ/Filters/Base.php index 61bb2922..4c4bcbf7 100644 --- a/third_party/gameq/GameQ/Filters/Base.php +++ b/third_party/gameq/GameQ/Filters/Base.php @@ -33,14 +33,14 @@ abstract class Base * * @type array */ - protected $options = [ ]; + protected $options = []; /** * Construct * * @param array $options */ - public function __construct($options = [ ]) + public function __construct($options = []) { $this->options = $options; diff --git a/third_party/gameq/GameQ/Filters/Normalize.php b/third_party/gameq/GameQ/Filters/Normalize.php index f67b339f..5771c929 100644 --- a/third_party/gameq/GameQ/Filters/Normalize.php +++ b/third_party/gameq/GameQ/Filters/Normalize.php @@ -64,7 +64,7 @@ class Normalize extends Base if (isset($result['players']) && count($result['players']) > 0) { // Iterate foreach ($result['players'] as $key => $player) { - $result['players'][ $key ] = array_merge($player, $this->check('player', $player)); + $result['players'][$key] = array_merge($player, $this->check('player', $player)); } } else { $result['players'] = []; @@ -74,7 +74,7 @@ class Normalize extends Base if (isset($result['teams']) && count($result['teams']) > 0) { // Iterate foreach ($result['teams'] as $key => $team) { - $result['teams'][ $key ] = array_merge($team, $this->check('team', $team)); + $result['teams'][$key] = array_merge($team, $this->check('team', $team)); } } else { $result['teams'] = []; @@ -104,8 +104,8 @@ class Normalize extends Base // Normalized return array $normalized = []; - if (isset($this->normalize[ $section ]) && !empty($this->normalize[ $section ])) { - foreach ($this->normalize[ $section ] as $property => $raw) { + if (isset($this->normalize[$section]) && !empty($this->normalize[$section])) { + foreach ($this->normalize[$section] as $property => $raw) { // Default the value for the new key as null $value = null; @@ -113,18 +113,18 @@ class Normalize extends Base // Iterate over the raw property we want to use foreach ($raw as $check) { if (array_key_exists($check, $data)) { - $value = $data[ $check ]; + $value = $data[$check]; break; } } } else { // String if (array_key_exists($raw, $data)) { - $value = $data[ $raw ]; + $value = $data[$raw]; } } - $normalized[ 'gq_' . $property ] = $value; + $normalized['gq_' . $property] = $value; } } diff --git a/third_party/gameq/GameQ/Filters/Stripcolors.php b/third_party/gameq/GameQ/Filters/Stripcolors.php index 17020da5..635061c8 100644 --- a/third_party/gameq/GameQ/Filters/Stripcolors.php +++ b/third_party/gameq/GameQ/Filters/Stripcolors.php @@ -51,7 +51,6 @@ class Stripcolors extends Base // Switch based on the base (not game) protocol switch ($server->protocol()->getProtocol()) { - case 'quake2': case 'quake3': case 'doom3': diff --git a/third_party/gameq/GameQ/GameQ.php b/third_party/gameq/GameQ/GameQ.php index 543594fa..4470e8a2 100644 --- a/third_party/gameq/GameQ/GameQ.php +++ b/third_party/gameq/GameQ/GameQ.php @@ -44,11 +44,6 @@ class GameQ * Constants */ - /** - * Current version - */ - const VERSION = '3.0.0-alpha2'; - /* Static Section */ /** @@ -389,7 +384,6 @@ class GameQ 'server_id' => $server_id, 'socket' => $socket, ]; - } catch (QueryException $e) { // Check to see if we are in debug, if so bubble up the exception if ($this->debug) { diff --git a/third_party/gameq/GameQ/Protocols/Arma3.php b/third_party/gameq/GameQ/Protocols/Arma3.php new file mode 100644 index 00000000..6ea46979 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Arma3.php @@ -0,0 +1,183 @@ +. + */ + +namespace GameQ\Protocols; + +use GameQ\Buffer; +use GameQ\Result; + +/** + * Class Armed Assault 3 + * + * Rules protocol reference: https://community.bistudio.com/wiki/Arma_3_ServerBrowserProtocol2 + * + * @package GameQ\Protocols + * @author Austin Bischoff + * @author Memphis017 + */ +class Arma3 extends Source +{ + /** + * DLC ^2 constants + */ + const DLC_KARTS = 1, + DLC_MARKSMEN = 2, + DLC_HELICOPTERS = 4, + DLC_APEX = 16; + + /** + * Defines the names for the specific game DLCs + * + * @var array + */ + protected $dlcNames = [ + self::DLC_KARTS => 'Karts', + self::DLC_MARKSMEN => 'Marksmen', + self::DLC_HELICOPTERS => 'Helicopters', + self::DLC_APEX => 'Apex', + ]; + + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'arma3'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Arma3"; + + /** + * Query port = client_port + 1 + * + * @type int + */ + protected $port_diff = 1; + + /** + * Process the rules since Arma3 changed their response for rules + * + * @param Buffer $buffer + * + * @return array + * @throws \GameQ\Exception\Protocol + */ + protected function processRules(Buffer $buffer) + { + // Total number of packets, burn it + $buffer->readInt16(); + + // Will hold the data string + $data = ''; + + // Loop until we run out of strings + while ($buffer->getLength()) { + // Burn the delimiters (i.e. \x01\x04\x00) + $buffer->readString(); + + // Add the data to the string, we are reassembling it + $data .= $buffer->readString(); + } + + // Restore escaped sequences + $data = str_replace(["\x01\x01", "\x01\x02", "\x01\x03"], ["\x01", "\x00", "\xFF"], $data); + + // Make a new buffer with the reassembled data + $responseBuffer = new Buffer($data); + + // Kill the old buffer, should be empty + unset($buffer, $data); + + // Set the result to a new result instance + $result = new Result(); + + // Get results + $result->add('rules_protocol_version', $responseBuffer->readInt8()); + $result->add('overflow', $responseBuffer->readInt8()); + $dlcBit = $responseBuffer->readInt8(); // Grab DLC bit and use it later + $responseBuffer->skip(); // Reserved, burn it + // Grab difficulty so we can man handle it... + $difficulty = $responseBuffer->readInt8(); + + // Process difficulty + $result->add('3rd_person', $difficulty >> 7); + $result->add('advanced_flight_mode', ($difficulty >> 6) & 1); + $result->add('difficulty_ai', ($difficulty >> 3) & 3); + $result->add('difficulty_level', $difficulty & 3); + + unset($difficulty); + + // Crosshair + $result->add('crosshair', $responseBuffer->readInt8()); + + // Next are the dlc bits so loop over the dlcBit so we can determine which DLC's are running + for ($x = 1; $x <= $dlcBit; $x *= 2) { + // Enabled, add it to the list + if ($x & $dlcBit) { + $result->addSub('dlcs', 'name', $this->dlcNames[$x]); + $result->addSub('dlcs', 'hash', dechex($responseBuffer->readInt32())); + } + } + + // No longer needed + unset($dlcBit); + + // Grab the mod count + $modCount = $responseBuffer->readInt8(); + + // Add mod count + $result->add('mod_count', $modCount); + + // Loop the mod count and add them + for ($x = 0; $x < $modCount; $x++) { + // Add the mod to the list + $result->addSub('mods', 'hash', dechex($responseBuffer->readInt32())); + $result->addSub('mods', 'steam_id', hexdec($responseBuffer->readPascalString(0, true))); + $result->addSub('mods', 'name', $responseBuffer->readPascalString(0, true)); + } + + unset($modCount, $x); + + // Burn the signatures count, we will just loop until we run out of strings + $responseBuffer->read(); + + // Make signatures array + $signatures = []; + + // Loop until we run out of strings + while ($responseBuffer->getLength()) { + //$result->addSub('signatures', 0, $responseBuffer->readPascalString(0, true)); + $signatures[] = $responseBuffer->readPascalString(0, true); + } + + // Add as a simple array + $result->add('signatures', $signatures); + + // Add signatures count + $result->add('signature_count', count($signatures)); + + unset($responseBuffer, $signatures); + + return $result->fetch(); + } +} diff --git a/third_party/gameq/GameQ/Protocols/Armedassault3.php b/third_party/gameq/GameQ/Protocols/Armedassault3.php index 1d938f81..5bbca429 100644 --- a/third_party/gameq/GameQ/Protocols/Armedassault3.php +++ b/third_party/gameq/GameQ/Protocols/Armedassault3.php @@ -19,31 +19,14 @@ namespace GameQ\Protocols; /** - * Class Armed Assault 3 + * Armed assault 3 dummy Protocol Class * - * @package GameQ\Protocols - * @author Austin Bischoff + * Added for backward compatibility, please update to class arma3 + * + * @deprecated v3.0.10 + * @package GameQ\Protocols + * @author Austin Bischoff */ -class Armedassault3 extends Source +class Armedassault3 extends Arma3 { - /** - * String name of this protocol class - * - * @type string - */ - protected $name = 'armedassault3'; - - /** - * Longer string name of this protocol class - * - * @type string - */ - protected $name_long = "Armed Assault 3"; - - /** - * Query port = client_port + 1 - * - * @type int - */ - protected $port_diff = 1; } diff --git a/third_party/gameq/GameQ/Protocols/Bf1942.php b/third_party/gameq/GameQ/Protocols/Bf1942.php new file mode 100644 index 00000000..4cf06c5e --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Bf1942.php @@ -0,0 +1,88 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Battlefield 1942 + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Bf1942 extends Gamespy +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'bf1942'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Battlefield 1942"; + + /** + * query_port = client_port + 8433 + * 23000 = 14567 + 8433 + * + * @type int + */ + protected $port_diff = 8433; + + /** + * The client join link + * + * @type string + */ + protected $join_link = "bf1942://%s:%d"; + + /** + * Normalize settings for this protocol + * + * @type array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'dedicated' => 'dedicated', + 'gametype' => 'gametype', + 'hostname' => 'hostname', + 'mapname' => 'mapname', + 'maxplayers' => 'maxplayers', + 'numplayers' => 'numplayers', + 'password' => 'password', + ], + // Individual + 'player' => [ + 'name' => 'playername', + 'kills' => 'kills', + 'deaths' => 'deaths', + 'ping' => 'ping', + 'score' => 'score', + ], + 'team' => [ + 'name' => 'teamname', + ], + ]; +} diff --git a/third_party/gameq/GameQ/Protocols/Bf2.php b/third_party/gameq/GameQ/Protocols/Bf2.php index 118eaae0..0610f9d0 100644 --- a/third_party/gameq/GameQ/Protocols/Bf2.php +++ b/third_party/gameq/GameQ/Protocols/Bf2.php @@ -18,11 +18,8 @@ namespace GameQ\Protocols; -use GameQ\Protocol; -use GameQ\Buffer; - /** - * Battlefield 2 Protocol Class + * Class Battlefield 2 * * @package GameQ\Protocols * @author Austin Bischoff @@ -30,15 +27,6 @@ use GameQ\Buffer; class Bf2 extends Gamespy3 { - /** - * Array of packets we want to query. - * - * @type array - */ - protected $packets = [ - self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40\xFF\xFF\xFF\x01", - ]; - /** * String name of this protocol class * @@ -52,4 +40,59 @@ class Bf2 extends Gamespy3 * @type string */ protected $name_long = "Battlefield 2"; -} \ No newline at end of file + + /** + * query_port = client_port + 8433 + * 29900 = 16567 + 13333 + * + * @type int + */ + protected $port_diff = 13333; + + /** + * The client join link + * + * @type string + */ + protected $join_link = "bf2://%s:%d"; + + /** + * BF2 has a different query packet to send than "normal" Gamespy 3 + * + * @var array + */ + protected $packets = [ + self::PACKET_ALL => "\xFE\xFD\x00\x10\x20\x30\x40\xFF\xFF\xFF\x01", + ]; + + /** + * Normalize settings for this protocol + * + * @type array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'dedicated' => 'dedicated', + 'gametype' => 'gametype', + 'hostname' => 'hostname', + 'mapname' => 'mapname', + 'maxplayers' => 'maxplayers', + 'numplayers' => 'numplayers', + 'password' => 'password', + ], + // Individual + 'player' => [ + 'name' => 'player', + 'kills' => 'score', + 'deaths' => 'deaths', + 'ping' => 'ping', + 'score' => 'score', + ], + 'team' => [ + 'name' => 'team', + 'score' => 'score', + ], + ]; +} diff --git a/third_party/gameq/GameQ/Protocols/Bf3.php b/third_party/gameq/GameQ/Protocols/Bf3.php index 20300e91..90845159 100644 --- a/third_party/gameq/GameQ/Protocols/Bf3.php +++ b/third_party/gameq/GameQ/Protocols/Bf3.php @@ -111,6 +111,7 @@ class Bf3 extends Protocol // target => source 'dedicated' => 'dedicated', 'hostname' => 'hostname', + 'mapname' => 'map', 'maxplayers' => 'max_players', 'numplayers' => 'num_players', 'password' => 'password', @@ -118,7 +119,7 @@ class Bf3 extends Protocol 'player' => [ 'name' => 'name', 'score' => 'score', - 'ping' => 'ping', + 'ping' => 'ping', ], 'team' => [ 'score' => 'tickets', @@ -135,10 +136,10 @@ class Bf3 extends Protocol { // Holds the results sent back - $results = [ ]; + $results = []; // Holds the processed packets after having been reassembled - $processed = [ ]; + $processed = []; // Start up the index for the processed $sequence_id_last = 0; @@ -182,7 +183,7 @@ class Bf3 extends Protocol // Now we need to call the proper method $results = array_merge( $results, - call_user_func_array([ $this, $this->responses[$sequence_id] ], [ $buffer ]) + call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer]) ); } @@ -203,7 +204,7 @@ class Bf3 extends Protocol protected function decode(Buffer $buffer) { - $items = [ ]; + $items = []; // Get the number of words in this buffer $itemCount = $buffer->readInt32(); @@ -241,13 +242,13 @@ class Bf3 extends Protocol // These are the same no matter what mode the server is in $result->add('hostname', $items[1]); - $result->add('num_players', (int) $items[2]); - $result->add('max_players', (int) $items[3]); + $result->add('num_players', (int)$items[2]); + $result->add('max_players', (int)$items[3]); $result->add('gametype', $items[4]); $result->add('map', $items[5]); - $result->add('roundsplayed', (int) $items[6]); - $result->add('roundstotal', (int) $items[7]); - $result->add('num_teams', (int) $items[8]); + $result->add('roundsplayed', (int)$items[6]); + $result->add('roundstotal', (int)$items[7]); + $result->add('num_teams', (int)$items[8]); // Set the current index $index_current = 9; @@ -264,22 +265,22 @@ class Bf3 extends Protocol } // Get and set the rest of the data points. - $result->add('targetscore', (int) $items[$index_current]); + $result->add('targetscore', (int)$items[$index_current]); $result->add('online', 1); // Forced true, it seems $words[$index_current + 1] is always empty - $result->add('ranked', (int) $items[$index_current + 2]); - $result->add('punkbuster', (int) $items[$index_current + 3]); - $result->add('password', (int) $items[$index_current + 4]); - $result->add('uptime', (int) $items[$index_current + 5]); - $result->add('roundtime', (int) $items[$index_current + 6]); + $result->add('ranked', (int)$items[$index_current + 2]); + $result->add('punkbuster', (int)$items[$index_current + 3]); + $result->add('password', (int)$items[$index_current + 4]); + $result->add('uptime', (int)$items[$index_current + 5]); + $result->add('roundtime', (int)$items[$index_current + 6]); // Added in R9 $result->add('ip_port', $items[$index_current + 7]); $result->add('punkbuster_version', $items[$index_current + 8]); - $result->add('join_queue', (int) $items[$index_current + 9]); + $result->add('join_queue', (int)$items[$index_current + 9]); $result->add('region', $items[$index_current + 10]); $result->add('pingsite', $items[$index_current + 11]); $result->add('country', $items[$index_current + 12]); // Added in R29, No docs as of yet - $result->add('quickmatch', (int) $items[$index_current + 13]); // Guessed from research + $result->add('quickmatch', (int)$items[$index_current + 13]); // Guessed from research unset($items, $index_current, $teamCount, $buffer); diff --git a/third_party/gameq/GameQ/Protocols/Bfbc2.php b/third_party/gameq/GameQ/Protocols/Bfbc2.php new file mode 100644 index 00000000..b7167a02 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Bfbc2.php @@ -0,0 +1,326 @@ +. + */ + +namespace GameQ\Protocols; + +use GameQ\Protocol; +use GameQ\Buffer; +use GameQ\Result; +use GameQ\Exception\Protocol as Exception; + +/** + * Battlefield Bad Company 2 Protocol Class + * + * NOTE: There are no qualifiers to the response packets sent back from the server as to which response packet + * belongs to which query request. For now this class assumes the responses are in the same order as the order in + * which the packets were sent to the server. If this assumption turns out to be wrong there is easy way to tell which + * response belongs to which query. Hopefully this assumption will hold true as it has in my testing. + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Bfbc2 extends Protocol +{ + + /** + * Array of packets we want to query. + * + * @type array + */ + protected $packets = [ + self::PACKET_VERSION => "\x00\x00\x00\x00\x18\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00version\x00", + self::PACKET_STATUS => "\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00", + self::PACKET_PLAYERS => "\x00\x00\x00\x00\x24\x00\x00\x00\x02\x00\x00\x00\x0b\x00\x00\x00listPlayers\x00\x03\x00\x00\x00\x61ll\x00", + ]; + + /** + * Use the response flag to figure out what method to run + * + * @type array + */ + protected $responses = [ + "processVersion", + "processDetails", + "processPlayers", + ]; + + /** + * The transport mode for this protocol is TCP + * + * @type string + */ + protected $transport = self::TRANSPORT_TCP; + + /** + * The query protocol used to make the call + * + * @type string + */ + protected $protocol = 'bfbc2'; + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'bfbc2'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Battlefield Bad Company 2"; + + /** + * The client join link + * + * @type string + */ + protected $join_link = null; + + /** + * query_port = client_port + 29321 + * 48888 = 19567 + 29321 + * + * @type int + */ + protected $port_diff = 29321; + + /** + * Normalize settings for this protocol + * + * @type array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'dedicated' => 'dedicated', + 'hostname' => 'hostname', + 'mapname' => 'map', + 'maxplayers' => 'max_players', + 'numplayers' => 'num_players', + 'password' => 'password', + ], + 'player' => [ + 'name' => 'name', + 'score' => 'score', + 'ping' => 'ping', + ], + 'team' => [ + 'score' => 'tickets', + ], + ]; + + /** + * Process the response for the StarMade server + * + * @return array + * @throws \GameQ\Exception\Protocol + */ + public function processResponse() + { + + //print_r($this->packets_response); + + // Holds the results sent back + $results = []; + + // Iterate over the response packets + // @todo: This protocol has no packet ordering, ids or anyway to identify which packet coming back belongs to which initial call. + foreach ($this->packets_response as $i => $packet) { + // Create a new buffer + $buffer = new Buffer($packet); + + // Burn first 4 bytes, same across all packets + $buffer->skip(4); + + // Get the packet length + $packetLength = $buffer->getLength(); + + // Check to make sure the expected length matches the real length + // Subtract 4 for the header burn + if ($packetLength != ($buffer->readInt32() - 4)) { + throw new Exception(__METHOD__ . " packet length does not match expected length!"); + } + + // We assume the packets are coming back in the same order as sent, this maybe incorrect... + $results = array_merge( + $results, + call_user_func_array([$this, $this->responses[$i]], [$buffer]) + ); + } + + unset($buffer, $packetLength); + + return $results; + } + + /* + * Internal Methods + */ + + /** + * Decode the buffer into a usable format + * + * @param \GameQ\Buffer $buffer + * + * @return array + */ + protected function decode(Buffer $buffer) + { + + $items = []; + + // Get the number of words in this buffer + $itemCount = $buffer->readInt32(); + + // Loop over the number of items + for ($i = 0; $i < $itemCount; $i++) { + // Length of the string + $buffer->readInt32(); + + // Just read the string + $items[$i] = $buffer->readString(); + } + + return $items; + } + + /** + * Process the server details + * + * @param \GameQ\Buffer $buffer + * + * @return array + */ + protected function processDetails(Buffer $buffer) + { + + // Decode into items + $items = $this->decode($buffer); + + // Set the result to a new result instance + $result = new Result(); + + // Server is always dedicated + $result->add('dedicated', 1); + + // These are the same no matter what mode the server is in + $result->add('hostname', $items[1]); + $result->add('num_players', (int)$items[2]); + $result->add('max_players', (int)$items[3]); + $result->add('gametype', $items[4]); + $result->add('map', $items[5]); + $result->add('roundsplayed', (int)$items[6]); + $result->add('roundstotal', (int)$items[7]); + $result->add('num_teams', (int)$items[8]); + + // Set the current index + $index_current = 9; + + // Pull the team count + $teamCount = $result->get('num_teams'); + + // Loop for the number of teams found, increment along the way + for ($id = 1; $id <= $teamCount; $id++, $index_current++) { + // Shows the tickets + $result->addTeam('tickets', $items[$index_current]); + // We add an id so we know which team this is + $result->addTeam('id', $id); + } + + // Get and set the rest of the data points. + $result->add('targetscore', (int)$items[$index_current]); + $result->add('online', 1); // Forced true, shows accepting players + $result->add('ranked', (($items[$index_current + 2] == 'true') ? 1 : 0)); + $result->add('punkbuster', (($items[$index_current + 3] == 'true') ? 1 : 0)); + $result->add('password', (($items[$index_current + 4] == 'true') ? 1 : 0)); + $result->add('uptime', (int)$items[$index_current + 5]); + $result->add('roundtime', (int)$items[$index_current + 6]); + $result->add('mod', $items[$index_current + 7]); + + $result->add('ip_port', $items[$index_current + 9]); + $result->add('punkbuster_version', $items[$index_current + 10]); + $result->add('join_queue', (($items[$index_current + 11] == 'true') ? 1 : 0)); + $result->add('region', $items[$index_current + 12]); + + unset($items, $index_current, $teamCount, $buffer); + + return $result->fetch(); + } + + /** + * Process the server version + * + * @param \GameQ\Buffer $buffer + * + * @return array + */ + protected function processVersion(Buffer $buffer) + { + // Decode into items + $items = $this->decode($buffer); + + // Set the result to a new result instance + $result = new Result(); + + $result->add('version', $items[2]); + + unset($buffer, $items); + + return $result->fetch(); + } + + /** + * Process the players + * + * @param \GameQ\Buffer $buffer + * + * @return array + */ + protected function processPlayers(Buffer $buffer) + { + + // Decode into items + $items = $this->decode($buffer); + + // Set the result to a new result instance + $result = new Result(); + + // Number of data points per player + $numTags = $items[1]; + + // Grab the tags for each player + $tags = array_slice($items, 2, $numTags); + + // Get the player count + $playerCount = $items[$numTags + 2]; + + // Iterate over the index until we run out of players + for ($i = 0, $x = $numTags + 3; $i < $playerCount; $i++, $x += $numTags) { + // Loop over the player tags and extract the info for that tag + foreach ($tags as $index => $tag) { + $result->addPlayer($tag, $items[($x + $index)]); + } + } + + return $result->fetch(); + } +} diff --git a/third_party/gameq/GameQ/Protocols/Codmw3.php b/third_party/gameq/GameQ/Protocols/Codmw3.php new file mode 100644 index 00000000..1049b602 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Codmw3.php @@ -0,0 +1,50 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Codmw3 + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Codmw3 extends Source +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'codmw3'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Call of Duty: Modern Warfare 3"; + + /** + * query_port = client_port + 2 + * + * @type int + */ + protected $port_diff = 2; +} diff --git a/third_party/gameq/GameQ/Protocols/Cs16.php b/third_party/gameq/GameQ/Protocols/Cs16.php index 0629284c..25a66029 100644 --- a/third_party/gameq/GameQ/Protocols/Cs16.php +++ b/third_party/gameq/GameQ/Protocols/Cs16.php @@ -51,7 +51,7 @@ class Cs16 extends Source * @return string * @throws \GameQ\Exception\Protocol */ - protected function processPackets($packet_id, array $packets = [ ]) + protected function processPackets($packet_id, array $packets = []) { // The response is gold source if the packets are split diff --git a/third_party/gameq/GameQ/Protocols/Ffe.php b/third_party/gameq/GameQ/Protocols/Ffe.php new file mode 100644 index 00000000..c0947bdc --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Ffe.php @@ -0,0 +1,43 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Ffe - Fortress Forever + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Ffe extends Source +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'ffe'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Fortress Forever"; +} diff --git a/third_party/gameq/GameQ/Protocols/Ffow.php b/third_party/gameq/GameQ/Protocols/Ffow.php new file mode 100644 index 00000000..00c33d47 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Ffow.php @@ -0,0 +1,243 @@ + "\xFF\xFF\xFF\xFF\x57", + self::PACKET_RULES => "\xFF\xFF\xFF\xFF\x56%s", + self::PACKET_PLAYERS => "\xFF\xFF\xFF\xFF\x55%s", + self::PACKET_INFO => "\xFF\xFF\xFF\xFF\x46\x4C\x53\x51", + ]; + + /** + * Use the response flag to figure out what method to run + * + * @type array + */ + protected $responses = [ + "\xFF\xFF\xFF\xFF\x49\x02" => 'processInfo', // I + "\xFF\xFF\xFF\xFF\x45\x00" => 'processRules', // E + "\xFF\xFF\xFF\xFF\x44\x00" => 'processPlayers', // D + ]; + + /** + * The query protocol used to make the call + * + * @type string + */ + protected $protocol = 'ffow'; + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'ffow'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Frontlines Fuel of War"; + + /** + * The client join link + * + * @type string + */ + protected $join_link = null; + + /** + * query_port = client_port + 2 + * + * @type int + */ + protected $port_diff = 2; + + /** + * Normalize settings for this protocol + * + * @type array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'gametype' => 'gamemode', + 'hostname' => 'servername', + 'mapname' => 'mapname', + 'maxplayers' => 'max_players', + 'mod' => 'modname', + 'numplayers' => 'num_players', + 'password' => 'password', + ], + // Individual + 'player' => [ + 'name' => 'name', + 'ping' => 'ping', + 'score' => 'frags', + ], + ]; + + /** + * Parse the challenge response and apply it to all the packet types + * + * @param \GameQ\Buffer $challenge_buffer + * + * @return bool + * @throws \GameQ\Exception\Protocol + */ + public function challengeParseAndApply(Buffer $challenge_buffer) + { + // Burn padding + $challenge_buffer->skip(5); + + // Apply the challenge and return + return $this->challengeApply($challenge_buffer->read(4)); + } + + /** + * Handle response from the server + * + * @return mixed + * @throws Exception + */ + public function processResponse() + { + // Init results + $results = []; + + foreach ($this->packets_response as $response) { + $buffer = new Buffer($response); + + // Figure out what packet response this is for + $response_type = $buffer->read(6); + + // Figure out which packet response this is + if (!array_key_exists($response_type, $this->responses)) { + throw new Exception(__METHOD__ . " response type '" . bin2hex($response_type) . "' is not valid"); + } + + // Now we need to call the proper method + $results = array_merge( + $results, + call_user_func_array([$this, $this->responses[$response_type]], [$buffer]) + ); + + unset($buffer); + } + + return $results; + } + + /** + * Handle processing the server information + * + * @param Buffer $buffer + * + * @return array + */ + protected function processInfo(Buffer $buffer) + { + // Set the result to a new result instance + $result = new Result(); + + $result->add('servername', $buffer->readString()); + $result->add('mapname', $buffer->readString()); + $result->add('modname', $buffer->readString()); + $result->add('gamemode', $buffer->readString()); + $result->add('description', $buffer->readString()); + $result->add('version', $buffer->readString()); + $result->add('port', $buffer->readInt16()); + $result->add('num_players', $buffer->readInt8()); + $result->add('max_players', $buffer->readInt8()); + $result->add('dedicated', $buffer->readInt8()); + $result->add('os', $buffer->readInt8()); + $result->add('password', $buffer->readInt8()); + $result->add('anticheat', $buffer->readInt8()); + $result->add('average_fps', $buffer->readInt8()); + $result->add('round', $buffer->readInt8()); + $result->add('max_rounds', $buffer->readInt8()); + $result->add('time_left', $buffer->readInt16()); + + unset($buffer); + + return $result->fetch(); + } + + /** + * Handle processing the server rules + * + * @param Buffer $buffer + * + * @return array + */ + protected function processRules(Buffer $buffer) + { + // Set the result to a new result instance + $result = new Result(); + + // Burn extra header + $buffer->skip(1); + + // Read rules until we run out of buffer + while ($buffer->getLength()) { + $key = $buffer->readString(); + // Check for map + if (strstr($key, "Map:")) { + $result->addSub("maplist", "name", $buffer->readString()); + } else // Regular rule + { + $result->add($key, $buffer->readString()); + } + } + + unset($buffer); + + return $result->fetch(); + } + + /** + * Handle processing of player data + * + * @todo: Build this out when there is a server with players to test against + * + * @param Buffer $buffer + * + * @return array + */ + protected function processPlayers(Buffer $buffer) + { + // Set the result to a new result instance + $result = new Result(); + + unset($buffer); + + return $result->fetch(); + } +} diff --git a/third_party/gameq/GameQ/Protocols/Gamespy3.php b/third_party/gameq/GameQ/Protocols/Gamespy3.php index 88c73e75..7aa5764c 100644 --- a/third_party/gameq/GameQ/Protocols/Gamespy3.php +++ b/third_party/gameq/GameQ/Protocols/Gamespy3.php @@ -107,7 +107,7 @@ class Gamespy3 extends Protocol { // Holds the processed packets - $processed = [ ]; + $processed = []; // Iterate over the packets foreach ($this->packets_response as $response) { @@ -141,8 +141,14 @@ class Gamespy3 extends Protocol // Offload cleaning up the packets if they happen to be split $packets = $this->cleanPackets(array_values($processed)); + /* + * Fix: when server name contains string "\u0000" - query fails. "\u0000" also separates properties from + * server, so we are replacing double "\u0000" in server response. + */ + $packets = preg_replace("/(\\x00){2,}gametype/", "\x00gametype", implode('', $packets)); + // Create a new buffer - $buffer = new Buffer(implode('', $packets), Buffer::NUMBER_TYPE_BIGENDIAN); + $buffer = new Buffer($packets, Buffer::NUMBER_TYPE_BIGENDIAN); // Create a new result $result = new Result(); @@ -169,7 +175,7 @@ class Gamespy3 extends Protocol * * @return array */ - protected function cleanPackets(array $packets = [ ]) + protected function cleanPackets(array $packets = []) { // Get the number of packets @@ -289,7 +295,7 @@ class Gamespy3 extends Protocol // Set the item group $item_group = 'teams'; // Set the item type, rip off any trailing stuff and bad chars - $item_type = rtrim(str_replace([ "\x00", "\x02" ], '', $item), '_t'); + $item_type = rtrim(str_replace(["\x00", "\x02"], '', $item), '_t'); } else { // We can assume it is data belonging to a previously defined item diff --git a/third_party/gameq/GameQ/Protocols/Http.php b/third_party/gameq/GameQ/Protocols/Http.php new file mode 100644 index 00000000..2a86d8d1 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Http.php @@ -0,0 +1,67 @@ +. + */ + +namespace GameQ\Protocols; + +use GameQ\Protocol; + +/** + * Class Http + * + * Generic HTTP protocol class. Useful for making http based requests + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +abstract class Http extends Protocol +{ + /** + * The query protocol used to make the call + * + * @type string + */ + protected $protocol = 'http'; + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'http'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Generic HTTP protocol"; + + /** + * Http protocol is TCP + * + * @var string + */ + protected $transport = self::TRANSPORT_TCP; + + /** + * The client join link + * + * @type string + */ + protected $join_link = null; +} diff --git a/third_party/gameq/GameQ/Protocols/Mumble.php b/third_party/gameq/GameQ/Protocols/Mumble.php index 4713ac1b..299389cf 100644 --- a/third_party/gameq/GameQ/Protocols/Mumble.php +++ b/third_party/gameq/GameQ/Protocols/Mumble.php @@ -108,7 +108,7 @@ class Mumble extends Protocol ], // Team 'team' => [ - 'name' => 'name', + 'name' => 'name', ], ]; @@ -135,7 +135,7 @@ class Mumble extends Protocol // Let's iterate over the response items, there are a lot foreach ($data as $key => $value) { // Ignore root for now, that is where all of the channel/player info is housed - if (in_array($key, [ 'root' ])) { + if (in_array($key, ['root'])) { continue; } @@ -170,7 +170,7 @@ class Mumble extends Protocol // Let's add all of the channel information foreach ($data as $key => $value) { // We will handle these later - if (in_array($key, [ 'channels', 'users' ])) { + if (in_array($key, ['channels', 'users'])) { // skip continue; } diff --git a/third_party/gameq/GameQ/Protocols/Projectrealitybf2.php b/third_party/gameq/GameQ/Protocols/Projectrealitybf2.php new file mode 100644 index 00000000..6f4b5ce0 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Projectrealitybf2.php @@ -0,0 +1,45 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Projectrealitybf2 + * + * Based off of BF2 + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Projectrealitybf2 extends Bf2 +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'projectrealitybf2'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Project Reality: Battlefield 2"; +} diff --git a/third_party/gameq/GameQ/Protocols/Quake2.php b/third_party/gameq/GameQ/Protocols/Quake2.php new file mode 100644 index 00000000..f0366c2c --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Quake2.php @@ -0,0 +1,219 @@ + "\xFF\xFF\xFF\xFFstatus\x00", + ]; + + /** + * Use the response flag to figure out what method to run + * + * @type array + */ + protected $responses = [ + "\xFF\xFF\xFF\xFF\x70\x72\x69\x6e\x74" => 'processStatus', + ]; + + /** + * The query protocol used to make the call + * + * @type string + */ + protected $protocol = 'quake2'; + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'quake2'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Quake 2 Server"; + + /** + * The client join link + * + * @type string + */ + protected $join_link = null; + + /** + * Normalize settings for this protocol + * + * @type array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'gametype' => 'gamename', + 'hostname' => 'hostname', + 'mapname' => 'mapname', + 'maxplayers' => 'maxclients', + 'mod' => 'g_gametype', + 'numplayers' => 'clients', + 'password' => 'password', + ], + // Individual + 'player' => [ + 'name' => 'name', + 'ping' => 'ping', + 'score' => 'frags', + ], + ]; + + /** + * Handle response from the server + * + * @return mixed + * @throws Exception + */ + public function processResponse() + { + // Make a buffer + $buffer = new Buffer(implode('', $this->packets_response)); + + // Grab the header + $header = $buffer->readString("\x0A"); + + // Figure out which packet response this is + if (empty($header) || !array_key_exists($header, $this->responses)) { + throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); + } + + return call_user_func_array([$this, $this->responses[$header]], [$buffer]); + } + + /** + * Process the status response + * + * @param Buffer $buffer + * + * @return array + */ + protected function processStatus(Buffer $buffer) + { + // We need to split the data and offload + $results = $this->processServerInfo(new Buffer($buffer->readString("\x0A"))); + + $results = array_merge_recursive( + $results, + $this->processPlayers(new Buffer($buffer->getBuffer())) + ); + + unset($buffer); + + // Return results + return $results; + } + + /** + * Handle processing the server information + * + * @param Buffer $buffer + * + * @return array + */ + protected function processServerInfo(Buffer $buffer) + { + // Set the result to a new result instance + $result = new Result(); + + // Burn leading \ if one exists + $buffer->readString('\\'); + + // Key / value pairs + while ($buffer->getLength()) { + // Add result + $result->add( + trim($buffer->readString('\\')), + utf8_encode(trim($buffer->readStringMulti(['\\', "\x0a"]))) + ); + } + + $result->add('password', 0); + $result->add('mod', 0); + + unset($buffer); + + return $result->fetch(); + } + + /** + * Handle processing of player data + * + * @param Buffer $buffer + * + * @return array + */ + protected function processPlayers(Buffer $buffer) + { + // Some games do not have a number of current players + $playerCount = 0; + + // Set the result to a new result instance + $result = new Result(); + + // Loop until we are out of data + while ($buffer->getLength()) { + // Make a new buffer with this block + $playerInfo = new Buffer($buffer->readString("\x0A")); + + // Add player info + $result->addPlayer('frags', $playerInfo->readString("\x20")); + $result->addPlayer('ping', $playerInfo->readString("\x20")); + + // Skip first " + $playerInfo->skip(1); + + // Add player name, encoded + $result->addPlayer('name', utf8_encode(trim(($playerInfo->readString('"'))))); + + // Skip first " + $playerInfo->skip(2); + + // Add address + $result->addPlayer('address', trim($playerInfo->readString('"'))); + + // Increment + $playerCount++; + + // Clear + unset($playerInfo); + } + + $result->add('clients', $playerCount); + + // Clear + unset($buffer, $playerCount); + + return $result->fetch(); + } +} diff --git a/third_party/gameq/GameQ/Protocols/Quake3.php b/third_party/gameq/GameQ/Protocols/Quake3.php index 6119a729..3ed177bb 100644 --- a/third_party/gameq/GameQ/Protocols/Quake3.php +++ b/third_party/gameq/GameQ/Protocols/Quake3.php @@ -104,11 +104,11 @@ class Quake3 extends Protocol $header = $buffer->readString("\x0A"); // Figure out which packet response this is - if (!array_key_exists($header, $this->responses)) { + if (empty($header) || !array_key_exists($header, $this->responses)) { throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid"); } - return call_user_func_array([$this, $this->responses[ $header ]], [$buffer]); + return call_user_func_array([$this, $this->responses[$header]], [$buffer]); } protected function processStatus(Buffer $buffer) diff --git a/third_party/gameq/GameQ/Protocols/Quakelive.php b/third_party/gameq/GameQ/Protocols/Quakelive.php new file mode 100644 index 00000000..d5df3501 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Quakelive.php @@ -0,0 +1,42 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Quake Live + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Quakelive extends Source +{ + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'quakelive'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Quake Live"; +} diff --git a/third_party/gameq/GameQ/Protocols/Samp.php b/third_party/gameq/GameQ/Protocols/Samp.php index 0392c235..443eb067 100644 --- a/third_party/gameq/GameQ/Protocols/Samp.php +++ b/third_party/gameq/GameQ/Protocols/Samp.php @@ -201,7 +201,7 @@ class Samp extends Protocol $result->add('dedicated', 1); // Pull out the server information - $result->add('password', (bool) $buffer->readInt8()); + $result->add('password', $buffer->readInt8()); $result->add('num_players', $buffer->readInt16()); $result->add('max_players', $buffer->readInt16()); diff --git a/third_party/gameq/GameQ/Protocols/Sevendaystodie.php b/third_party/gameq/GameQ/Protocols/Sevendaystodie.php new file mode 100644 index 00000000..a5ddbf2e --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Sevendaystodie.php @@ -0,0 +1,49 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class 7 Days to Die + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Sevendaystodie extends Source +{ + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'sevendaystodie'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "7 Days to Die"; + + /** + * query_port = client_port + 1 + * + * @type int + */ + protected $port_diff = 1; +} diff --git a/third_party/gameq/GameQ/Protocols/Source.php b/third_party/gameq/GameQ/Protocols/Source.php index e67a7451..e77e28f3 100644 --- a/third_party/gameq/GameQ/Protocols/Source.php +++ b/third_party/gameq/GameQ/Protocols/Source.php @@ -18,10 +18,10 @@ namespace GameQ\Protocols; -use GameQ\Protocol; use GameQ\Buffer; -use GameQ\Result; use GameQ\Exception\Protocol as Exception; +use GameQ\Protocol; +use GameQ\Result; /** * Valve Source Engine Protocol Class (A2S) @@ -155,9 +155,9 @@ class Source extends Protocol public function processResponse() { - $results = [ ]; + $results = []; - $packets = [ ]; + $packets = []; // We need to pre-sort these for split packets so we can do extra work where needed foreach ($this->packets_response as $response) { @@ -206,7 +206,7 @@ class Source extends Protocol // Now we need to call the proper method $results = array_merge( $results, - call_user_func_array([ $this, $this->responses[$response_type] ], [ $buffer ]) + call_user_func_array([$this, $this->responses[$response_type]], [$buffer]) ); unset($buffer); @@ -232,11 +232,11 @@ class Source extends Protocol * @return string * @throws \GameQ\Exception\Protocol */ - protected function processPackets($packet_id, array $packets = [ ]) + protected function processPackets($packet_id, array $packets = []) { // Init array so we can order - $packs = [ ]; + $packs = []; // We have multiple packets so we need to get them and order them foreach ($packets as $i => $packet) { @@ -267,16 +267,18 @@ class Source extends Protocol if ($packet_id & 0x80000000) { // Check to see if we have Bzip2 installed if (!function_exists('bzdecompress')) { + // @codeCoverageIgnoreStart throw new Exception( 'Bzip2 is not installed. See http://www.php.net/manual/en/book.bzip2.php for more info.', 0 ); + // @codeCoverageIgnoreEnd } // Get the length of the packet (long) $packet_length = $buffer->readInt32Signed(); - // Checksum for the decompressed packet (long) + // Checksum for the decompressed packet (long), burn it - doesnt work in split responses $buffer->readInt32Signed(); // Try to decompress @@ -284,13 +286,20 @@ class Source extends Protocol // Now verify the length if (strlen($result) != $packet_length) { + // @codeCoverageIgnoreStart throw new Exception( "Checksum for compressed packet failed! Length expected: {$packet_length}, length returned: " . strlen($result) ); + // @codeCoverageIgnoreEnd + } + + // We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop + if ($i == 0) { + $result = substr($result, 4); } } else { - // Get the packet length (short) + // Get the packet length (short), burn it $buffer->readInt16Signed(); // We need to burn the extra header (\xFF\xFF\xFF\xFF) on first loop diff --git a/third_party/gameq/GameQ/Protocols/Squad.php b/third_party/gameq/GameQ/Protocols/Squad.php new file mode 100644 index 00000000..3c021885 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Squad.php @@ -0,0 +1,53 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Squad + * + * Port reference: http://forums.joinsquad.com/topic/9559-query-ports/ + * + * @package GameQ\Protocols + * @author Austin Bischoff + */ +class Squad extends Source +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'squad'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Squad"; + + /** + * query_port = client_port + 19378 + * 27165 = 7787 + 19378 + * + * @type int + */ + protected $port_diff = 19378; +} diff --git a/third_party/gameq/GameQ/Protocols/Starmade.php b/third_party/gameq/GameQ/Protocols/Starmade.php index 6196b18a..09a033fb 100644 --- a/third_party/gameq/GameQ/Protocols/Starmade.php +++ b/third_party/gameq/GameQ/Protocols/Starmade.php @@ -162,7 +162,7 @@ class Starmade extends Protocol { // Init the parsed data array - $parsed = [ ]; + $parsed = []; // Read the number of parameters to parse $parameterSize = $buffer->readInt32Signed(); @@ -201,7 +201,7 @@ class Starmade extends Protocol // Boolean case 5: - $parsed[$i] = (bool) $buffer->readInt8Signed(); + $parsed[$i] = (bool)$buffer->readInt8Signed(); break; // 8-bit int diff --git a/third_party/gameq/GameQ/Protocols/Teamspeak3.php b/third_party/gameq/GameQ/Protocols/Teamspeak3.php index ed19e24f..c66f6a44 100644 --- a/third_party/gameq/GameQ/Protocols/Teamspeak3.php +++ b/third_party/gameq/GameQ/Protocols/Teamspeak3.php @@ -220,7 +220,7 @@ class Teamspeak3 extends Protocol { // Will hold the properties we are sending back - $properties = [ ]; + $properties = []; // All of these are split on space $items = explode(' ', $data); diff --git a/third_party/gameq/GameQ/Protocols/Terraria.php b/third_party/gameq/GameQ/Protocols/Terraria.php new file mode 100644 index 00000000..d9455ef5 --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Terraria.php @@ -0,0 +1,59 @@ +. + */ + +namespace GameQ\Protocols; + +/** + * Class Terraria + * + * @package GameQ\Protocols + * + * @author Austin Bischoff + */ +class Terraria extends Tshock +{ + + /** + * String name of this protocol class + * + * @type string + */ + protected $name = 'terraria'; + + /** + * Longer string name of this protocol class + * + * @type string + */ + protected $name_long = "Terraria"; + + /** + * query_port = client_port + 101 + * 7878 = 7777 + 101 + * + * @type int + */ + protected $port_diff = 101; + + /** + * The client join link + * + * @type string + */ + protected $join_link = "steam://connect/%s:%d/"; +} diff --git a/third_party/gameq/GameQ/Protocols/Tshock.php b/third_party/gameq/GameQ/Protocols/Tshock.php new file mode 100644 index 00000000..e97fd7fc --- /dev/null +++ b/third_party/gameq/GameQ/Protocols/Tshock.php @@ -0,0 +1,153 @@ +. + */ +namespace GameQ\Protocols; + +use GameQ\Exception\Protocol as Exception; +use GameQ\Result; + +/** + * Tshock Protocol Class + * + * Result from this call should be a header + JSON response + * + * References: + * - https://tshock.atlassian.net/wiki/display/TSHOCKPLUGINS/REST+API+Endpoints#RESTAPIEndpoints-/status + * - http://tshock.co/xf/index.php?threads/rest-tshock-server-status-image.430/ + * + * Special thanks to intradox and Ruok2bu for game & protocol references + * + * @author Austin Bischoff + */ +class Tshock extends Http +{ + /** + * Packets to send + * + * @var array + */ + protected $packets = [ + self::PACKET_STATUS => "GET /v2/server/status?players=true&rules=true HTTP/1.0\r\nAccept: */*\r\n\r\n", + ]; + + /** + * The protocol being used + * + * @var string + */ + protected $protocol = 'tshock'; + + /** + * String name of this protocol class + * + * @var string + */ + protected $name = 'tshock'; + + /** + * Longer string name of this protocol class + * + * @var string + */ + protected $name_long = "Tshock"; + + /** + * Normalize some items + * + * @var array + */ + protected $normalize = [ + // General + 'general' => [ + // target => source + 'dedicated' => 'dedicated', + 'hostname' => 'hostname', + 'mapname' => 'world', + 'maxplayers' => 'maxplayers', + 'numplayers' => 'numplayers', + 'password' => 'password', + ], + // Individual + 'player' => [ + 'name' => 'nickname', + 'team' => 'team', + ], + ]; + + /** + * Process the response + * + * @return array + * @throws Exception + */ + public function processResponse() + { + // Implode and rip out the JSON + preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); + + // Return should be JSON, let's validate + if (($json = json_decode($matches[0])) === null) { + throw new Exception("JSON response from Tshock protocol is invalid."); + } + + // Check the status response + if ($json->status != 200) { + throw new Exception("JSON status from Tshock protocol response was '{$json->status}', expected '200'."); + } + + $result = new Result(); + + // Server is always dedicated + $result->add('dedicated', 1); + + // Add server items + $result->add('hostname', $json->name); + $result->add('game_port', $json->port); + $result->add('serverversion', $json->serverversion); + $result->add('world', $json->world); + $result->add('uptime', $json->uptime); + $result->add('password', (int)$json->serverpassword); + $result->add('numplayers', $json->playercount); + $result->add('maxplayers', $json->maxplayers); + + // Parse players + foreach ($json->players as $player) { + $result->addPlayer('nickname', $player->nickname); + $result->addPlayer('username', $player->username); + $result->addPlayer('group', $player->group); + $result->addPlayer('active', (int)$player->active); + $result->addPlayer('state', $player->state); + $result->addPlayer('team', $player->team); + } + + // Make rules into simple array + $rules = []; + + // Parse rules + foreach ($json->rules as $rule => $value) { + // Add rule but convert boolean into int (0|1) + $rules[$rule] = (is_bool($value)) ? (int)$value : $value; + } + + // Add rules + $result->add('rules', $rules); + + unset($rules, $rule, $player, $value); + + return $result->fetch(); + } +} diff --git a/third_party/gameq/GameQ/Protocols/Ut3.php b/third_party/gameq/GameQ/Protocols/Ut3.php index 7b23a6a3..b55cc340 100644 --- a/third_party/gameq/GameQ/Protocols/Ut3.php +++ b/third_party/gameq/GameQ/Protocols/Ut3.php @@ -18,9 +18,6 @@ namespace GameQ\Protocols; -use GameQ\Buffer; -use GameQ\Result; - /** * Unreal Tournament 3 Protocol Class * @@ -64,7 +61,7 @@ class Ut3 extends Gamespy3 /** * Overload the response process so we can make some changes * - * @return \GameQ\Result + * @return array */ public function processResponse() { diff --git a/third_party/gameq/GameQ/Protocols/Ventrilo.php b/third_party/gameq/GameQ/Protocols/Ventrilo.php index a3a3313d..8a1e0e8e 100644 --- a/third_party/gameq/GameQ/Protocols/Ventrilo.php +++ b/third_party/gameq/GameQ/Protocols/Ventrilo.php @@ -85,7 +85,7 @@ class Ventrilo extends Protocol // General 'general' => [ 'dedicated' => 'dedicated', - 'password' => 'auth', + 'password' => 'auth', 'hostname' => 'name', 'numplayers' => 'clientcount', 'maxplayers' => 'maxclients', @@ -748,17 +748,17 @@ class Ventrilo extends Protocol * @return string * @throws \GameQ\Exception\Protocol */ - protected function decryptPackets(array $packets = [ ]) + protected function decryptPackets(array $packets = []) { // This will be returned - $decrypted = [ ]; + $decrypted = []; foreach ($packets as $packet) { # Header : $header = substr($packet, 0, 20); - $header_items = [ ]; + $header_items = []; $header_key = unpack("n1", $header); @@ -797,7 +797,7 @@ class Ventrilo extends Protocol 'totpck', 'pck', 'datakey', - 'crc' + 'crc', ], $header_items); // Check to make sure the number of packets match diff --git a/third_party/gameq/GameQ/Query/Native.php b/third_party/gameq/GameQ/Query/Native.php index dda9464f..ff347805 100644 --- a/third_party/gameq/GameQ/Query/Native.php +++ b/third_party/gameq/GameQ/Query/Native.php @@ -55,13 +55,17 @@ class Native extends Core public function write($data) { - // No socket for this server, make one - if (is_null($this->socket)) { - $this->create(); - } + try { + // No socket for this server, make one + if (is_null($this->socket)) { + $this->create(); + } - // Send the packet - return fwrite($this->socket, $data); + // Send the packet + return fwrite($this->socket, $data); + } catch (\Exception $e) { + throw new Exception($e->getMessage(), $e->getCode(), $e); + } } /** @@ -138,10 +142,10 @@ class Native extends Core $loop_active = true; // Will hold the responses read from the sockets - $responses = [ ]; + $responses = []; // To store the sockets - $sockets_tmp = [ ]; + $sockets_tmp = []; // Loop and pull out all the actual sockets we need to listen on foreach ($sockets as $socket_id => $socket_data) { @@ -150,7 +154,7 @@ class Native extends Core $socket = $socket_data['socket']; // Append the actual socket we are listening to - $sockets_tmp[ $socket_id ] = $socket->get(); + $sockets_tmp[$socket_id] = $socket->get(); unset($socket); } @@ -195,12 +199,12 @@ class Native extends Core // Check to see if the response is empty, if so we are done with this server if (strlen($response) == 0) { // Remove this server from any future read loops - unset($sockets_tmp[ (int) $socket ]); + unset($sockets_tmp[(int)$socket]); continue; } // Add the response we got back - $responses[ (int) $socket ][] = $response; + $responses[(int)$socket][] = $response; } // Because stream_select modifies read we need to reset it each time to the original array of sockets diff --git a/third_party/gameq/GameQ/Result.php b/third_party/gameq/GameQ/Result.php index a0343b32..1fe33d89 100644 --- a/third_party/gameq/GameQ/Result.php +++ b/third_party/gameq/GameQ/Result.php @@ -32,7 +32,7 @@ class Result * * @var array */ - protected $result = [ ]; + protected $result = []; /** * Adds variable to results @@ -82,7 +82,7 @@ class Result // Nothing of this type yet, set an empty array if (!isset($this->result[$sub]) or !is_array($this->result[$sub])) { - $this->result[$sub] = [ ]; + $this->result[$sub] = []; } // Find the first entry that doesn't have this variable