cssdm_main.cpp
changeset 0 67dcd379a2d5
child 7 1107793f4b1c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cssdm_main.cpp	Sun Jun 24 21:58:09 2007 +0000
     1.3 @@ -0,0 +1,245 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     1.8 + * By David "BAILOPAN" Anderson
     1.9 + * All rights reserved.
    1.10 + * ===============================================================
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify
    1.13 + * it under the terms of the GNU General Public License as published by
    1.14 + * the Free Software Foundation; either version 2 of the License, or (at
    1.15 + * your option) any later version.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but
    1.18 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.20 + * General Public License for more details.
    1.21 + * 
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; see the file COPYING; if not, write to the
    1.24 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    1.25 + * MA 02110-1301 USA
    1.26 + * 
    1.27 + * Version: $Id$
    1.28 + */
    1.29 +
    1.30 +
    1.31 +#include "cssdm_main.h"
    1.32 +#include "cssdm_headers.h"
    1.33 +#include "cssdm_utils.h"
    1.34 +#include "cssdm_events.h"
    1.35 +#include "cssdm_players.h"
    1.36 +#include "cssdm_ctrl.h"
    1.37 +#include "cssdm_config.h"
    1.38 +#include "cssdm_ffa.h"
    1.39 +#include "cssdm_callbacks.h"
    1.40 +#include "cssdm_weapons.h"
    1.41 +#include "cssdm_detours.h"
    1.42 +
    1.43 +SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
    1.44 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0)
    1.45 +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
    1.46 +SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, false, edict_t *);
    1.47 +
    1.48 +Deathmatch g_DM;
    1.49 +IGameEventManager2 *gameevents = NULL;
    1.50 +IBaseFileSystem *basefilesystem = NULL;
    1.51 +IBinTools *bintools = NULL;
    1.52 +IGameConfig *g_pDmConf = NULL;
    1.53 +CGlobalVars *gpGlobals = NULL;
    1.54 +IPlayerInfoManager *playerinfomngr = NULL;
    1.55 +IServerGameEnts *gameents = NULL;
    1.56 +IServerGameClients *gameclients = NULL;
    1.57 +ISourcePawnEngine *spengine = NULL;
    1.58 +IBotManager *botmanager = NULL;
    1.59 +char g_GlobError[255] = {0};
    1.60 +bool g_IsLoadedOkay = false;
    1.61 +bool g_Startup = false;
    1.62 +bool g_IsInGlobalShutdown = false;
    1.63 +
    1.64 +SMEXT_LINK(&g_DM);
    1.65 +
    1.66 +#define VERIFY_SIGNATURE(name) \
    1.67 +	if (!g_pDmConf->GetMemSig(name, &addr) || !addr) { \
    1.68 +		snprintf(error, maxlength, "Could not find signature \"%s\"", name); \
    1.69 +		return false; \
    1.70 +	}
    1.71 +
    1.72 +#define VERIFY_OFFSET(name) \
    1.73 +	if (!g_pDmConf->GetOffset(name, &offset) || !offset) { \
    1.74 +		snprintf(error, maxlength, "Could not find offset \"%s\"", offset); \
    1.75 +		return false; \
    1.76 +	}
    1.77 +
    1.78 +#define HOOK_EVENT2(name) \
    1.79 +	if (!gameevents->AddListener(&g_cls_event_##name, #name, true)) { \
    1.80 +		snprintf(error, maxlength, "Could not hook event \"%s\"", #name); \
    1.81 +		return false; \
    1.82 +	}
    1.83 +
    1.84 +#define UNHOOK_EVENT2(name) gameevents->RemoveListener(&g_cls_event_##name);
    1.85 +
    1.86 +bool Deathmatch::SDK_OnLoad(char *error, size_t maxlength, bool late)
    1.87 +{
    1.88 +	sharesys->AddDependency(myself, "bintools.ext", true, true);
    1.89 +	sharesys->AddNatives(myself, g_BaseNatives);
    1.90 +	if (!gameconfs->LoadGameConfigFile("cssdm.games", &g_pDmConf, error, maxlength))
    1.91 +	{
    1.92 +		return false;
    1.93 +	}
    1.94 +
    1.95 +	void *addr;
    1.96 +	int offset;
    1.97 +	VERIFY_SIGNATURE("UTIL_Remove");
    1.98 +	VERIFY_SIGNATURE("RoundRespawn");
    1.99 +	VERIFY_SIGNATURE("CSWeaponDrop");
   1.100 +	VERIFY_SIGNATURE("DropWeapons");
   1.101 +	VERIFY_OFFSET("Weapon_GetSlot");
   1.102 +	VERIFY_OFFSET("RemoveAllItems");
   1.103 +	VERIFY_OFFSET("DropWeaponsPatch");
   1.104 +	VERIFY_OFFSET("CSWeaponDropPatch");
   1.105 +	VERIFY_OFFSET("GiveAmmo");
   1.106 +
   1.107 +	if (!DM_ParseWeapons(error, maxlength))
   1.108 +	{
   1.109 +		return false;
   1.110 +	}
   1.111 +
   1.112 +	gpGlobals = g_SMAPI->pGlobals();
   1.113 +
   1.114 +	SM_InitConCommandBase();
   1.115 +
   1.116 +	spengine = g_pSM->GetScriptingEngine();
   1.117 +
   1.118 +	return true;
   1.119 +}
   1.120 +
   1.121 +bool Deathmatch::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   1.122 +{
   1.123 +	GET_V_IFACE_CURRENT(engineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
   1.124 +	GET_V_IFACE_CURRENT(fileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
   1.125 +	GET_V_IFACE_ANY(serverFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   1.126 +	GET_V_IFACE_ANY(serverFactory, gameents,IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   1.127 +	GET_V_IFACE_ANY(serverFactory, botmanager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
   1.128 +	GET_V_IFACE_ANY(serverFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   1.129 +
   1.130 +	return true;
   1.131 +}
   1.132 +
   1.133 +void OnDLLShutdown()
   1.134 +{
   1.135 +	g_IsInGlobalShutdown = true;
   1.136 +	RETURN_META(MRES_IGNORED);
   1.137 +}
   1.138 +
   1.139 +void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   1.140 +{
   1.141 +	OnLevelInitialized();
   1.142 +	RETURN_META(MRES_IGNORED);
   1.143 +}
   1.144 +
   1.145 +void LevelShutdown()
   1.146 +{
   1.147 +	OnLevelEnd();
   1.148 +	RETURN_META(MRES_IGNORED);
   1.149 +}
   1.150 +
   1.151 +bool Startup(char *error, size_t maxlength)
   1.152 +{
   1.153 +	playerhelpers->AddClientListener(&g_ClientListener);
   1.154 +
   1.155 +	HOOK_EVENT2(player_death);
   1.156 +	HOOK_EVENT2(player_spawn);
   1.157 +	HOOK_EVENT2(player_team);
   1.158 +	HOOK_EVENT2(round_start);
   1.159 +	HOOK_EVENT2(round_end);
   1.160 +	HOOK_EVENT2(server_shutdown);
   1.161 +	HOOK_EVENT2(item_pickup);
   1.162 +
   1.163 +	g_Startup = true;
   1.164 +
   1.165 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   1.166 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   1.167 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   1.168 +	SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   1.169 +
   1.170 +	DM_InitCallbacks();
   1.171 +
   1.172 +	char ffa_error[255];
   1.173 +	if (!DM_Prepare_FFA(ffa_error, sizeof(ffa_error)))
   1.174 +	{
   1.175 +		g_pSM->LogError(myself, "FFA will not work: %s", ffa_error);
   1.176 +	}
   1.177 +
   1.178 +	DM_InitDetours();
   1.179 +
   1.180 +	return InitializeUtils(error, maxlength);
   1.181 +}
   1.182 +
   1.183 +void Shutdown()
   1.184 +{
   1.185 +	/* Remove hooks that could have happened whether we got started up or not */
   1.186 +	playerhelpers->RemoveClientListener(&g_ClientListener);
   1.187 +
   1.188 +	UNHOOK_EVENT2(player_death);
   1.189 +	UNHOOK_EVENT2(player_spawn);
   1.190 +	UNHOOK_EVENT2(player_team);
   1.191 +	UNHOOK_EVENT2(round_start);
   1.192 +	UNHOOK_EVENT2(round_end);
   1.193 +	UNHOOK_EVENT2(server_shutdown);
   1.194 +	UNHOOK_EVENT2(item_pickup);
   1.195 +
   1.196 +	/* If we were never started up, the rest of this is invalid */
   1.197 +	if (!g_Startup)
   1.198 +	{
   1.199 +		return;
   1.200 +	}
   1.201 +
   1.202 +	/* Make sure stuff like FFA is cleaned up */
   1.203 +	DM_Disable();
   1.204 +
   1.205 +	/* Destroy various internal things */
   1.206 +	DM_ShutdownDetours();
   1.207 +	DM_ShutdownCallbacks();
   1.208 +	DM_FreeWeapons();
   1.209 +	ShutdownUtils();
   1.210 +
   1.211 +	/* Unhook everything from SourceHook */
   1.212 +	SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   1.213 +	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   1.214 +	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   1.215 +	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   1.216 +}
   1.217 +
   1.218 +void Deathmatch::SDK_OnAllLoaded()
   1.219 +{
   1.220 +	SM_GET_LATE_IFACE(BINTOOLS, bintools);
   1.221 +
   1.222 +	g_IsLoadedOkay = Startup(g_GlobError, sizeof(g_GlobError));
   1.223 +
   1.224 +	if (!QueryRunning(NULL, 0))
   1.225 +	{
   1.226 +		return;
   1.227 +	}
   1.228 +}
   1.229 +
   1.230 +void Deathmatch::SDK_OnUnload()
   1.231 +{
   1.232 +	DM_FreeWeapons();
   1.233 +	Shutdown();
   1.234 +	gameconfs->CloseGameConfigFile(g_pDmConf);
   1.235 +}
   1.236 +
   1.237 +bool Deathmatch::QueryRunning(char *error, size_t maxlength)
   1.238 +{
   1.239 +	SM_CHECK_IFACE(BINTOOLS, bintools);
   1.240 +
   1.241 +	if (!g_IsLoadedOkay && g_GlobError[0] != '\0')
   1.242 +	{
   1.243 +		snprintf(error, maxlength, "%s", g_GlobError);
   1.244 +		return false;
   1.245 +	}
   1.246 +
   1.247 +	return true;
   1.248 +}