core-legacy/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Nov 24 03:50:12 2008 -0600 (2008-11-24)
changeset 599 5b906351da4d
parent 576 fc3600fed257
child 608 30b546cc4e03
permissions -rw-r--r--
Replaced SVN_FULL_VERSION with MMS_FULL_VERSION and SVN_FILE_VERSION with MMS_FILE_VERSION in version headers.
ds@599
     1
#include <assert.h>
ds@599
     2
#include "sourcemm.h"
ds@599
     3
#include "concommands.h"
ds@599
     4
#include <loader_bridge.h>
ds@599
     5
ds@599
     6
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@599
     7
ds@599
     8
ConCommand *g_plugin_unload = NULL;
ds@599
     9
bool g_bIsTryingToUnload;
ds@599
    10
bool g_bIsBridgedAsVsp;
ds@599
    11
ds@599
    12
void InterceptPluginUnloads()
ds@599
    13
{
ds@599
    14
	g_bIsTryingToUnload = true;
ds@599
    15
}
ds@599
    16
ds@599
    17
void InterceptPluginUnloads_Post()
ds@599
    18
{
ds@599
    19
	g_bIsTryingToUnload = false;
ds@599
    20
}
ds@599
    21
ds@599
    22
class VspBridge : public IVspBridge
ds@599
    23
{
ds@599
    24
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@599
    25
	{
ds@599
    26
		assert(!g_GameDll.loaded && !g_bIsBridgedAsVsp);
ds@599
    27
ds@599
    28
		CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
ds@599
    29
		CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
ds@599
    30
ds@599
    31
		if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
ds@599
    32
			return false;
ds@599
    33
ds@599
    34
		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
ds@599
    35
		while (pBase != NULL)
ds@599
    36
		{
ds@599
    37
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@599
    38
			{
ds@599
    39
				g_plugin_unload = (ConCommand *)pBase;
ds@599
    40
				break;
ds@599
    41
			}
ds@599
    42
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@599
    43
		}
ds@599
    44
ds@599
    45
		if (g_plugin_unload != NULL)
ds@599
    46
		{
ds@599
    47
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@599
    48
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@599
    49
		}
ds@599
    50
ds@599
    51
		extern ConVar metamod_version;
ds@599
    52
		char buffer[255];
ds@599
    53
ds@599
    54
		UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
ds@599
    55
		metamod_version.SetValue(buffer);
ds@599
    56
ds@599
    57
		g_bIsBridgedAsVsp = true;
ds@599
    58
		g_pRealVspCallbacks = info->vsp_callbacks;
ds@599
    59
ds@599
    60
		g_PluginMngr.SetVSPAsLoaded();
ds@599
    61
ds@599
    62
		return true;
ds@599
    63
	}
ds@599
    64
ds@599
    65
	virtual void Unload()
ds@599
    66
	{
ds@576
    67
		if (g_bIsTryingToUnload)
ds@576
    68
		{
ds@576
    69
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
    70
			return;
ds@576
    71
		}
ds@576
    72
		if (g_plugin_unload != NULL)
ds@576
    73
		{
ds@576
    74
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
    75
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
    76
			g_plugin_unload = NULL;
ds@599
    77
		}
ds@599
    78
		g_SMConVarAccessor.UnloadMetamodCommands();
ds@599
    79
		UnloadMetamod(false);
ds@599
    80
	}
ds@599
    81
ds@599
    82
	virtual const char *GetDescription()
ds@599
    83
	{
ds@599
    84
		return "Metamod:Source " MMS_FULL_VERSION;
ds@599
    85
	}
ds@599
    86
};
ds@599
    87
ds@599
    88
VspBridge mm14_vsp_bridge;
ds@599
    89
ds@599
    90
SMM_API IVspBridge *
ds@599
    91
GetVspBridge()
ds@599
    92
{
ds@599
    93
	return &mm14_vsp_bridge;
ds@599
    94
}