extensions/cstrike/natives.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jul 12 03:19:55 2008 +0000 (2008-07-12)
changeset 2198 22752d8c5cc3
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
fixed cstrike build
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Counter-Strike:Source Extension
     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 "extension.h"
    33 #include "RegNatives.h"
    34 
    35 #define REGISTER_NATIVE_ADDR(name, code) \
    36 	void *addr; \
    37 	if (!g_pGameConf->GetMemSig(name, &addr)) \
    38 	{ \
    39 		return pContext->ThrowNativeError("Failed to locate function"); \
    40 	} \
    41 	code; \
    42 	g_RegNatives.Register(pWrapper);
    43 
    44 inline CBaseEntity *GetCBaseEntity(int num, bool isplayer)
    45 {
    46 	edict_t *pEdict = engine->PEntityOfEntIndex(num);
    47 	if (!pEdict || pEdict->IsFree())
    48 	{
    49 		return NULL;
    50 	}
    51 
    52 	if (num > 0 && num <= playerhelpers->GetMaxClients())
    53 	{
    54 		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
    55 		if (!pPlayer || !pPlayer->IsConnected())
    56 		{
    57 			return NULL;
    58 		}
    59 	}
    60 	else if (isplayer) 
    61 	{
    62 		return NULL;
    63 	}
    64 
    65 	IServerUnknown *pUnk;
    66 	if ((pUnk=pEdict->GetUnknown()) == NULL)
    67 	{
    68 		return NULL;
    69 	}
    70 
    71 	return pUnk->GetBaseEntity();
    72 }
    73 
    74 static cell_t CS_PrintHintText(IPluginContext *pContext, const cell_t *params)
    75 {
    76 	int client = params[1];
    77 	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
    78 
    79 	if (!pPlayer)
    80 	{
    81 		return pContext->ThrowNativeError("Client index %d is invalid", client);
    82 	}
    83 
    84 	if (!pPlayer->IsInGame())
    85 	{
    86 		return pContext->ThrowNativeError("Client %d is not in game", client);
    87 	}
    88 
    89 	g_pSM->SetGlobalTarget(client);
    90 
    91 	char buffer[192];
    92 	g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 2);
    93 
    94 	/* Check for an error before printing to the client */
    95 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
    96 	{
    97 		return 0;
    98 	}
    99 
   100 	bf_write *pBitBuf = usermsgs->StartMessage(g_msgHintText, &params[1], 1, USERMSG_RELIABLE);
   101 	if (pBitBuf == NULL)
   102 	{
   103 		return pContext->ThrowNativeError("Could not send a usermessage");
   104 	}
   105 	pBitBuf->WriteByte(1);
   106 	pBitBuf->WriteString(buffer);
   107 	usermsgs->EndMessage();
   108 
   109 	return 1;
   110 }
   111 
   112 
   113 static cell_t CS_RespawnPlayer(IPluginContext *pContext, const cell_t *params)
   114 {
   115 	static ICallWrapper *pWrapper = NULL;
   116 	if (!pWrapper)
   117 	{
   118 		REGISTER_NATIVE_ADDR("RoundRespawn", 
   119 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, NULL, 0));
   120 	}
   121 
   122 	CBaseEntity *pEntity;
   123 	if (!(pEntity=GetCBaseEntity(params[1], true)))
   124 	{
   125 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   126 	}
   127 	pWrapper->Execute(&pEntity, NULL);
   128 
   129 	return 1;
   130 }
   131 
   132 static cell_t CS_SwitchTeam(IPluginContext *pContext, const cell_t *params)
   133 {
   134 	static ICallWrapper *pWrapper = NULL;
   135 	if (!pWrapper)
   136 	{
   137 		REGISTER_NATIVE_ADDR("SwitchTeam", 
   138 			PassInfo pass[1]; \
   139 			pass[0].flags = PASSFLAG_BYVAL; \
   140 			pass[0].size = sizeof(int); \
   141 			pass[0].type = PassType_Basic; \
   142 			pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, NULL, pass, 1))
   143 	}
   144 
   145 	CBaseEntity *pEntity;
   146 	if (!(pEntity=GetCBaseEntity(params[1], true)))
   147 	{
   148 		return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
   149 	}
   150 
   151 	unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
   152 	unsigned char *vptr = vstk;
   153 
   154 	*(CBaseEntity **)vptr = pEntity;
   155 	vptr += sizeof(CBaseEntity *);
   156 	*(int *)vptr = params[2];
   157 	pWrapper->Execute(vstk, NULL);
   158 
   159 	return 1;
   160 }
   161 
   162 sp_nativeinfo_t g_CSNatives[] = 
   163 {
   164 	{"CS_RespawnPlayer",			CS_RespawnPlayer}, 
   165 	{"CS_SwitchTeam",				CS_SwitchTeam}, 
   166 	{NULL,							NULL}
   167 };
   168 
   169 sp_nativeinfo_t g_CS_PrintHintText[] = 
   170 {
   171 	{"PrintHintText",				CS_PrintHintText},
   172 	{NULL,							NULL},
   173 };