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