core/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Wed Nov 26 00:19:30 2008 -0600 (2008-11-26)
changeset 608 30b546cc4e03
parent 599 5b906351da4d
child 637 b66489d8aedc
permissions -rw-r--r--
Updated changelog, added license headers.
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@576
    37
SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@576
    38
ds@576
    39
ConCommand *g_plugin_unload = NULL;
ds@576
    40
bool g_bIsTryingToUnload;
ds@576
    41
ds@576
    42
void InterceptPluginUnloads(const CCommand &args)
ds@576
    43
{
ds@576
    44
	g_bIsTryingToUnload = true;
ds@576
    45
}
ds@576
    46
ds@576
    47
void InterceptPluginUnloads_Post(const CCommand &args)
ds@576
    48
{
ds@576
    49
	g_bIsTryingToUnload = false;
ds@576
    50
}
ds@576
    51
ds@576
    52
class VspBridge : public IVspBridge
ds@576
    53
{
ds@576
    54
public:
ds@576
    55
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@576
    56
	{
ds@576
    57
		assert(!g_Metamod.IsLoadedAsGameDLL());
ds@576
    58
ds@576
    59
		CGlobalVars *pGlobals;
ds@576
    60
		IPlayerInfoManager *playerInfoManager;
ds@576
    61
ds@576
    62
		playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
ds@576
    63
		if (playerInfoManager == NULL)
ds@576
    64
		{
ds@576
    65
			UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
ds@576
    66
			return false;
ds@576
    67
		}
ds@576
    68
ds@576
    69
		pGlobals = playerInfoManager->GetGlobalVars();
ds@576
    70
ds@576
    71
		char gamedll_iface[] = "ServerGameDLL000";
ds@576
    72
		for (unsigned int i = 5; i <= 50; i++)
ds@576
    73
		{
ds@576
    74
			gamedll_iface[15] = '0' + i;
ds@576
    75
			if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
ds@576
    76
			{
dvander@589
    77
				g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
ds@576
    78
				break;
ds@576
    79
			}
ds@576
    80
		}
ds@576
    81
ds@576
    82
		if (server == NULL)
ds@576
    83
		{
ds@576
    84
			UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
ds@576
    85
			return false;
ds@576
    86
		}
ds@576
    87
ds@576
    88
		char gameclients_iface[] = "ServerGameClients000";
ds@576
    89
		for (unsigned int i = 3; i <= 4; i++)
ds@576
    90
		{
ds@576
    91
			gameclients_iface[19] = '0' + i;
ds@576
    92
			if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
ds@576
    93
				break;
ds@576
    94
		}
ds@576
    95
ds@576
    96
		if (!mm_DetectGameInformation())
ds@576
    97
		{
ds@576
    98
			UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
ds@576
    99
			return false;
ds@576
   100
		}
ds@576
   101
ds@576
   102
		mm_InitializeForLoad();
ds@576
   103
		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
ds@576
   104
							 (CreateInterfaceFn)info->engineFactory,
ds@576
   105
							 (CreateInterfaceFn)info->engineFactory,
ds@576
   106
							 pGlobals);
dvander@592
   107
		g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@576
   108
		mm_StartupMetamod(true);
ds@576
   109
		
ds@576
   110
		g_plugin_unload = icvar->FindCommand("plugin_unload");
ds@576
   111
ds@576
   112
		if (g_plugin_unload != NULL)
ds@576
   113
		{
ds@576
   114
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   115
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   116
		}
ds@576
   117
ds@576
   118
		return true;
ds@576
   119
	}
ds@576
   120
ds@576
   121
	virtual void Unload()
ds@576
   122
	{
ds@576
   123
		if (g_bIsTryingToUnload)
ds@576
   124
		{
ds@576
   125
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   126
			return;
ds@576
   127
		}
ds@576
   128
		if (g_plugin_unload != NULL)
ds@576
   129
		{
ds@576
   130
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   131
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   132
			g_plugin_unload = NULL;
ds@576
   133
		}
ds@576
   134
		mm_UnloadMetamod();
ds@576
   135
	}
ds@576
   136
ds@576
   137
	virtual const char *GetDescription()
ds@576
   138
	{
ds@599
   139
		return "Metamod:Source " MMS_FULL_VERSION;
ds@576
   140
	}
ds@576
   141
};
ds@576
   142
ds@576
   143
VspBridge mm16_vsp_bridge;
ds@576
   144
ds@576
   145
SMM_API IVspBridge *
ds@576
   146
GetVspBridge()
ds@576
   147
{
ds@576
   148
	return &mm16_vsp_bridge;
ds@576
   149
}
ds@576
   150