3 * ===============================================================
4 * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
5 * By David "BAILOPAN" Anderson
7 * ===============================================================
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.
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.
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,
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"
40 SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
41 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, 0)
42 SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
43 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, false, edict_t *);
46 IGameEventManager2 *gameevents = NULL;
47 IBaseFileSystem *basefilesystem = NULL;
48 IBinTools *bintools = NULL;
49 IGameConfig *g_pDmConf = NULL;
50 CGlobalVars *gpGlobals = NULL;
51 IPlayerInfoManager *playerinfomngr = NULL;
52 IServerGameEnts *gameents = NULL;
53 IServerGameClients *gameclients = NULL;
54 ISourcePawnEngine *spengine = NULL;
55 IBotManager *botmanager = NULL;
56 char g_GlobError[255] = {0};
57 bool g_IsLoadedOkay = false;
58 bool g_Startup = false;
59 bool g_IsInGlobalShutdown = false;
63 #define VERIFY_SIGNATURE(name) \
64 if (!g_pDmConf->GetMemSig(name, &addr) || !addr) { \
65 snprintf(error, maxlength, "Could not find signature \"%s\"", name); \
69 #define VERIFY_OFFSET(name) \
70 if (!g_pDmConf->GetOffset(name, &offset) || !offset) { \
71 snprintf(error, maxlength, "Could not find offset \"%s\"", name); \
75 #define HOOK_EVENT2(name) \
76 if (!gameevents->AddListener(&g_cls_event_##name, #name, true)) { \
77 snprintf(error, maxlength, "Could not hook event \"%s\"", #name); \
81 #define UNHOOK_EVENT2(name) gameevents->RemoveListener(&g_cls_event_##name);
83 bool Deathmatch::SDK_OnLoad(char *error, size_t maxlength, bool late)
85 sharesys->AddDependency(myself, "bintools.ext", true, true);
86 sharesys->AddNatives(myself, g_BaseNatives);
87 if (!gameconfs->LoadGameConfigFile("cssdm.games", &g_pDmConf, error, maxlength))
94 VERIFY_SIGNATURE("UTIL_Remove");
95 VERIFY_SIGNATURE("RoundRespawn");
96 VERIFY_SIGNATURE("CSWeaponDrop");
97 VERIFY_SIGNATURE("DropWeapons");
98 VERIFY_OFFSET("Weapon_GetSlot");
99 VERIFY_OFFSET("RemoveAllItems");
100 VERIFY_OFFSET("DropWeaponsPatch");
101 VERIFY_OFFSET("CSWeaponDropPatch");
102 VERIFY_OFFSET("GiveAmmo");
104 if (!DM_ParseWeapons(error, maxlength))
109 gpGlobals = g_SMAPI->pGlobals();
111 SM_InitConCommandBase();
113 spengine = g_pSM->GetScriptingEngine();
118 bool Deathmatch::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
120 GET_V_IFACE_CURRENT(engineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
121 GET_V_IFACE_CURRENT(fileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
122 GET_V_IFACE_ANY(serverFactory, playerinfomngr, IPlayerInfoManager, INTERFACEVERSION_PLAYERINFOMANAGER);
123 GET_V_IFACE_ANY(serverFactory, gameents,IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
124 GET_V_IFACE_ANY(serverFactory, botmanager, IBotManager, INTERFACEVERSION_PLAYERBOTMANAGER);
125 GET_V_IFACE_ANY(serverFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
132 g_IsInGlobalShutdown = true;
133 RETURN_META(MRES_IGNORED);
136 void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
138 OnLevelInitialized();
139 RETURN_META(MRES_IGNORED);
145 RETURN_META(MRES_IGNORED);
148 bool Startup(char *error, size_t maxlength)
150 playerhelpers->AddClientListener(&g_ClientListener);
152 HOOK_EVENT2(player_death);
153 HOOK_EVENT2(player_spawn);
154 HOOK_EVENT2(player_team);
155 HOOK_EVENT2(round_start);
156 HOOK_EVENT2(round_end);
157 HOOK_EVENT2(server_shutdown);
158 HOOK_EVENT2(item_pickup);
162 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
163 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
164 SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
165 SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
170 if (!DM_Prepare_FFA(ffa_error, sizeof(ffa_error)))
172 g_pSM->LogError(myself, "FFA will not work: %s", ffa_error);
177 return InitializeUtils(error, maxlength);
182 /* Remove hooks that could have happened whether we got started up or not */
183 playerhelpers->RemoveClientListener(&g_ClientListener);
185 UNHOOK_EVENT2(player_death);
186 UNHOOK_EVENT2(player_spawn);
187 UNHOOK_EVENT2(player_team);
188 UNHOOK_EVENT2(round_start);
189 UNHOOK_EVENT2(round_end);
190 UNHOOK_EVENT2(server_shutdown);
191 UNHOOK_EVENT2(item_pickup);
193 /* If we were never started up, the rest of this is invalid */
199 /* Make sure stuff like FFA is cleaned up */
202 /* Destroy various internal things */
203 DM_ShutdownDetours();
204 DM_ShutdownCallbacks();
208 /* Unhook everything from SourceHook */
209 SH_REMOVE_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, OnClientCommand_Post, true);
210 SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, gamedll, OnDLLShutdown, false);
211 SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, gamedll, LevelShutdown, false);
212 SH_REMOVE_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, gamedll, ServerActivate, true);
215 void Deathmatch::SDK_OnAllLoaded()
217 SM_GET_LATE_IFACE(BINTOOLS, bintools);
219 g_IsLoadedOkay = Startup(g_GlobError, sizeof(g_GlobError));
221 if (!QueryRunning(NULL, 0))
227 void Deathmatch::SDK_OnUnload()
231 gameconfs->CloseGameConfigFile(g_pDmConf);
234 bool Deathmatch::QueryRunning(char *error, size_t maxlength)
236 SM_CHECK_IFACE(BINTOOLS, bintools);
238 if (!g_IsLoadedOkay && g_GlobError[0] != '\0')
240 snprintf(error, maxlength, "%s", g_GlobError);
247 bool Deathmatch::QueryInterfaceDrop(SMInterface *pInterface)
249 if (pInterface == bintools)
257 void Deathmatch::NotifyInterfaceDrop(SMInterface *pInterface)
259 /* We have to take care of bintools early then... */
260 if (pInterface == bintools)