core/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Nov 14 00:49:35 2009 -0600 (2009-11-14)
changeset 680 39d0803bdb6e
parent 637 b66489d8aedc
child 683 b507295429f9
permissions -rw-r--r--
Fixed issue with vsnprintf macro being defined under MSVC 9/2008 (bug 3887, r=dvander).
dvander@608
     1
/**
dvander@608
     2
 * vim: set ts=4 :
dvander@608
     3
 * ======================================================
dvander@608
     4
 * Metamod:Source
dvander@608
     5
 * Copyright (C) 2004-2008 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
 * Version: $Id$
dvander@608
    26
 */
dvander@608
    27
ds@576
    28
#include "metamod.h"
ds@576
    29
#include "metamod_util.h"
ds@576
    30
#include <interface.h>
ds@576
    31
#include <eiface.h>
ds@576
    32
#include <iplayerinfo.h>
ds@576
    33
#include <assert.h>
ds@576
    34
#include <loader_bridge.h>
ds@576
    35
#include "provider/provider_ep2.h"
ds@576
    36
ds@637
    37
#if 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@576
    45
ds@637
    46
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    47
void InterceptPluginUnloads(const CCommand &args)
ds@637
    48
#else
ds@637
    49
void InterceptPluginUnloads()
ds@637
    50
#endif
ds@576
    51
{
ds@576
    52
	g_bIsTryingToUnload = true;
ds@576
    53
}
ds@576
    54
ds@637
    55
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    56
void InterceptPluginUnloads_Post(const CCommand &args)
ds@637
    57
#else
ds@637
    58
void InterceptPluginUnloads_Post()
ds@637
    59
#endif
ds@576
    60
{
ds@576
    61
	g_bIsTryingToUnload = false;
ds@576
    62
}
ds@576
    63
ds@576
    64
class VspBridge : public IVspBridge
ds@576
    65
{
ds@576
    66
public:
ds@576
    67
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@576
    68
	{
ds@576
    69
		assert(!g_Metamod.IsLoadedAsGameDLL());
ds@576
    70
ds@576
    71
		CGlobalVars *pGlobals;
ds@576
    72
		IPlayerInfoManager *playerInfoManager;
ds@576
    73
ds@576
    74
		playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
ds@576
    75
		if (playerInfoManager == NULL)
ds@576
    76
		{
ds@576
    77
			UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
ds@576
    78
			return false;
ds@576
    79
		}
ds@576
    80
ds@576
    81
		pGlobals = playerInfoManager->GetGlobalVars();
ds@576
    82
ds@576
    83
		char gamedll_iface[] = "ServerGameDLL000";
ds@637
    84
		for (unsigned int i = 3; i <= 50; i++)
ds@576
    85
		{
ds@576
    86
			gamedll_iface[15] = '0' + i;
ds@576
    87
			if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
ds@576
    88
			{
dvander@589
    89
				g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
ds@576
    90
				break;
ds@576
    91
			}
ds@576
    92
		}
ds@576
    93
ds@576
    94
		if (server == NULL)
ds@576
    95
		{
ds@576
    96
			UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
ds@576
    97
			return false;
ds@576
    98
		}
ds@576
    99
ds@576
   100
		char gameclients_iface[] = "ServerGameClients000";
ds@576
   101
		for (unsigned int i = 3; i <= 4; i++)
ds@576
   102
		{
ds@576
   103
			gameclients_iface[19] = '0' + i;
ds@576
   104
			if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
ds@576
   105
				break;
ds@576
   106
		}
ds@576
   107
ds@576
   108
		if (!mm_DetectGameInformation())
ds@576
   109
		{
ds@576
   110
			UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
ds@576
   111
			return false;
ds@576
   112
		}
ds@576
   113
ds@576
   114
		mm_InitializeForLoad();
ds@576
   115
		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
ds@576
   116
							 (CreateInterfaceFn)info->engineFactory,
ds@576
   117
							 (CreateInterfaceFn)info->engineFactory,
ds@576
   118
							 pGlobals);
dvander@592
   119
		g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@576
   120
		mm_StartupMetamod(true);
ds@576
   121
		
ds@637
   122
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
   123
		g_plugin_unload = icvar->FindCommand("plugin_unload");
ds@637
   124
#else
ds@637
   125
		const ConCommandBase *pBase = icvar->GetCommands();
ds@637
   126
		while (pBase != NULL)
ds@637
   127
		{
ds@637
   128
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@637
   129
			{
ds@637
   130
				g_plugin_unload = (ConCommand *)pBase;
ds@637
   131
				break;
ds@637
   132
			}
ds@637
   133
			pBase = pBase->GetNext();
ds@637
   134
		}
ds@637
   135
#endif
ds@576
   136
ds@576
   137
		if (g_plugin_unload != NULL)
ds@576
   138
		{
ds@576
   139
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   140
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   141
		}
ds@576
   142
ds@576
   143
		return true;
ds@576
   144
	}
ds@576
   145
ds@576
   146
	virtual void Unload()
ds@576
   147
	{
ds@576
   148
		if (g_bIsTryingToUnload)
ds@576
   149
		{
ds@576
   150
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   151
			return;
ds@576
   152
		}
ds@576
   153
		if (g_plugin_unload != NULL)
ds@576
   154
		{
ds@576
   155
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   156
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   157
			g_plugin_unload = NULL;
ds@576
   158
		}
ds@576
   159
		mm_UnloadMetamod();
ds@576
   160
	}
ds@576
   161
ds@576
   162
	virtual const char *GetDescription()
ds@576
   163
	{
ds@599
   164
		return "Metamod:Source " MMS_FULL_VERSION;
ds@576
   165
	}
ds@576
   166
};
ds@576
   167
ds@576
   168
VspBridge mm16_vsp_bridge;
ds@576
   169
ds@576
   170
SMM_API IVspBridge *
ds@576
   171
GetVspBridge()
ds@576
   172
{
ds@576
   173
	return &mm16_vsp_bridge;
ds@576
   174
}
ds@576
   175