Cfx.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\Buffer;
  20. use GameQ\Exception\Protocol as Exception;
  21. use GameQ\Protocol;
  22. use GameQ\Result;
  23. use GameQ\Server;
  24. use GameQ\Protocols\Http;
  25. /**
  26. * GTA Five M Protocol Class
  27. *
  28. * Server base can be found at https://fivem.net/
  29. *
  30. * Based on code found at https://github.com/LiquidObsidian/fivereborn-query
  31. *
  32. * @author Austin Bischoff <[email protected]>
  33. *
  34. * Adding FiveM Player List by
  35. * @author Jesse Lukas <[email protected]>
  36. */
  37. class Cfx extends Protocol
  38. {
  39. /**
  40. * Array of packets we want to look up.
  41. * Each key should correspond to a defined method in this or a parent class
  42. *
  43. * @type array
  44. */
  45. protected $packets = [
  46. self::PACKET_STATUS => "\xFF\xFF\xFF\xFFgetinfo xxx",
  47. ];
  48. /**
  49. * Use the response flag to figure out what method to run
  50. *
  51. * @type array
  52. */
  53. protected $responses = [
  54. "\xFF\xFF\xFF\xFFinfoResponse" => "processStatus",
  55. ];
  56. /**
  57. * The query protocol used to make the call
  58. *
  59. * @type string
  60. */
  61. protected $protocol = 'cfx';
  62. /**
  63. * String name of this protocol class
  64. *
  65. * @type string
  66. */
  67. protected $name = 'cfx';
  68. /**
  69. * Longer string name of this protocol class
  70. *
  71. * @type string
  72. */
  73. protected $name_long = "CitizenFX";
  74. /**
  75. * Holds the Player list so we can overwrite it back
  76. *
  77. * @var string
  78. */
  79. protected $PlayerList = [];
  80. /**
  81. * Normalize settings for this protocol
  82. *
  83. * @type array
  84. */
  85. protected $normalize = [
  86. // General
  87. 'general' => [
  88. // target => source
  89. 'gametype' => 'gametype',
  90. 'hostname' => 'hostname',
  91. 'mapname' => 'mapname',
  92. 'maxplayers' => 'sv_maxclients',
  93. 'mod' => 'gamename',
  94. 'numplayers' => 'clients',
  95. 'password' => 'privateClients',
  96. ],
  97. ];
  98. /**
  99. * Get FiveM players list using a sub query
  100. */
  101. public function beforeSend(Server $server)
  102. {
  103. $GameQ = new \GameQ\GameQ();
  104. $GameQ->addServer([
  105. 'type' => 'cfxplayers',
  106. 'host' => "$server->ip:$server->port_query",
  107. ]);
  108. $results = $GameQ->process();
  109. $this->PlayerList = isset($results[0]) && isset($results[0][0]) ? $results[0][0] : [];
  110. }
  111. /**
  112. * Process the response
  113. *
  114. * @return array
  115. * @throws \GameQ\Exception\Protocol
  116. */
  117. public function processResponse()
  118. {
  119. // In case it comes back as multiple packets (it shouldn't)
  120. $buffer = new Buffer(implode('', $this->packets_response));
  121. // Figure out what packet response this is for
  122. $response_type = $buffer->readString(PHP_EOL);
  123. // Figure out which packet response this is
  124. if (empty($response_type) || !array_key_exists($response_type, $this->responses)) {
  125. throw new Exception(__METHOD__ . " response type '{$response_type}' is not valid");
  126. }
  127. // Offload the call
  128. $results = call_user_func_array([$this, $this->responses[$response_type]], [$buffer]);
  129. return $results;
  130. }
  131. /*
  132. * Internal methods
  133. */
  134. /**
  135. * Handle processing the status response
  136. *
  137. * @param Buffer $buffer
  138. *
  139. * @return array
  140. */
  141. protected function processStatus(Buffer $buffer)
  142. {
  143. // Set the result to a new result instance
  144. $result = new Result();
  145. // Lets peek and see if the data starts with a \
  146. if ($buffer->lookAhead(1) == '\\') {
  147. // Burn the first one
  148. $buffer->skip(1);
  149. }
  150. // Explode the data
  151. $data = explode('\\', $buffer->getBuffer());
  152. // No longer needed
  153. unset($buffer);
  154. $itemCount = count($data);
  155. // Now lets loop the array
  156. for ($x = 0; $x < $itemCount; $x += 2) {
  157. // Set some local vars
  158. $key = $data[$x];
  159. $val = $data[$x + 1];
  160. if (in_array($key, ['challenge'])) {
  161. continue; // skip
  162. }
  163. // Regular variable so just add the value.
  164. $result->add($key, $val);
  165. }
  166. // Add result of sub http-protocol if available
  167. if ($this->PlayerList) {
  168. $result->add('players', $this->PlayerList);
  169. }
  170. return $result->fetch();
  171. }
  172. }