core/NextMap.cpp
author David Anderson <dvander@alliedmods.net>
Mon Oct 26 23:51:39 2009 -0700 (2009-10-26)
changeset 2832 bf5d37088b6d
parent 2583 d3b2571c378b
child 2843 bf16cf249d3d
permissions -rw-r--r--
Don't pass unconnected clients into OnClientCommand (bug 3732, r=fyren).
pred@2077
     1
/**
pred@2077
     2
 * vim: set ts=4 :
pred@2077
     3
 * =============================================================================
pred@2077
     4
 * SourceMod
pred@2077
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
pred@2077
     6
 * =============================================================================
pred@2077
     7
 *
pred@2077
     8
 * This program is free software; you can redistribute it and/or modify it under
pred@2077
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
pred@2077
    10
 * Free Software Foundation.
pred@2077
    11
 * 
pred@2077
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
pred@2077
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
pred@2077
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
pred@2077
    15
 * details.
pred@2077
    16
 *
pred@2077
    17
 * You should have received a copy of the GNU General Public License along with
pred@2077
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
pred@2077
    19
 *
pred@2077
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
pred@2077
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
pred@2077
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
pred@2077
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
pred@2077
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
pred@2077
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
pred@2077
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
pred@2077
    27
 * or <http://www.sourcemod.net/license.php>.
pred@2077
    28
 *
pred@2077
    29
 * Version: $Id$
pred@2077
    30
 */
pred@2077
    31
pred@2077
    32
#include "NextMap.h"
pred@2077
    33
#include "Logger.h"
pred@2077
    34
#include "sourcemm_api.h"
pred@2077
    35
#include "sm_stringutil.h"
pred@2203
    36
#include "sourcehook.h"
pred@2203
    37
#include "sm_srvcmds.h"
pred@2077
    38
pred@2077
    39
NextMapManager g_NextMap;
pred@2077
    40
ds@2583
    41
#if SOURCE_ENGINE != SE_DARKMESSIAH
pred@2077
    42
SH_DECL_HOOK2_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *);
ds@2583
    43
#else
ds@2583
    44
SH_DECL_HOOK4_void(IVEngineServer, ChangeLevel, SH_NOATTRIB, 0, const char *, const char *, const char *, bool);
ds@2583
    45
#endif
pred@2077
    46
ds@2400
    47
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2203
    48
SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@2583
    49
#elif SOURCE_ENGINE == SE_DARKMESSIAH
ds@2583
    50
SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
pred@2203
    51
#else
pred@2203
    52
extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
pred@2203
    53
extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
pred@2203
    54
#endif
pred@2203
    55
pred@2203
    56
ConCommand *changeLevelCmd = NULL;
pred@2203
    57
pred@2077
    58
ConVar sm_nextmap("sm_nextmap", "", FCVAR_NOTIFY);
pred@2077
    59
pred@2203
    60
bool g_forcedChange = false;
pred@2203
    61
pred@2077
    62
void NextMapManager::OnSourceModAllInitialized_Post()
pred@2077
    63
{
ds@2400
    64
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2077
    65
	SH_ADD_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
pred@2077
    66
#else
pred@2077
    67
	SH_ADD_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
pred@2077
    68
#endif
pred@2203
    69
ds@2400
    70
	ConCommand *pCmd = FindCommand("changelevel");
pred@2203
    71
	if (pCmd != NULL)
pred@2203
    72
	{
pred@2203
    73
		SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, pCmd, CmdChangeLevelCallback, false);
pred@2203
    74
		changeLevelCmd = pCmd;
pred@2203
    75
	}
pred@2077
    76
}
pred@2077
    77
pred@2077
    78
void NextMapManager::OnSourceModShutdown()
pred@2077
    79
{
ds@2400
    80
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2077
    81
	SH_REMOVE_HOOK(IVEngineServer, ChangeLevel, engine, SH_MEMBER(this, &NextMapManager::HookChangeLevel), false);
pred@2077
    82
#else
pred@2077
    83
	SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, ChangeLevel, engine, this, &NextMapManager::HookChangeLevel, false);
