Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.42 KB | None | 0 0
  1. #pragma semicolon 1
  2.  
  3. #include <amxmodx>
  4. #include <reapi>
  5. #include <grip>
  6. #include <PersistentDataStorage>
  7. #include <gmx>
  8. #tryinclude <uac>
  9.  
  10. #define CHECK_NATIVE_ARGS_NUM(%1,%2,%3) \
  11. if (%1 < %2) { \
  12. log_error(AMX_ERR_NATIVE, "Invalid num of arguments %d. Expected %d", %1, %2); \
  13. return %3; \
  14. }
  15.  
  16. #define CHECK_NATIVE_PLAYER(%1,%2) \
  17. if (!is_user_connected(%1)) { \
  18. log_error(AMX_ERR_NATIVE, "Invalid player %d", %1); \
  19. return %2; \
  20. }
  21.  
  22. #define CHECK_NATIVE_PLAYER_LOADED(%1,%2) \
  23. if (Players[%1][PlayerStatus] != STATUS_LOADED) { \
  24. log_error(AMX_ERR_NATIVE, "Player %d not loaded", %1); \
  25. return %2; \
  26. }
  27.  
  28. #define CHECK_PLAYER_STATUS(%1,%2) (Players[%1][PlayerStatus] == %2)
  29.  
  30. const Float:CMD_DELAY = 0.8;
  31. const ATTEMPTS_COUNT = 3;
  32.  
  33. enum FWD {
  34. FWD_Init,
  35. FWD_Loading,
  36. FWD_Loaded,
  37. FWD_Disconnecting,
  38. // FWD_Disconnected,
  39. }
  40.  
  41. new Forwards[FWD], FReturn;
  42.  
  43. enum FUNC {
  44. FnOnConnected,
  45. FnOnAssigned,
  46. FnOnInfoResponse,
  47. }
  48.  
  49. new Functions[FUNC];
  50.  
  51. enum _:REQUEST {
  52. RequestPluginId,
  53. RequestFuncId,
  54. RequestParam,
  55. };
  56.  
  57. new PluginId, bool:ApiEnabled = true, LogFile;
  58. new Token[65], Url[256], GmxLogLevel:LogLvl;
  59. new GripRequestOptions:RequestOptions = Empty_GripRequestOptions;
  60. new Array:Requests = Invalid_Array, Request[REQUEST];
  61.  
  62. #if defined _uac_included
  63. new bool:UAC_IsLoaded = false;
  64. #endif
  65.  
  66. enum {
  67. STATUS_NONE = 0,
  68. STATUS_WAITING,
  69. STATUS_LOADING,
  70. STATUS_LOADED,
  71. };
  72.  
  73. enum _:SERVER {
  74. ServerID,
  75. ServerTime,
  76. ServerTimeDiff
  77. };
  78.  
  79. enum _:PLAYER {
  80. PlayerStatus,
  81. PlayerId,
  82. PlayerUserId,
  83. PlayerSessionId,
  84. PlayerSteamId[32],
  85. Float:PlayerFloodTime,
  86. PlayerAttemptsCount
  87. };
  88.  
  89. new ServerData[SERVER];
  90. new Players[MAX_PLAYERS + 1][PLAYER];
  91.  
  92. // Begin forwards
  93. public plugin_precache() {
  94. PluginId = register_plugin("GMX Core", GMX_VERSION_STR, "GM-X Team");
  95.  
  96. new path[128];
  97. get_localinfo("amxx_logs", path, charsmax(path));
  98. add(path, charsmax(path), "/gmx");
  99. if (!dir_exists(path)) {
  100. mkdir(path);
  101. }
  102.  
  103. add(path, charsmax(path), "/L%Y%m%d.log");
  104. format_time(path, charsmax(path), path);
  105. LogFile = fopen(path, "a");
  106. if (!LogFile) {
  107. set_fail_state("Could not open %s for write", path);
  108. }
  109.  
  110. loadConfig();
  111. }
  112.  
  113. public plugin_init() {
  114. register_concmd("gmx_reloadcfg", "CmdReloadConfig", ADMIN_RCON);
  115. register_clcmd("gmx_assign", "CmdAssign");
  116.  
  117. RegisterHookChain(RH_SV_DropClient, "SV_DropClient_Post", true);
  118.  
  119. Functions[FnOnConnected] = get_func_id("OnConnected");
  120. Functions[FnOnAssigned] = get_func_id("OnAssigned");
  121. Functions[FnOnInfoResponse] = get_func_id("OnInfoResponse");
  122.  
  123. Forwards[FWD_Init] = CreateMultiForward("GMX_Init", ET_IGNORE);
  124. Forwards[FWD_Loading] = CreateMultiForward("GMX_PlayerLoading", ET_STOP, FP_CELL);
  125. Forwards[FWD_Loaded] = CreateMultiForward("GMX_PlayerLoaded", ET_IGNORE, FP_CELL, FP_CELL);
  126. Forwards[FWD_Disconnecting] = CreateMultiForward("GMX_PlayerDisconnecting", ET_STOP, FP_CELL);
  127.  
  128. makeInfoRequest();
  129. }
  130.  
  131. public plugin_cfg() {
  132. checkAPIVersion();
  133. }
  134.  
  135. public plugin_end() {
  136. if (Requests != Invalid_Array) {
  137. ArrayDestroy(Requests);
  138. }
  139. if (RequestOptions != Empty_GripRequestOptions) {
  140. grip_destroy_options(RequestOptions);
  141. }
  142.  
  143. fclose(LogFile);
  144. }
  145.  
  146. public PDS_Save() {
  147. for (new player = 1, data[2]; player < MaxClients; player++) {
  148. if (CHECK_PLAYER_STATUS(player, STATUS_LOADED)) {
  149. data[0] = Players[player][PlayerId];
  150. data[1] = Players[player][PlayerSessionId];
  151. PDS_SetArray(Players[player][PlayerSteamId], data, sizeof data);
  152. }
  153. }
  154. }
  155.  
  156. public client_connect(id) {
  157. Players[id][PlayerStatus] = STATUS_NONE;
  158. }
  159.  
  160. public client_putinserver(id) {
  161. get_user_authid(id, Players[id][PlayerSteamId], 31);
  162. #if defined _uac_included
  163. if (!UAC_IsLoaded || CHECK_PLAYER_STATUS(id, STATUS_WAITING)) {
  164. loadPlayer(id);
  165. } else if (UAC_IsLoaded && CHECK_PLAYER_STATUS(id, STATUS_NONE)) {
  166. Players[id][PlayerStatus] = STATUS_WAITING;
  167. }
  168. #else
  169. loadPlayer(id);
  170. #endif
  171. }
  172.  
  173. #if defined _uac_included
  174. public UAC_Loaded() {
  175. UAC_IsLoaded = true;
  176. }
  177.  
  178. public UAC_Checked(const id, const UAC_CheckResult:result) {
  179. if (result == UAC_CHECK_KICK) {
  180. return;
  181. }
  182.  
  183. switch (Players[id][PlayerStatus]) {
  184. case STATUS_NONE: {
  185. Players[id][PlayerStatus] = STATUS_WAITING;
  186. }
  187.  
  188. case STATUS_WAITING: {
  189. loadPlayer(id);
  190. }
  191. }
  192. }
  193. #endif
  194.  
  195. public SV_DropClient_Post(const id) {
  196. if (Players[id][PlayerStatus] != STATUS_LOADED || Players[id][PlayerId] <= 0) {
  197. arrayset(Players[id], 0, sizeof Players[]);
  198. return HC_CONTINUE;
  199. }
  200.  
  201. ExecuteForward(Forwards[FWD_Disconnecting], FReturn, id);
  202. if (FReturn == PLUGIN_HANDLED) {
  203. arrayset(Players[id], 0, sizeof Players[]);
  204. return HC_CONTINUE;
  205. }
  206.  
  207. logToFile(GmxLogDebug, "Player #%d <player: %d> <session: %d> <user: %d> disconnecting from server", get_user_userid(id), Players[id][PlayerId], Players[id][PlayerSessionId], Players[id][PlayerUserId]);
  208.  
  209. new GripJSONValue:data = grip_json_init_object();
  210. grip_json_object_set_number(data, "session_id", Players[id][PlayerSessionId]);
  211. makeRequest("player/disconnect", data);
  212. grip_destroy_json_value(data);
  213. arrayset(Players[id], 0, sizeof Players[]);
  214. Players[id][PlayerStatus] = STATUS_NONE;
  215. return HC_CONTINUE;
  216. }
  217.  
  218. public TaskPing() {
  219. new GripJSONValue:data = grip_json_init_object();
  220. grip_json_object_set_number(data, "num_players", get_playersnum());
  221.  
  222. new GripJSONValue:sessions = grip_json_init_array();
  223. for (new id = 1; id <= MaxClients; id++) {
  224. if (CHECK_PLAYER_STATUS(id, STATUS_LOADED)) {
  225. grip_json_array_append_number(sessions, Players[id][PlayerSessionId]);
  226. }
  227. }
  228. grip_json_object_set_value(data, "sessions", sessions);
  229.  
  230. makeRequest("server/ping", data);
  231. grip_destroy_json_value(sessions);
  232. grip_destroy_json_value(data);
  233. }
  234.  
  235. public CmdReloadConfig(id, level) {
  236. if (~get_user_flags(id) & level) {
  237. console_print(id, "You have no access to that command");
  238. return PLUGIN_HANDLED;
  239. }
  240. loadConfig();
  241. makeInfoRequest();
  242. return PLUGIN_HANDLED;
  243. }
  244.  
  245. public CmdAssign(id) {
  246. new Float:gametime = get_gametime();
  247. if(gametime < Players[id][PlayerFloodTime] + CMD_DELAY) {
  248. if(++Players[id][PlayerAttemptsCount] >= ATTEMPTS_COUNT) {
  249. console_print(id, "Stop flooding the server by sending a command!");
  250. return PLUGIN_HANDLED;
  251. }
  252. } else if (Players[id][PlayerAttemptsCount]) {
  253. Players[id][PlayerAttemptsCount] = 0;
  254. }
  255. Players[id][PlayerFloodTime] = gametime;
  256.  
  257. new token[36];
  258. read_args(token, charsmax(token));
  259. remove_quotes(token);
  260. new GripJSONValue:data = grip_json_init_object();
  261. grip_json_object_set_number(data, "id", Players[id][PlayerId]);
  262. grip_json_object_set_string(data, "token", token);
  263. makeRequest("player/assign", data, PluginId, Functions[FnOnAssigned], get_user_userid(id));
  264. grip_destroy_json_value(data);
  265. return PLUGIN_HANDLED;
  266. }
  267. // End forwards
  268.  
  269. // Begin callbacks
  270. public OnInfoResponse(const GmxResponseStatus:status, GripJSONValue:data) {
  271. if (status == GmxResponseStatusBadToken) {
  272. ApiEnabled = false;
  273. logToFile(GmxLogError, "Bad token. Change valid in gmx.json and reload config");
  274. return;
  275. }
  276.  
  277. if (status != GmxResponseStatusOk) {
  278. return;
  279. }
  280.  
  281. ExecuteForward(Forwards[FWD_Init], FReturn);
  282. set_task(60.0, "TaskPing", .flags = "b");
  283.  
  284. if (grip_json_get_type(data) != GripJSONObject) {
  285. return;
  286. }
  287.  
  288. ServerData[ServerID] = grip_json_object_get_number(data, "server_id");
  289. ServerData[ServerTime] = grip_json_object_get_number(data, "time");
  290. ServerData[ServerTimeDiff] = get_systime(0) - ServerData[ServerTime];
  291. }
  292.  
  293. public OnConnected(const GmxResponseStatus:status, GripJSONValue:data, const userid) {
  294. if (status != GmxResponseStatusOk) {
  295. return;
  296. }
  297.  
  298. new id = GMX_GetPlayerByUserID(userid);
  299. if (id == 0) {
  300. return;
  301. }
  302.  
  303. if (grip_json_get_type(data) != GripJSONObject) {
  304. return;
  305. }
  306.  
  307. Players[id][PlayerId] = grip_json_object_get_number(data, "player_id");
  308. Players[id][PlayerSessionId] = grip_json_object_get_number(data, "session_id");
  309. new GripJSONValue:userIdVal = grip_json_object_get_value(data, "user_id");
  310. Players[id][PlayerUserId] = grip_json_get_type(userIdVal) != GripJSONNull ? grip_json_get_number(userIdVal) : 0;
  311.  
  312. logToFile(GmxLogDebug, "Player #%d <player: %d> <session: %d> <user: %d> connected to server", userid, Players[id][PlayerId], Players[id][PlayerSessionId], Players[id][PlayerUserId]);
  313.  
  314. Players[id][PlayerStatus] = STATUS_LOADED;
  315.  
  316. new stored[2];
  317. stored[0] = Players[id][PlayerId];
  318. stored[1] = Players[id][PlayerSessionId];
  319. PDS_SetArray(Players[id][PlayerSteamId], stored, sizeof stored);
  320.  
  321. ExecuteForward(Forwards[FWD_Loaded], FReturn, id, data);
  322. }
  323.  
  324. public OnAssigned(const GmxResponseStatus:status, GripJSONValue:data, const userid) {
  325. if(grip_get_response_state() == GripResponseStateSuccessful) {
  326. if(grip_get_response_status_code() == GripHTTPStatusBadRequest) {
  327. client_print_color(id, print_team_default, "^3Access has already been granted");
  328. return;
  329. }
  330. }
  331.  
  332. new id = GMX_GetPlayerByUserID(userid);
  333. if (id == 0) {
  334. log_amx("Invalid userid");
  335. return;
  336. }
  337.  
  338. switch (status) {
  339. case GmxResponseStatusOk: {
  340. if (grip_json_get_type(data) == GripJSONObject) {
  341. Players[id][PlayerUserId] = grip_json_object_get_number(data, "user_id");
  342. log_amx("Доступ предоставлен для userid = %d", Players[id][PlayerUserId]);
  343. }
  344. }
  345. case GripHTTPStatusBadRequest: {
  346. if (grip_json_get_type(data) == GripJSONObject) {
  347. new code = grip_json_object_get_number(data, "error.code", true);
  348. client_print_color(id, print_team_red, "Error %d", code);
  349. }
  350. }
  351. default: {
  352. client_print_color(id, print_team_red, "Something was wrong. Please try again later");
  353. }
  354. }
  355. }
  356. // End callbacks
  357.  
  358. // Begin request
  359. makeRequest(const endpoint[], GripJSONValue:data = Invalid_GripJSONValue, const pluginId = INVALID_PLUGIN_ID, const funcId = INVALID_PLUGIN_ID, const param = 0) {
  360. if (RequestOptions == Empty_GripRequestOptions) {
  361. RequestOptions = grip_create_default_options();
  362. grip_options_add_header(RequestOptions, "Content-Type", "application/json");
  363. grip_options_add_header(RequestOptions, "User-Agent", "Grip");
  364. grip_options_add_header(RequestOptions, "X-Token", Token);
  365. }
  366.  
  367. Request[RequestPluginId] = pluginId;
  368. Request[RequestFuncId] = funcId;
  369. Request[RequestParam] = param;
  370.  
  371. if (Requests == Invalid_Array) {
  372. Requests = ArrayCreate(REQUEST);
  373. }
  374. new id = ArrayPushArray(Requests, Request, sizeof Request);
  375.  
  376. new GripBody:body = data != Invalid_GripJSONValue ? grip_body_from_json(data) : Empty_GripBody;
  377. grip_request(fmt("%s/api/%s", Url, endpoint), body, GripRequestTypePost, "RequestHandler", RequestOptions, id);
  378. if (body != Empty_GripBody) {
  379. grip_destroy_body(body);
  380. }
  381. return id;
  382. }
  383.  
  384. public RequestHandler(const id) {
  385. if (id < 0 || id >= ArraySize(Requests)) {
  386. logToFile(GmxLogError, "Bad request id %d", id);
  387. return;
  388. }
  389.  
  390. if (grip_get_response_state() != GripResponseStateSuccessful) {
  391. switch (grip_get_response_state()) {
  392. case GripResponseStateCancelled: {
  393. logToFile(GmxLogError, "Request %d was cancaled", id);
  394. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusCanceled, Invalid_GripJSONValue, Request[RequestParam]);
  395. }
  396. case GripResponseStateError: {
  397. new err[256];
  398. grip_get_error_description(err, charsmax(err));
  399. logToFile(GmxLogError, "Request %d finished with error: %s", id, err);
  400. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusError, Invalid_GripJSONValue, Request[RequestParam]);
  401. }
  402. case GripResponseStateTimeout: {
  403. logToFile(GmxLogError, "Request %d finished with timeout", id);
  404. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusTimeout, Invalid_GripJSONValue, Request[RequestParam]);
  405. }
  406. }
  407. return;
  408. }
  409.  
  410. new GripHTTPStatus:code = GripHTTPStatus:grip_get_response_status_code();
  411. if (code != GripHTTPStatusOk) {
  412. logToFile(GmxLogError, "Request %d finished with %d status", id, code);
  413. switch (code) {
  414. case GripHTTPStatusForbidden: {
  415. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusBadToken, Invalid_GripJSONValue, Request[RequestParam]);
  416. }
  417.  
  418. case GripHTTPStatusNotFound: {
  419. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusNotFound, Invalid_GripJSONValue, Request[RequestParam]);
  420. }
  421.  
  422. case GripHTTPStatusInternalServerError: {
  423. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusServerError, Invalid_GripJSONValue, Request[RequestParam]);
  424. }
  425.  
  426. default: {
  427. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusUnknownError, Invalid_GripJSONValue, Request[RequestParam]);
  428. }
  429. }
  430.  
  431. return;
  432. }
  433.  
  434. ArrayGetArray(Requests, id, Request, sizeof Request);
  435. new error[128];
  436. new GripJSONValue:data = grip_json_parse_response_body(error, charsmax(error));
  437. if (data == Invalid_GripJSONValue) {
  438. logToFile(GmxLogInfo, "Error parse response: %s", error);
  439. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusBadResponse, Invalid_GripJSONValue, Request[RequestParam]);
  440. return;
  441. }
  442.  
  443. callCallback(Request[RequestPluginId], Request[RequestFuncId], GmxResponseStatusOk, data, Request[RequestParam]);
  444. grip_destroy_json_value(data);
  445. }
  446. // Endrequest
  447.  
  448. // Begin functions
  449. loadConfig() {
  450. new filePath[128];
  451. get_localinfo("amxx_configsdir", filePath, charsmax(filePath));
  452. add(filePath, charsmax(filePath), "/gmx.json");
  453. if (!file_exists(filePath)) {
  454. set_fail_state("Coudn't open %s", filePath);
  455. }
  456.  
  457. new error[128];
  458. new GripJSONValue:cfg = grip_json_parse_file(filePath, error, charsmax(error));
  459. if (cfg == Invalid_GripJSONValue) {
  460. set_fail_state("Coudn't open %s. Error %s", filePath, error);
  461. }
  462.  
  463. if (grip_json_get_type(cfg) != GripJSONObject) {
  464. grip_destroy_json_value(cfg);
  465. set_fail_state("Coudn't open %s. Bad format", filePath);
  466. }
  467.  
  468. grip_json_object_get_string(cfg, "token", Token, charsmax(Token));
  469. grip_json_object_get_string(cfg, "url", Url, charsmax(Url));
  470. LogLvl = GmxLogLevel:grip_json_object_get_number(cfg, "loglevel");
  471. grip_destroy_json_value(cfg);
  472.  
  473. logToFile(GmxLogInfo, "Load configuration. URL is '%s'", Url);
  474.  
  475. new fwd = CreateMultiForward("GMX_CfgLoaded", ET_IGNORE);
  476. new ret;
  477. ExecuteForward(fwd, ret);
  478. DestroyForward(fwd);
  479. }
  480.  
  481. makeInfoRequest() {
  482. new map[64];
  483. get_mapname(map, charsmax(map));
  484. new GripJSONValue:data = grip_json_init_object();
  485. grip_json_object_set_string(data, "map", map);
  486. grip_json_object_set_number(data, "max_players", MaxClients);
  487. makeRequest("server/info", data, PluginId, Functions[FnOnInfoResponse]);
  488. grip_destroy_json_value(data);
  489. }
  490.  
  491. loadPlayer(id) {
  492. if (!canBeLoaded(id)) {
  493. return;
  494. }
  495.  
  496. arrayset(Players[id], 0, sizeof Players[]);
  497. ExecuteForward(Forwards[FWD_Loading], FReturn, id);
  498. if (FReturn == PLUGIN_HANDLED) {
  499. return;
  500. }
  501.  
  502. Players[id][PlayerStatus] = STATUS_LOADING;
  503.  
  504. new steamid[24], nick[32], ip[32];
  505. get_user_authid(id, steamid, charsmax(steamid));
  506. get_user_name(id, nick, charsmax(nick));
  507. get_user_ip(id, ip, charsmax(ip), 1);
  508.  
  509. new emulator = 0;
  510. if (has_reunion()) {
  511. emulator = _:REU_GetAuthtype(id);
  512. }
  513.  
  514. new GripJSONValue:data = grip_json_init_object();
  515. grip_json_object_set_number(data, "emulator", emulator);
  516. grip_json_object_set_string(data, "steamid", steamid);
  517. grip_json_object_set_string(data, "nick", nick);
  518. grip_json_object_set_string(data, "ip", ip);
  519.  
  520. new stored[2];
  521. if (PDS_GetArray(steamid, stored, sizeof stored)) {
  522. grip_json_object_set_number(data, "id", stored[0]);
  523. grip_json_object_set_number(data, "session_id", stored[1]);
  524. } else {
  525. stored[0] = 0;
  526. stored[1] = 0;
  527. grip_json_object_set_null(data, "id");
  528. grip_json_object_set_null(data, "session_id");
  529. }
  530.  
  531. new userid = get_user_userid(id);
  532.  
  533. logToFile(GmxLogDebug, "Player #%d <emu: %d> <steamid: %s> <ip: %s> <nick: %s> <id: %d> <session %d> connecting to server", userid, emulator, steamid, ip, nick, stored[0], stored[1]);
  534. makeRequest("player/connect", data, PluginId, Functions[FnOnConnected], userid);
  535. grip_destroy_json_value(data);
  536. }
  537.  
  538. bool:canBeLoaded(const id) {
  539. return bool:(Players[id][PlayerStatus] != STATUS_LOADING && Players[id][PlayerStatus] != STATUS_LOADING && !is_user_bot(id) && !is_user_hltv(id));
  540. }
  541.  
  542. callCallback(const pluginId, const funcId, const GmxResponseStatus:status, const GripJSONValue:data, const param) {
  543. if (pluginId != INVALID_PLUGIN_ID && funcId != INVALID_PLUGIN_ID && callfunc_begin_i(funcId, pluginId) == 1) {
  544. callfunc_push_int(_:status);
  545. callfunc_push_int(_:data);
  546. callfunc_push_int(param);
  547. callfunc_end();
  548. }
  549. }
  550.  
  551. logToFile(const GmxLogLevel:level, const msg[], any:...) {
  552. if (level > LogLvl) {
  553. return;
  554. }
  555.  
  556. new message[512];
  557. vformat(message, charsmax(message), msg, 3);
  558.  
  559. new hour, minute, second;
  560. time(hour, minute, second);
  561.  
  562. server_print("[GMX] %02d:%02d:%02d: %s", hour, minute, second, message);
  563. fprintf(LogFile, "%02d:%02d:%02d: %s^n", hour, minute, second, message);
  564. fflush(LogFile);
  565. }
  566. // End functions
  567.  
  568. // Begin natives
  569. public plugin_natives() {
  570. register_native("GMX_MakeRequest", "NativeMakeRequest", 0);
  571. register_native("GMX_Log", "NativeLog", 0);
  572. register_native("GMX_GetServerID", "NativeGetServerID", 0);
  573. register_native("GMX_GetServerTime", "NativeGetServerTime", 0);
  574. register_native("GMX_GetServerTimeDiff", "NativeGetServerTimeDiff", 0);
  575. register_native("GMX_PlayerIsLoaded", "NativeIsLoaded", 0);
  576. register_native("GMX_PlayerGetPlayerId", "NativeGetPlayerId", 0);
  577. register_native("GMX_PlayerGetUserId", "NativeGetUserId", 0);
  578. register_native("GMX_PlayerGetSessionId", "NativeGetSessionId", 0);
  579. }
  580.  
  581. public NativeMakeRequest(plugin, argc) {
  582. CHECK_NATIVE_ARGS_NUM(argc, 3, -1)
  583.  
  584. if (!ApiEnabled) {
  585. log_error(AMX_ERR_NATIVE, "API is not enabled. Please check log files");
  586. return -1;
  587. }
  588.  
  589. enum { arg_endpoint = 1, arg_data, arg_callback, arg_param };
  590.  
  591. new endpoint[128];
  592. get_string(arg_endpoint, endpoint, charsmax(endpoint));
  593.  
  594. new GripJSONValue:data = GripJSONValue:get_param(arg_data);
  595. new callback[64], funcId;
  596. get_string(arg_callback, callback, charsmax(callback));
  597. if (callback[0] != EOS) {
  598. funcId = get_func_id(callback, plugin);
  599. if (funcId == -1) {
  600. log_error(AMX_ERR_NATIVE, "Could not find function %s", callback);
  601. return -1;
  602. }
  603. } else {
  604. funcId = INVALID_PLUGIN_ID;
  605. }
  606.  
  607. return makeRequest(endpoint, data, plugin, funcId, argc >= 4 ? get_param(arg_param) : 0);
  608. }
  609.  
  610. public NativeLog(plugin, argc) {
  611. CHECK_NATIVE_ARGS_NUM(argc, 2, 0)
  612.  
  613. enum { arg_level = 1, arg_fmt, arg_params };
  614.  
  615. new message[512];
  616. vdformat(message, charsmax(message), arg_fmt, arg_params);
  617. logToFile(GmxLogLevel:get_param(arg_level), message);
  618. return 1;
  619. }
  620.  
  621. public NativeGetServerID() {
  622. return ServerData[ServerID];
  623. }
  624.  
  625. public NativeGetServerTime() {
  626. return ServerData[ServerTime];
  627. }
  628.  
  629. public NativeGetServerTimeDiff() {
  630. return ServerData[ServerTimeDiff];
  631. }
  632.  
  633. public NativeIsLoaded(plugin, argc) {
  634. enum { arg_player = 1 };
  635.  
  636. CHECK_NATIVE_ARGS_NUM(argc, 1, false)
  637.  
  638. new player = get_param(arg_player);
  639. if (player <= 0 || player > MaxClients) {
  640. return false;
  641. }
  642. return bool:CHECK_PLAYER_STATUS(player, STATUS_LOADED);
  643. }
  644.  
  645. public NativeGetPlayerId(plugin, argc) {
  646. enum { arg_player = 1 };
  647.  
  648. CHECK_NATIVE_ARGS_NUM(argc, 1, 0)
  649.  
  650. new player = get_param(arg_player);
  651. CHECK_NATIVE_PLAYER(player, 0)
  652. CHECK_NATIVE_PLAYER_LOADED(player, 0)
  653.  
  654. return Players[player][PlayerId];
  655. }
  656.  
  657. public NativeGetUserId(plugin, argc) {
  658. enum { arg_player = 1 };
  659.  
  660. CHECK_NATIVE_ARGS_NUM(argc, 1, 0)
  661.  
  662. new player = get_param(arg_player);
  663. CHECK_NATIVE_PLAYER(player, 0)
  664. CHECK_NATIVE_PLAYER_LOADED(player, 0)
  665.  
  666. return Players[player][PlayerUserId];
  667. }
  668.  
  669. public NativeGetSessionId(plugin, argc) {
  670. enum { arg_player = 1 };
  671.  
  672. CHECK_NATIVE_ARGS_NUM(argc, 1, 0)
  673.  
  674. new player = get_param(arg_player);
  675. CHECK_NATIVE_PLAYER(player, 0)
  676. CHECK_NATIVE_PLAYER_LOADED(player, 0)
  677.  
  678. return Players[player][PlayerSessionId];
  679. }
  680. // End natives
  681.  
  682. checkAPIVersion() {
  683. for(new i, n = get_pluginsnum(), status[2], func; i < n; i++) {
  684. if(i == PluginId) {
  685. continue;
  686. }
  687.  
  688. get_plugin(i, .status = status, .len5 = charsmax(status));
  689.  
  690. //status debug || status running
  691. if(status[0] != 'd' && status[0] != 'r') {
  692. continue;
  693. }
  694.  
  695. func = get_func_id("__gmx_version_check", i);
  696.  
  697. if(func == -1) {
  698. continue;
  699. }
  700.  
  701. if(callfunc_begin_i(func, i) == 1) {
  702. callfunc_push_int(GMX_MAJOR_VERSION);
  703. callfunc_push_int(GMX_MINOR_VERSION);
  704. callfunc_end();
  705. }
  706. }
  707. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement