Samp.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\Server;
  23. use GameQ\Exception\Protocol as Exception;
  24. /**
  25. * San Andreas Multiplayer Protocol Class (samp)
  26. *
  27. * Note:
  28. * Player information will not be returned if player count is over 256
  29. *
  30. * @author Austin Bischoff <[email protected]>
  31. */
  32. class Samp extends Protocol
  33. {
  34. /**
  35. * Array of packets we want to look up.
  36. * Each key should correspond to a defined method in this or a parent class
  37. *
  38. * @type array
  39. */
  40. protected $packets = [
  41. self::PACKET_STATUS => "SAMP%si",
  42. self::PACKET_PLAYERS => "SAMP%sd",
  43. self::PACKET_RULES => "SAMP%sr",
  44. ];
  45. /**
  46. * Use the response flag to figure out what method to run
  47. *
  48. * @type array
  49. */
  50. protected $responses = [
  51. "\x69" => "processStatus", // i
  52. "\x64" => "processPlayers", // d
  53. "\x72" => "processRules", // r
  54. ];
  55. /**
  56. * The query protocol used to make the call
  57. *
  58. * @type string
  59. */
  60. protected $protocol = 'samp';
  61. /**
  62. * String name of this protocol class
  63. *
  64. * @type string
  65. */
  66. protected $name = 'samp';
  67. /**
  68. * Longer string name of this protocol class
  69. *
  70. * @type string
  71. */
  72. protected $name_long = "San Andreas Multiplayer";
  73. /**
  74. * Holds the calculated server code that is passed when querying for information
  75. *
  76. * @type string
  77. */
  78. protected $server_code = null;
  79. /**
  80. * The client join link
  81. *
  82. * @type string
  83. */
  84. protected $join_link = "samp://%s:%d/";
  85. /**
  86. * Normalize settings for this protocol
  87. *
  88. * @type array
  89. */
  90. protected $normalize = [
  91. // General
  92. 'general' => [
  93. // target => source
  94. 'dedicated' => 'dedicated',
  95. 'hostname' => ['hostname', 'servername'],
  96. 'mapname' => 'mapname',
  97. 'maxplayers' => 'max_players',
  98. 'numplayers' => 'num_players',
  99. 'password' => 'password',
  100. ],
  101. // Individual
  102. 'player' => [
  103. 'name' => 'name',
  104. 'score' => 'score',
  105. 'ping' => 'ping',
  106. ],
  107. ];
  108. /**
  109. * Handle some work before sending the packets out to the server
  110. *
  111. * @param \GameQ\Server $server
  112. */
  113. public function beforeSend(Server $server)
  114. {
  115. // Build the server code
  116. $this->server_code = implode('', array_map('chr', explode('.', $server->ip()))) .
  117. pack("S", $server->portClient());
  118. // Loop over the packets and update them
  119. foreach ($this->packets as $packetType => $packet) {
  120. // Fill out the packet with the server info
  121. $this->packets[$packetType] = sprintf($packet, $this->server_code);
  122. }
  123. }
  124. /**
  125. * Process the response
  126. *
  127. * @return array
  128. * @throws \GameQ\Exception\Protocol
  129. */
  130. public function processResponse()
  131. {
  132. // Results that will be returned
  133. $results = [];
  134. // Get the length of the server code so we can figure out how much to read later
  135. $serverCodeLength = strlen($this->server_code);
  136. // We need to pre-sort these for split packets so we can do extra work where needed
  137. foreach ($this->packets_response as $response) {
  138. // Make new buffer
  139. $buffer = new Buffer($response);
  140. // Check the header, should be SAMP
  141. if (($header = $buffer->read(4)) !== 'SAMP') {
  142. throw new Exception(__METHOD__ . " header response '{$header}' is not valid");
  143. }
  144. // Check to make sure the server response code matches what we sent
  145. if ($buffer->read($serverCodeLength) !== $this->server_code) {
  146. throw new Exception(__METHOD__ . " code check failed.");
  147. }
  148. // Figure out what packet response this is for
  149. $response_type = $buffer->read(1);
  150. // Figure out which packet response this is
  151. if (!array_key_exists($response_type, $this->responses)) {
  152. throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
  153. }
  154. // Now we need to call the proper method
  155. $results = array_merge(
  156. $results,
  157. call_user_func_array([$this, $this->responses[$response_type]], [$buffer])
  158. );
  159. unset($buffer);
  160. }
  161. return $results;
  162. }
  163. /*
  164. * Internal methods
  165. */
  166. /**
  167. * Handles processing the server status data
  168. *
  169. * @param \GameQ\Buffer $buffer
  170. *
  171. * @return array
  172. * @throws \GameQ\Exception\Protocol
  173. */
  174. protected function processStatus(Buffer $buffer)
  175. {
  176. // Set the result to a new result instance
  177. $result = new Result();
  178. // Always dedicated
  179. $result->add('dedicated', 1);
  180. // Pull out the server information
  181. $result->add('password', $buffer->readInt8());
  182. $result->add('num_players', $buffer->readInt16());
  183. $result->add('max_players', $buffer->readInt16());
  184. // These are read differently for these last 3
  185. $result->add('servername', utf8_encode($buffer->read($buffer->readInt32())));
  186. $result->add('gametype', $buffer->read($buffer->readInt32()));
  187. $result->add('language', $buffer->read($buffer->readInt32()));
  188. unset($buffer);
  189. return $result->fetch();
  190. }
  191. /**
  192. * Handles processing the player data into a usable format
  193. *
  194. * @param \GameQ\Buffer $buffer
  195. *
  196. * @return array
  197. */
  198. protected function processPlayers(Buffer $buffer)
  199. {
  200. // Set the result to a new result instance
  201. $result = new Result();
  202. // Number of players
  203. $result->add('num_players', $buffer->readInt16());
  204. // Run until we run out of buffer
  205. while ($buffer->getLength()) {
  206. $result->addPlayer('id', $buffer->readInt8());
  207. $result->addPlayer('name', utf8_encode($buffer->readPascalString()));
  208. $result->addPlayer('score', $buffer->readInt32());
  209. $result->addPlayer('ping', $buffer->readInt32());
  210. }
  211. unset($buffer);
  212. return $result->fetch();
  213. }
  214. /**
  215. * Handles processing the rules data into a usable format
  216. *
  217. * @param \GameQ\Buffer $buffer
  218. *
  219. * @return array
  220. */
  221. protected function processRules(Buffer $buffer)
  222. {
  223. // Set the result to a new result instance
  224. $result = new Result();
  225. // Number of rules
  226. $result->add('num_rules', $buffer->readInt16());
  227. // Run until we run out of buffer
  228. while ($buffer->getLength()) {
  229. $result->add($buffer->readPascalString(), $buffer->readPascalString());
  230. }
  231. unset($buffer);
  232. return $result->fetch();
  233. }
  234. }