loader/serverplugin.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 22 17:42:01 2010 -0500 (2010-07-22)
changeset 728 6fd06d6c6e30
parent 687 cf809a183e16
child 729 ce8a0557380a
permissions -rw-r--r--
Fixed crash on Alien Swarm when client connected (bug 4529, r=dvander).
     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 	const char *game_name;
    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 ((game_name = mm_GetGameName()) == NULL)
    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 		else if (mm_backend >= MMBackend_Episode2)
   105 		{
   106 			/* We need to insert the right type of call into this vtable */
   107 			void **vtable_src;
   108 			void **vtable_dest;
   109 			IRandomThings sample;
   110 			SourceHook::MemFuncInfo mfp_dest, mfp_src;
   111 
   112 			mfp_dest.isVirtual = false;
   113 			mfp_src.isVirtual = false;
   114 
   115 			SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
   116 			SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
   117 
   118 			assert(mfp_dest.isVirtual);
   119 			assert(mfp_dest.thisptroffs == 0);
   120 			assert(mfp_dest.vtbloffs == 0);
   121 			assert(mfp_src.isVirtual);
   122 			assert(mfp_src.thisptroffs == 0);
   123 			assert(mfp_src.vtbloffs == 0);
   124 
   125 			vtable_src = (void **)*(void **)&sample;
   126 			vtable_dest = (void **)*(void **)this;
   127 			SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
   128 									 sizeof(void*),
   129 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   130 			vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
   131 
   132 			/* AS engine inserted ClientFullyConnect into the vtable, so move entries up on older engines */
   133 			if (mm_backend != MMBackend_AlienSwarm)
   134 			{
   135 				SourceHook::MemFuncInfo mfp_fconnect;
   136 				mfp_fconnect.isVirtual = false;
   137 
   138 				SourceHook::GetFuncInfo(&ServerPlugin::ClientFullyConnect, mfp_fconnect);
   139 
   140 				assert(mfp_fconnect.isVirtual);
   141 				assert(mfp_fconnect.thisptroffs == 0);
   142 				assert(mfp_fconnect.vtbloffs == 0);
   143 
   144 				/* Shifting ClientDisconnect through OnQueryCvarValueFinished up into slot for ClientFullyConnect (8 entries) */
   145 				SourceHook::SetMemAccess(&vtable_dest[mfp_fconnect.vtblindex],
   146 										 sizeof(void *) * 8,
   147 										 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   148 				memmove(&vtable_dest[mfp_fconnect.vtblindex], &vtable_dest[mfp_fconnect.vtblindex + 1], sizeof(void *) * 8);
   149 			}
   150 		}
   151 
   152 		char error[255];
   153 		if (gamedll_bridge == NULL)
   154 		{
   155 			if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
   156 			{
   157 				mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
   158 				return false;
   159 			}
   160 		}
   161 
   162 		typedef IVspBridge *(*GetVspBridge)();
   163 		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
   164 		if (get_bridge == NULL)
   165 		{
   166 			if (gamedll_bridge == NULL)
   167 			{
   168 				mm_UnloadMetamodLibrary();
   169 			}
   170 			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", mm_backend);
   171 			return false;
   172 		}
   173 
   174 		vsp_bridge = get_bridge();
   175 
   176 		vsp_bridge_info info;
   177 
   178 		info.engineFactory = engineFactory;
   179 		info.gsFactory = gsFactory;
   180 		info.vsp_callbacks = (IServerPluginCallbacks*)this;
   181 		info.vsp_version = vsp_version;
   182 
   183 		strcpy(error, "Unknown error");
   184 		if (!vsp_bridge->Load(&info, error, sizeof(error)))
   185 		{
   186 			vsp_bridge = NULL;
   187 			if (gamedll_bridge == NULL)
   188 			{
   189 				mm_UnloadMetamodLibrary();
   190 			}
   191 			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);	
   192 			return false;
   193 		}
   194 
   195 		return true;
   196 	}
   197 	virtual void Unload()
   198 	{
   199 		if (vsp_bridge == NULL)
   200 			return;
   201 		vsp_bridge->Unload();
   202 
   203 		if (gamedll_bridge == NULL)
   204 		{
   205 			mm_UnloadMetamodLibrary();
   206 		}
   207 	}
   208 	virtual void Pause()
   209 	{
   210 	}
   211 	virtual void UnPause()
   212 	{
   213 	}
   214 	virtual const char *GetPluginDescription()
   215 	{
   216 		if (vsp_bridge == NULL)
   217 			return "Metamod:Source Loader Shim";
   218 		return vsp_bridge->GetDescription();
   219 	}
   220 	virtual void LevelInit(char const *pMapName)
   221 	{
   222 	}
   223 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   224 	{
   225 	}
   226 	virtual void GameFrame(bool simulating)
   227 	{
   228 	}
   229 	virtual void LevelShutdown()
   230 	{
   231 	}
   232 	virtual void ClientActive(edict_t *pEntity)
   233 	{
   234 	}
   235 	virtual void ClientFullyConnect(edict_t *pEntity)
   236 	{
   237 	}
   238 	virtual void ClientDisconnect(edict_t *pEntity)
   239 	{
   240 	}
   241 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   242 	{
   243 	}
   244 	virtual void SetCommandClient(int index)
   245 	{
   246 	}
   247 	virtual void ClientSettingsChanged(edict_t *pEdict)
   248 	{
   249 	}
   250 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   251 										edict_t *pEntity,
   252 										const char *pszName,
   253 										const char *pszAddress,
   254 										char *reject,
   255 										int maxrejectlen) 
   256 	{
   257 		return PLUGIN_CONTINUE;
   258 	}
   259 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   260 	{
   261 		return PLUGIN_CONTINUE;
   262 	}
   263 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   264 	{
   265 		return PLUGIN_CONTINUE;
   266 	}
   267 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   268 										  edict_t *pPlayerEntity,
   269 										  EQueryCvarValueStatus eStatus,
   270 										  const char *pCvarName,
   271 										  const char *pCvarValue)
   272 	{
   273 	}
   274 	void PrepForLoad(unsigned int version)
   275 	{
   276 		vsp_version = version;
   277 		load_allowed = true;
   278 	}
   279 };
   280 
   281 ServerPlugin mm_vsp_callbacks;
   282 
   283 void *mm_GetVspCallbacks(unsigned int version)
   284 {
   285 	if (vsp_bridge != NULL)
   286 		return NULL;
   287 
   288 	/* Only support versions 1 or 2 right now */
   289 	if (version > 2)
   290 		return NULL;
   291 
   292 	mm_vsp_callbacks.PrepForLoad(version);
   293 
   294 	return &mm_vsp_callbacks;
   295 }
   296