Gamespy2.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. namespace GameQ\Protocols;
  19. use GameQ\Exception\Protocol as Exception;
  20. use GameQ\Protocol;
  21. use GameQ\Buffer;
  22. use GameQ\Result;
  23. /**
  24. * GameSpy2 Protocol class
  25. *
  26. * Given the ability for non utf-8 characters to be used as hostnames, player names, etc... this
  27. * version returns all strings utf-8 encoded (utf8_encode). To access the proper version of a
  28. * string response you must use utf8_decode() on the specific response.
  29. *
  30. * @author Austin Bischoff <[email protected]>
  31. */
  32. class Gamespy2 extends Protocol
  33. {
  34. /**
  35. * Define the state of this class
  36. *
  37. * @type int
  38. */
  39. protected $state = self::STATE_BETA;
  40. /**
  41. * Array of packets we want to look up.
  42. * Each key should correspond to a defined method in this or a parent class
  43. *
  44. * @type array
  45. */
  46. protected $packets = [
  47. self::PACKET_DETAILS => "\xFE\xFD\x00\x43\x4F\x52\x59\xFF\x00\x00",
  48. self::PACKET_PLAYERS => "\xFE\xFD\x00\x43\x4F\x52\x58\x00\xFF\xFF",
  49. ];
  50. /**
  51. * Use the response flag to figure out what method to run
  52. *
  53. * @type array
  54. */
  55. protected $responses = [
  56. "\x00\x43\x4F\x52\x59" => "processDetails",
  57. "\x00\x43\x4F\x52\x58" => "processPlayers",
  58. ];
  59. /**
  60. * The query protocol used to make the call
  61. *
  62. * @type string
  63. */
  64. protected $protocol = 'gamespy2';
  65. /**
  66. * String name of this protocol class
  67. *
  68. * @type string
  69. */
  70. protected $name = 'gamespy2';
  71. /**
  72. * Longer string name of this protocol class
  73. *
  74. * @type string
  75. */
  76. protected $name_long = "GameSpy2 Server";
  77. /**
  78. * The client join link
  79. *
  80. * @type string
  81. */
  82. protected $join_link = null;
  83. /**
  84. * Normalize settings for this protocol
  85. *
  86. * @type array
  87. */
  88. protected $normalize = [
  89. // General
  90. 'general' => [
  91. // target => source
  92. 'dedicated' => 'dedicated',
  93. 'gametype' => 'gametype',
  94. 'hostname' => 'hostname',
  95. 'mapname' => 'mapname',
  96. 'maxplayers' => 'maxplayers',
  97. 'mod' => 'mod',
  98. 'numplayers' => 'numplayers',
  99. 'password' => 'password',
  100. ],
  101. ];
  102. /**
  103. * Process the response
  104. *
  105. * @return array
  106. * @throws Exception
  107. */
  108. public function processResponse()
  109. {
  110. // Will hold the packets after sorting
  111. $packets = [];
  112. // We need to pre-sort these for split packets so we can do extra work where needed
  113. foreach ($this->packets_response as $response) {
  114. $buffer = new Buffer($response);
  115. // Pull out the header
  116. $header = $buffer->read(5);
  117. // Add the packet to the proper section, we will combine later
  118. $packets[$header][] = $buffer->getBuffer();
  119. }
  120. unset($buffer);
  121. $results = [];
  122. // Now let's iterate and process
  123. foreach ($packets as $header => $packetGroup) {
  124. // Figure out which packet response this is
  125. if (!array_key_exists($header, $this->responses)) {
  126. throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
  127. }
  128. // Now we need to call the proper method
  129. $results = array_merge(
  130. $results,
  131. call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))])
  132. );
  133. }
  134. unset($packets);
  135. return $results;
  136. }
  137. /*
  138. * Internal methods
  139. */
  140. /**
  141. * Handles processing the details data into a usable format
  142. *
  143. * @param \GameQ\Buffer $buffer
  144. *
  145. * @return array
  146. * @throws Exception
  147. */
  148. protected function processDetails(Buffer $buffer)
  149. {
  150. // Set the result to a new result instance
  151. $result = new Result();
  152. // We go until we hit an empty key
  153. while ($buffer->getLength()) {
  154. $key = $buffer->readString();
  155. if (strlen($key) == 0) {
  156. break;
  157. }
  158. $result->add($key, utf8_encode($buffer->readString()));
  159. }
  160. unset($buffer);
  161. return $result->fetch();
  162. }
  163. /**
  164. * Handles processing the players data into a usable format
  165. *
  166. * @param \GameQ\Buffer $buffer
  167. *
  168. * @return array
  169. * @throws Exception
  170. */
  171. protected function processPlayers(Buffer $buffer)
  172. {
  173. // Set the result to a new result instance
  174. $result = new Result();
  175. // Skip the header
  176. $buffer->skip(1);
  177. // Players are first
  178. $this->parsePlayerTeam('players', $buffer, $result);
  179. // Teams are next
  180. $this->parsePlayerTeam('teams', $buffer, $result);
  181. unset($buffer);
  182. return $result->fetch();
  183. }
  184. /**
  185. * Parse the player/team info returned from the player call
  186. *
  187. * @param string $dataType
  188. * @param \GameQ\Buffer $buffer
  189. * @param \GameQ\Result $result
  190. *
  191. * @throws Exception
  192. */
  193. protected function parsePlayerTeam($dataType, Buffer &$buffer, Result &$result)
  194. {
  195. // Do count
  196. $result->add('num_' . $dataType, $buffer->readInt8());
  197. // Variable names
  198. $varNames = [];
  199. // Loop until we run out of length
  200. while ($buffer->getLength()) {
  201. $varNames[] = str_replace('_', '', $buffer->readString());
  202. if ($buffer->lookAhead() === "\x00") {
  203. $buffer->skip();
  204. break;
  205. }
  206. }
  207. // Check if there are any value entries
  208. if ($buffer->lookAhead() == "\x00") {
  209. $buffer->skip();
  210. return;
  211. }
  212. // Get the values
  213. while ($buffer->getLength() > 4) {
  214. foreach ($varNames as $varName) {
  215. $result->addSub($dataType, utf8_encode($varName), utf8_encode($buffer->readString()));
  216. }
  217. if ($buffer->lookAhead() === "\x00") {
  218. $buffer->skip();
  219. break;
  220. }
  221. }
  222. return;
  223. }
  224. }