core/vsp_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 887 e30d4a37f5e5
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     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 "metamod.h"
    27 #include "metamod_util.h"
    28 #include <interface.h>
    29 #include <eiface.h>
    30 #include <iplayerinfo.h>
    31 #include <assert.h>
    32 #include <loader_bridge.h>
    33 #include <versionlib.h>
    34 #include <sh_string.h>
    35 #include "provider/provider_ep2.h"
    36 
    37 #if SOURCE_ENGINE == SE_DOTA
    38 SH_DECL_HOOK2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
    39 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    40 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    41 #else
    42 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    43 #endif
    44 
    45 ConCommand *g_plugin_unload = NULL;
    46 bool g_bIsTryingToUnload;
    47 SourceHook::String vsp_desc("Metamod:Source");
    48 
    49 #if SOURCE_ENGINE == SE_DOTA
    50 void InterceptPluginUnloads(const CCommandContext &context, const CCommand &args)
    51 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    52 void InterceptPluginUnloads(const CCommand &args)
    53 #else
    54 void InterceptPluginUnloads()
    55 #endif
    56 {
    57 	g_bIsTryingToUnload = true;
    58 }
    59 
    60 #if SOURCE_ENGINE == SE_DOTA
    61 void InterceptPluginUnloads_Post(const CCommandContext &context, const CCommand &args)
    62 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    63 void InterceptPluginUnloads_Post(const CCommand &args)
    64 #else
    65 void InterceptPluginUnloads_Post()
    66 #endif
    67 {
    68 	g_bIsTryingToUnload = false;
    69 }
    70 
    71 class VspBridge : public IVspBridge
    72 {
    73 public:
    74 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    75 	{
    76 		if (!g_Metamod.IsLoadedAsGameDLL())
    77 		{
    78 			vsp_desc.append(" ");
    79 			vsp_desc.append(METAMOD_VERSION);
    80 
    81 			CGlobalVars *pGlobals;
    82 			IPlayerInfoManager *playerInfoManager;
    83 
    84 			playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
    85 			if (playerInfoManager == NULL)
    86 			{
    87 				UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
    88 				return false;
    89 			}
    90 
    91 			pGlobals = playerInfoManager->GetGlobalVars();
    92 
    93 			char gamedll_iface[] = "ServerGameDLL000";
    94 			for (unsigned int i = 3; i <= 50; i++)
    95 			{
    96 				gamedll_iface[15] = '0' + i;
    97 				if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
    98 				{
    99 					g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
   100 					break;
   101 				}
   102 			}
   103 
   104 			if (server == NULL)
   105 			{
   106 				UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
   107 				return false;
   108 			}
   109 
   110 			char gameclients_iface[] = "ServerGameClients000";
   111 			for (unsigned int i = 3; i <= 4; i++)
   112 			{
   113 				gameclients_iface[19] = '0' + i;
   114 				if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
   115 					break;
   116 			}
   117 
   118 			if (!mm_DetectGameInformation())
   119 			{
   120 				UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
   121 				return false;
   122 			}
   123 
   124 			mm_InitializeForLoad();
   125 			mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
   126 				(CreateInterfaceFn)info->engineFactory,
   127 				(CreateInterfaceFn)info->engineFactory,
   128 				pGlobals);
   129 			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   130 			mm_StartupMetamod(true);
   131 		}
   132 		else
   133 		{
   134 			vsp_desc.append(" Interface ");
   135 			vsp_desc.append(METAMOD_VERSION);
   136 
   137 			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   138 		}
   139 
   140 #if SOURCE_ENGINE >= SE_ORANGEBOX
   141 		g_plugin_unload = icvar->FindCommand("plugin_unload");
   142 #else
   143 		const ConCommandBase *pBase = icvar->GetCommands();
   144 		while (pBase != NULL)
   145 		{
   146 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
   147 			{
   148 				g_plugin_unload = (ConCommand *)pBase;
   149 				break;
   150 			}
   151 			pBase = pBase->GetNext();
   152 		}
   153 #endif
   154 
   155 		if (g_plugin_unload != NULL)
   156 		{
   157 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   158 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   159 		}
   160 
   161 		return true;
   162 	}
   163 
   164 	virtual void Unload()
   165 	{
   166 		if (g_bIsTryingToUnload)
   167 		{
   168 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
   169 			return;
   170 		}
   171 		if (g_plugin_unload != NULL)
   172 		{
   173 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   174 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   175 			g_plugin_unload = NULL;
   176 		}
   177 		if (!g_Metamod.IsLoadedAsGameDLL())
   178 		{
   179 			mm_UnloadMetamod();
   180 		}
   181 	}
   182 
   183 	virtual const char *GetDescription()
   184 	{
   185 		return vsp_desc.c_str();
   186 	}
   187 };
   188 
   189 VspBridge mm16_vsp_bridge;
   190 
   191 SMM_API IVspBridge *
   192 GetVspBridge()
   193 {
   194 	return &mm16_vsp_bridge;
   195 }
   196