core/vsp_bridge.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 23 23:22:33 2008 -0600 (2008-11-23)
changeset 589 8e97a1e960a5
parent 576 fc3600fed257
child 592 a38d1ebe28d6
permissions -rw-r--r--
Added gamedll bridging to ep2/l4d provider (bug 3412).
     1 #include "metamod.h"
     2 #include "metamod_util.h"
     3 #include <interface.h>
     4 #include <eiface.h>
     5 #include <iplayerinfo.h>
     6 #include <assert.h>
     7 #include <loader_bridge.h>
     8 #include "provider/provider_ep2.h"
     9 
    10 SH_DECL_HOOK1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    11 
    12 ConCommand *g_plugin_unload = NULL;
    13 bool g_bIsTryingToUnload;
    14 
    15 void InterceptPluginUnloads(const CCommand &args)
    16 {
    17 	g_bIsTryingToUnload = true;
    18 }
    19 
    20 void InterceptPluginUnloads_Post(const CCommand &args)
    21 {
    22 	g_bIsTryingToUnload = false;
    23 }
    24 
    25 class VspBridge : public IVspBridge
    26 {
    27 public:
    28 	virtual bool Load(const vsp_bridge_info *info, char *error, size_t maxlength)
    29 	{
    30 		assert(!g_Metamod.IsLoadedAsGameDLL());
    31 
    32 		CGlobalVars *pGlobals;
    33 		IPlayerInfoManager *playerInfoManager;
    34 
    35 		playerInfoManager = (IPlayerInfoManager *)info->gsFactory("PlayerInfoManager002", NULL);
    36 		if (playerInfoManager == NULL)
    37 		{
    38 			UTIL_Format(error, maxlength, "Metamod:Source requires gameinfo.txt modification to load on this game");
    39 			return false;
    40 		}
    41 
    42 		pGlobals = playerInfoManager->GetGlobalVars();
    43 
    44 		char gamedll_iface[] = "ServerGameDLL000";
    45 		for (unsigned int i = 5; i <= 50; i++)
    46 		{
    47 			gamedll_iface[15] = '0' + i;
    48 			if ((server = (IServerGameDLL *)info->gsFactory(gamedll_iface, NULL)) != NULL)
    49 			{
    50 				g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory, i, false);
    51 				break;
    52 			}
    53 		}
    54 
    55 		if (server == NULL)
    56 		{
    57 			UTIL_Format(error, maxlength, "Metamod:Source could not load (GameDLL version not compatible).");
    58 			return false;
    59 		}
    60 
    61 		char gameclients_iface[] = "ServerGameClients000";
    62 		for (unsigned int i = 3; i <= 4; i++)
    63 		{
    64 			gameclients_iface[19] = '0' + i;
    65 			if ((gameclients = (IServerGameClients *)info->gsFactory(gameclients_iface, NULL)) == NULL)
    66 				break;
    67 		}
    68 
    69 		if (!mm_DetectGameInformation())
    70 		{
    71 			UTIL_Format(error, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
    72 			return false;
    73 		}
    74 
    75 		mm_InitializeForLoad();
    76 		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
    77 							 (CreateInterfaceFn)info->engineFactory,
    78 							 (CreateInterfaceFn)info->engineFactory,
    79 							 pGlobals);
    80 		mm_StartupMetamod(true);
    81 		
    82 		g_plugin_unload = icvar->FindCommand("plugin_unload");
    83 
    84 		if (g_plugin_unload != NULL)
    85 		{
    86 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
    87 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
    88 		}
    89 
    90 		return true;
    91 	}
    92 
    93 	virtual void Unload()
    94 	{
    95 		if (g_bIsTryingToUnload)
    96 		{
    97 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
    98 			return;
    99 		}
   100 		if (g_plugin_unload != NULL)
   101 		{
   102 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   103 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   104 			g_plugin_unload = NULL;
   105 		}
   106 		mm_UnloadMetamod();
   107 	}
   108 
   109 	virtual const char *GetDescription()
   110 	{
   111 		return "Metamod:Source " SVN_FULL_VERSION;
   112 	}
   113 };
   114 
   115 VspBridge mm16_vsp_bridge;
   116 
   117 SMM_API IVspBridge *
   118 GetVspBridge()
   119 {
   120 	return &mm16_vsp_bridge;
   121 }
   122