core/vsp_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 887 e30d4a37f5e5
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
dvander@608
     1
/**
ds@683
     2
 * vim: set ts=4 sw=4 tw=99 noet :
dvander@608
     3
 * ======================================================
dvander@608
     4
 * Metamod:Source
ds@683
     5
 * Copyright (C) 2004-2009 AlliedModders LLC and authors.
dvander@608
     6
 * All rights reserved.
dvander@608
     7
 * ======================================================
dvander@608
     8
 *
dvander@608
     9
 * This software is provided 'as-is', without any express or implied warranty.
dvander@608
    10
 * In no event will the authors be held liable for any damages arising from 
dvander@608
    11
 * the use of this software.
dvander@608
    12
 * 
dvander@608
    13
 * Permission is granted to anyone to use this software for any purpose, 
dvander@608
    14
 * including commercial applications, and to alter it and redistribute it 
dvander@608
    15
 * freely, subject to the following restrictions:
dvander@608
    16
 *
dvander@608
    17
 * 1. The origin of this software must not be misrepresented; you must not 
dvander@608
    18
 * claim that you wrote the original software. If you use this software in a 
dvander@608
    19
 * product, an acknowledgment in the product documentation would be 
dvander@608
    20
 * appreciated but is not required.
dvander@608
    21
 * 2. Altered source versions must be plainly marked as such, and must not be
dvander@608
    22
 * misrepresented as being the original software.
dvander@608
    23
 * 3. This notice may not be removed or altered from any source distribution.
dvander@608
    24
 */
dvander@608
    25
ds@576
    26
#include "metamod.h"
ds@576
    27
#include "metamod_util.h"
ds@576
    28
#include <interface.h>
ds@576
    29
#include <eiface.h>
ds@576
    30
#include <iplayerinfo.h>
ds@576
    31
#include <assert.h>
ds@576
    32
#include <loader_bridge.h>
dvander@887
    33
#include <versionlib.h>
dvander@887
    34
#include <sh_string.h>
ds@576
    35
#include "provider/provider_ep2.h"
ds@576
    36
skamonkey@833
    37
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    38
SH_DECL_HOOK2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
skamonkey@833
    39
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    40
SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@637
    41
#else
ds@637
    42
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@637
    43
#endif
ds@576
    44
ds@576
    45
ConCommand *g_plugin_unload = NULL;
ds@576
    46
bool g_bIsTryingToUnload;
dvander@887
    47
SourceHook::String vsp_desc("Metamod:Source");
ds@576
    48
skamonkey@833
    49
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    50
void InterceptPluginUnloads(const CCommandContext &context, const CCommand &args)
skamonkey@833
    51
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    52
void InterceptPluginUnloads(const CCommand &args)
ds@637
    53
#else
ds@637
    54
void InterceptPluginUnloads()
ds@637
    55
#endif
ds@576
    56
{
ds@576
    57
	g_bIsTryingToUnload = true;
ds@576
    58
}
ds@576
    59
skamonkey@833
    60
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    61
void InterceptPluginUnloads_Post(const CCommandContext &context, const CCommand &args)
skamonkey@833
    62
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    63
void InterceptPluginUnloads_Post(const CCommand &args)
ds@637
    64
#else
ds@637
    65
void InterceptPluginUnloads_Post()
ds@637
    66
#endif
ds@576
    67
{
ds@576
    68
	g_bIsTryingToUnload = false;
ds@576
    69
}
ds@576
    70
ds@576
    71
