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