core/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Mon Feb 16 15:52:46 2009 -0500 (2009-02-16)
changeset 633 153e2d943cc3
parent 608 30b546cc4e03
child 637 b66489d8aedc
permissions -rw-r--r--
Fixed mprotect() being called with invalid length values (bug 3642, 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 "metamod.h"
    29 #include "metamod_util.h"
    30 #include <interface.h>
    31 #include <eiface.h>
    32 #include <iplayerinfo.h>
    33 #include <assert.h>
    34 #include <loader_bridge.h>
    35 #include "provider/provider_ep2.h"
    36 
    37 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    38 
    39 ConCommand *g_plugin_unload = NULL;
    40 bool g_bIsTryingToUnload;
    41 
    42 void InterceptPluginUnloads(const CCommand &args)
    43 {
    44 	g_bIsTryingToUnload = true;
    45 }
    46 
    47 void InterceptPluginUnloads_Post(const CCommand &args)
    48 {
    49 	g_bIsTryingToUnload = false;
    50 }
    51 
    52 class VspBridge : public IVspBridge
    53 {
    54 public:
    55 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    56 	{
    57 		assert(!g_Metamod.IsLoadedAsGameDLL());
    58 
    59 		CGlobalVars *pGlobals;
    60 		IPlayerInfoManager *playerInfoManager;
    61 
    62 		playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
    63 		if (playerInfoManager == NULL)
    64 		{
    65 			UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
    66 			return false;
    67 		}
    68 
    69 		pGlobals = playerInfoManager->GetGlobalVars();
    70 
    71 		char gamedll_iface[] = "ServerGameDLL000";
    72 		for (unsigned int i = 5; i <= 50; i++)
    73 		{
    74 			gamedll_iface[15] = '0' + i;
    75 			if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
    76 			{
    77 				g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
    78 				break;
    79 			}
    80 		}
    81 
    82 		if (server == NULL)
    83 		{
    84 			UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
    85 			return false;
    86 		}
    87 
    88 		char gameclients_iface[] = "ServerGameClients000";
    89 		for (unsigned int i = 3; i <= 4; i++)
    90 		{
    91 			gameclients_iface[19] = '0' + i;
    92 			if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
    93 				break;
    94 		}
    95 
    96 		if (!mm_DetectGameInformation())
    97 		{
    98 			UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
    99 			return false;
   100 		}
   101 
   102 		mm_InitializeForLoad();
   103 		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
   104 							 (CreateInterfaceFn)info->engineFactory,
   105 							 (CreateInterfaceFn)info->engineFactory,
   106 							 pGlobals);
   107 		g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   108 		mm_StartupMetamod(true);
   109 		
   110 		g_plugin_unload = icvar->FindCommand("plugin_unload");
   111 
   112 		if (g_plugin_unload != NULL)
   113 		{
   114 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   115 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   116 		}
   117 
   118 		return true;
   119 	}
   120 
   121 	virtual void Unload()
   122 	{
   123 		if (g_bIsTryingToUnload)
   124 		{
   125 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
   126 			return;
   127 		}
   128 		if (g_plugin_unload != NULL)
   129 		{
   130 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   131 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   132 			g_plugin_unload = NULL;
   133 		}
   134 		mm_UnloadMetamod();
   135 	}
   136 
   137 	virtual const char *GetDescription()
   138 	{
   139 		return "Metamod:Source " MMS_FULL_VERSION;
   140 	}
   141 };
   142 
   143 VspBridge mm16_vsp_bridge;
   144 
   145 SMM_API IVspBridge *
   146 GetVspBridge()
   147 {
   148 	return &mm16_vsp_bridge;
   149 }
   150