From efcc3952b5d2a96280521d98d1ec6072c5d39175 Mon Sep 17 00:00:00 2001 From: Franco Romaniello Date: Sat, 23 Oct 2021 12:43:28 +0200 Subject: [PATCH] Implement `rh_get_net_from()` native (#221) * Implement native rh_get_net_from * Add ws2_32.lib * Add ws2_32.lib in release * newline at end of reapi_utils.cpp * Update reapi_utils.cpp * fix end of file Co-authored-by: Sergey Shorokhov --- .../scripting/include/reapi_engine.inc | 11 +++++++++ reapi/msvc/reapi.vcxproj | 4 ++-- reapi/src/natives/natives_misc.cpp | 23 +++++++++++++++++++ reapi/src/reapi_utils.cpp | 19 +++++++++++++++ reapi/src/reapi_utils.h | 2 ++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/reapi/extra/amxmodx/scripting/include/reapi_engine.inc b/reapi/extra/amxmodx/scripting/include/reapi_engine.inc index 8b797c30..52c17a5d 100644 --- a/reapi/extra/amxmodx/scripting/include/reapi_engine.inc +++ b/reapi/extra/amxmodx/scripting/include/reapi_engine.inc @@ -148,3 +148,14 @@ native rh_update_user_info(playerEntIndex); * */ native rh_drop_client(const index, const message[] = ""); + +/* +* - +* +* @param output Buffer to copy the ip address +* @param len Maximum buffer size +* +* @noreturn +* +*/ +native rh_get_net_from(output[], len); diff --git a/reapi/msvc/reapi.vcxproj b/reapi/msvc/reapi.vcxproj index 0877c6bd..5fdef54e 100644 --- a/reapi/msvc/reapi.vcxproj +++ b/reapi/msvc/reapi.vcxproj @@ -366,7 +366,7 @@ Windows true - %(AdditionalDependencies) + ws2_32.lib;%(AdditionalDependencies) reapi.def @@ -420,7 +420,7 @@ true true true - %(AdditionalDependencies) + ws2_32.lib;%(AdditionalDependencies) reapi.def diff --git a/reapi/src/natives/natives_misc.cpp b/reapi/src/natives/natives_misc.cpp index a52d6af5..d06a1224 100644 --- a/reapi/src/natives/natives_misc.cpp +++ b/reapi/src/natives/natives_misc.cpp @@ -2723,6 +2723,28 @@ cell AMX_NATIVE_CALL rh_drop_client(AMX *amx, cell *params) return TRUE; } +/* +* - +* +* @param output Buffer to copy the ip address +* @param len Maximum buffer size +* +* @noreturn +* +* native rh_get_net_from(output[], len); +*/ +cell AMX_NATIVE_CALL rh_get_net_from(AMX* amx, cell* params) +{ + enum args_e { arg_count, arg_output, arg_maxlen }; + + cell *dest = getAmxAddr(amx, params[arg_output]); + char *addr = NET_AdrToString(*g_RehldsData->GetNetFrom()); + + setAmxString(dest, addr, params[arg_maxlen]); + + return TRUE; +} + AMX_NATIVE_INFO Misc_Natives_RH[] = { { "rh_set_mapname", rh_set_mapname }, @@ -2731,6 +2753,7 @@ AMX_NATIVE_INFO Misc_Natives_RH[] = { "rh_emit_sound2", rh_emit_sound2 }, { "rh_update_user_info", rh_update_user_info }, { "rh_drop_client", rh_drop_client }, + { "rh_get_net_from", rh_get_net_from }, { nullptr, nullptr } }; diff --git a/reapi/src/reapi_utils.cpp b/reapi/src/reapi_utils.cpp index 77c0693a..2d30416e 100644 --- a/reapi/src/reapi_utils.cpp +++ b/reapi/src/reapi_utils.cpp @@ -240,3 +240,22 @@ const char *getATypeStr(AType type) return s_ATypes[type]; } + +char* NET_AdrToString(const netadr_t& a) +{ + static char s[64]; + + Q_memset(s, 0, sizeof(s)); + + if (a.type == NA_LOOPBACK) + Q_snprintf(s, sizeof(s), "loopback"); + else if (a.type == NA_IP) + Q_snprintf(s, sizeof(s), "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs(a.port)); +#ifdef _WIN32 + else // NA_IPX + Q_snprintf(s, sizeof(s), "%02x%02x%02x%02x:%02x%02x%02x%02x%02x%02x:%i", a.ipx[0], a.ipx[1], a.ipx[2], a.ipx[3], a.ipx[4], a.ipx[5], a.ipx[6], a.ipx[7], a.ipx[8], a.ipx[9], ntohs(a.port)); +#endif // _WIN32 + + return s; +} + diff --git a/reapi/src/reapi_utils.h b/reapi/src/reapi_utils.h index 86a8adfa..994864c3 100644 --- a/reapi/src/reapi_utils.h +++ b/reapi/src/reapi_utils.h @@ -59,4 +59,6 @@ void RemoveOrDropItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, GiveType typ const char *getATypeStr(AType type); +char *NET_AdrToString(const netadr_t& a); + extern void NORETURN UTIL_SysError(const char *fmt, ...);