M2mp.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * Mafia 2 Multiplayer Protocol Class
  25. *
  26. * Loosely based on SAMP protocol
  27. *
  28. * Query port = server port + 1
  29. *
  30. * Handles processing Mafia 2 Multiplayer servers
  31. *
  32. * @package GameQ\Protocols
  33. * @author Wilson Jesus <>
  34. */
  35. class M2mp extends Protocol
  36. {
  37. /**
  38. * Array of packets we want to look up.
  39. * Each key should correspond to a defined method in this or a parent class
  40. *
  41. * @type array
  42. */
  43. protected $packets = [
  44. self::PACKET_ALL => "M2MP",
  45. ];
  46. /**
  47. * Use the response flag to figure out what method to run
  48. *
  49. * @type array
  50. */
  51. protected $responses = [
  52. "M2MP" => 'processStatus',
  53. ];
  54. /**
  55. * The query protocol used to make the call
  56. *
  57. * @type string
  58. */
  59. protected $protocol = 'm2mp';
  60. /**
  61. * String name of this protocol class
  62. *
  63. * @type string
  64. */
  65. protected $name = 'm2mp';
  66. /**
  67. * Longer string name of this protocol class
  68. *
  69. * @type string
  70. */
  71. protected $name_long = "Mafia 2 Multiplayer";
  72. /**
  73. * The client join link
  74. *
  75. * @type string
  76. */
  77. protected $join_link = null;
  78. /**
  79. * The difference between the client port and query port
  80. *
  81. * @type int
  82. */
  83. protected $port_diff = 1;
  84. /**
  85. * Normalize settings for this protocol
  86. *
  87. * @type array
  88. */
  89. protected $normalize = [
  90. // General
  91. 'general' => [
  92. // target => source
  93. 'hostname' => 'servername',
  94. 'gametype' => 'gamemode',
  95. 'maxplayers' => 'max_players',
  96. 'numplayers' => 'num_players',
  97. 'password' => 'password',
  98. ],
  99. // Individual
  100. 'player' => [
  101. 'name' => 'name',
  102. ],
  103. ];
  104. /**
  105. * Handle response from the server
  106. *
  107. * @return mixed
  108. * @throws Exception
  109. */
  110. public function processResponse()
  111. {
  112. // Make a buffer
  113. $buffer = new Buffer(implode('', $this->packets_response));
  114. // Grab the header
  115. $header = $buffer->read(4);
  116. // Header
  117. // Figure out which packet response this is
  118. if ($header != "M2MP") {
  119. throw new Exception(__METHOD__ . " response type '" . bin2hex($header) . "' is not valid");
  120. }
  121. return call_user_func_array([$this, $this->responses[$header]], [$buffer]);
  122. }
  123. /**
  124. * Process the status response
  125. *
  126. * @param Buffer $buffer
  127. *
  128. * @return array
  129. */
  130. protected function processStatus(Buffer $buffer)
  131. {
  132. // We need to split the data and offload
  133. $results = $this->processServerInfo($buffer);
  134. $results = array_merge_recursive(
  135. $results,
  136. $this->processPlayers($buffer)
  137. );
  138. unset($buffer);
  139. // Return results
  140. return $results;
  141. }
  142. /**
  143. * Handle processing the server information
  144. *
  145. * @param Buffer $buffer
  146. *
  147. * @return array
  148. */
  149. protected function processServerInfo(Buffer $buffer)
  150. {
  151. // Set the result to a new result instance
  152. $result = new Result();
  153. // Always dedicated
  154. $result->add('dedicated', 1);
  155. // Pull out the server information
  156. // Note the length information is incorrect, we correct using offset options in pascal method
  157. $result->add('servername', $buffer->readPascalString(1, true));
  158. $result->add('num_players', $buffer->readPascalString(1, true));
  159. $result->add('max_players', $buffer->readPascalString(1, true));
  160. $result->add('gamemode', $buffer->readPascalString(1, true));
  161. $result->add('password', (bool) $buffer->readInt8());
  162. unset($buffer);
  163. return $result->fetch();
  164. }
  165. /**
  166. * Handle processing of player data
  167. *
  168. * @param Buffer $buffer
  169. *
  170. * @return array
  171. */
  172. protected function processPlayers(Buffer $buffer)
  173. {
  174. // Set the result to a new result instance
  175. $result = new Result();
  176. // Parse players
  177. // Read the player info, it's in the same query response for some odd reason.
  178. while ($buffer->getLength()) {
  179. // Check to see if we ran out of info, length bug from response
  180. if ($buffer->getLength() <= 1) {
  181. break;
  182. }
  183. // Only player name information is available
  184. // Add player name, encoded
  185. $result->addPlayer('name', utf8_encode(trim($buffer->readPascalString(1, true))));
  186. }
  187. // Clear
  188. unset($buffer);
  189. return $result->fetch();
  190. }
  191. }