diff --git a/csgo2/Server.cpp b/csgo2/Server.cpp index 6d6ccb3..b4da975 100644 --- a/csgo2/Server.cpp +++ b/csgo2/Server.cpp @@ -13,7 +13,14 @@ CURLcode HttpGet(const std::string& strUrl, std::string& strResponse, return CURLE_FAILED_INIT; } struct curl_slist* headers = NULL; - headers = curl_slist_append(headers, (char*)header.c_str()); + + if (header.empty() == false) { + headers = curl_slist_append(headers, (char*)header.c_str()); + } + headers = curl_slist_append(headers, "Accept: application/json"); + headers = curl_slist_append(headers, + "Content-Type: application/json"); // text/html + headers = curl_slist_append(headers, "charsets: utf-8"); curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str()); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); @@ -24,7 +31,9 @@ CURLcode HttpGet(const std::string& strUrl, std::string& strResponse, curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data); curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse); res = curl_easy_perform(pCURL); + curl_slist_free_all(headers); curl_easy_cleanup(pCURL); + return res; } @@ -36,6 +45,9 @@ CURLcode HttpPost(const std::string& strUrl, std::string header, if (pCURL == NULL) { return CURLE_FAILED_INIT; } + if (header.empty() == false) { + headers = curl_slist_append(headers, (char*)header.c_str()); + } CURLcode ret; ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str()); // std::string data = curl_easy_escape(pCURL, szJson.c_str(), diff --git a/csgo2/script_apis.cpp b/csgo2/script_apis.cpp index 3275597..c40020f 100644 --- a/csgo2/script_apis.cpp +++ b/csgo2/script_apis.cpp @@ -772,7 +772,7 @@ auto luaApi_HttpGet(lua_State* luaVm) -> int { const auto timeOut = lua_tointeger(luaVm, 3); auto strHeader = std::string(header); std::string response; - auto result = Server::HttpGet(url, strHeader, response, timeOut); + auto result = Server::HttpGet(url, response, strHeader, timeOut); lua_pushinteger(luaVm, result); lua_pushstring(luaVm, response.c_str()); return 2; @@ -790,6 +790,87 @@ auto luaApi_HttpPost(lua_State* luaVm) -> int { lua_pushstring(luaVm, response.c_str()); return 2; } +auto luaApi_HttpAsyncPost(lua_State* luaVm) -> int { + // param: url:string header:string postdata:string timeOut:int + // metadata:string + const auto url = lua_tostring(luaVm, 1); + const auto header = lua_tostring(luaVm, 2); + const auto postdata = lua_tostring(luaVm, 3); + const auto theTimeOut = lua_tointeger(luaVm, 4); + const auto metadata = lua_tostring(luaVm, 5); + struct _ctx { + std::shared_ptr url; + std::shared_ptr header; + std::shared_ptr postdata; + std::shared_ptr metadata; + long timeOut; + }; + _ctx* ctx = new _ctx{ + .url = std::make_shared(url), + .header = std::make_shared(header), + .postdata = std::make_shared(postdata), + .metadata = std::make_shared(metadata), + .timeOut = (int)theTimeOut, + }; + if (ctx) { + CreateThread( + nullptr, 0, + [](void* ctx) -> DWORD { + const auto theCtx = reinterpret_cast<_ctx*>(ctx); + auto strHeader = std::string(*theCtx->header.get()); + std::string response; + auto result = Server::HttpPost(*theCtx->url.get(), strHeader, + *theCtx->postdata.get(), + response, theCtx->timeOut); + ScriptCallBacks::luaCall_onHttpRequest( + *theCtx->url.get(), *theCtx->metadata.get(), + response.c_str(), result); + delete theCtx; + return 0; + }, + ctx, 0, nullptr); + } + lua_pop(luaVm, 5); + return 0; +} +auto luaApi_HttpAsyncGet(lua_State* luaVm) -> int { + // param: url:string header:string timeOut:int + // metadata:string + const auto url = lua_tostring(luaVm, 1); + const auto header = lua_tostring(luaVm, 2); + const auto theTimeOut = lua_tointeger(luaVm, 3); + const auto metadata = lua_tostring(luaVm, 4); + struct _ctx { + std::shared_ptr url; + std::shared_ptr header; + std::shared_ptr metadata; + long timeOut; + }; + _ctx* ctx = new _ctx{ + .url = std::make_shared(url), + .header = std::make_shared(header), + .metadata = std::make_shared(metadata), + .timeOut = (long)theTimeOut, + }; + if (ctx) { + CreateThread( + nullptr, 0, + [](void* ctx) -> DWORD { + const auto theCtx = reinterpret_cast<_ctx*>(ctx); + std::string response; + auto httpCode = + Server::HttpGet(*theCtx->url.get(), response, *theCtx->header.get(),theCtx->timeOut); + ScriptCallBacks::luaCall_onHttpRequest( + *theCtx->url.get(), *theCtx->metadata.get(), + response.c_str(), httpCode); + delete theCtx; + return 0; + }, + const_cast<_ctx*>(ctx), 0, nullptr); + } + lua_pop(luaVm, 4); + return 0; +} auto initFunciton(lua_State* luaVm) -> void { lua_register(luaVm, "ListenToGameEvent", luaApi_ListenToGameEvent); @@ -838,8 +919,11 @@ auto initFunciton(lua_State* luaVm) -> void { lua_register(luaVm, "luaApi_KickPlayer", luaApi_KickPlayer); lua_register(luaVm, "luaApi_HttpGet", luaApi_HttpGet); lua_register(luaVm, "luaApi_HttpPost", luaApi_HttpPost); + lua_register(luaVm, "luaApi_HttpAsyncGet", luaApi_HttpAsyncGet); + lua_register(luaVm, "luaApi_HttpAsyncPost", luaApi_HttpAsyncPost); + lua_register(luaVm, "luaApi_GetPlayerSteamId", luaApi_GetPlayerSteamId); - //lua_register(luaVm, "luaApi_TeleportPlayer", luaApi_TeleportPlayer); + // lua_register(luaVm, "luaApi_TeleportPlayer", luaApi_TeleportPlayer); luabridge::getGlobalNamespace(luaVm) .beginClass<_luaApi_WeaponInfo>("WeaponInfo") diff --git a/csgo2/script_callbacks.cpp b/csgo2/script_callbacks.cpp index 8135715..6a1c885 100644 --- a/csgo2/script_callbacks.cpp +++ b/csgo2/script_callbacks.cpp @@ -15,6 +15,8 @@ std::unordered_map callbackNameWithEnumMap{ {hash_32_fnv1a_const("round_end"), _CallbackNames::kOnRoundEnd}, {hash_32_fnv1a_const("player_hurt"), _CallbackNames::kOnPlayerHurt}, {hash_32_fnv1a_const("player_team"), _CallbackNames::kOnPlayerTeamChange}, + {hash_32_fnv1a_const("http_request"), _CallbackNames::kOnHttpRequest}, + }; auto CallBackNameToEnum(const char* name) -> _CallbackNames { if (name == nullptr) { @@ -206,30 +208,51 @@ auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor, } }); } -auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool { +auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, + bool disconnect, bool slient, bool isBot) + -> bool { bool result = false; ExcuteCallbackInAllLuaVm(_CallbackNames::kOnPlayerTeamChange, - [&](lua_State* luaVm, int refIndex) -> void { - lua_rawgeti(luaVm, LUA_REGISTRYINDEX, - refIndex); - if (lua_isfunction(luaVm, -1)) { - lua_pushinteger(luaVm, userid); - lua_pushinteger(luaVm, team); - lua_pushinteger(luaVm, oldteam); - lua_pushboolean(luaVm, disconnect); - lua_pushboolean(luaVm, slient); - lua_pushboolean(luaVm, isBot); + [&](lua_State* luaVm, int refIndex) -> void { + lua_rawgeti(luaVm, LUA_REGISTRYINDEX, + refIndex); + if (lua_isfunction(luaVm, -1)) { + lua_pushinteger(luaVm, userid); + lua_pushinteger(luaVm, team); + lua_pushinteger(luaVm, oldteam); + lua_pushboolean(luaVm, disconnect); + lua_pushboolean(luaVm, slient); + lua_pushboolean(luaVm, isBot); - if (lua_pcall(luaVm, 6, 1, 0) != LUA_OK) { - LOG("Error calling Lua callback: %s\n", - lua_tostring(luaVm, -1)); - lua_pop(luaVm, 1); - } - if (lua_isboolean(luaVm, -1)) { - result = lua_toboolean(luaVm, -1); - } - } - }); + if (lua_pcall(luaVm, 6, 1, 0) != LUA_OK) { + LOG("Error calling Lua callback: %s\n", + lua_tostring(luaVm, -1)); + lua_pop(luaVm, 1); + } + if (lua_isboolean(luaVm, -1)) { + result = lua_toboolean(luaVm, -1); + } + } + }); return result; } +auto luaCall_onHttpRequest(std::string url, std::string metaData, + std::string respon, int statusCode) -> void { + ExcuteCallbackInAllLuaVm(_CallbackNames::kOnHttpRequest, + [&](lua_State* luaVm, int refIndex) -> void { + lua_rawgeti(luaVm, LUA_REGISTRYINDEX, + refIndex); + if (lua_isfunction(luaVm, -1)) { + lua_pushstring(luaVm, url.c_str()); + lua_pushstring(luaVm, metaData.c_str()); + lua_pushstring(luaVm, respon.c_str()); + lua_pushinteger(luaVm, statusCode); + if (lua_pcall(luaVm, 4, 0, 0) != LUA_OK) { + LOG("Error calling Lua callback: %s\n", + lua_tostring(luaVm, -1)); + lua_pop(luaVm, 1); + } + } + }); +} } // namespace ScriptCallBacks diff --git a/csgo2/script_callbacks.h b/csgo2/script_callbacks.h index 664cf17..9e4b6ad 100644 --- a/csgo2/script_callbacks.h +++ b/csgo2/script_callbacks.h @@ -12,7 +12,8 @@ enum class _CallbackNames { kOnRoundStart, kOnRoundEnd, kOnPlayerHurt, - kOnPlayerTeamChange + kOnPlayerTeamChange, + kOnHttpRequest }; extern std::unordered_map> callbackList; @@ -34,5 +35,9 @@ auto luaCall_onRoundEnd(int winnerTeam, int reason, const char* message) auto luaCall_onPlayerHurt(int userid, int attacker, int health, int armor, const char* weapon, int dmg_health, int dmg_armor, int hitgroup) -> void; -auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, bool disconnect, bool slient, bool isBot) -> bool; +auto luaCall_onPlayerTeamChange(int userid, int team, int oldteam, + bool disconnect, bool slient, bool isBot) + -> bool; +auto luaCall_onHttpRequest(std::string url, std::string metaData, + std::string respon, int statusCode) -> void; } // namespace ScriptCallBacks