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