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