Unreal2.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Protocol;
  20. use GameQ\Buffer;
  21. use GameQ\Result;
  22. use GameQ\Exception\Protocol as Exception;
  23. /**
  24. * Unreal 2 Protocol class
  25. *
  26. * @author Austin Bischoff <[email protected]>
  27. */
  28. class Unreal2 extends Protocol
  29. {
  30. /**
  31. * Array of packets we want to look up.
  32. * Each key should correspond to a defined method in this or a parent class
  33. *
  34. * @type array
  35. */
  36. protected $packets = [
  37. self::PACKET_DETAILS => "\x79\x00\x00\x00\x00",
  38. self::PACKET_RULES => "\x79\x00\x00\x00\x01",
  39. self::PACKET_PLAYERS => "\x79\x00\x00\x00\x02",
  40. ];
  41. /**
  42. * Use the response flag to figure out what method to run
  43. *
  44. * @type array
  45. */
  46. protected $responses = [
  47. "\x80\x00\x00\x00\x00" => "processDetails", // 0
  48. "\x80\x00\x00\x00\x01" => "processRules", // 1
  49. "\x80\x00\x00\x00\x02" => "processPlayers", // 2
  50. ];
  51. /**
  52. * The query protocol used to make the call
  53. *
  54. * @type string
  55. */
  56. protected $protocol = 'unreal2';
  57. /**
  58. * String name of this protocol class
  59. *
  60. * @type string
  61. */
  62. protected $name = 'unreal2';
  63. /**
  64. * Longer string name of this protocol class
  65. *
  66. * @type string
  67. */
  68. protected $name_long = "Unreal 2";
  69. /**
  70. * Normalize settings for this protocol
  71. *
  72. * @type array
  73. */
  74. protected $normalize = [
  75. // General
  76. 'general' => [
  77. // target => source
  78. 'dedicated' => 'ServerMode',
  79. 'gametype' => 'gametype',
  80. 'hostname' => 'servername',
  81. 'mapname' => 'mapname',
  82. 'maxplayers' => 'maxplayers',
  83. 'numplayers' => 'numplayers',
  84. 'password' => 'password',
  85. ],
  86. // Individual
  87. 'player' => [
  88. 'name' => 'name',
  89. 'score' => 'score',
  90. ],
  91. ];
  92. /**
  93. * Process the response
  94. *
  95. * @return array
  96. * @throws \GameQ\Exception\Protocol
  97. */
  98. public function processResponse()
  99. {
  100. // Will hold the packets after sorting
  101. $packets = [];
  102. // We need to pre-sort these for split packets so we can do extra work where needed
  103. foreach ($this->packets_response as $response) {
  104. $buffer = new Buffer($response);
  105. // Pull out the header
  106. $header = $buffer->read(5);
  107. // Add the packet to the proper section, we will combine later
  108. $packets[$header][] = $buffer->getBuffer();
  109. }
  110. unset($buffer);
  111. $results = [];
  112. // Now let's iterate and process
  113. foreach ($packets as $header => $packetGroup) {
  114. // Figure out which packet response this is
  115. if (!array_key_exists($header, $this->responses)) {
  116. throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
  117. }
  118. // Now we need to call the proper method
  119. $results = array_merge(
  120. $results,
  121. call_user_func_array([$this, $this->responses[$header]], [new Buffer(implode($packetGroup))])
  122. );
  123. }
  124. unset($packets);
  125. return $results;
  126. }
  127. /*
  128. * Internal methods
  129. */
  130. /**
  131. * Handles processing the details data into a usable format
  132. *
  133. * @param \GameQ\Buffer $buffer
  134. *
  135. * @return mixed
  136. * @throws \GameQ\Exception\Protocol
  137. */
  138. protected function processDetails(Buffer $buffer)
  139. {
  140. // Set the result to a new result instance
  141. $result = new Result();
  142. $result->add('serverid', $buffer->readInt32()); // 0
  143. $result->add('serverip', $buffer->readPascalString(1)); // empty
  144. $result->add('gameport', $buffer->readInt32());
  145. $result->add('queryport', $buffer->readInt32()); // 0
  146. $result->add('servername', utf8_encode($buffer->readPascalString(1)));
  147. $result->add('mapname', utf8_encode($buffer->readPascalString(1)));
  148. $result->add('gametype', $buffer->readPascalString(1));
  149. $result->add('numplayers', $buffer->readInt32());
  150. $result->add('maxplayers', $buffer->readInt32());
  151. $result->add('ping', $buffer->readInt32()); // 0
  152. unset($buffer);
  153. return $result->fetch();
  154. }
  155. /**
  156. * Handles processing the player data into a usable format
  157. *
  158. * @param \GameQ\Buffer $buffer
  159. *
  160. * @return mixed
  161. */
  162. protected function processPlayers(Buffer $buffer)
  163. {
  164. // Set the result to a new result instance
  165. $result = new Result();
  166. // Parse players
  167. while ($buffer->getLength()) {
  168. // Player id
  169. if (($id = $buffer->readInt32()) !== 0) {
  170. // Add the results
  171. $result->addPlayer('id', $id);
  172. $result->addPlayer('name', utf8_encode($buffer->readPascalString(1)));
  173. $result->addPlayer('ping', $buffer->readInt32());
  174. $result->addPlayer('score', $buffer->readInt32());
  175. // Skip the next 4, unsure what they are for
  176. $buffer->skip(4);
  177. }
  178. }
  179. unset($buffer, $id);
  180. return $result->fetch();
  181. }
  182. /**
  183. * Handles processing the rules data into a usable format
  184. *
  185. * @param \GameQ\Buffer $buffer
  186. *
  187. * @return mixed
  188. */
  189. protected function processRules(Buffer $buffer)
  190. {
  191. // Set the result to a new result instance
  192. $result = new Result();
  193. // Named values
  194. $inc = -1;
  195. while ($buffer->getLength()) {
  196. // Grab the key
  197. $key = $buffer->readPascalString(1);
  198. // Make sure mutators don't overwrite each other
  199. if ($key === 'Mutator') {
  200. $key .= ++$inc;
  201. }
  202. $result->add(strtolower($key), utf8_encode($buffer->readPascalString(1)));
  203. }
  204. unset($buffer);
  205. return $result->fetch();
  206. }
  207. }