From 307a8037d62025452d7a4cc7d10f7fc1b342dae9 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Mon, 14 Nov 2016 03:09:03 -0600 Subject: [PATCH] Add SourceTV_PrintToConsole native Print text to single spectators' console. --- natives.cpp | 46 ++++++++++++++++++++++++++++++++++++++++----- sourcetv_test.sp | 25 ++++++++++++++++++++++-- sourcetvmanager.inc | 12 ++++++++++++ 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/natives.cpp b/natives.cpp index e3ab27b..7293687 100644 --- a/natives.cpp +++ b/natives.cpp @@ -40,12 +40,13 @@ extern const sp_nativeinfo_t sourcetv_natives[]; // Print to client consoles SH_DECL_MANUALHOOK0_void_vafmt(CBaseServer_BroadcastPrintf, 0, 0, 0); - -bool g_bHasClientPrintfOffset = false; +// For print to chat SH_DECL_MANUALHOOK1_void(CBaseClient_FireGameEvent, 0, 0, 0, IGameEvent *); -void SetupNativeCalls() +bool g_bHasBroadcastPrintfOffset = false; bool g_bHasClientFireGameEventOffset = false; + +void SetupNativeCalls() { int offset = -1; if (!g_pGameConf->GetOffset("CBaseServer::BroadcastPrintf", &offset) || offset == -1) @@ -55,7 +56,7 @@ bool g_bHasClientFireGameEventOffset = false; else { SH_MANUALHOOK_RECONFIGURE(CBaseServer_BroadcastPrintf, offset, 0, 0); - g_bHasClientPrintfOffset = true; + g_bHasBroadcastPrintfOffset = true; } offset = -1; @@ -307,7 +308,7 @@ static cell_t Native_BroadcastConsoleMessage(IPluginContext *pContext, const cel if (hltvserver == nullptr) return 0; - if (!g_bHasClientPrintfOffset) + if (!g_bHasBroadcastPrintfOffset) return 0; char buffer[1024]; @@ -854,6 +855,40 @@ static cell_t Native_PrintToChat(IPluginContext *pContext, const cell_t *params) return 0; } +// native SourceTV_PrintToConsole(client, const String:format[], any:...); +static cell_t Native_PrintToConsole(IPluginContext *pContext, const cell_t *params) +{ + if (hltvserver == nullptr) + return 0; + + cell_t client = params[1]; + if (client < 1 || client > hltvserver->GetBaseServer()->GetClientCount()) + { + pContext->ReportError("Invalid spectator client index %d.", client); + return 0; + } + + HLTVClientWrapper *pClient = hltvserver->GetClient(client); + if (!pClient->IsConnected()) + { + pContext->ReportError("Client %d is not connected.", client); + return 0; + } + + char buffer[1024]; + size_t len; + { + DetectExceptions eh(pContext); + len = smutils->FormatString(buffer, sizeof(buffer), pContext, params, 2); + if (eh.HasException()) + return 0; + } + + pClient->BaseClient()->ClientPrintf("%s\n", buffer); + + return 0; +} + const sp_nativeinfo_t sourcetv_natives[] = { { "SourceTV_GetServerInstanceCount", Native_GetServerInstanceCount }, @@ -891,5 +926,6 @@ const sp_nativeinfo_t sourcetv_natives[] = { "SourceTV_GetClientPassword", Native_GetClientPassword }, { "SourceTV_KickClient", Native_KickClient }, { "SourceTV_PrintToChat", Native_PrintToChat }, + { "SourceTV_PrintToConsole", Native_PrintToConsole }, { NULL, NULL }, }; diff --git a/sourcetv_test.sp b/sourcetv_test.sp index defe03a..d9dc50d 100644 --- a/sourcetv_test.sp +++ b/sourcetv_test.sp @@ -20,7 +20,7 @@ public OnPluginStart() RegConsoleCmd("sm_spechintmsg", Cmd_SendHintMessage); RegConsoleCmd("sm_specchat", Cmd_SendChatMessage); RegConsoleCmd("sm_specchatlocal", Cmd_SendChatMessageLocal); - RegConsoleCmd("sm_specmsg", Cmd_SendMessage); + RegConsoleCmd("sm_specconsole", Cmd_SendMessage); RegConsoleCmd("sm_viewentity", Cmd_GetViewEntity); RegConsoleCmd("sm_vieworigin", Cmd_GetViewOrigin); RegConsoleCmd("sm_forcechasecam", Cmd_ForceChaseCameraShot); @@ -35,6 +35,7 @@ public OnPluginStart() RegConsoleCmd("sm_botcmd", Cmd_ExecuteStringCommand); RegConsoleCmd("sm_speckick", Cmd_KickClient); RegConsoleCmd("sm_specchatone", Cmd_PrintToChat); + RegConsoleCmd("sm_specconsoleone", Cmd_PrintToConsole); } public SourceTV_OnStartRecording(instance, const String:filename[]) @@ -228,7 +229,7 @@ public Action:Cmd_SendMessage(client, args) { if (args < 1) { - ReplyToCommand(client, "Usage: sm_specmsg "); + ReplyToCommand(client, "Usage: sm_specconsole "); return Plugin_Handled; } @@ -450,4 +451,24 @@ public Action:Cmd_PrintToChat(client, args) SourceTV_PrintToChat(iTarget, "%s", sMsg); ReplyToCommand(client, "SourceTV sending chat message to spectator %d: %s", iTarget, sMsg); return Plugin_Handled; +} + +public Action:Cmd_PrintToConsole(client, args) +{ + if (args < 2) + { + ReplyToCommand(client, "Usage: sm_specconsoleone "); + return Plugin_Handled; + } + + new String:sIndex[16], String:sMsg[1024]; + GetCmdArg(1, sIndex, sizeof(sIndex)); + StripQuotes(sIndex); + GetCmdArg(2, sMsg, sizeof(sMsg)); + StripQuotes(sMsg); + + new iTarget = StringToInt(sIndex); + SourceTV_PrintToConsole(iTarget, "%s", sMsg); + ReplyToCommand(client, "SourceTV sending console message to spectator %d: %s", iTarget, sMsg); + return Plugin_Handled; } \ No newline at end of file diff --git a/sourcetvmanager.inc b/sourcetvmanager.inc index a5c099a..42b1304 100644 --- a/sourcetvmanager.inc +++ b/sourcetvmanager.inc @@ -404,6 +404,17 @@ native SourceTV_KickClient(client, const String:sReason[]); */ native SourceTV_PrintToChat(client, const String:format[], any:...); +/** + * Print a message to a single client's console. + * + * @param client The spectator client index. + * @param format The format string. + * @param ... Variable number of format string arguments. + * @noreturn + * @error Invalid client index or not connected. + */ +native SourceTV_PrintToConsole(client, const String:format[], any:...); + /** * Called when a spectator wants to connect to the SourceTV server. * This is called before any other validation has happened. @@ -531,5 +542,6 @@ public __ext_stvmngr_SetNTVOptional() MarkNativeAsOptional("SourceTV_GetClientPassword"); MarkNativeAsOptional("SourceTV_KickClient"); MarkNativeAsOptional("SourceTV_PrintToChat"); + MarkNativeAsOptional("SourceTV_PrintToConsole"); } #endif