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