pred@2077
    84
#endif
pred@2203
    85
pred@2203
    86
	if (changeLevelCmd != NULL)
pred@2203
    87
	{
pred@2203
    88
		SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, changeLevelCmd, CmdChangeLevelCallback, false);
pred@2203
    89
	}
pred@2203
    90
pred@2203
    91
	SourceHook::List<MapChangeData *>::iterator iter;
pred@2203
    92
	iter = m_mapHistory.begin();
pred@2203
    93
pred@2203
    94
	while (iter != m_mapHistory.end())
pred@2203
    95
	{
pred@2203
    96
		delete (MapChangeData *)*iter;
pred@2203
    97
		iter = m_mapHistory.erase(iter);
pred@2203
    98
	}
pred@2077
    99
}
pred@2077
   100
pred@2077
   101
const char *NextMapManager::GetNextMap()
pred@2077
   102
{
pred@2077
   103
	return sm_nextmap.GetString();
pred@2077
   104
}
pred@2077
   105
pred@2077
   106
bool NextMapManager::SetNextMap(const char *map)
pred@2077
   107
{
pred@2077
   108
	if (!engine->IsMapValid(map))
pred@2077
   109
	{
pred@2077
   110
		return false;
pred@2077
   111
	}
pred@2077
   112
pred@2077
   113
	sm_nextmap.SetValue(map);
pred@2077
   114
pred@2077
   115
	return true;
pred@2077
   116
}
pred@2077
   117
ds@2583
   118
#if SOURCE_ENGINE != SE_DARKMESSIAH
pred@2077
   119
void NextMapManager::HookChangeLevel(const char *map, const char *unknown)
ds@2583
   120
#else
ds@2583
   121
void NextMapManager::HookChangeLevel(const char *map, const char *unknown, const char *video, bool bLongLoading)
ds@2583
   122
#endif
pred@2077
   123
{
pred@2203
   124
	if (g_forcedChange)
pred@2203
   125
	{
pred@2203
   126
		g_Logger.LogMessage("[SM] Changed map to \"%s\"", map);
pred@2203
   127
		RETURN_META(MRES_IGNORED);
pred@2203
   128
	}
pred@2203
   129
pred@2077
   130
	const char *newmap = sm_nextmap.GetString();
pred@2077
   131
pred@2077
   132
	if (newmap[0] == 0 || !engine->IsMapValid(newmap))
pred@2077
   133
	{
pred@2077
   134
		RETURN_META(MRES_IGNORED);
pred@2077
   135
	}
pred@2077
   136
pred@2077
   137
	g_Logger.LogMessage("[SM] Changed map to \"%s\"", newmap);
pred@2077
   138
pred@2203
   139
	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), newmap);
pred@2203
   140
	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), "Normal level change");
pred@2203
   141
ds@2583
   142
#if SOURCE_ENGINE != SE_DARKMESSIAH
pred@2077
   143
	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown));
ds@2583
   144
#else
ds@2583
   145
	RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::ChangeLevel, (newmap, unknown, video, bLongLoading));
ds@2583
   146
#endif
pred@2077
   147
}
pred@2203
   148
pred@2203
   149
