Advertisement
Guest User

Untitled

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