cssdm_main.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Wed Oct 01 11:50:24 2014 -0400 (2014-10-01)
changeset 193 277c856152b0
parent 59 41feee1fac07
permissions -rw-r--r--
Update linix CS:GO gamedata
     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("RoundRespawn");
    97 	VERIFY_SIGNATURE("CSWeaponDrop");
    98 	VERIFY_SIGNATURE("DropWeapons");
    99 	VERIFY_OFFSET("Weapon_GetSlot");
   100 	VERIFY_OFFSET("RemoveAllItems");
   101 	VERIFY_OFFSET("DropWeaponsPatch");
   102 	VERIFY_OFFSET("GiveAmmo");
   103 
   104 	if (!DM_ParseWeapons(error, maxlength))
   105 	{
   106 		return false;
   107 	}
   108 
   109 	gpGlobals = g_SMAPI->GetCGlobals();
   110 
   111 	SM_InitConCommandBase();
   112 
   113 	spengine = g_pSM->GetScriptingEngine();
   114 
   115 	return true;
   116 }
   117 
   118 bool Deathmatch::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
   119 {
   120 	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
   121 	GET_V_IFACE_CURRENT(GetFileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
   122 	GET_V_IFACE_ANY(GetServerFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
   123 	GET_V_IFACE_ANY(GetServerFactory, gameents,IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
   124 	GET_V_IFACE_ANY(GetServerFactory, botmanager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
   125 	GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
   126 	GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
   127 
   128 	return true;
   129 }
   130 
   131 void OnDLLShutdown()
   132 {
   133 	g_IsInGlobalShutdown = true;
   134 	RETURN_META(MRES_IGNORED);
   135 }
   136 
   137 void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   138 {
   139 	OnLevelInitialized();
   140 	RETURN_META(MRES_IGNORED);
   141 }
   142 
   143 void LevelShutdown()
   144 {
   145 	OnLevelEnd();
   146 	RETURN_META(MRES_IGNORED);
   147 }
   148 
   149 bool Startup(char *error, size_t maxlength)
   150 {
   151 	playerhelpers->AddClientListener(&g_ClientListener);
   152 
   153 	HOOK_EVENT2(player_death);
   154 	HOOK_EVENT2(player_spawn);
   155 	HOOK_EVENT2(player_team);
   156 	HOOK_EVENT2(round_start);
   157 	HOOK_EVENT2(round_end);
   158 	HOOK_EVENT2(server_shutdown);
   159 	HOOK_EVENT2(item_pickup);
   160 
   161 	g_Startup = true;
   162 
   163 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   164 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   165 	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   166 	SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   167 
   168 	DM_InitCallbacks();
   169 
   170 	char ffa_error[255];
   171 	if (!DM_Prepare_FFA(ffa_error, sizeof(ffa_error)))
   172 	{
   173 		g_pSM->LogError(myself, "FFA will not work: %s", ffa_error);
   174 	}
   175 
   176 	DM_InitDetours();
   177 
   178 	return InitializeUtils(error, maxlength);
   179 }
   180 
   181 void Shutdown()
   182 {
   183 	/* Remove hooks that could have happened whether we got started up or not */
   184 	playerhelpers->RemoveClientListener(&g_ClientListener);
   185 
   186 	UNHOOK_EVENT2(player_death);
   187 	UNHOOK_EVENT2(player_spawn);
   188 	UNHOOK_EVENT2(player_team);
   189 	UNHOOK_EVENT2(round_start);
   190 	UNHOOK_EVENT2(round_end);
   191 	UNHOOK_EVENT2(server_shutdown);
   192 	UNHOOK_EVENT2(item_pickup);
   193 
   194 	/* If we were never started up, the rest of this is invalid */
   195 	if (!g_Startup)
   196 	{
   197 		return;
   198 	}
   199 
   200 	/* Make sure stuff like FFA is cleaned up */
   201 	DM_Disable();
   202 
   203 	/* Destroy various internal things */
   204 	DM_ShutdownDetours();
   205 	DM_ShutdownCallbacks();
   206 	DM_FreeWeapons();
   207 	ShutdownUtils();
   208 
   209 	/* Unhook everything from SourceHook */
   210 	SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
   211 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
   212 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
   213 	SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
   214 }
   215 
   216 void Deathmatch::SDK_OnAllLoaded()
   217 {
   218 	SM_GET_LATE_IFACE(BINTOOLS, bintools);
   219 
   220 	g_IsLoadedOkay = Startup(g_GlobError, sizeof(g_GlobError));
   221 
   222 	if (!QueryRunning(NULL, 0))
   223 	{
   224 		return;
   225 	}
   226 }
   227 
   228 void Deathmatch::SDK_OnUnload()
   229 {
   230 	DM_FreeWeapons();
   231 	Shutdown();
   232 	gameconfs->CloseGameConfigFile(g_pDmConf);
   233 }
   234 
   235 bool Deathmatch::QueryRunning(char *error, size_t maxlength)
   236 {
   237 	SM_CHECK_IFACE(BINTOOLS, bintools);
   238 
   239 	if (!g_IsLoadedOkay && g_GlobError[0] != '\0')
   240 	{
   241 		snprintf(error, maxlength, "%s", g_GlobError);
   242 		return false;
   243 	}
   244 
   245 	return true;
   246 }
   247 
   248 bool Deathmatch::QueryInterfaceDrop(SMInterface *pInterface)
   249 {
   250 	if (pInterface == bintools)
   251 	{
   252 		return false;
   253 	}
   254 
   255 	return true;
   256 }
   257 
   258 void Deathmatch::NotifyInterfaceDrop(SMInterface *pInterface)
   259 {
   260 	/* We have to take care of bintools early then... */
   261 	if (pInterface == bintools)
   262 	{
   263 		ShutdownUtils();
   264 		bintools = NULL;
   265 	}
   266 }
   267 
   268 const char *Deathmatch::GetExtensionVerString()
   269 {
   270 	return CSSDM_FULL_VERSION;
   271 }
   272