From fb03d5f2d570ceae3d715c9e75e15ae5d2e4eb8e Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Thu, 3 Mar 2016 12:28:49 +0100 Subject: [PATCH] Add natives to get SourceTV ip and port --- extension.h | 1 + forwards.h | 1 - natives.cpp | 36 ++++++++++++++++++++++++++++++++++++ sourcetv_test.sp | 23 +++++++++++++++++++++++ sourcetvmanager.inc | 25 +++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) diff --git a/extension.h b/extension.h index 8107342..15214da 100644 --- a/extension.h +++ b/extension.h @@ -47,6 +47,7 @@ #include "ihltvdemorecorder.h" #include "igameevents.h" #include "inetmessage.h" +#include "netadr.h" #include "hltvdirectorwrapper.h" #include "hltvclientwrapper.h" diff --git a/forwards.h b/forwards.h index e6a1933..4d577fe 100644 --- a/forwards.h +++ b/forwards.h @@ -33,7 +33,6 @@ #define _INCLUDE_SOURCEMOD_EXTENSION_FORWARDS_H_ #include "extension.h" -#include "netadr.h" #if SOURCE_ENGINE == SE_CSGO #include "netmessages.pb.h" diff --git a/natives.cpp b/natives.cpp index 91131ba..7248f7a 100644 --- a/natives.cpp +++ b/natives.cpp @@ -110,6 +110,39 @@ static cell_t Native_GetSelectedServerInstance(IPluginContext *pContext, const c #endif } +// native SourceTV_IsMasterProxy(); +static cell_t Native_IsMasterProxy(IPluginContext *pContext, const cell_t *params) +{ + if (hltvserver == nullptr) + return 0; + + return hltvserver->IsMasterProxy(); +} + +// native bool:SourceTV_GetServerIP(String:ip[], maxlen); +static cell_t Native_GetServerIP(IPluginContext *pContext, const cell_t *params) +{ + if (hltvserver == nullptr) + return 0; + + const netadr_t *adr = hltvserver->GetRelayAddress(); + + char buf[16]; + V_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", adr->ip[0], adr->ip[1], adr->ip[2], adr->ip[3]); + pContext->StringToLocalUTF8(params[1], static_cast(params[2]), buf, NULL); + + return 1; +} + +// native SourceTV_GetServerPort(); +static cell_t Native_GetServerPort(IPluginContext *pContext, const cell_t *params) +{ + if (hltvserver == nullptr) + return 0; + + return hltvserver->GetBaseServer()->GetUDPPort(); +} + // native SourceTV_GetBotIndex(); static cell_t Native_GetBotIndex(IPluginContext *pContext, const cell_t *params) { @@ -709,6 +742,9 @@ const sp_nativeinfo_t sourcetv_natives[] = { "SourceTV_GetServerInstanceCount", Native_GetServerInstanceCount }, { "SourceTV_SelectServerInstance", Native_SelectServerInstance }, { "SourceTV_GetSelectedServerInstance", Native_GetSelectedServerInstance }, + { "SourceTV_IsMasterProxy", Native_IsMasterProxy }, + { "SourceTV_GetServerIP", Native_GetServerIP }, + { "SourceTV_GetServerPort", Native_GetServerPort }, { "SourceTV_GetBotIndex", Native_GetBotIndex }, { "SourceTV_GetLocalStats", Native_GetLocalStats }, { "SourceTV_GetGlobalStats", Native_GetGlobalStats }, diff --git a/sourcetv_test.sp b/sourcetv_test.sp index 9e3d822..ce368eb 100644 --- a/sourcetv_test.sp +++ b/sourcetv_test.sp @@ -8,6 +8,9 @@ public OnPluginStart() RegConsoleCmd("sm_servercount", Cmd_GetServerCount); RegConsoleCmd("sm_selectserver", Cmd_SelectServer); RegConsoleCmd("sm_selectedserver", Cmd_GetSelectedServer); + RegConsoleCmd("sm_ismaster", Cmd_IsMasterProxy); + RegConsoleCmd("sm_serverip", Cmd_GetServerIP); + RegConsoleCmd("sm_serverport", Cmd_GetServerPort); RegConsoleCmd("sm_botindex", Cmd_GetBotIndex); RegConsoleCmd("sm_broadcasttick", Cmd_GetBroadcastTick); RegConsoleCmd("sm_localstats", Cmd_Localstats); @@ -103,6 +106,26 @@ public Action:Cmd_GetSelectedServer(client, args) return Plugin_Handled; } +public Action:Cmd_IsMasterProxy(client, args) +{ + ReplyToCommand(client, "SourceTV is master proxy: %d", SourceTV_IsMasterProxy()); + return Plugin_Handled; +} + +public Action:Cmd_GetServerIP(client, args) +{ + new String:sIP[32]; + new bool:bSuccess = SourceTV_GetServerIP(sIP, sizeof(sIP)); + ReplyToCommand(client, "SourceTV server ip (ret %d): %s", bSuccess, sIP); + return Plugin_Handled; +} + +public Action:Cmd_GetServerPort(client, args) +{ + ReplyToCommand(client, "SourceTV server port: %d", SourceTV_GetServerPort()); + return Plugin_Handled; +} + public Action:Cmd_GetBotIndex(client, args) { ReplyToCommand(client, "SourceTV bot index: %d", SourceTV_GetBotIndex()); diff --git a/sourcetvmanager.inc b/sourcetvmanager.inc index cb2b0d1..ae5e88d 100644 --- a/sourcetvmanager.inc +++ b/sourcetvmanager.inc @@ -35,6 +35,30 @@ native SourceTV_SelectServerInstance(instance); */ native SourceTV_GetSelectedServerInstance(); +/** + * Returns whether this SourceTV instance is a master proxy or relay. + * + * @return True if SourceTV instance is master proxy, false otherwise. + */ +native SourceTV_IsMasterProxy(); + +/** + * Get the local ip of the SourceTV server. + * + * @param ip Buffer to save IP in. + * @param maxlen Maximum length of the buffer. + * @return True if IP written, false otherwise. + */ +native bool:SourceTV_GetServerIP(String:ip[], maxlen); + +/** + * Get the UDP port of the SourceTV server. + * This is the port clients use to connect. + * + * @return SourceTV server UDP port. + */ +native SourceTV_GetServerPort(); + /** * Get the client index of the SourceTV bot. * @@ -403,6 +427,7 @@ public __ext_stvmngr_SetNTVOptional() MarkNativeAsOptional("SourceTV_GetServerInstanceCount"); MarkNativeAsOptional("SourceTV_SelectServerInstance"); MarkNativeAsOptional("SourceTV_GetSelectedServerInstance"); + MarkNativeAsOptional("SourceTV_IsMasterProxy"); MarkNativeAsOptional("SourceTV_GetBotIndex"); MarkNativeAsOptional("SourceTV_GetLocalStats"); MarkNativeAsOptional("SourceTV_GetGlobalStats");