core/NextMap.cpp
author Dr!fter
Wed Jul 10 19:56:51 2013 -0400 (2013-07-10)
changeset 3939 31183c7eae14
parent 3899 fd4ec50a0d6c
child 3942 7aaa00382627
permissions -rw-r--r--
Update GroundEndChanged for CS:S and CS:GO
     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_DOTA
    49 SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, void *, const CCommand &);
    50 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    51 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    52 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    53 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    54 #else
    55 # if SH_IMPL_VERSION >= 4
    56  extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    57 # else
    58  extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    59 #endif
    60 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    61 #endif
    62 
    63 ConCommand *changeLevelCmd = NULL;
    64 
    65 ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
    66 
    67 bool g_forcedChange = false;
    68 
    69 void NextMapManager::OnSourceModAllInitialized_Post()
    70 {
    71 #if SOURCE_ENGINE >= SE_ORANGEBOX
    72 	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    73 #else
    74 	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    75 #endif
    76 
    77 	ConCommand *pCmd = FindCommand("changelevel");
    78 	if (pCmd != NULL)
    79 	{
    80 		SH_ADD_HOOK(ConCommand, Dispatch, pCmd, SH_STATIC(CmdChangeLevelCallback), false);
    81 		changeLevelCmd = pCmd;
    82 	}
    83 }
    84 
    85 void NextMapManager::OnSourceModShutdown()
    86 {
    87 #if SOURCE_ENGINE >= SE_ORANGEBOX
    88 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    89 #else
    90 	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
    91 #endif
    92 
    93 	if (changeLevelCmd != NULL)
    94 	{
    95 		SH_REMOVE_HOOK(ConCommand, Dispatch, changeLevelCmd, SH_STATIC(CmdChangeLevelCallback), false);
    96 	}
    97 
    98 	SourceHook::List<MapChangeData *>::iterator iter;
    99 	iter = m_mapHistory.begin();
   100 
   101 	while (iter != m_mapHistory.end())
   102 	{
   103 		delete (MapChangeData *)*iter;
   104 		iter = m_mapHistory.erase(iter);
   105 	}
   106 }
   107 
   108 const char *NextMapManager::GetNextMap()
   109 {
   110 	return sm_nextmap.GetString();
   111 }
   112 
   113 bool NextMapManager::SetNextMap(const char *map)
   114 {
   115 	if (!g_HL2.IsMapValid(map))
   116 	{
   117 		return false;
   118 	}
   119 
   120 	sm_nextmap.SetValue(map);
   121 
   122 	return true;
   123 }
   124 
   125 #if SOURCE_ENGINE != SE_DARKMESSIAH
   126 void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
   127 #else
   128 void NextMapManager::HookChangeLevel(const char *map, const char *unknown, const char *video, bool bLongLoading)
   129 #endif
   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 || !g_HL2.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 #if SOURCE_ENGINE != SE_DARKMESSIAH
   150 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
   151 #else
   152 	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown, video, bLongLoading));
   153 #endif
   154 }
   155 
   156 void NextMapManager::OnSourceModLevelChange( const char *mapName )
   157 {
   158 	/* Skip the first 'mapchange' when the server starts up */
   159 	if (m_tempChangeInfo.startTime != 0)
   160 	{
   161 		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
   162 		{
   163 			/* The map change was as we expected */
   164 			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
   165 		}
   166 		else
   167 		{
   168 			/* Something intercepted the mapchange */
   169 			char newReason[255];
   170 			UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
   171 			m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
   172 		}
   173 
   174 		/* TODO: Should this be customizable? */
   175 		if (m_mapHistory.size() > 20)
   176 		{
   177 			SourceHook::List<MapChangeData *>::iterator iter;
   178 			iter = m_mapHistory.begin();
   179 
   180 			delete (MapChangeData *)*iter;
   181 
   182 			m_mapHistory.erase(iter);
   183 		}
   184 	}
   185 
   186 	m_tempChangeInfo.m_mapName[0] ='\0';
   187 	m_tempChangeInfo.m_changeReason[0] = '\0';
   188 	m_tempChangeInfo.startTime = time(NULL);
   189 	UTIL_Format(lastMap, sizeof(lastMap), mapName);
   190 }
   191 
   192 void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
   193 {
   194 	/* Store the mapname and reason */
   195 	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
   196 	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
   197 
   198 	/* Change level and skip our hook */
   199 	g_forcedChange = true;
   200 	engine->ChangeLevel(mapName, NULL);
   201 	g_forcedChange = false;
   202 }
   203 
   204 NextMapManager::NextMapManager()
   205 {
   206 	m_tempChangeInfo = MapChangeData();
   207 	m_mapHistory = SourceHook::List<MapChangeData *>();
   208 }
   209 
   210 #if SOURCE_ENGINE == SE_DOTA
   211 void CmdChangeLevelCallback(void *pUnknown, const CCommand &command)
   212 {
   213 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   214 void CmdChangeLevelCallback(const CCommand &command)
   215 {
   216 #else
   217 void CmdChangeLevelCallback()
   218 {
   219 	CCommand command;
   220 #endif
   221 
   222 	if (command.ArgC() < 2)
   223 	{
   224 		return;
   225 	}
   226 
   227 	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
   228 	{
   229 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
   230 		UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");
   231 	}
   232 }