core/NextMap.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Thu Feb 14 19:28:12 2013 -0500 (2013-02-14)
changeset 3789 42e367aa4a00
parent 2843 bf16cf249d3d
child 3819 d17aed3d46bf
permissions -rw-r--r--
Added support for "fuzzy" map names in L4D and later (bug 5599, r=asherkin).
     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 "HalfLife2.h"
    35 #include "sourcemm_api.h"
    36 #include "sm_stringutil.h"
    37 #include "sourcehook.h"
    38 #include "sm_srvcmds.h"
    39 
    40 NextMapManager g_NextMap;
    41 
    42 #if SOURCE_ENGINE != SE_DARKMESSIAH
    43 SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
    44 #else
    45 SH_DECL_HOOK4_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *, const char *, bool);
    46 #endif
    47 
    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);
    52 #else
    53 # if SH_IMPL_VERSION >= 4
    54  extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    55 # else
    56  extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    57 #endif
    58 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    59 #endif
    60 
    61 ConCommand *changeLevelCmd = NULL;
    62 
    63 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
    64 
    65 bool g_forcedChange = false;
    66 
    67 void NextMapManager::OnSourceModAllInitialized_Post()
    68 {
    69 #if SOURCE_ENGINE >= SE_ORANGEBOX
    70 	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    71 #else
    72 	SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    73 #endif
    74 
    75 	ConCommand *pCmd = FindCommand("changelevel");
    76 	if (pCmd != NULL)
    77 	{
    78 		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
    79 		changeLevelCmd = pCmd;
    80 	}
    81 }
    82 
    83 void NextMapManager::OnSourceModShutdown()
    84 {
    85 #if SOURCE_ENGINE >= SE_ORANGEBOX
    86 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    87 #else
    88 	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    89 #endif
    90 
    91 	if (changeLevelCmd != NULL)
    92 	{
    93 		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
    94 	}
    95 
    96 	SourceHook::List<MapChangeData *>::iterator iter;
    97 	iter = m_mapHistory.begin();
    98 
    99 	while (iter != m_mapHistory.end())
   100 	{
   101 		delete (MapChangeData *)*iter;
   102 		iter = m_mapHistory.erase(iter);
   103 	}
   104 }
   105 
   106 const char *NextMapManager::GetNextMap()
   107 {
   108 	return sm_nextmap.GetString();
   109 }
   110 
   111 bool NextMapManager::SetNextMap(const char *map)
   112 {
   113 	if (!g_HL2.IsMapValid(map))
   114 	{
   115 		return false;
   116 	}
   117 
   118 	sm_nextmap.SetValue(map);
   119 
   120 	return true;
   121 }
   122 
   123 #if SOURCE_ENGINE != SE_DARKMESSIAH
   124 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
   125 #else
   126 void NextMapManager::HookChangeLevel(const char *map, const char *unknown, const char *video, bool bLongLoading)
   127 #endif
   128 {
   129 	if (g_forcedChange)
   130 	{
   131 		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
   132 		RETURN_META(MRES_IGNORED);
   133 	}
   134 
   135 	const char *newmap = sm_nextmap.GetString();
   136 
   137 	if (newmap[0] == 0 || !g_HL2.IsMapValid(newmap))
   138 	{
   139 		RETURN_META(MRES_IGNORED);
   140 	}
   141 
   142 	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
   143 
   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");
   146 
   147 #if SOURCE_ENGINE != SE_DARKMESSIAH
   148 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
   149 #else
   150 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown, video, bLongLoading));
   151 #endif
   152 }
   153 
   154 void NextMapManager::OnSourceModLevelChange( const char *mapName )
   155 {
   156 	/* Skip the first 'mapchange' when the server starts up */
   157 	if (m_tempChangeInfo.startTime != 0)
   158 	{
   159 		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
   160 		{
   161 			/* The map change was as we expected */
   162 			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
   163 		}
   164 		else
   165 		{
   166 			/* Something intercepted the mapchange */
   167 			char newReason[255];
   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));
   170 		}
   171 
   172 		/* TODO: Should this be customizable? */
   173 		if (m_mapHistory.size() > 20)
   174 		{
   175 			SourceHook::List<MapChangeData *>::iterator iter;
   176 			iter = m_mapHistory.begin();
   177 
   178 			delete (MapChangeData *)*iter;
   179 
   180 			m_mapHistory.erase(iter);
   181 		}
   182 	}
   183 
   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);
   188 }
   189 
   190 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
   191 {
   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);
   195 
   196 	/* Change level and skip our hook */
   197 	g_forcedChange = true;
   198 	engine->ChangeLevel(mapName, NULL);
   199 	g_forcedChange = false;
   200 }
   201 
   202 NextMapManager::NextMapManager()
   203 {
   204 	m_tempChangeInfo = MapChangeData();
   205 	m_mapHistory = SourceHook::List<MapChangeData *>();
   206 }
   207 
   208 #if SOURCE_ENGINE >= SE_ORANGEBOX
   209 void CmdChangeLevelCallback(const CCommand &command)
   210 {
   211 #else
   212 void CmdChangeLevelCallback()
   213 {
   214 	CCommand command;
   215 #endif
   216 
   217 	if (command.ArgC() < 2)
   218 	{
   219 		return;
   220 	}
   221 
   222 	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
   223 	{
   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");
   226 	}
   227 }