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