Initial Import of beta structs extension.
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 "sourcemm_api.h"
35 #include "sm_stringutil.h"
36 #include "sourcehook.h"
37 #include "sm_srvcmds.h"
39 NextMapManager g_NextMap;
41 SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
43 #if defined ORANGEBOX_BUILD
44 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
46 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
47 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
50 ConCommand *changeLevelCmd = NULL;
52 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
54 bool g_forcedChange = false;
56 void NextMapManager::OnSourceModAllInitialized_Post()
58 #if defined ORANGEBOX_BUILD
59 SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
61 SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
64 ConCommandBase *pBase = icvar->GetCommands();
65 ConCommand *pCmd = NULL;
68 if (strcmp(pBase->GetName(), "changelevel") == 0)
70 /* Don't want to return convar with same name */
71 if (!pBase->IsCommand())
76 pCmd = (ConCommand *)pBase;
79 pBase = const_cast<ConCommandBase *>(pBase->GetNext());
84 SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
85 changeLevelCmd = pCmd;
89 void NextMapManager::OnSourceModShutdown()
91 #if defined ORANGEBOX_BUILD
92 SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
94 SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
97 if (changeLevelCmd != NULL)
99 SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
102 SourceHook::List<MapChangeData *>::iterator iter;
103 iter = m_mapHistory.begin();
105 while (iter != m_mapHistory.end())
107 delete (MapChangeData *)*iter;
108 iter = m_mapHistory.erase(iter);
112 const char *NextMapManager::GetNextMap()
114 return sm_nextmap.GetString();
117 bool NextMapManager::SetNextMap(const char *map)
119 if (!engine->IsMapValid(map))
124 sm_nextmap.SetValue(map);
129 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
133 g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
134 RETURN_META(MRES_IGNORED);
137 const char *newmap = sm_nextmap.GetString();
139 if (newmap[0] == 0 || !engine->IsMapValid(newmap))
141 RETURN_META(MRES_IGNORED);
144 g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
146 UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
147 UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
149 RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
152 void NextMapManager::OnSourceModLevelChange( const char *mapName )
154 /* Skip the first 'mapchange' when the server starts up */
155 if (m_tempChangeInfo.startTime != 0)
157 if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
159 /* The map change was as we expected */
160 m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
164 /* Something intercepted the mapchange */
166 UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
167 m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
170 /* TODO: Should this be customizable? */
171 if (m_mapHistory.size() > 20)
173 SourceHook::List<MapChangeData *>::iterator iter;
174 iter = m_mapHistory.begin();
176 delete (MapChangeData *)*iter;
178 m_mapHistory.erase(iter);
182 m_tempChangeInfo.m_mapName[0] ='\0';
183 m_tempChangeInfo.m_changeReason[0] = '\0';
184 m_tempChangeInfo.startTime = time(NULL);
185 UTIL_Format(lastMap, sizeof(lastMap), mapName);
188 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
190 /* Store the mapname and reason */
191 UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
192 UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
194 /* Change level and skip our hook */
195 g_forcedChange = true;
196 engine->ChangeLevel(mapName, NULL);
197 g_forcedChange = false;
200 NextMapManager::NextMapManager()
202 m_tempChangeInfo = MapChangeData();
203 m_mapHistory = SourceHook::List<MapChangeData *>();
206 #if defined ORANGEBOX_BUILD
207 void CmdChangeLevelCallback(const CCommand &command)
210 void CmdChangeLevelCallback()
215 if (command.ArgC() < 2)
220 if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
222 UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
223 UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");