core/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 683 b507295429f9
child 833 9a8182b48222
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
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
ds@637
    35
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    36
SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
ds@637
    37
#else
ds@637
    38
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@637
    39
#endif
ds@576
    40
ds@576
    41
ConCommand *g_plugin_unload = NULL;
ds@576
    42
bool g_bIsTryingToUnload;
ds@683
    43
const char *vsp_desc = "Metamod:Source " MMS_FULL_VERSION;
ds@576
    44
ds@637
    45
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    46
void InterceptPluginUnloads(const CCommand &args)
ds@637
    47
#else
ds@637
    48
void InterceptPluginUnloads()
ds@637
    49
#endif
ds@576
    50
{
ds@576
    51
	g_bIsTryingToUnload = true;
ds@576
    52
}
ds@576
    53
ds@637
    54
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
    55
void InterceptPluginUnloads_Post(const CCommand &args)
ds@637
    56
#else
ds@637
    57
void InterceptPluginUnloads_Post()
ds@637
    58
#endif
ds@576
    59
{
ds@576
    60
	g_bIsTryingToUnload = false;
ds@576
    61
}
ds@576
    62
ds@576
    63
class VspBridge : public IVspBridge
ds@576
    64
{
ds@576
    65
public:
ds@576
    66
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@576
    67
	{
ds@683
    68
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
    69
		{
ds@683
    70
			CGlobalVars *pGlobals;
ds@683
    71
			IPlayerInfoManager *playerInfoManager;
ds@576
    72
ds@683
    73
			playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
ds@683
    74
			if (playerInfoManager == NULL)
ds@683
    75
			{
ds@683
    76
				UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
ds@683
    77
				return false;
ds@683
    78
			}
ds@576
    79
ds@683
    80
			pGlobals = playerInfoManager->GetGlobalVars();
ds@683
    81
ds@683
    82
			char gamedll_iface[] = "ServerGameDLL000";
ds@683
    83
			for (unsigned int i = 3; i <= 50; i++)
ds@683
    84
			{
ds@683
    85
				gamedll_iface[15] = '0' + i;
ds@683
    86
				if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
ds@683
    87
				{
ds@683
    88
					g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
ds@683
    89
					break;
ds@683
    90
				}
ds@683
    91
			}
ds@576
    92
ds@683
    93
			if (server == NULL)
ds@683
    94
			{
ds@683
    95
				UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
ds@683
    96
				return false;
ds@683
    97
			}
ds@683
    98
ds@683
    99
			char gameclients_iface[] = "ServerGameClients000";
ds@683
   100
			for (unsigned int i = 3; i <= 4; i++)
ds@683
   101
			{
ds@683
   102
				gameclients_iface[19] = '0' + i;
ds@683
   103
				if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
ds@683
   104
					break;
ds@683
   105
			}
ds@576
   106
ds@683
   107
			if (!mm_DetectGameInformation())
ds@683
   108
			{
ds@683
   109
				UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
ds@683
   110
				return false;
ds@683
   111
			}
ds@683
   112
ds@683
   113
			mm_InitializeForLoad();
ds@683
   114
			mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
ds@683
   115
				(CreateInterfaceFn)info->engineFactory,
ds@683
   116
				(CreateInterfaceFn)info->engineFactory,
ds@683
   117
				pGlobals);
ds@683
   118
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@683
   119
			mm_StartupMetamod(true);
ds@683
   120
		}
ds@683
   121
		else
ds@576
   122
		{
ds@683
   123
			vsp_desc = "Metamod:Source Interface " MMS_FULL_VERSION;
ds@683
   124
			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
ds@576
   125
		}
ds@576
   126
ds@637
   127
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@576
   128
		g_plugin_unload = icvar->FindCommand("plugin_unload");
ds@637
   129
#else
ds@637
   130
		const ConCommandBase *pBase = icvar->GetCommands();
ds@637
   131
		while (pBase != NULL)
ds@637
   132
		{
ds@637
   133
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@637
   134
			{
ds@637
   135
				g_plugin_unload = (ConCommand *)pBase;
ds@637
   136
				break;
ds@637
   137
			}
ds@637
   138
			pBase = pBase->GetNext();
ds@637
   139
		}
ds@637
   140
#endif
ds@576
   141
ds@576
   142
		if (g_plugin_unload != NULL)
ds@576
   143
		{
ds@576
   144
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   145
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   146
		}
ds@576
   147
ds@576
   148
		return true;
ds@576
   149
	}
ds@576
   150
ds@576
   151
	virtual void Unload()
ds@576
   152
	{
ds@576
   153
		if (g_bIsTryingToUnload)
ds@576
   154
		{
ds@576
   155
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   156
			return;
ds@576
   157
		}
ds@576
   158
		if (g_plugin_unload != NULL)
ds@576
   159
		{
ds@576
   160
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   161
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   162
			g_plugin_unload = NULL;
ds@576
   163
		}
ds@683
   164
		if (!g_Metamod.IsLoadedAsGameDLL())
ds@683
   165
		{
ds@683
   166
			mm_UnloadMetamod();
ds@683
   167
		}
ds@576
   168
	}
ds@576
   169
ds@576
   170
	virtual const char *GetDescription()
ds@576
   171
	{
ds@683
   172
		return vsp_desc;
ds@576
   173
	}
ds@576
   174
};
ds@576
   175
ds@576
   176
VspBridge mm16_vsp_bridge;
ds@576
   177
ds@576
   178
SMM_API IVspBridge *
ds@576
   179
GetVspBridge()
ds@576
   180
{
ds@576
   181
	return &mm16_vsp_bridge;
ds@576
   182
}
ds@576
   183