cssdm_main.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Sat Feb 08 18:38:34 2014 -0500 (2014-02-08)
changeset 167 6e95cf358424
parent 59 41feee1fac07
child 173 97ba0a6d9444
permissions -rw-r--r--
Remove the need for a UTIL_Remove call.
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * By David "BAILOPAN" Anderson
     6  * All rights reserved.
     7  * ===============================================================
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or (at
    12  * your option) any later version.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but
    15  * WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17  * General Public License for more details.
    18  * 
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; see the file COPYING; if not, write to the
    21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    22  * MA 02110-1301 USA
    23  * 
    24  * Version: $Id$
    25  */
    26 
    27 
    28 #include "cssdm_main.h"
    29 #include "cssdm_headers.h"
    30 #include "cssdm_utils.h"
    31 #include "cssdm_events.h"
    32 #include "cssdm_players.h"
    33 #include "cssdm_ctrl.h"
    34 #include "cssdm_config.h"
    35 #include "cssdm_ffa.h"
    36 #include "cssdm_callbacks.h"
    37 #include "cssdm_weapons.h"
    38 #include "cssdm_detours.h"
    39 #include "cssdm_version.h"
    40 
    41 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    42 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0)
    43 SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
    44 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, false, edict_t *, const CCommand &);
    45 
    46 Deathmatch g_DM;
    47 IGameEventManager2 *gameevents = NULL;
    48 IBaseFileSystem *basefilesystem = NULL;
    49 IBinTools *bintools = NULL;
    50 IGameConfig *g_pDmConf = NULL;
    51 CGlobalVars *gpGlobals = NULL;
    52 IPlayerInfoManager *playerinfomngr = NULL;
    53 IServerGameEnts *gameents = NULL;
    54 IServerGameClients *gameclients = NULL;
    55 ISourcePawnEngine *spengine = NULL;
    56 IBotManager *botmanager = NULL;
    57 char g_GlobError[255] = {0};
    58 bool g_IsLoadedOkay = false;
    59 bool g_Startup = false;
    60 bool g_IsInGlobalShutdown = false;
    61 
    62 SMEXT_LINK(&g_DM);
    63 
    64 #define VERIFY_SIGNATURE(name) \
    65 	if (!g_pDmConf->GetMemSig(name, &addr) || !addr) { \
    66 		snprintf(error, maxlength, "Could not find signature \"%s\"", name); \
    67 		return false; \
    68 	}
    69 
    70 #define VERIFY_OFFSET(name) \
    71 	if (!g_pDmConf->GetOffset(name, &offset) || !offset) { \
    72 		snprintf(error, maxlength, "Could not find offset \"%s\"", name); \
    73 		return false; \
    74 	}
    75 
    76 #define HOOK_EVENT2(name) \
    77 	if (!gameevents->AddListener(&g_cls_event_##name, #name, true)) { \
    78 		snprintf(error, maxlength, "Could not hook event \"%s\"", #name); \
    79 		return false; \
    80 	}
    81 
    82 #define UNHOOK_EVENT2(name) gameevents->RemoveListener(&g_cls_event_##name);
    83 
    84 bool Deathmatch::SDK_OnLoad(char *error, size_t maxlength, bool late)
    85 {
    86 	sharesys->AddDependency(myself, "bintools.ext", true, true);
    87 	sharesys->AddNatives(myself, g_BaseNatives);
    88 	if (!gameconfs->LoadGameConfigFile("cssdm.games", &g_pDmConf, error, maxlength))
    89 	{
    90 		return false;
    91 	}
    92 
    93 	void *addr;
    94 	int offset;
    95 	VERIFY_OFFSET("CSWeaponDropPatch");
    96 	VERIFY_SIGNATURE("UTIL_Remove");
    97 	VERIFY_SIGNATURE("RoundRespawn");
    98 	VERIFY_SIGNATURE("CSWeaponDrop");
    99 	VERIFY_SIGNATURE("DropWeapons");
   100 	VERIFY_OFFSET("Weapon_GetSlot");
   101 	VERIFY_OFFSET("RemoveAllItems");
   102 	VERIFY_OFFSET("DropWeaponsPatch");
   103 	VERIFY_OFFSET("GiveAmmo");
   104 
   105 	if (!DM_ParseWeapons(error, maxlength))
   106 	{
   107 		return false;
   108 	}
   109 
   110 	gpGlobals = g_SMAPI->GetCGlobals();
   111 
   112 	SM_InitConCommandBase();
   113 
   114 	spengine = g_pSM->GetScriptingEngine();
   115 
   116 	return true;
   117 }
   118 
   119 bool Deathmatch::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   120 {
   121 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
   122 	GET_V_IFACE_CURRENT(GetFileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
   123 	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   124 	GET_V_IFACE_ANY(GetServerFactory, gameents,IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   125 	GET_V_IFACE_ANY(GetServerFactory, botmanager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
   126 	GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   127 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
   128 
   129 	return true;
   130 }
   131 
   132 void OnDLLShutdown()
   133 {
   134 	g_IsInGlobalShutdown = true;
   135 	RETURN_META(MRES_IGNORED);
   136 }
   137 
   138 void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   139 {
   140 	OnLevelInitialized();
   141 	RETURN_META(MRES_IGNORED);
   142 }
   143 
   144 void LevelShutdown()
   145 {
   146 	OnLevelEnd();
   147 	RETURN_META(MRES_IGNORED);
   148 }
   149 
   150 bool Startup(char *error, size_t maxlength)
   151 {
   152 	playerhelpers->AddClientListener(&g_ClientListener);
   153 
   154 	HOOK_EVENT2(player_death);
   155 	HOOK_EVENT2(player_spawn);
   156 	HOOK_EVENT2(player_team);
   157 	HOOK_EVENT2(round_start);
   158 	HOOK_EVENT2(round_end);
   159 	HOOK_EVENT2(server_shutdown);
   160 	HOOK_EVENT2(item_pickup);
   161 
   162 	g_Startup = true;
   163 
   164 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   165 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   166 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   167 	SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   168 
   169 	DM_InitCallbacks();
   170 
   171 	char ffa_error[255];
   172 	if (!DM_Prepare_FFA(ffa_error, sizeof(ffa_error)))
   173 	{
   174 		g_pSM->LogError(myself, "FFA will not work: %s", ffa_error);
   175 	}
   176 
   177 	DM_InitDetours();
   178 
   179 	return InitializeUtils(error, maxlength);
   180 }
   181 
   182 void Shutdown()
   183 {
   184 	/* Remove hooks that could have happened whether we got started up or not */
   185 	playerhelpers->RemoveClientListener(&g_ClientListener);
   186 
   187 	UNHOOK_EVENT2(player_death);
   188 	UNHOOK_EVENT2(player_spawn);
   189 	UNHOOK_EVENT2(player_team);
   190 	UNHOOK_EVENT2(round_start);
   191 	UNHOOK_EVENT2(round_end);
   192 	UNHOOK_EVENT2(server_shutdown);
   193 	UNHOOK_EVENT2(item_pickup);
   194 
   195 	/* If we were never started up, the rest of this is invalid */
   196 	if (!g_Startup)
   197 	{
   198 		return;
   199 	}
   200 
   201 	/* Make sure stuff like FFA is cleaned up */
   202 	DM_Disable();
   203 
   204 	/* Destroy various internal things */
   205 	DM_ShutdownDetours();
   206 	DM_ShutdownCallbacks();
   207 	DM_FreeWeapons();
   208 	ShutdownUtils();
   209 
   210 	/* Unhook everything from SourceHook */
   211 	SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   212 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   213 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   214 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   215 }
   216 
   217 void Deathmatch::SDK_OnAllLoaded()
   218 {
   219 	SM_GET_LATE_IFACE(BINTOOLS, bintools);
   220 
   221 	g_IsLoadedOkay = Startup(g_GlobError, sizeof(g_GlobError));
   222 
   223 	if (!QueryRunning(NULL, 0))
   224 	{
   225 		return;
   226 	}
   227 }
   228 
   229 void Deathmatch::SDK_OnUnload()
   230 {
   231 	DM_FreeWeapons();
   232 	Shutdown();
   233 	gameconfs->CloseGameConfigFile(g_pDmConf);
   234 }
   235 
   236 bool Deathmatch::QueryRunning(char *error, size_t maxlength)
   237 {
   238 	SM_CHECK_IFACE(BINTOOLS, bintools);
   239 
   240 	if (!g_IsLoadedOkay && g_GlobError[0] != '\0')
   241 	{
   242 		snprintf(error, maxlength, "%s", g_GlobError);
   243 		return false;
   244 	}
   245 
   246 	return true;
   247 }
   248 
   249 bool Deathmatch::QueryInterfaceDrop(SMInterface *pInterface)
   250 {
   251 	if (pInterface == bintools)
   252 	{
   253 		return false;
   254 	}
   255 
   256 	return true;
   257 }
   258 
   259 void Deathmatch::NotifyInterfaceDrop(SMInterface *pInterface)
   260 {
   261 	/* We have to take care of bintools early then... */
   262 	if (pInterface == bintools)
   263 	{
   264 		ShutdownUtils();
   265 		bintools = NULL;
   266 	}
   267 }
   268 
   269 const char *Deathmatch::GetExtensionVerString()
   270 {
   271 	return CSSDM_FULL_VERSION;
   272 }
   273