core/vsp_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Nov 24 07:25:46 2008 -0600 (2008-11-24)
changeset 601 bfbed17a3375
parent 599 5b906351da4d
child 608 30b546cc4e03
permissions -rw-r--r--
The 'meta refresh' command now handles plugins loaded via VDF files.
     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 		g_Metamod.NotifyVSPListening(info->vsp_callbacks, info->vsp_version);
    81 		mm_StartupMetamod(true);
    82 		
    83 		g_plugin_unload = icvar->FindCommand("plugin_unload");
    84 
    85 		if (g_plugin_unload != NULL)
    86 		{
    87 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
    88 			SH_ADD_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
    89 		}
    90 
    91 		return true;
    92 	}
    93 
    94 	virtual void Unload()
    95 	{
    96 		if (g_bIsTryingToUnload)
    97 		{
    98 			Error("Metamod:Source cannot be unloaded from VSP mode.  Use \"meta unload\" to unload specific plugins.\n");
    99 			return;
   100 		}
   101 		if (g_plugin_unload != NULL)
   102 		{
   103 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads, false);
   104 			SH_REMOVE_HOOK_STATICFUNC(ConCommand, Dispatch, g_plugin_unload, InterceptPluginUnloads_Post, true);
   105 			g_plugin_unload = NULL;
   106 		}
   107 		mm_UnloadMetamod();
   108 	}
   109 
   110 	virtual const char *GetDescription()
   111 	{
   112 		return "Metamod:Source " MMS_FULL_VERSION;
   113 	}
   114 };
   115 
   116 VspBridge mm16_vsp_bridge;
   117 
   118 SMM_API IVspBridge *
   119 GetVspBridge()
   120 {
   121 	return &mm16_vsp_bridge;
   122 }
   123