core-legacy/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 29 00:52:39 2009 -0700 (2009-10-29)
changeset 666 acab40e6dbd7
parent 663 e07fd1dc5290
child 683 b507295429f9
permissions -rw-r--r--
Backported GetVSPInfo from new API to legacy API (bug 4079, r=ds).
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 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  * Version: $Id$
    26  */
    27 
    28 #include <assert.h>
    29 #include "sourcemm.h"
    30 #include "concommands.h"
    31 #include "util.h"
    32 #include <loader_bridge.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 
    40 void InterceptPluginUnloads()
    41 {
    42 	g_bIsTryingToUnload = true;
    43 }
    44 
    45 void InterceptPluginUnloads_Post()
    46 {
    47 	g_bIsTryingToUnload = false;
    48 }
    49 
    50 class VspBridge : public IVspBridge
    51 {
    52 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    53 	{
    54 		assert(!g_GameDll.loaded && !g_bIsBridgedAsVsp);
    55 
    56 		CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
    57 		CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
    58 
    59 		if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
    60 			return false;
    61 
    62 		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
    63 		while (pBase != NULL)
    64 		{
    65 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
    66 			{
    67 				g_plugin_unload = (ConCommand *)pBase;
    68 				break;
    69 			}
    70 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
    71 		}
    72 
    73 		if (g_plugin_unload != NULL)
    74 		{
    75 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
    76 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
    77 		}
    78 
    79 		extern ConVar metamod_version;
    80 		char buffer[255];
    81 
    82 		UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
    83 		metamod_version.SetValue(buffer);
    84 
    85 		g_bIsBridgedAsVsp = true;
    86 		g_pRealVspCallbacks = info->vsp_callbacks;
    87 		g_vsp_version = info->vsp_version;
    88 
    89 		g_PluginMngr.SetVSPAsLoaded();
    90 
    91 		return true;
    92 	}
    93 
    94 	virtual void Unload()
    95 	{
    96 		if (g_bIsTryingToUnload)
    97 		{
    98 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
    99 			return;
   100 		}
   101 		if (g_plugin_unload != NULL)
   102 		{
   103 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   104 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   105 			g_plugin_unload = NULL;
   106 		}
   107 		g_SMConVarAccessor.UnloadMetamodCommands();
   108 		UnloadMetamod(false);
   109 	}
   110 
   111 	virtual const char *GetDescription()
   112 	{
   113 		return "Metamod:Source " MMS_FULL_VERSION;
   114 	}
   115 };
   116 
   117 VspBridge mm14_vsp_bridge;
   118 
   119 SMM_API IVspBridge *
   120 GetVspBridge()
   121 {
   122 	return &mm14_vsp_bridge;
   123 }