loader/serverplugin.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed Oct 09 09:00:23 2013 -0400 (2013-10-09)
changeset 858 a534b75f1b59
parent 857 d8cc9c7087b0
child 867 091cd2fb1a41
permissions -rw-r--r--
Add support for Insurgency (bug 5950, r=asherkin).
     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 <stdio.h>
    27 #include <string.h>
    28 #include <assert.h>
    29 #include <stddef.h>
    30 #include "loader.h"
    31 #include <sh_memfuncinfo.h>
    32 #include <sh_memory.h>
    33 #include "serverplugin.h"
    34 #include "gamedll.h"
    35 
    36 typedef enum
    37 {
    38 	PLUGIN_CONTINUE = 0,
    39 	PLUGIN_OVERRIDE,
    40 	PLUGIN_STOP,
    41 } PLUGIN_RESULT;
    42 
    43 typedef enum
    44 {
    45     eQueryCvarValueStatus_ValueIntact=0,
    46     eQueryCvarValueStatus_CvarNotFound=1,
    47     eQueryCvarValueStatus_NotACvar=2,
    48     eQueryCvarValueStatus_CvarProtected=3
    49 } EQueryCvarValueStatus;
    50 
    51 typedef int QueryCvarCookie_t;
    52 class CCommand;
    53 class IServerPluginCallbacks;
    54 struct edict_t;
    55 
    56 class IRandomThings
    57 {
    58 public:
    59 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
    60 	{
    61 		return PLUGIN_CONTINUE;
    62 	}
    63 };
    64 
    65 IVspBridge *vsp_bridge = NULL;
    66 
    67 /**
    68  * The vtable must match the general layout for ISPC.  We modify the vtable
    69  * based on what we get back.
    70  */
    71 class ServerPlugin
    72 {
    73 	char game_name[128];
    74 	unsigned int vsp_version;
    75 	bool load_allowed;
    76 public:
    77 	ServerPlugin()
    78 	{
    79 		load_allowed = false;
    80 	}
    81 	virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
    82 	{
    83 		if (!load_allowed)
    84 			return false;
    85 
    86 		load_allowed = false;
    87 
    88 		/* Backend should already filled in if loaded as gamedll */
    89 		if (gamedll_bridge == NULL)
    90 		{
    91 			if (!mm_GetGameName(game_name, sizeof(game_name)))
    92 			{
    93 				return false;
    94 			}
    95 
    96 			mm_backend = mm_DetermineBackend(engineFactory, game_name);
    97 		}
    98 
    99 		if (mm_backend == MMBackend_UNKNOWN)
   100 		{
   101 			mm_LogFatal("Could not detect engine version");
   102 			return false;
   103 		}
   104 		
   105 		void **this_vtable;
   106 		this_vtable = (void **)*(void **)this;
   107 
   108 		if (mm_backend != MMBackend_Episode1
   109 			&& mm_backend != MMBackend_DarkMessiah)
   110 		{
   111 			/* We need to insert the right type of call into this vtable */
   112 			void **vtable_src;
   113 			IRandomThings sample;
   114 			SourceHook::MemFuncInfo mfp_dest, mfp_src;
   115 
   116 			mfp_dest.isVirtual = false;
   117 			mfp_src.isVirtual = false;
   118 
   119 			SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
   120 			SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
   121 
   122 			assert(mfp_dest.isVirtual);
   123 			assert(mfp_dest.thisptroffs == 0);
   124 			assert(mfp_dest.vtbloffs == 0);
   125 			assert(mfp_src.isVirtual);
   126 			assert(mfp_src.thisptroffs == 0);
   127 			assert(mfp_src.vtbloffs == 0);
   128 
   129 			vtable_src = (void **)*(void **)&sample;
   130 			SourceHook::SetMemAccess(&this_vtable[mfp_dest.vtblindex],
   131 									 sizeof(void*),
   132 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   133 			this_vtable[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
   134 		}
   135 
   136 		/* AS inserted ClientFullyConnect into vtable, so move entries up on older engines */
   137 		if (mm_backend != MMBackend_AlienSwarm
   138 			&& mm_backend != MMBackend_Portal2
   139 			&& mm_backend != MMBackend_Blade
   140 			&& mm_backend != MMBackend_Insurgency
   141 			&& mm_backend != MMBackend_CSGO
   142 			&& mm_backend != MMBackend_DOTA)
   143 		{
   144 			SourceHook::MemFuncInfo mfp_fconnect;
   145 			mfp_fconnect.isVirtual = false;
   146 
   147 			SourceHook::GetFuncInfo(&ServerPlugin::ClientFullyConnect, mfp_fconnect);
   148 
   149 			assert(mfp_fconnect.isVirtual);
   150 			assert(mfp_fconnect.thisptroffs == 0);
   151 			assert(mfp_fconnect.vtbloffs == 0);
   152 
   153 			/* Shifting ClientDisconnect through OnQueryCvarValueFinished up into slot for
   154 			 * ClientFullyConnect (8 entries)
   155 			 */
   156 			SourceHook::SetMemAccess(&this_vtable[mfp_fconnect.vtblindex],
   157 									 sizeof(void *) * 8,
   158 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   159 			memmove(&this_vtable[mfp_fconnect.vtblindex],
   160 					&this_vtable[mfp_fconnect.vtblindex + 1],
   161 					sizeof(void *) * 8);
   162 		}
   163 
   164 		char error[255];
   165 		if (gamedll_bridge == NULL)
   166 		{
   167 			if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
   168 			{
   169 				mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
   170 				return false;
   171 			}
   172 		}
   173 
   174 		typedef IVspBridge *(*GetVspBridge)();
   175 		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
   176 		if (get_bridge == NULL)
   177 		{
   178 			if (gamedll_bridge == NULL)
   179 			{
   180 				mm_UnloadMetamodLibrary();
   181 			}
   182 			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", mm_backend);
   183 			return false;
   184 		}
   185 
   186 		vsp_bridge = get_bridge();
   187 
   188 		vsp_bridge_info info;
   189 
   190 		info.engineFactory = engineFactory;
   191 		info.gsFactory = gsFactory;
   192 		info.vsp_callbacks = (IServerPluginCallbacks*)this;
   193 		info.vsp_version = vsp_version;
   194 
   195 		strcpy(error, "Unknown error");
   196 		if (!vsp_bridge->Load(&info, error, sizeof(error)))
   197 		{
   198 			vsp_bridge = NULL;
   199 			if (gamedll_bridge == NULL)
   200 			{
   201 				mm_UnloadMetamodLibrary();
   202 			}
   203 			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);	
   204 			return false;
   205 		}
   206 
   207 		return true;
   208 	}
   209 	virtual void Unload()
   210 	{
   211 		if (vsp_bridge == NULL)
   212 			return;
   213 		vsp_bridge->Unload();
   214 
   215 		if (gamedll_bridge == NULL)
   216 		{
   217 			mm_UnloadMetamodLibrary();
   218 		}
   219 	}
   220 	virtual void Pause()
   221 	{
   222 	}
   223 	virtual void UnPause()
   224 	{
   225 	}
   226 	virtual const char *GetPluginDescription()
   227 	{
   228 		if (vsp_bridge == NULL)
   229 			return "Metamod:Source Loader Shim";
   230 		return vsp_bridge->GetDescription();
   231 	}
   232 	virtual void LevelInit(char const *pMapName)
   233 	{
   234 	}
   235 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   236 	{
   237 	}
   238 	virtual void GameFrame(bool simulating)
   239 	{
   240 	}
   241 	virtual void LevelShutdown()
   242 	{
   243 	}
   244 	virtual void ClientActive(edict_t *pEntity)
   245 	{
   246 	}
   247 	virtual void ClientFullyConnect(edict_t *pEntity)
   248 	{
   249 	}
   250 	virtual void ClientDisconnect(edict_t *pEntity)
   251 	{
   252 	}
   253 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   254 	{
   255 	}
   256 	virtual void SetCommandClient(int index)
   257 	{
   258 	}
   259 	virtual void ClientSettingsChanged(edict_t *pEdict)
   260 	{
   261 	}
   262 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   263 										edict_t *pEntity,
   264 										const char *pszName,
   265 										const char *pszAddress,
   266 										char *reject,
   267 										int maxrejectlen) 
   268 	{
   269 		return PLUGIN_CONTINUE;
   270 	}
   271 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   272 	{
   273 		return PLUGIN_CONTINUE;
   274 	}
   275 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   276 	{
   277 		return PLUGIN_CONTINUE;
   278 	}
   279 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   280 										  edict_t *pPlayerEntity,
   281 										  EQueryCvarValueStatus eStatus,
   282 										  const char *pCvarName,
   283 										  const char *pCvarValue)
   284 	{
   285 	}
   286 	void PrepForLoad(unsigned int version)
   287 	{
   288 		vsp_version = version;
   289 		load_allowed = true;
   290 	}
   291 };
   292 
   293 ServerPlugin mm_vsp_callbacks;
   294 
   295 void *mm_GetVspCallbacks(unsigned int version)
   296 {
   297 	if (vsp_bridge != NULL)
   298 		return NULL;
   299 
   300 	/* Only support versions 1 or 2 right now */
   301 	if (version > 2)
   302 		return NULL;
   303 
   304 	mm_vsp_callbacks.PrepForLoad(version);
   305 
   306 	return &mm_vsp_callbacks;
   307 }
   308