core/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 683 b507295429f9
child 833 9a8182b48222
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     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 "provider/provider_ep2.h"
    34 
    35 #if SOURCE_ENGINE >= SE_ORANGEBOX
    36 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    37 #else
    38 SH_DECL_HOOK0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    39 #endif
    40 
    41 ConCommand *g_plugin_unload = NULL;
    42 bool g_bIsTryingToUnload;
    43 const char *vsp_desc = "Metamod:Source " MMS_FULL_VERSION;
    44 
    45 #if SOURCE_ENGINE >= SE_ORANGEBOX
    46 void InterceptPluginUnloads(const CCommand &args)
    47 #else
    48 void InterceptPluginUnloads()
    49 #endif
    50 {
    51 	g_bIsTryingToUnload = true;
    52 }
    53 
    54 #if SOURCE_ENGINE >= SE_ORANGEBOX
    55 void InterceptPluginUnloads_Post(const CCommand &args)
    56 #else
    57 void InterceptPluginUnloads_Post()
    58 #endif
    59 {
    60 	g_bIsTryingToUnload = false;
    61 }
    62 
    63 class VspBridge : public IVspBridge
    64 {
    65 public:
    66 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    67 	{
    68 		if (!g_Metamod.IsLoadedAsGameDLL())
    69 		{
    70 			CGlobalVars *pGlobals;
    71 			IPlayerInfoManager *playerInfoManager;
    72 
    73 			playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
    74 			if (playerInfoManager == NULL)
    75 			{
    76 				UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
    77 				return false;
    78 			}
    79 
    80 			pGlobals = playerInfoManager->GetGlobalVars();
    81 
    82 			char gamedll_iface[] = "ServerGameDLL000";
    83 			for (unsigned int i = 3; i <= 50; i++)
    84 			{
    85 				gamedll_iface[15] = '0' + i;
    86 				if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
    87 				{
    88 					g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
    89 					break;
    90 				}
    91 			}
    92 
    93 			if (server == NULL)
    94 			{
    95 				UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
    96 				return false;
    97 			}
    98 
    99 			char gameclients_iface[] = "ServerGameClients000";
   100 			for (unsigned int i = 3; i <= 4; i++)
   101 			{
   102 				gameclients_iface[19] = '0' + i;
   103 				if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
   104 					break;
   105 			}
   106 
   107 			if (!mm_DetectGameInformation())
   108 			{
   109 				UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
   110 				return false;
   111 			}
   112 
   113 			mm_InitializeForLoad();
   114 			mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
   115 				(CreateInterfaceFn)info->engineFactory,
   116 				(CreateInterfaceFn)info->engineFactory,
   117 				pGlobals);
   118 			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   119 			mm_StartupMetamod(true);
   120 		}
   121 		else
   122 		{
   123 			vsp_desc = "Metamod:Source Interface " MMS_FULL_VERSION;
   124 			g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
   125 		}
   126 
   127 #if SOURCE_ENGINE >= SE_ORANGEBOX
   128 		g_plugin_unload = icvar->FindCommand("plugin_unload");
   129 #else
   130 		const ConCommandBase *pBase = icvar->GetCommands();
   131 		while (pBase != NULL)
   132 		{
   133 			if (pBase->IsCommand() && strcmp(pBase->GetName(), "plugin_unload") == 0)
   134 			{
   135 				g_plugin_unload = (ConCommand *)pBase;
   136 				break;
   137 			}
   138 			pBase = pBase->GetNext();
   139 		}
   140 #endif
   141 
   142 		if (g_plugin_unload != NULL)
   143 		{
   144 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   145 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   146 		}
   147 
   148 		return true;
   149 	}
   150 
   151 	virtual void Unload()
   152 	{
   153 		if (g_bIsTryingToUnload)
   154 		{
   155 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
   156 			return;
   157 		}
   158 		if (g_plugin_unload != NULL)
   159 		{
   160 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   161 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   162 			g_plugin_unload = NULL;
   163 		}
   164 		if (!g_Metamod.IsLoadedAsGameDLL())
   165 		{
   166 			mm_UnloadMetamod();
   167 		}
   168 	}
   169 
   170 	virtual const char *GetDescription()
   171 	{
   172 		return vsp_desc;
   173 	}
   174 };
   175 
   176 VspBridge mm16_vsp_bridge;
   177 
   178 SMM_API IVspBridge *
   179 GetVspBridge()
   180 {
   181 	return &mm16_vsp_bridge;
   182 }
   183