core/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Feb 27 00:19:50 2009 -0600 (2009-02-27)
changeset 657 bee78b10b2c0
parent 637 b66489d8aedc
child 683 b507295429f9
permissions -rw-r--r--
Merge from 1.7 branch.
     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 #if SOURCE_ENGINE >= SE_ORANGEBOX
    38 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    39 #else
    40 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    41 #endif
    42 
    43 ConCommand *g_plugin_unload = NULL;
    44 bool g_bIsTryingToUnload;
    45 
    46 #if SOURCE_ENGINE >= SE_ORANGEBOX
    47 void InterceptPluginUnloads(const CCommand &args)
    48 #else
    49 void InterceptPluginUnloads()
    50 #endif
    51 {
    52 	g_bIsTryingToUnload = true;
    53 }
    54 
    55 #if SOURCE_ENGINE >= SE_ORANGEBOX
    56 void InterceptPluginUnloads_Post(const CCommand &args)
    57 #else
    58 void InterceptPluginUnloads_Post()
    59 #endif
    60 {
    61 	g_bIsTryingToUnload = false;
    62 }
    63 
    64 class VspBridge : public IVspBridge
    65 {
    66 public:
    67 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    68 	{
    69 		assert(!g_Metamod.IsLoadedAsGameDLL());
    70 
    71 		CGlobalVars *pGlobals;
    72 		IPlayerInfoManager *playerInfoManager;
    73 
    74 		playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
    75 		if (playerInfoManager == NULL)
    76 		{
    77 			UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
    78 			return false;
    79 		}
    80 
    81 		pGlobals = playerInfoManager->GetGlobalVars();
    82 
    83 		char gamedll_iface[] = "ServerGameDLL000";
    84 		for (unsigned int i = 3; i <= 50; i++)
    85 		{
    86 			gamedll_iface[15] = '0' + i;
    87 			if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
    88 			{
    89 				g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
    90 				break;
    91 			}
    92 		}
    93 
    94 		if (server == NULL)
    95 		{
    96 			UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
    97 			return false;
    98 		}
    99 
   100 		char gameclients_iface[] = "ServerGameClients000";
   101 		for (unsigned int i = 3; i <= 4; i++)
   102 		{
   103 			gameclients_iface[19] = '0' + i;
   104 			if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
   105 				break;
   106 		}
   107 
   108 		if (!mm_DetectGameInformation())
   109 		{
   110 			UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
   111 			return false;
   112 		}
   113 
   114 		mm_InitializeForLoad();
   115 		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
   116 							 (CreateInterfaceFn)info->engineFactory,
   117 							 (CreateInterfaceFn)info->engineFactory,
   118 							 pGlobals);
   119 		g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   120 		mm_StartupMetamod(true);
   121 		
   122 #if SOURCE_ENGINE >= SE_ORANGEBOX
   123 		g_plugin_unload = icvar->FindCommand("plugin_unload");
   124 #else
   125 		const ConCommandBase *pBase = icvar->GetCommands();
   126 		while (pBase != NULL)
   127 		{
   128 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
   129 			{
   130 				g_plugin_unload = (ConCommand *)pBase;
   131 				break;
   132 			}
   133 			pBase = pBase->GetNext();
   134 		}
   135 #endif
   136 
   137 		if (g_plugin_unload != NULL)
   138 		{
   139 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   140 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   141 		}
   142 
   143 		return true;
   144 	}
   145 
   146 	virtual void Unload()
   147 	{
   148 		if (g_bIsTryingToUnload)
   149 		{
   150 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
   151 			return;
   152 		}
   153 		if (g_plugin_unload != NULL)
   154 		{
   155 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   156 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   157 			g_plugin_unload = NULL;
   158 		}
   159 		mm_UnloadMetamod();
   160 	}
   161 
   162 	virtual const char *GetDescription()
   163 	{
   164 		return "Metamod:Source " MMS_FULL_VERSION;
   165 	}
   166 };
   167 
   168 VspBridge mm16_vsp_bridge;
   169 
   170 SMM_API IVspBridge *
   171 GetVspBridge()
   172 {
   173 	return &mm16_vsp_bridge;
   174 }
   175