core-legacy/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 663 e07fd1dc5290
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@599
    28
#include <assert.h>
ds@599
    29
#include "sourcemm.h"
ds@599
    30
#include "concommands.h"
ds@599
    31
#include <loader_bridge.h>
ds@599
    32
ds@599
    33
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@599
    34
ds@599
    35
ConCommand *g_plugin_unload = NULL;
ds@599
    36
bool g_bIsTryingToUnload;
ds@599
    37
bool g_bIsBridgedAsVsp;
ds@599
    38
ds@599
    39
void InterceptPluginUnloads()
ds@599
    40
{
ds@599
    41
	g_bIsTryingToUnload = true;
ds@599
    42
}
ds@599
    43
ds@599
    44
void InterceptPluginUnloads_Post()
ds@599
    45
{
ds@599
    46
	g_bIsTryingToUnload = false;
ds@599
    47
}
ds@599
    48
ds@599
    49
class VspBridge : public IVspBridge
ds@599
    50
{
ds@599
    51
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@599
    52
	{
ds@599
    53
		assert(!g_GameDll.loaded && !g_bIsBridgedAsVsp);
ds@599
    54
ds@599
    55
		CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
ds@599
    56
		CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
ds@599
    57
ds@599
    58
		if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
ds@599
    59
			return false;
ds@599
    60
ds@599
    61
		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
ds@599
    62
		while (pBase != NULL)
ds@599
    63
		{
ds@599
    64
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@599
    65
			{
ds@599
    66
				g_plugin_unload = (ConCommand *)pBase;
ds@599
    67
				break;
ds@599
    68
			}
ds@599
    69
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@599
    70
		}
ds@599
    71
ds@599
    72
		if (g_plugin_unload != NULL)
ds@599
    73
		{
ds@599
    74
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@599
    75
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@599
    76
		}
ds@599
    77
ds@599
    78
		extern ConVar metamod_version;
ds@599
    79
		char buffer[255];
ds@599
    80
ds@599
    81
		UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
ds@599
    82
		metamod_version.SetValue(buffer);
ds@599
    83
ds@599
    84
		g_bIsBridgedAsVsp = true;
ds@599
    85
		g_pRealVspCallbacks = info->vsp_callbacks;
ds@599
    86
ds@599
    87
		g_PluginMngr.SetVSPAsLoaded();
ds@599
    88
ds@599
    89
		return true;
ds@599
    90
	}
ds@599
    91
ds@599
    92
	virtual void Unload()
ds@599
    93
	{
ds@576
    94
		if (g_bIsTryingToUnload)
ds@576
    95
		{
ds@576
    96
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
    97
			return;
ds@576
    98
		}
ds@576
    99
		if (g_plugin_unload != NULL)
ds@576
   100
		{
ds@576
   101
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   102
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   103
			g_plugin_unload = NULL;
ds@599
   104
		}
ds@599
   105
		g_SMConVarAccessor.UnloadMetamodCommands();
ds@599
   106
		UnloadMetamod(false);
ds@599
   107
	}
ds@599
   108
ds@599
   109
	virtual const char *GetDescription()
ds@599
   110
	{
ds@599
   111
		return "Metamod:Source " MMS_FULL_VERSION;
ds@599
   112
	}
ds@599
   113
};
ds@599
   114
ds@599
   115
VspBridge mm14_vsp_bridge;
ds@599
   116
ds@599
   117
SMM_API IVspBridge *
ds@599
   118
GetVspBridge()
ds@599
   119
{
ds@599
   120
	return &mm14_vsp_bridge;
ds@599
   121
}