class VspBridge : public IVspBridge
ds@576
    72
{
ds@576
    73
public:
ds@576
    74
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@576
    75
	{
ds@683
    76
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
    77
		{
dvander@887
    78
			vsp_desc.append(" ");
dvander@887
    79
			vsp_desc.append(METAMOD_VERSION);
dvander@887
    80
ds@683
    81
			CGlobalVars *pGlobals;
ds@683
    82
			IPlayerInfoManager *playerInfoManager;
ds@576
    83
ds@683
    84
			playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
ds@683
    85
			if (playerInfoManager == NULL)
ds@683
    86
			{
ds@683
    87
				UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
ds@683
    88
				return false;
ds@683
    89
			}
ds@576
    90
ds@683
    91
			pGlobals = playerInfoManager->GetGlobalVars();
ds@683
    92
ds@683
    93
			char gamedll_iface[] = "ServerGameDLL000";
ds@683
    94
			for (unsigned int i = 3; i <= 50; i++)
ds@683
    95
			{
ds@683
    96
				gamedll_iface[15] = '0' + i;
ds@683
    97
				if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
ds@683
    98
				{
ds@683
    99
					g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
ds@683
   100
					break;
ds@683
   101
				}
ds@683
   102
			}
ds@576
   103
ds@683
   104
			if (server == NULL)
ds@683
   105
			{
ds@683
   106
				UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
ds@683
   107
				return false;
ds@683
   108
			}
ds@683
   109
ds@683
   110
			char gameclients_iface[] = "ServerGameClients000";
ds@683
   111
			for (unsigned int i = 3; i <= 4; i++)
ds@683
   112
			{
ds@683
   113
				gameclients_iface[19] = '0' + i;
ds@683
   114
				if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
ds@683
   115
					break;
ds@683
   116
			}
ds@576
   117
ds@683
   118
			if (!mm_DetectGameInformation())
ds@683
   119
			{
ds@683
   120
				UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
ds@683
   121
				return false;
ds@683
   122
			}
ds@683
   123
ds@683
   124
			mm_InitializeForLoad();
ds@683
   125
			mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
ds@683
   126
				(CreateInterfaceFn)info->engineFactory,
ds@683
   127
				(CreateInterfaceFn)info->engineFactory,
ds@683
   128
				pGlobals);
ds@683
   129
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@683
   130
			mm_StartupMetamod(true);
ds@683
   131
		}
ds@683
   132
		else
ds@576
   133
		{
dvander@887
   134
			vsp_desc.append(" Interface ");
dvander@887
   135
			vsp_desc.append(METAMOD_VERSION);
dvander@887
   136
ds@683
   137
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@576
   138
		}
ds@576
   139
ds@637
   140
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
   141
		g_plugin_unload = icvar->FindCommand("plugin_unload");
ds@637
   142
#else
ds@637
   143
		const ConCommandBase *pBase = icvar->GetCommands();
ds@637
   144
		while (pBase != NULL)
ds@637
   145
		{
ds@637
   146
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@637
   147
			{
ds@637
   148
				g_plugin_unload = (ConCommand *)pBase;
ds@637
   149
				break;
ds@637
   150
			}
ds@637
   151
			pBase = pBase->GetNext();
ds@637
   152
		}
ds@637
   153
#endif
ds@576
   154
ds@576
   155
		if (g_plugin_unload != NULL)
ds@576
   156
		{
ds@576
   157
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   158
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   159
		}
ds@576
   160
ds@576
   161
		return true;
ds@576
   162
	}
ds@576
   163
ds@576
   164
	virtual void Unload()
ds@576
   165
	{
ds@576
   166
		if (g_bIsTryingToUnload)
ds@576
   167
		{
ds@576
   168
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   169
			return;
ds@576
   170
		}
ds@576
   171
		if (g_plugin_unload != NULL)
ds@576
   172
		{
ds@576
   173
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   174
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   175
			g_plugin_unload = NULL;
ds@576
   176
		}
ds@683
   177
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
   178
		{
ds@683
   179
			mm_UnloadMetamod();
ds@683
   180
		}
ds@576
   181
	}
ds@576
   182
ds@576
   183
	virtual const char *GetDescription()
ds@576
   184
	{
dvander@887
   185
		return vsp_desc.c_str();
ds@576
   186
	}
ds@576
   187
};
ds@576
   188
ds@576
   189
VspBridge mm16_vsp_bridge;
ds@576
   190
ds@576
   191
SMM_API IVspBridge *
ds@576
   192
GetVspBridge()
ds@576
   193
{
ds@576
   194
	return &mm16_vsp_bridge;
ds@576
   195
}
ds@576
   196