extension.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Feb 20 15:24:04 2009 +1300 (2009-02-20)
changeset 0 4c53fd0b014e
permissions -rw-r--r--
IMPORT
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Sample 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: extension.cpp 249 2008-08-27 21:48:35Z pred $
    30  */
    31 
    32 #include "extension.h"
    33 
    34 /**
    35  * @file extension.cpp
    36  * @brief Implement extension code here.
    37  */
    38 
    39 Sample g_Sample;		/**< Global singleton for extension's main interface */
    40 
    41 SMEXT_LINK(&g_Sample);
    42 
    43 CDetour *runTeamLogicDetour;
    44 CDetour *playerByIndexDetour;
    45 
    46 IGameConfig *g_pGameConf = NULL;
    47 
    48 //edict_t *(*UTIL_PlayerByIndex_Actual)(int) = NULL;
    49 
    50 //void (CTFGameRules::* CTFGameRules::Arena_RunTeamLogic_Actual)(void);
    51 
    52 int TempMax = 0;
    53 CGlobalVars *gpGlobals;
    54 
    55 DETOUR_DECL_STATIC1(UTIL_PlayerByIndex, edict_t *, int, client)
    56 {
    57 	if (TempMax)
    58 	{
    59 		gpGlobals->maxClients = TempMax;
    60 	}
    61 
    62 	edict_t *pPlayer = DETOUR_STATIC_CALL(UTIL_PlayerByIndex)(client);
    63 
    64 	if (TempMax)
    65 	{
    66 		gpGlobals->maxClients = 48;
    67 	}
    68 
    69 	return pPlayer;
    70 }
    71 
    72 DETOUR_DECL_MEMBER0(Arena_RunTeamLogic, void)
    73 {
    74 	TempMax = gpGlobals->maxClients;
    75 	gpGlobals->maxClients = 48;
    76 
    77 	//(this->*Arena_RunTeamLogic_Actual)();
    78 	DETOUR_MEMBER_CALL(Arena_RunTeamLogic)();
    79 
    80 	gpGlobals->maxClients = TempMax;
    81 	TempMax = 0;
    82 }
    83 
    84 void SetupDetours()
    85 {
    86 	//void *callback = GET_MEMBER_CALLBACK(CTFGameRules, Arena_RunTeamLogic);
    87 	//void **trampoline = GET_MEMBER_TRAMPOLINE(CTFGameRules, Arena_RunTeamLogic);
    88 
    89 	runTeamLogicDetour = DETOUR_CREATE_MEMBER(Arena_RunTeamLogic, "RunTeamLogic");
    90 	runTeamLogicDetour->EnableDetour();
    91 
    92 	//void *callback = (void *)&UTIL_PlayerByIndex;
    93 	//void **trampoline = (void **)&UTIL_PlayerByIndex_Actual;
    94 
    95 	playerByIndexDetour = DETOUR_CREATE_STATIC(UTIL_PlayerByIndex, "PlayerByIndex");
    96 	playerByIndexDetour->EnableDetour();
    97 }
    98 
    99 void RemoveDetours()
   100 {
   101 	runTeamLogicDetour->Destroy();
   102 	playerByIndexDetour->Destroy();
   103 }
   104 
   105 bool Sample::SDK_OnLoad(char *error, size_t maxlength, bool late)
   106 {
   107 	char conf_error[255] = "";
   108 	if (!gameconfs->LoadGameConfigFile("runteamlogic", &g_pGameConf, conf_error, sizeof(conf_error)))
   109 	{
   110 		if (conf_error)
   111 		{
   112 			snprintf(error, maxlength, "Could not read runteamlogic.txt: %s", conf_error);
   113 		}
   114 		return false;
   115 	}
   116 
   117 	CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
   118 
   119 	SetupDetours();
   120 
   121 	return true;
   122 }
   123 
   124 void Sample::SDK_OnUnload()
   125 {
   126 	RemoveDetours();
   127 }
   128 
   129 bool Sample::SDK_OnMetamodLoad( ISmmAPI *ismm, char *error, size_t maxlength, bool late )
   130 {
   131 	gpGlobals = ismm->GetCGlobals();
   132 
   133 	return true;
   134 }