void NextMapManager::OnSourceModLevelChange( const char *mapName )
pred@2203
   150
{
pred@2203
   151
	/* Skip the first 'mapchange' when the server starts up */
pred@2203
   152
	if (m_tempChangeInfo.startTime != 0)
pred@2203
   153
	{
pred@2203
   154
		if (strcmp(mapName, m_tempChangeInfo.m_mapName) == 0)
pred@2203
   155
		{
pred@2203
   156
			/* The map change was as we expected */
pred@2203
   157
			m_mapHistory.push_back(new MapChangeData(lastMap, m_tempChangeInfo.m_changeReason, m_tempChangeInfo.startTime));
pred@2203
   158
		}
pred@2203
   159
		else
pred@2203
   160
		{
pred@2203
   161
			/* Something intercepted the mapchange */
pred@2203
   162
			char newReason[255];
pred@2203
   163
			UTIL_Format(newReason, sizeof(newReason), "%s (Map overridden)", m_tempChangeInfo.m_changeReason);
pred@2203
   164
			m_mapHistory.push_back(new MapChangeData(lastMap, newReason, m_tempChangeInfo.startTime));
pred@2203
   165
		}
pred@2203
   166
pred@2203
   167
		/* TODO: Should this be customizable? */
pred@2203
   168
		if (m_mapHistory.size() > 20)
pred@2203
   169
		{
pred@2203
   170
			SourceHook::List<MapChangeData *>::iterator iter;
pred@2203
   171
			iter = m_mapHistory.begin();
pred@2203
   172
pred@2203
   173
			delete (MapChangeData *)*iter;
pred@2203
   174
pred@2203
   175
			m_mapHistory.erase(iter);
pred@2203
   176
		}
pred@2203
   177
	}
pred@2203
   178
pred@2203
   179
	m_tempChangeInfo.m_mapName[0] ='\0';
pred@2203
   180
	m_tempChangeInfo.m_changeReason[0] = '\0';
pred@2203
   181
	m_tempChangeInfo.startTime = time(NULL);
pred@2203
   182
	UTIL_Format(lastMap, sizeof(lastMap), mapName);
pred@2203
   183
}
pred@2203
   184
pred@2203
   185
void NextMapManager::ForceChangeLevel( const char *mapName, const char* changeReason )
pred@2203
   186
{
pred@2203
   187
	/* Store the mapname and reason */
pred@2203
   188
	UTIL_Format(m_tempChangeInfo.m_mapName, sizeof(m_tempChangeInfo.m_mapName), mapName);
pred@2203
   189
	UTIL_Format(m_tempChangeInfo.m_changeReason, sizeof(m_tempChangeInfo.m_changeReason), changeReason);
pred@2203
   190
pred@2203
   191
	/* Change level and skip our hook */
pred@2203
   192
	g_forcedChange = true;
pred@2203
   193
	engine->ChangeLevel(mapName, NULL);
pred@2203
   194
	g_forcedChange = false;
pred@2203
   195
}
pred@2203
   196
pred@2203
   197
NextMapManager::NextMapManager()
pred@2203
   198
{
pred@2203
   199
	m_tempChangeInfo = MapChangeData();
pred@2203
   200
	m_mapHistory = SourceHook::List<MapChangeData *>();
pred@2203
   201
}
pred@2203
   202
ds@2400
   203
#if SOURCE_ENGINE >= SE_ORANGEBOX
pred@2203
   204
void CmdChangeLevelCallback(const CCommand &command)
pred@2203
   205
{
pred@2203
   206
#else
pred@2203
   207
void CmdChangeLevelCallback()
pred@2203
   208
{
pred@2203
   209
	CCommand command;
pred@2203
   210
#endif
pred@2203
   211
pred@2203
   212
	if (command.ArgC() < 2)
pred@2203
   213
	{
pred@2203
   214
		return;
pred@2203
   215
	}
pred@2203
   216
pred@2203
   217
	if (g_NextMap.m_tempChangeInfo.m_mapName[0] == '\0')
pred@2203
   218
	{
pred@2203
   219
		UTIL_Format(g_NextMap.m_tempChangeInfo.m_mapName, sizeof(g_NextMap.m_tempChangeInfo.m_mapName), command.Arg(1));
pred@2203
   220
		UTIL_Format(g_NextMap.m_tempChangeInfo.m_changeReason, sizeof(g_NextMap.m_tempChangeInfo.m_changeReason), "changelevel Command");
pred@2203
   221
	}
pred@2205
   222
}