core/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Mon Nov 18 09:46:46 2013 -0800 (2013-11-18)
changeset 870 004b1437bd14
parent 849 43df9a463b6e
child 887 e30d4a37f5e5
permissions -rw-r--r--
Upgrade build scripts to AMBuild 2 (bug 5970, r=ds).
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>
ds@576
    33
#include "provider/provider_ep2.h"
ds@576
    34
skamonkey@833
    35
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    36
SH_DECL_HOOK2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
skamonkey@833
    37
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    38
SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@637
    39
#else
ds@637
    40
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@637
    41
#endif
ds@576
    42
ds@576
    43
ConCommand *g_plugin_unload = NULL;
ds@576
    44
bool g_bIsTryingToUnload;
ds@683
    45
const char *vsp_desc = "Metamod:Source " MMS_FULL_VERSION;
ds@576
    46
skamonkey@833
    47
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    48
void InterceptPluginUnloads(const CCommandContext &context, const CCommand &args)
skamonkey@833
    49
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    50
void InterceptPluginUnloads(const CCommand &args)
ds@637
    51
#else
ds@637
    52
void InterceptPluginUnloads()
ds@637
    53
#endif
ds@576
    54
{
ds@576
    55
	g_bIsTryingToUnload = true;
ds@576
    56
}
ds@576
    57
skamonkey@833
    58
#if SOURCE_ENGINE == SE_DOTA
skamonkey@849
    59
void InterceptPluginUnloads_Post(const CCommandContext &context, const CCommand &args)
skamonkey@833
    60
#elif SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    61
void InterceptPluginUnloads_Post(const CCommand &args)
ds@637
    62
#else
ds@637
    63
void InterceptPluginUnloads_Post()
ds@637
    64
#endif
ds@576
    65
{
ds@576
    66
	g_bIsTryingToUnload = false;
ds@576
    67
}
ds@576
    68
ds@576
    69
class VspBridge : public IVspBridge
ds@576
    70
{
ds@576
    71
public:
ds@576
    72
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@576
    73
	{
ds@683
    74
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
    75
		{
ds@683
    76
			CGlobalVars *pGlobals;
ds@683
    77
			IPlayerInfoManager *playerInfoManager;
ds@576
    78
ds@683
    79
			playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
ds@683
    80
			if (playerInfoManager == NULL)
ds@683
    81
			{
ds@683
    82
				UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
ds@683
    83
				return false;
ds@683
    84
			}
ds@576
    85
ds@683
    86
			pGlobals = playerInfoManager->GetGlobalVars();
ds@683
    87
ds@683
    88
			char gamedll_iface[] = "ServerGameDLL000";
ds@683
    89
			for (unsigned int i = 3; i <= 50; i++)
ds@683
    90
			{
ds@683
    91
				gamedll_iface[15] = '0' + i;
ds@683
    92
				if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
ds@683
    93
				{
ds@683
    94
					g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
ds@683
    95
					break;
ds@683
    96
				}
ds@683
    97
			}
ds@576
    98
ds@683
    99
			if (server == NULL)
ds@683
   100
			{
ds@683
   101
				UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
ds@683
   102
				return false;
ds@683
   103
			}
ds@683
   104
ds@683
   105
			char gameclients_iface[] = "ServerGameClients000";
ds@683
   106
			for (unsigned int i = 3; i <= 4; i++)
ds@683
   107
			{
ds@683
   108
				gameclients_iface[19] = '0' + i;
ds@683
   109
				if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
ds@683
   110
					break;
ds@683
   111
			}
ds@576
   112
ds@683
   113
			if (!mm_DetectGameInformation())
ds@683
   114
			{
ds@683
   115
				UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
ds@683
   116
				return false;
ds@683
   117
			}
ds@683
   118
ds@683
   119
			mm_InitializeForLoad();
ds@683
   120
			mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
ds@683
   121
				(CreateInterfaceFn)info->engineFactory,
ds@683
   122
				(CreateInterfaceFn)info->engineFactory,
ds@683
   123
				pGlobals);
ds@683
   124
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@683
   125
			mm_StartupMetamod(true);
ds@683
   126
		}
ds@683
   127
		else
ds@576
   128
		{
ds@683
   129
			vsp_desc = "Metamod:Source Interface " MMS_FULL_VERSION;
ds@683
   130
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@576
   131
		}
ds@576
   132
ds@637
   133
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
   134
		g_plugin_unload = icvar->FindCommand("plugin_unload");
ds@637
   135
#else
ds@637
   136
		const ConCommandBase *pBase = icvar->GetCommands();
ds@637
   137
		while (pBase != NULL)
ds@637
   138
		{
ds@637
   139
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@637
   140
			{
ds@637
   141
				g_plugin_unload = (ConCommand *)pBase;
ds@637
   142
				break;
ds@637
   143
			}
ds@637
   144
			pBase = pBase->GetNext();
ds@637
   145
		}
ds@637
   146
#endif
ds@576
   147
ds@576
   148
		if (g_plugin_unload != NULL)
ds@576
   149
		{
ds@576
   150
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   151
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   152
		}
ds@576
   153
ds@576
   154
		return true;
ds@576
   155
	}
ds@576
   156
ds@576
   157
	virtual void Unload()
ds@576
   158
	{
ds@576
   159
		if (g_bIsTryingToUnload)
ds@576
   160
		{
ds@576
   161
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   162
			return;
ds@576
   163
		}
ds@576
   164
		if (g_plugin_unload != NULL)
ds@576
   165
		{
ds@576
   166
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   167
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   168
			g_plugin_unload = NULL;
ds@576
   169
		}
ds@683
   170
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
   171
		{
ds@683
   172
			mm_UnloadMetamod();
ds@683
   173
		}
ds@576
   174
	}
ds@576
   175
ds@576
   176
	virtual const char *GetDescription()
ds@576
   177
	{
ds@683
   178
		return vsp_desc;
ds@576
   179
	}
ds@576
   180
};
ds@576
   181
ds@576
   182
VspBridge mm16_vsp_bridge;
ds@576
   183
ds@576
   184
SMM_API IVspBridge *
ds@576
   185
GetVspBridge()
ds@576
   186
{
ds@576
   187
	return &mm16_vsp_bridge;
ds@576
   188
}
ds@576
   189