core-legacy/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:26:19 2009 -0500 (2009-02-25)
changeset 649 a0e24af92af0
parent 608 30b546cc4e03
child 663 e07fd1dc5290
permissions -rw-r--r--
Bumped all versions.
     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 <loader_bridge.h>
    32 
    33 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    34 
    35 ConCommand *g_plugin_unload = NULL;
    36 bool g_bIsTryingToUnload;
    37 bool g_bIsBridgedAsVsp;
    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_GameDll.loaded && !g_bIsBridgedAsVsp);
    54 
    55 		CreateInterfaceFn engineFactory = (CreateInterfaceFn)info->engineFactory;
    56 		CreateInterfaceFn gsFactory = (CreateInterfaceFn)info->gsFactory;
    57 
    58 		if (!AlternatelyLoadMetamod(engineFactory, gsFactory))
    59 			return false;
    60 
    61 		ConCommandBase *pBase = g_Engine.icvar->GetCommands();
    62 		while (pBase != NULL)
    63 		{
    64 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
    65 			{
    66 				g_plugin_unload = (ConCommand *)pBase;
    67 				break;
    68 			}
    69 			pBase = const_cast<ConCommandBase *>(pBase->GetNext());
    70 		}
    71 
    72 		if (g_plugin_unload != NULL)
    73 		{
    74 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
    75 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
    76 		}
    77 
    78 		extern ConVar metamod_version;
    79 		char buffer[255];
    80 
    81 		UTIL_Format(buffer, sizeof(buffer), "%sV", metamod_version.GetString());
    82 		metamod_version.SetValue(buffer);
    83 
    84 		g_bIsBridgedAsVsp = true;
    85 		g_pRealVspCallbacks = info->vsp_callbacks;
    86 
    87 		g_PluginMngr.SetVSPAsLoaded();
    88 
    89 		return true;
    90 	}
    91 
    92 	virtual void Unload()
    93 	{
    94 		if (g_bIsTryingToUnload)
    95 		{
    96 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
    97 			return;
    98 		}
    99 		if (g_plugin_unload != NULL)
   100 		{
   101 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   102 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   103 			g_plugin_unload = NULL;
   104 		}
   105 		g_SMConVarAccessor.UnloadMetamodCommands();
   106 		UnloadMetamod(false);
   107 	}
   108 
   109 	virtual const char *GetDescription()
   110 	{
   111 		return "Metamod:Source " MMS_FULL_VERSION;
   112 	}
   113 };
   114 
   115 VspBridge mm14_vsp_bridge;
   116 
   117 SMM_API IVspBridge *
   118 GetVspBridge()
   119 {
   120 	return &mm14_vsp_bridge;
   121 }