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