core-legacy/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Sat Nov 23 19:10:27 2013 -0800 (2013-11-23)
changeset 887 e30d4a37f5e5
parent 683 b507295429f9
permissions -rw-r--r--
Extract version info into a static library to speed up builds (bug 5990, r=ds).
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>
dvander@887
    31
#include <sh_string.h>
dvander@887
    32
#include <versionlib.h>
ds@599
    33
ds@599
    34
SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
ds@599
    35
ds@599
    36
ConCommand *g_plugin_unload = NULL;
ds@599
    37
bool g_bIsTryingToUnload;
ds@599
    38
bool g_bIsBridgedAsVsp;
dvander@887
    39
SourceHook::String vsp_desc("Metamod:Source");
ds@599
    40
ds@599
    41
void InterceptPluginUnloads()
ds@599
    42
{
ds@599
    43
	g_bIsTryingToUnload = true;
ds@599
    44
}
ds@599
    45
ds@599
    46
void InterceptPluginUnloads_Post()
ds@599
    47
{
ds@599
    48
	g_bIsTryingToUnload = false;
ds@599
    49
}
ds@599
    50
ds@599
    51
class VspBridge : public IVspBridge
ds@599
    52
{
ds@599
    53
	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
ds@599
    54
	{
ds@683
    55
		assert(!g_bIsBridgedAsVsp);
ds@683
    56
ds@683
    57
		if (!g_GameDll.loaded)
ds@683
    58
		{
dvander@887
    59
			vsp_desc.append(" ");
dvander@887
    60
			vsp_desc.append(METAMOD_VERSION);
dvander@887
    61
ds@683
    62
			CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
ds@683
    63
			CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
ds@683
    64
ds@683
    65
			if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
ds@683
    66
				return false;
ds@599
    67
ds@683
    68
			extern ConVar metamod_version;
ds@683
    69
			char buffer[255];
ds@599
    70
ds@683
    71
			UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
ds@683
    72
			metamod_version.SetValue(buffer);
ds@683
    73
		}
ds@683
    74
		else
ds@683
    75
		{
dvander@887
    76
			vsp_desc.append(" Interface ");
dvander@887
    77
			vsp_desc.append(METAMOD_VERSION);
ds@683
    78
		}
ds@599
    79
ds@599
    80
		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
ds@599
    81
		while (pBase != NULL)
ds@599
    82
		{
ds@599
    83
			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
ds@599
    84
			{
ds@599
    85
				g_plugin_unload = (ConCommand *)pBase;
ds@599
    86
				break;
ds@599
    87
			}
ds@599
    88
			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
ds@599
    89
		}
ds@599
    90
ds@599
    91
		if (g_plugin_unload != NULL)
ds@599
    92
		{
ds@599
    93
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@599
    94
			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@599
    95
		}
ds@599
    96
ds@599
    97
		g_bIsBridgedAsVsp = true;
ds@599
    98
		g_pRealVspCallbacks = info->vsp_callbacks;
dvander@666
    99
		g_vsp_version = info->vsp_version;
ds@599
   100
ds@599
   101
		g_PluginMngr.SetVSPAsLoaded();
ds@599
   102
ds@599
   103
		return true;
ds@599
   104
	}
ds@599
   105
ds@599
   106
	virtual void Unload()
ds@599
   107
	{
ds@576
   108
		if (g_bIsTryingToUnload)
ds@576
   109
		{
ds@576
   110
			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
ds@576
   111
			return;
ds@576
   112
		}
ds@576
   113
		if (g_plugin_unload != NULL)
ds@576
   114
		{
ds@576
   115
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
ds@576
   116
			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
ds@576
   117
			g_plugin_unload = NULL;
ds@599
   118
		}
ds@683
   119
		if (!g_GameDll.loaded)
ds@683
   120
		{
ds@683
   121
			g_SMConVarAccessor.UnloadMetamodCommands();
ds@683
   122
			UnloadMetamod(false);
ds@683
   123
		}
ds@599
   124
	}
ds@599
   125
ds@599
   126
	virtual const char *GetDescription()
ds@599
   127
	{
dvander@887
   128
		return vsp_desc.c_str();
ds@599
   129
	}
ds@599
   130
};
ds@599
   131
ds@599
   132
VspBridge mm14_vsp_bridge;
ds@599
   133
ds@599
   134
SMM_API IVspBridge *
ds@599
   135
GetVspBridge()
ds@599
   136
{
ds@599
   137
	return &mm14_vsp_bridge;
ds@599
   138
}