3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
34 #include "HalfLife2.h"
35 #include "sourcemm_api.h"
36 #include "sm_stringutil.h"
37 #include "sourcehook.h"
38 #include "sm_srvcmds.h"
40 NextMapManager g_NextMap;
42 #if SOURCE_ENGINE != SE_DARKMESSIAH
43 SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
45 SH_DECL_HOOK4_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *, const char *, bool);
48 #if SOURCE_ENGINE >= SE_ORANGEBOX
49 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
50 #elif SOURCE_ENGINE == SE_DARKMESSIAH
51 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
53 # if SH_IMPL_VERSION >= 4
54 extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
56 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
58 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
61 ConCommand *changeLevelCmd = NULL;
63 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
65 bool g_forcedChange = false;
67 void NextMapManager::OnSourceModAllInitialized_Post()
69 #if SOURCE_ENGINE >= SE_ORANGEBOX
70 SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
72 SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
75 ConCommand *pCmd = FindCommand("changelevel");
78 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
79 changeLevelCmd = pCmd;
83 void NextMapManager::OnSourceModShutdown()
85 #if SOURCE_ENGINE >= SE_ORANGEBOX
86 SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
88 SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
91 if (changeLevelCmd != NULL)
93 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
96 SourceHook::List<MapChangeData *>::iterator iter;
97 iter = m_mapHistory.begin();
99 while (iter != m_mapHistory.end())
101 delete (MapChangeData *)*iter;
102 iter = m_mapHistory.erase(iter);
106 const char *NextMapManager::GetNextMap()
108 return sm_nextmap.GetString();
111 bool NextMapManager::SetNextMap(const char *map)
113 if (!g_HL2.IsMapValid(map))
118 sm_nextmap.SetValue(map);
123 #if SOURCE_ENGINE != SE_DARKMESSIAH
124 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
126 void NextMapManager::HookChangeLevel(const char *map, const char *unknown, const char *video, bool bLongLoading)
131 g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
132 RETURN_META(MRES_IGNORED);
135 const char *newmap = sm_nextmap.GetString();
137 if (newmap[0] == 0 || !g_HL2.IsMapValid(newmap))
139 RETURN_META(MRES_IGNORED);
142 g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
144 UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
145 UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
147 #if SOURCE_ENGINE != SE_DARKMESSIAH
148 RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
150 RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown, video, bLongLoading));
154 void NextMapManager::OnSourceModLevelChange( const char *mapName )
156 /* Skip the first 'mapchange' when the server starts up */
157 if (m_tempChangeInfo.startTime != 0)
159 if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
161 /* The map change was as we expected */
162 m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
166 /* Something intercepted the mapchange */
168 UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
169 m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
172 /* TODO: Should this be customizable? */
173 if (m_mapHistory.size() > 20)
175 SourceHook::List<MapChangeData *>::iterator iter;
176 iter = m_mapHistory.begin();
178 delete (MapChangeData *)*iter;
180 m_mapHistory.erase(iter);
184 m_tempChangeInfo.m_mapName[0] ='\0';
185 m_tempChangeInfo.m_changeReason[0] = '\0';
186 m_tempChangeInfo.startTime = time(NULL);
187 UTIL_Format(lastMap, sizeof(lastMap), mapName);
190 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
192 /* Store the mapname and reason */
193 UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
194 UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
196 /* Change level and skip our hook */
197 g_forcedChange = true;
198 engine->ChangeLevel(mapName, NULL);
199 g_forcedChange = false;
202 NextMapManager::NextMapManager()
204 m_tempChangeInfo = MapChangeData();
205 m_mapHistory = SourceHook::List<MapChangeData *>();
208 #if SOURCE_ENGINE >= SE_ORANGEBOX
209 void CmdChangeLevelCallback(const CCommand &command)
212 void CmdChangeLevelCallback()
217 if (command.ArgC() < 2)
222 if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
224 UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
225 UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");