core/NextMap.cpp
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 13 06:13:28 2008 +0000 (2008-07-13)
changeset 2205 2e628fb36eef
parent 2203 159ab9bad58c
child 2400 76b7b4575e21
permissions -rw-r--r--
Fixed build
     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 defined ORANGEBOX_BUILD
    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 defined ORANGEBOX_BUILD
    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 	ConCommandBase *pBase = icvar->GetCommands();
    65 	ConCommand *pCmd = NULL;
    66 	while (pBase)
    67 	{
    68 		if (strcmp(pBase->GetName(), "changelevel") == 0)
    69 		{
    70 			/* Don't want to return convar with same name */
    71 			if (!pBase->IsCommand())
    72 			{
    73 				break;
    74 			}
    75 
    76 			pCmd = (ConCommand *)pBase;
    77 			break;
    78 		}
    79 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
    80 	}
    81 
    82 	if (pCmd != NULL)
    83 	{
    84 		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
    85 		changeLevelCmd = pCmd;
    86 	}
    87 }
    88 
    89 void NextMapManager::OnSourceModShutdown()
    90 {
    91 #if defined ORANGEBOX_BUILD
    92 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    93 #else
    94 	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    95 #endif
    96 
    97 	if (changeLevelCmd != NULL)
    98 	{
    99 		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
   100 	}
   101 
   102 	SourceHook::List<MapChangeData *>::iterator iter;
   103 	iter = m_mapHistory.begin();
   104 
   105 	while (iter != m_mapHistory.end())
   106 	{
   107 		delete (MapChangeData *)*iter;
   108 		iter = m_mapHistory.erase(iter);
   109 	}
   110 }
   111 
   112 const char *NextMapManager::GetNextMap()
   113 {
   114 	return sm_nextmap.GetString();
   115 }
   116 
   117 bool NextMapManager::SetNextMap(const char *map)
   118 {
   119 	if (!engine->IsMapValid(map))
   120 	{
   121 		return false;
   122 	}
   123 
   124 	sm_nextmap.SetValue(map);
   125 
   126 	return true;
   127 }
   128 
   129 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
   130 {
   131 	if (g_forcedChange)
   132 	{
   133 		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
   134 		RETURN_META(MRES_IGNORED);
   135 	}
   136 
   137 	const char *newmap = sm_nextmap.GetString();
   138 
   139 	if (newmap[0] == 0 || !engine->IsMapValid(newmap))
   140 	{
   141 		RETURN_META(MRES_IGNORED);
   142 	}
   143 
   144 	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
   145 
   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");
   148 
   149 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
   150 }
   151 
   152 void NextMapManager::OnSourceModLevelChange( const char *mapName )
   153 {
   154 	/* Skip the first 'mapchange' when the server starts up */
   155 	if (m_tempChangeInfo.startTime != 0)
   156 	{
   157 		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
   158 		{
   159 			/* The map change was as we expected */
   160 			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
   161 		}
   162 		else
   163 		{
   164 			/* Something intercepted the mapchange */
   165 			char newReason[255];
   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));
   168 		}
   169 
   170 		/* TODO: Should this be customizable? */
   171 		if (m_mapHistory.size() > 20)
   172 		{
   173 			SourceHook::List<MapChangeData *>::iterator iter;
   174 			iter = m_mapHistory.begin();
   175 
   176 			delete (MapChangeData *)*iter;
   177 
   178 			m_mapHistory.erase(iter);
   179 		}
   180 	}
   181 
   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);
   186 }
   187 
   188 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
   189 {
   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);
   193 
   194 	/* Change level and skip our hook */
   195 	g_forcedChange = true;
   196 	engine->ChangeLevel(mapName, NULL);
   197 	g_forcedChange = false;
   198 }
   199 
   200 NextMapManager::NextMapManager()
   201 {
   202 	m_tempChangeInfo = MapChangeData();
   203 	m_mapHistory = SourceHook::List<MapChangeData *>();
   204 }
   205 
   206 #if defined ORANGEBOX_BUILD
   207 void CmdChangeLevelCallback(const CCommand &command)
   208 {
   209 #else
   210 void CmdChangeLevelCallback()
   211 {
   212 	CCommand command;
   213 #endif
   214 
   215 	if (command.ArgC() < 2)
   216 	{
   217 		return;
   218 	}
   219 
   220 	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
   221 	{
   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");
   224 	}
   225 }