core/smn_hudtext.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jan 22 21:43:12 2013 -0500 (2013-01-22)
changeset 3743 a98686a77974
parent 3010 8ea042c00c80
child 3855 d8ba5eddf543
permissions -rw-r--r--
Added initial support for protobuf usermessages on CS:GO (bug 5579, r=asherkin).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include <IGameConfigs.h>
    34 #include "UserMessages.h"
    35 #include "TimerSys.h"
    36 #include "PlayerManager.h"
    37 #include "HandleSys.h"
    38 #include "logic_bridge.h"
    39 
    40 #if SOURCE_ENGINE == SE_CSGO
    41 #include <game/shared/csgo/protobuf/cstrike15_usermessages.pb.h>
    42 #endif
    43 
    44 #define MAX_HUD_CHANNELS		6
    45 
    46 int g_HudMsgNum = -1;
    47 
    48 struct hud_syncobj_t
    49 {
    50 	int player_channels[256 + 1];
    51 };
    52 
    53 struct player_chaninfo_t
    54 {
    55 	double chan_times[MAX_HUD_CHANNELS];
    56 	hud_syncobj_t *chan_syncobjs[MAX_HUD_CHANNELS];
    57 };
    58 
    59 struct hud_text_parms
    60 {
    61 	float       x;
    62 	float       y;
    63 	int         effect;
    64 	byte        r1, g1, b1, a1;
    65 	byte        r2, g2, b2, a2;
    66 	float       fadeinTime;
    67 	float       fadeoutTime;
    68 	float       holdTime;
    69 	float       fxTime;
    70 	int         channel;
    71 };
    72 
    73 class HudMsgHelpers : 
    74 	public SMGlobalClass,
    75 	public IHandleTypeDispatch,
    76 	public IClientListener
    77 {
    78 public:
    79 	bool IsSupported()
    80 	{
    81 		return (g_HudMsgNum != -1);
    82 	}
    83 
    84 	virtual void OnSourceModAllInitialized_Post()
    85 	{
    86 		const char *key;
    87 
    88 		key = g_pGameConf->GetKeyValue("HudTextMsg");
    89 		if (key != NULL)
    90 		{
    91 			g_HudMsgNum = g_UserMsgs.GetMessageIndex(key);
    92 		}
    93 
    94 		if (!IsSupported())
    95 		{
    96 			m_hHudSyncObj = 0;
    97 			m_PlayerHuds = NULL;
    98 			return;
    99 		}
   100 
   101 		m_PlayerHuds = new player_chaninfo_t[256+1];
   102 		m_hHudSyncObj = g_HandleSys.CreateType("HudSyncObj", this, 0, NULL, NULL, g_pCoreIdent, NULL);
   103 
   104 		g_Players.AddClientListener(this);
   105 	}
   106 
   107 	virtual void OnSourceModShutdown()
   108 	{
   109 		if (!IsSupported())
   110 		{
   111 			return;
   112 		}
   113 
   114 		delete [] m_PlayerHuds;
   115 		g_HandleSys.RemoveType(m_hHudSyncObj, g_pCoreIdent);
   116 
   117 		g_Players.RemoveClientListener(this);
   118 	}
   119 
   120 	virtual void OnHandleDestroy(HandleType_t type, void *object)
   121 	{
   122 		hud_syncobj_t *obj = (hud_syncobj_t *)object;
   123 
   124 		delete obj;
   125 	}
   126 
   127 	virtual bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
   128 	{
   129 		*pSize = sizeof(unsigned int) * g_Players.GetMaxClients();
   130 		return true;
   131 	}
   132 
   133 	virtual void OnClientConnected(int client)
   134 	{
   135 		player_chaninfo_t *player;
   136 
   137 		player = &m_PlayerHuds[client];
   138 		memset(player->chan_syncobjs, 0, sizeof(player->chan_syncobjs));
   139 		memset(player->chan_times, 0, sizeof(player->chan_times));
   140 	}
   141 
   142 	Handle_t CreateHudSyncObj(IdentityToken_t *pIdent)
   143 	{
   144 		Handle_t hndl;
   145 		HandleError err;
   146 		hud_syncobj_t *obj;
   147 		HandleSecurity sec;
   148 
   149 		obj = new hud_syncobj_t;
   150 
   151 		memset(obj->player_channels, 0, sizeof(obj->player_channels));
   152 
   153 		sec = HandleSecurity(pIdent, g_pCoreIdent);
   154 		
   155 		if ((hndl = g_HandleSys.CreateHandleEx(m_hHudSyncObj, obj, &sec, NULL, &err))
   156 			== BAD_HANDLE)
   157 		{
   158 			delete obj;
   159 		}
   160 
   161 		return hndl;
   162 	}
   163 
   164 	HandleError ReadHudSyncObj(Handle_t hndl, 
   165 		IdentityToken_t *pOwner, 
   166 		hud_syncobj_t **pObj)
   167 	{
   168 		HandleSecurity sec(pOwner, g_pCoreIdent);
   169 		return g_HandleSys.ReadHandle(hndl, m_hHudSyncObj, &sec, (void **)pObj);
   170 	}
   171 
   172 	unsigned int AutoSelectChannel(unsigned int client)
   173 	{
   174 		int last_channel;
   175 		player_chaninfo_t *player;
   176 
   177 		player = &m_PlayerHuds[client];
   178 
   179 		last_channel = 0;
   180 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   181 		{
   182 			if (player->chan_times[i] < player->chan_times[last_channel])
   183 			{
   184 				last_channel = i;
   185 			}
   186 		}
   187 
   188 		ManualSelectChannel(client, last_channel);
   189 
   190 		return last_channel;
   191 	}
   192 
   193 	int TryReuseLastChannel(unsigned int client, hud_syncobj_t *obj)
   194 	{
   195 		int last_channel;
   196 		player_chaninfo_t *player;
   197 
   198 		player = &m_PlayerHuds[client];
   199 
   200 		/* First, see if we can re-use the previous channel. */
   201 		last_channel = obj->player_channels[client];
   202 
   203 		if (player->chan_syncobjs[last_channel] == obj)
   204 		{
   205 			player->chan_times[last_channel] = *g_pUniversalTime;
   206 			return last_channel;
   207 		}
   208 
   209 		return -1;
   210 	}
   211 
   212 	int AutoSelectChannel(unsigned int client, hud_syncobj_t *obj)
   213 	{
   214 		int last_channel;
   215 		player_chaninfo_t *player;
   216 
   217 		player = &m_PlayerHuds[client];
   218 		
   219 		/* First, see if we can re-use the previous channel. */
   220 		last_channel = obj->player_channels[client];
   221 
   222 		if (player->chan_syncobjs[last_channel] == obj)
   223 		{
   224 			player->chan_times[last_channel] = *g_pUniversalTime;
   225 			return last_channel;
   226 		}
   227 
   228 		last_channel = 0;
   229 		for (unsigned int i = 1; i < MAX_HUD_CHANNELS; i++)
   230 		{
   231 			if (player->chan_times[i] < player->chan_times[last_channel])
   232 			{
   233 				last_channel = i;
   234 			}
   235 		}
   236 
   237 		obj->player_channels[client] = last_channel;
   238 		player->chan_syncobjs[last_channel] = obj;
   239 		player->chan_times[last_channel] = *g_pUniversalTime;
   240 
   241 		return last_channel;
   242 	}
   243 
   244 	int ManualSelectChannel(unsigned int client, int channel)
   245 	{
   246 		player_chaninfo_t *player;
   247 
   248 		player = &m_PlayerHuds[client];
   249 		player->chan_times[channel] = *g_pUniversalTime;
   250 		player->chan_syncobjs[channel] = NULL;
   251 
   252 		return channel;
   253 	}
   254 private:
   255 	HandleType_t m_hHudSyncObj;
   256 	player_chaninfo_t *m_PlayerHuds;
   257 } s_HudMsgHelpers;
   258 
   259 hud_text_parms g_hud_params;
   260 
   261 static cell_t CreateHudSynchronizer(IPluginContext *pContext, const cell_t *params)
   262 {
   263 	return s_HudMsgHelpers.CreateHudSyncObj(pContext->GetIdentity());
   264 }
   265 
   266 static cell_t SetHudTextParams(IPluginContext *pContext, const cell_t *params)
   267 {
   268 	g_hud_params.x = sp_ctof(params[1]);
   269 	g_hud_params.y = sp_ctof(params[2]);
   270 	g_hud_params.holdTime = sp_ctof(params[3]);
   271 	g_hud_params.r1 = static_cast<byte>(params[4]);
   272 	g_hud_params.g1 = static_cast<byte>(params[5]);
   273 	g_hud_params.b1 = static_cast<byte>(params[6]);
   274 	g_hud_params.a1 = static_cast<byte>(params[7]);
   275 	g_hud_params.effect = params[8];
   276 	g_hud_params.fxTime = sp_ctof(params[9]);
   277 	g_hud_params.fadeinTime = sp_ctof(params[10]);
   278 	g_hud_params.fadeoutTime = sp_ctof(params[11]);
   279 	g_hud_params.r2 = 255;
   280 	g_hud_params.g2 = 255;
   281 	g_hud_params.b2 = 250;
   282 	g_hud_params.a2 = 0;
   283 
   284 	return 1;
   285 }
   286 
   287 static cell_t SetHudTextParamsEx(IPluginContext *pContext, const cell_t *params)
   288 {
   289 	cell_t *color1, *color2;
   290 
   291 	pContext->LocalToPhysAddr(params[4], &color1);
   292 	pContext->LocalToPhysAddr(params[5], &color2);
   293 
   294 	g_hud_params.x = sp_ctof(params[1]);
   295 	g_hud_params.y = sp_ctof(params[2]);
   296 	g_hud_params.holdTime = sp_ctof(params[3]);
   297 	g_hud_params.r1 = static_cast<byte>(color1[0]);
   298 	g_hud_params.g1 = static_cast<byte>(color1[1]);
   299 	g_hud_params.b1 = static_cast<byte>(color1[2]);
   300 	g_hud_params.a1 = static_cast<byte>(color1[3]);
   301 	g_hud_params.effect = params[6];
   302 	g_hud_params.fxTime = sp_ctof(params[7]);
   303 	g_hud_params.fadeinTime = sp_ctof(params[8]);
   304 	g_hud_params.fadeoutTime = sp_ctof(params[9]);
   305 	g_hud_params.r2 = static_cast<byte>(color2[0]);
   306 	g_hud_params.g2 = static_cast<byte>(color2[1]);
   307 	g_hud_params.b2 = static_cast<byte>(color2[2]);
   308 	g_hud_params.a2 = static_cast<byte>(color2[3]);
   309 
   310 	return 1;
   311 }
   312 
   313 void UTIL_SendHudText(int client, const hud_text_parms &textparms, const char *pMessage)
   314 {
   315 	cell_t players[1];
   316 
   317 	players[0] = client;
   318 
   319 #if SOURCE_ENGINE == SE_CSGO
   320 	// If or when we need to support multiple games per engine with this, we can switch to reflection
   321 	CCSUsrMsg_HudMsg *msg = (CCSUsrMsg_HudMsg *)g_UserMsgs.StartProtobufMessage(g_HudMsgNum, players, 1, 0);
   322 	msg->set_channel(textparms.channel & 0xFF);
   323 
   324 	CMsgVector2D *pos = msg->mutable_pos();
   325 	pos->set_x(textparms.x);
   326 	pos->set_y(textparms.y);
   327 
   328 	CMsgRGBA *color1 = msg->mutable_clr1();
   329 	color1->set_r(textparms.r1);
   330 	color1->set_g(textparms.g1);
   331 	color1->set_b(textparms.b1);
   332 	color1->set_a(textparms.a1);
   333 
   334 	CMsgRGBA *color2 = msg->mutable_clr2();
   335 	color2->set_r(textparms.r2);
   336 	color2->set_g(textparms.g2);
   337 	color2->set_b(textparms.b2);
   338 	color2->set_a(textparms.a2);
   339 
   340 	msg->set_effect(textparms.effect);
   341 	msg->set_fade_in_time(textparms.fadeinTime);
   342 	msg->set_fade_out_time(textparms.fadeoutTime);
   343 	msg->set_hold_time(textparms.holdTime);
   344 	msg->set_fx_time(textparms.fxTime);
   345 	msg->set_text(pMessage);
   346 #else
   347 	bf_write *bf = g_UserMsgs.StartBitBufMessage(g_HudMsgNum, players, 1, 0);
   348 	bf->WriteByte(textparms.channel & 0xFF );
   349 	bf->WriteFloat(textparms.x);
   350 	bf->WriteFloat(textparms.y);
   351 	bf->WriteByte(textparms.r1);
   352 	bf->WriteByte(textparms.g1);
   353 	bf->WriteByte(textparms.b1);
   354 	bf->WriteByte(textparms.a1);
   355 	bf->WriteByte(textparms.r2);
   356 	bf->WriteByte(textparms.g2);
   357 	bf->WriteByte(textparms.b2);
   358 	bf->WriteByte(textparms.a2);
   359 	bf->WriteByte(textparms.effect);
   360 	bf->WriteFloat(textparms.fadeinTime);
   361 	bf->WriteFloat(textparms.fadeoutTime);
   362 	bf->WriteFloat(textparms.holdTime);
   363 	bf->WriteFloat(textparms.fxTime);
   364 	bf->WriteString(pMessage);
   365 #endif
   366 	g_UserMsgs.EndMessage();
   367 }
   368 
   369 static cell_t ShowSyncHudText(IPluginContext *pContext, const cell_t *params)
   370 {
   371 	int client;
   372 	Handle_t err;
   373 	CPlayer *pPlayer;
   374 	hud_syncobj_t *obj;
   375 	char message_buffer[255-36];
   376 
   377 	if (!s_HudMsgHelpers.IsSupported())
   378 	{
   379 		return -1;
   380 	}
   381 
   382 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   383 	{
   384 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   385 	}
   386 
   387 	client = params[1];
   388 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   389 	{
   390 		return pContext->ThrowNativeError("Invalid client index %d", client);
   391 	}
   392 	else if (!pPlayer->IsInGame())
   393 	{
   394 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   395 	}
   396 
   397 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   398 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   399 	{
   400 		return 0;
   401 	}
   402 
   403 	g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client, obj);
   404 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   405 
   406 	return 1;
   407 }
   408 
   409 static cell_t ClearSyncHud(IPluginContext *pContext, const cell_t *params)
   410 {
   411 	int client;
   412 	int channel;
   413 	Handle_t err;
   414 	CPlayer *pPlayer;
   415 	hud_syncobj_t *obj;
   416 
   417 	if (!s_HudMsgHelpers.IsSupported())
   418 	{
   419 		return -1;
   420 	}
   421 
   422 	if ((err = s_HudMsgHelpers.ReadHudSyncObj(params[2], pContext->GetIdentity(), &obj)) != HandleError_None)
   423 	{
   424 		return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
   425 	}
   426 
   427 	client = params[1];
   428 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   429 	{
   430 		return pContext->ThrowNativeError("Invalid client index %d", client);
   431 	}
   432 	else if (!pPlayer->IsInGame())
   433 	{
   434 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   435 	}
   436 
   437 	if ((channel = s_HudMsgHelpers.TryReuseLastChannel(client, obj)) == -1)
   438 	{
   439 		return -1;
   440 	}
   441 
   442 	g_hud_params.channel = channel;
   443 	UTIL_SendHudText(client, g_hud_params, "");
   444 
   445 	return g_hud_params.channel;
   446 }
   447 
   448 static cell_t ShowHudText(IPluginContext *pContext, const cell_t *params)
   449 {
   450 	int client;
   451 	CPlayer *pPlayer;
   452 	char message_buffer[255-36];
   453 
   454 	if (!s_HudMsgHelpers.IsSupported())
   455 	{
   456 		return -1;
   457 	}
   458 
   459 	client = params[1];
   460 	if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   461 	{
   462 		return pContext->ThrowNativeError("Invalid client index %d", client);
   463 	}
   464 	else if (!pPlayer->IsInGame())
   465 	{
   466 		return pContext->ThrowNativeError("Client %d is not in-game", client);
   467 	}
   468 
   469 	g_SourceMod.FormatString(message_buffer, sizeof(message_buffer), pContext, params, 3);
   470 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   471 	{
   472 		return 0;
   473 	}
   474 
   475 	if (params[2] == -1)
   476 	{
   477 		g_hud_params.channel = s_HudMsgHelpers.AutoSelectChannel(client);
   478 	}
   479 	else
   480 	{
   481 		g_hud_params.channel = params[2] % MAX_HUD_CHANNELS;
   482 		s_HudMsgHelpers.ManualSelectChannel(client, g_hud_params.channel);
   483 	}
   484 
   485 	UTIL_SendHudText(client, g_hud_params, message_buffer);
   486 
   487 	return g_hud_params.channel;
   488 }
   489 
   490 REGISTER_NATIVES(hudNatives)
   491 {
   492 	{"ClearSyncHud",				ClearSyncHud},
   493 	{"CreateHudSynchronizer",		CreateHudSynchronizer},
   494 	{"SetHudTextParams",			SetHudTextParams},
   495 	{"SetHudTextParamsEx",			SetHudTextParamsEx},
   496 	{"ShowHudText",					ShowHudText},
   497 	{"ShowSyncHudText",				ShowSyncHudText},
   498 	{NULL,							NULL},
   499 };