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