core-legacy/vsp_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 887 e30d4a37f5e5
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2009 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  */
    25 
    26 #include <assert.h>
    27 #include "sourcemm.h"
    28 #include "concommands.h"
    29 #include "util.h"
    30 #include <loader_bridge.h>
    31 #include <sh_string.h>
    32 #include <versionlib.h>
    33 
    34 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    35 
    36 ConCommand *g_plugin_unload = NULL;
    37 bool g_bIsTryingToUnload;
    38 bool g_bIsBridgedAsVsp;
    39 SourceHook::String vsp_desc("Metamod:Source");
    40 
    41 void InterceptPluginUnloads()
    42 {
    43 	g_bIsTryingToUnload = true;
    44 }
    45 
    46 void InterceptPluginUnloads_Post()
    47 {
    48 	g_bIsTryingToUnload = false;
    49 }
    50 
    51 class VspBridge : public IVspBridge
    52 {
    53 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    54 	{
    55 		assert(!g_bIsBridgedAsVsp);
    56 
    57 		if (!g_GameDll.loaded)
    58 		{
    59 			vsp_desc.append(" ");
    60 			vsp_desc.append(METAMOD_VERSION);
    61 
    62 			CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
    63 			CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
    64 
    65 			if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
    66 				return false;
    67 
    68 			extern ConVar metamod_version;
    69 			char buffer[255];
    70 
    71 			UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
    72 			metamod_version.SetValue(buffer);
    73 		}
    74 		else
    75 		{
    76 			vsp_desc.append(" Interface ");
    77 			vsp_desc.append(METAMOD_VERSION);
    78 		}
    79 
    80 		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
    81 		while (pBase != NULL)
    82 		{
    83 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
    84 			{
    85 				g_plugin_unload = (ConCommand *)pBase;
    86 				break;
    87 			}
    88 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
    89 		}
    90 
    91 		if (g_plugin_unload != NULL)
    92 		{
    93 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
    94 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
    95 		}
    96 
    97 		g_bIsBridgedAsVsp = true;
    98 		g_pRealVspCallbacks = info->vsp_callbacks;
    99 		g_vsp_version = info->vsp_version;
   100 
   101 		g_PluginMngr.SetVSPAsLoaded();
   102 
   103 		return true;
   104 	}
   105 
   106 	virtual void Unload()
   107 	{
   108 		if (g_bIsTryingToUnload)
   109 		{
   110 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
   111 			return;
   112 		}
   113 		if (g_plugin_unload != NULL)
   114 		{
   115 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   116 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   117 			g_plugin_unload = NULL;
   118 		}
   119 		if (!g_GameDll.loaded)
   120 		{
   121 			g_SMConVarAccessor.UnloadMetamodCommands();
   122 			UnloadMetamod(false);
   123 		}
   124 	}
   125 
   126 	virtual const char *GetDescription()
   127 	{
   128 		return vsp_desc.c_str();
   129 	}
   130 };
   131 
   132 VspBridge mm14_vsp_bridge;
   133 
   134 SMM_API IVspBridge *
   135 GetVspBridge()
   136 {
   137 	return &mm14_vsp_bridge;
   138 }