core-legacy/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Mon Nov 18 12:42:17 2013 -0800 (2013-11-18)
changeset 885 a4c8a8c571a0
parent 683 b507295429f9
child 887 e30d4a37f5e5
permissions -rw-r--r--
Fix dependency graph.
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@599
    26
#include <assert.h>
ds@599
    27
#include "sourcemm.h"
ds@599
    28
#include "concommands.h"
dvander@663
    29
#include "util.h"
ds@599
    30
#include <loader_bridge.h>
ds@599
    31
ds@599
    32
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@599
    33
ds@599
    34
ConCommand *g_plugin_unload = NULL;
ds@599
    35
bool g_bIsTryingToUnload;
ds@599
    36
bool g_bIsBridgedAsVsp;
ds@683
    37
const char *vsp_desc = "Metamod:Source " MMS_FULL_VERSION;
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@683
    53
		assert(!g_bIsBridgedAsVsp);
ds@683
    54
ds@683
    55
		if (!g_GameDll.loaded)
ds@683
    56
		{
ds@683
    57
			CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
ds@683
    58
			CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
ds@683
    59
ds@683
    60
			if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
ds@683
    61
				return false;
ds@599
    62
ds@683
    63
			extern ConVar metamod_version;
ds@683
    64
			char buffer[255];
ds@599
    65
ds@683
    66
			UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
ds@683
    67
			metamod_version.SetValue(buffer);
ds@683
    68
		}
ds@683
    69
		else
ds@683
    70
		{
ds@683
    71
			vsp_desc = "Metamod:Source Interface " MMS_FULL_VERSION;
ds@683
    72
		}
ds@599
    73
ds@599
    74
		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
ds@599
    75
		while (pBase != NULL)
ds@599
    76
		{
ds@599
    77
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@599
    78
			{
ds@599
    79
				g_plugin_unload = (ConCommand *)pBase;
ds@599
    80
				break;
ds@599
    81
			}
ds@599
    82
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@599
    83
		}
ds@599
    84
ds@599
    85
		if (g_plugin_unload != NULL)
ds@599
    86
		{
ds@599
    87
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@599
    88
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@599
    89
		}
ds@599
    90
ds@599
    91
		g_bIsBridgedAsVsp = true;
ds@599
    92
		g_pRealVspCallbacks = info->vsp_callbacks;
dvander@666
    93
		g_vsp_version = info->vsp_version;
ds@599
    94
ds@599
    95
		g_PluginMngr.SetVSPAsLoaded();
ds@599
    96
ds@599
    97
		return true;
ds@599
    98
	}
ds@599
    99
ds@599
   100
	virtual void Unload()
ds@599
   101
	{
ds@576
   102
		if (g_bIsTryingToUnload)
ds@576
   103
		{
ds@576
   104
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   105
			return;
ds@576
   106
		}
ds@576
   107
		if (g_plugin_unload != NULL)
ds@576
   108
		{
ds@576
   109
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   110
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   111
			g_plugin_unload = NULL;
ds@599
   112
		}
ds@683
   113
		if (!g_GameDll.loaded)
ds@683
   114
		{
ds@683
   115
			g_SMConVarAccessor.UnloadMetamodCommands();
ds@683
   116
			UnloadMetamod(false);
ds@683
   117
		}
ds@599
   118
	}
ds@599
   119
ds@599
   120
	virtual const char *GetDescription()
ds@599
   121
	{
ds@683
   122
		return vsp_desc;
ds@599
   123
	}
ds@599
   124
};
ds@599
   125
ds@599
   126
VspBridge mm14_vsp_bridge;
ds@599
   127
ds@599
   128
SMM_API IVspBridge *
ds@599
   129
GetVspBridge()
ds@599
   130
{
ds@599
   131
	return &mm14_vsp_bridge;
ds@599
   132
}