core/NextMap.cpp
author Matt Woodrow <pred@alliedmods.net>
Mon Aug 24 09:41:53 2009 +1200 (2009-08-24)
changeset 2781 472f27784e9b
parent 2583 d3b2571c378b
child 2843 bf16cf249d3d
permissions -rw-r--r--
Build fix
     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 #if SOURCE_ENGINE != SE_DARKMESSIAH
    42 SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
    43 #else
    44 SH_DECL_HOOK4_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *, const char *, bool);
    45 #endif
    46 
    47 #if SOURCE_ENGINE >= SE_ORANGEBOX
    48 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    49 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    50 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    51 #else
    52 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    53 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    54 #endif
    55 
    56 ConCommand *changeLevelCmd = NULL;
    57 
    58 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
    59 
    60 bool g_forcedChange = false;
    61 
    62 void NextMapManager::OnSourceModAllInitialized_Post()
    63 {
    64 #if SOURCE_ENGINE >= SE_ORANGEBOX
    65 	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    66 #else
    67 	SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    68 #endif
    69 
    70 	ConCommand *pCmd = FindCommand("changelevel");
    71 	if (pCmd != NULL)
    72 	{
    73 		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
    74 		changeLevelCmd = pCmd;
    75 	}
    76 }
    77 
    78 void NextMapManager::OnSourceModShutdown()
    79 {
    80 #if SOURCE_ENGINE >= SE_ORANGEBOX
    81 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    82 #else
    83 	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
    84 #endif
    85 
    86 	if (changeLevelCmd != NULL)
    87 	{
    88 		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
    89 	}
    90 
    91 	SourceHook::List<MapChangeData *>::iterator iter;
    92 	iter = m_mapHistory.begin();
    93 
    94 	while (iter != m_mapHistory.end())
    95 	{
    96 		delete (MapChangeData *)*iter;
    97 		iter = m_mapHistory.erase(iter);
    98 	}
    99 }
   100 
   101 const char *NextMapManager::GetNextMap()
   102 {
   103 	return sm_nextmap.GetString();
   104 }
   105 
   106 bool NextMapManager::SetNextMap(const char *map)
   107 {
   108 	if (!engine->IsMapValid(map))
   109 	{
   110 		return false;
   111 	}
   112 
   113 	sm_nextmap.SetValue(map);
   114 
   115 	return true;
   116 }
   117 
   118 #if SOURCE_ENGINE != SE_DARKMESSIAH
   119 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
   120 #else
   121 void NextMapManager::HookChangeLevel(const char *map, const char *unknown, const char *video, bool bLongLoading)
   122 #endif
   123 {
   124 	if (g_forcedChange)
   125 	{
   126 		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
   127 		RETURN_META(MRES_IGNORED);
   128 	}
   129 
   130 	const char *newmap = sm_nextmap.GetString();
   131 
   132 	if (newmap[0] == 0 || !engine->IsMapValid(newmap))
   133 	{
   134 		RETURN_META(MRES_IGNORED);
   135 	}
   136 
   137 	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
   138 
   139 	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
   140 	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
   141 
   142 #if SOURCE_ENGINE != SE_DARKMESSIAH
   143 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
   144 #else
   145 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown, video, bLongLoading));
   146 #endif
   147 }
   148 
   149 void NextMapManager::OnSourceModLevelChange( const char *mapName )
   150 {
   151 	/* Skip the first 'mapchange' when the server starts up */
   152 	if (m_tempChangeInfo.startTime != 0)
   153 	{
   154 		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
   155 		{
   156 			/* The map change was as we expected */
   157 			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
   158 		}
   159 		else
   160 		{
   161 			/* Something intercepted the mapchange */
   162 			char newReason[255];
   163 			UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
   164 			m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
   165 		}
   166 
   167 		/* TODO: Should this be customizable? */
   168 		if (m_mapHistory.size() > 20)
   169 		{
   170 			SourceHook::List<MapChangeData *>::iterator iter;
   171 			iter = m_mapHistory.begin();
   172 
   173 			delete (MapChangeData *)*iter;
   174 
   175 			m_mapHistory.erase(iter);
   176 		}
   177 	}
   178 
   179 	m_tempChangeInfo.m_mapName[0] ='\0';
   180 	m_tempChangeInfo.m_changeReason[0] = '\0';
   181 	m_tempChangeInfo.startTime = time(NULL);
   182 	UTIL_Format(lastMap, sizeof(lastMap), mapName);
   183 }
   184 
   185 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
   186 {
   187 	/* Store the mapname and reason */
   188 	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
   189 	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
   190 
   191 	/* Change level and skip our hook */
   192 	g_forcedChange = true;
   193 	engine->ChangeLevel(mapName, NULL);
   194 	g_forcedChange = false;
   195 }
   196 
   197 NextMapManager::NextMapManager()
   198 {
   199 	m_tempChangeInfo = MapChangeData();
   200 	m_mapHistory = SourceHook::List<MapChangeData *>();
   201 }
   202 
   203 #if SOURCE_ENGINE >= SE_ORANGEBOX
   204 void CmdChangeLevelCallback(const CCommand &command)
   205 {
   206 #else
   207 void CmdChangeLevelCallback()
   208 {
   209 	CCommand command;
   210 #endif
   211 
   212 	if (command.ArgC() < 2)
   213 	{
   214 		return;
   215 	}
   216 
   217 	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
   218 	{
   219 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
   220 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");
   221 	}
   222 }