core/NextMap.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 09:18:30 2008 -0600 (2008-11-14)
changeset 2400 76b7b4575e21
parent 2205 2e628fb36eef
child 2583 d3b2571c378b
permissions -rw-r--r--
Added preliminary support for Left 4 Dead; some things may not yet work.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "NextMap.h"
    33 #include "Logger.h"
    34 #include "sourcemm_api.h"
    35 #include "sm_stringutil.h"
    36 #include "sourcehook.h"
    37 #include "sm_srvcmds.h"
    38 
    39 NextMapManager g_NextMap;
    40 
    41 SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
    42 
    43 #if SOURCE_ENGINE >= SE_ORANGEBOX
    44 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    45 #else
    46 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    47 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    48 #endif
    49 
    50 ConCommand *changeLevelCmd = NULL;
    51 
    52 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
    53 
    54 bool g_forcedChange = false;
    55 
    56 void NextMapManager::OnSourceModAllInitialized_Post()
    57 {
    58 #if SOURCE_ENGINE >= SE_ORANGEBOX
    59 	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    60 #else
    61 	SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    62 #endif
    63 
    64 	ConCommand *pCmd = FindCommand("changelevel");
    65 	if (pCmd != NULL)
    66 	{
    67 		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
    68 		changeLevelCmd = pCmd;
    69 	}
    70 }
    71 
    72 void NextMapManager::OnSourceModShutdown()
    73 {
    74 #if SOURCE_ENGINE >= SE_ORANGEBOX
    75 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    76 #else
    77 	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    78 #endif
    79 
    80 	if (changeLevelCmd != NULL)
    81 	{
    82 		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
    83 	}
    84 
    85 	SourceHook::List<MapChangeData *>::iterator iter;
    86 	iter = m_mapHistory.begin();
    87 
    88 	while (iter != m_mapHistory.end())
    89 	{
    90 		delete (MapChangeData *)*iter;
    91 		iter = m_mapHistory.erase(iter);
    92 	}
    93 }
    94 
    95 const char *NextMapManager::GetNextMap()
    96 {
    97 	return sm_nextmap.GetString();
    98 }
    99 
   100 bool NextMapManager::SetNextMap(const char *map)
   101 {
   102 	if (!engine->IsMapValid(map))
   103 	{
   104 		return false;
   105 	}
   106 
   107 	sm_nextmap.SetValue(map);
   108 
   109 	return true;
   110 }
   111 
   112 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
   113 {
   114 	if (g_forcedChange)
   115 	{
   116 		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
   117 		RETURN_META(MRES_IGNORED);
   118 	}
   119 
   120 	const char *newmap = sm_nextmap.GetString();
   121 
   122 	if (newmap[0] == 0 || !engine->IsMapValid(newmap))
   123 	{
   124 		RETURN_META(MRES_IGNORED);
   125 	}
   126 
   127 	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
   128 
   129 	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
   130 	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
   131 
   132 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
   133 }
   134 
   135 void NextMapManager::OnSourceModLevelChange( const char *mapName )
   136 {
   137 	/* Skip the first 'mapchange' when the server starts up */
   138 	if (m_tempChangeInfo.startTime != 0)
   139 	{
   140 		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
   141 		{
   142 			/* The map change was as we expected */
   143 			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
   144 		}
   145 		else
   146 		{
   147 			/* Something intercepted the mapchange */
   148 			char newReason[255];
   149 			UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
   150 			m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
   151 		}
   152 
   153 		/* TODO: Should this be customizable? */
   154 		if (m_mapHistory.size() > 20)
   155 		{
   156 			SourceHook::List<MapChangeData *>::iterator iter;
   157 			iter = m_mapHistory.begin();
   158 
   159 			delete (MapChangeData *)*iter;
   160 
   161 			m_mapHistory.erase(iter);
   162 		}
   163 	}
   164 
   165 	m_tempChangeInfo.m_mapName[0] ='\0';
   166 	m_tempChangeInfo.m_changeReason[0] = '\0';
   167 	m_tempChangeInfo.startTime = time(NULL);
   168 	UTIL_Format(lastMap, sizeof(lastMap), mapName);
   169 }
   170 
   171 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
   172 {
   173 	/* Store the mapname and reason */
   174 	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
   175 	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
   176 
   177 	/* Change level and skip our hook */
   178 	g_forcedChange = true;
   179 	engine->ChangeLevel(mapName, NULL);
   180 	g_forcedChange = false;
   181 }
   182 
   183 NextMapManager::NextMapManager()
   184 {
   185 	m_tempChangeInfo = MapChangeData();
   186 	m_mapHistory = SourceHook::List<MapChangeData *>();
   187 }
   188 
   189 #if SOURCE_ENGINE >= SE_ORANGEBOX
   190 void CmdChangeLevelCallback(const CCommand &command)
   191 {
   192 #else
   193 void CmdChangeLevelCallback()
   194 {
   195 	CCommand command;
   196 #endif
   197 
   198 	if (command.ArgC() < 2)
   199 	{
   200 		return;
   201 	}
   202 
   203 	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
   204 	{
   205 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
   206 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");
   207 	}
   208 }