loader/serverplugin.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Oct 29 10:57:27 2009 -0500 (2009-10-29)
changeset 669 2ae87570fab2
parent 637 b66489d8aedc
child 683 b507295429f9
permissions -rw-r--r--
Added support for Left 4 Dead 2 (bug 4078, 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 
    35 typedef enum
    36 {
    37 	PLUGIN_CONTINUE = 0,
    38 	PLUGIN_OVERRIDE,
    39 	PLUGIN_STOP,
    40 } PLUGIN_RESULT;
    41 
    42 typedef enum
    43 {
    44     eQueryCvarValueStatus_ValueIntact=0,
    45     eQueryCvarValueStatus_CvarNotFound=1,
    46     eQueryCvarValueStatus_NotACvar=2,
    47     eQueryCvarValueStatus_CvarProtected=3
    48 } EQueryCvarValueStatus;
    49 
    50 typedef int QueryCvarCookie_t;
    51 class CCommand;
    52 class IServerPluginCallbacks;
    53 struct edict_t;
    54 
    55 #if defined WIN32
    56 #define LIBRARY_EXT		".dll"
    57 #else
    58 #define LIBRARY_EXT		"_i486.so"
    59 #endif
    60 
    61 class IRandomThings
    62 {
    63 public:
    64 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
    65 	{
    66 		return PLUGIN_CONTINUE;
    67 	}
    68 };
    69 
    70 IVspBridge *vsp_bridge = NULL;
    71 
    72 /**
    73  * The vtable must match the general layout for ISPC.  We modify the vtable
    74  * based on what we get back.
    75  */
    76 class ServerPlugin
    77 {
    78 	const char *game_name;
    79 	unsigned int vsp_version;
    80 	bool load_allowed;
    81 public:
    82 	ServerPlugin()
    83 	{
    84 		load_allowed = false;
    85 	}
    86 	virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
    87 	{
    88 		if (!load_allowed)
    89 			return false;
    90 
    91 		load_allowed = false;
    92 
    93 		if ((game_name = mm_GetGameName()) == NULL)
    94 		{
    95 			return false;
    96 		}
    97 
    98 		MetamodBackend backend = mm_DetermineBackend(engineFactory, game_name);
    99 
   100 		if (backend == MMBackend_UNKNOWN)
   101 		{
   102 			mm_LogFatal("Could not detect engine version");
   103 			return false;
   104 		}
   105 		else if (backend >= MMBackend_Episode2)
   106 		{
   107 			/* We need to insert the right type of call into this vtable */
   108 			void **vtable_src;
   109 			void **vtable_dest;
   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 			vtable_dest = (void **)*(void **)this;
   128 			SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
   129 									 sizeof(void*),
   130 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   131 			vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
   132 		}
   133 
   134 		char error[255];
   135 		if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
   136 		{
   137 			mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
   138 			return false;
   139 		}
   140 
   141 		typedef IVspBridge *(*GetVspBridge)();
   142 		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
   143 		if (get_bridge == NULL)
   144 		{
   145 			mm_UnloadMetamodLibrary();
   146 			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", backend);
   147 			return false;
   148 		}
   149 
   150 		vsp_bridge = get_bridge();
   151 
   152 		vsp_bridge_info info;
   153 
   154 		info.engineFactory = engineFactory;
   155 		info.gsFactory = gsFactory;
   156 		info.vsp_callbacks = (IServerPluginCallbacks*)this;
   157 		info.vsp_version = vsp_version;
   158 
   159 		strcpy(error, "Unknown error");
   160 		if (!vsp_bridge->Load(&info, error, sizeof(error)))
   161 		{
   162 			vsp_bridge = NULL;
   163 			mm_UnloadMetamodLibrary();
   164 			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);	
   165 			return false;
   166 		}
   167 
   168 		return true;
   169 	}
   170 	virtual void Unload()
   171 	{
   172 		if (vsp_bridge == NULL)
   173 			return;
   174 		vsp_bridge->Unload();
   175 		mm_UnloadMetamodLibrary();
   176 	}
   177 	virtual void Pause()
   178 	{
   179 	}
   180 	virtual void UnPause()
   181 	{
   182 	}
   183 	virtual const char *GetPluginDescription()
   184 	{
   185 		if (vsp_bridge == NULL)
   186 			return "Metamod:Source Loader Shim";
   187 		return vsp_bridge->GetDescription();
   188 	}
   189 	virtual void LevelInit(char const *pMapName)
   190 	{
   191 	}
   192 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   193 	{
   194 	}
   195 	virtual void GameFrame(bool simulating)
   196 	{
   197 	}
   198 	virtual void LevelShutdown()
   199 	{
   200 	}
   201 	virtual void ClientActive(edict_t *pEntity)
   202 	{
   203 	}
   204 	virtual void ClientDisconnect(edict_t *pEntity)
   205 	{
   206 	}
   207 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   208 	{
   209 	}
   210 	virtual void SetCommandClient(int index)
   211 	{
   212 	}
   213 	virtual void ClientSettingsChanged(edict_t *pEdict)
   214 	{
   215 	}
   216 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   217 										edict_t *pEntity,
   218 										const char *pszName,
   219 										const char *pszAddress,
   220 										char *reject,
   221 										int maxrejectlen) 
   222 	{
   223 		return PLUGIN_CONTINUE;
   224 	}
   225 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   226 	{
   227 		return PLUGIN_CONTINUE;
   228 	}
   229 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   230 	{
   231 		return PLUGIN_CONTINUE;
   232 	}
   233 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   234 										  edict_t *pPlayerEntity,
   235 										  EQueryCvarValueStatus eStatus,
   236 										  const char *pCvarName,
   237 										  const char *pCvarValue)
   238 	{
   239 	}
   240 	void PrepForLoad(unsigned int version)
   241 	{
   242 		vsp_version = version;
   243 		load_allowed = true;
   244 	}
   245 };
   246 
   247 ServerPlugin mm_vsp_callbacks;
   248 
   249 void *mm_GetVspCallbacks(unsigned int version)
   250 {
   251 	if (vsp_bridge != NULL)
   252 		return NULL;
   253 
   254 	/* Only support versions 1 or 2 right now */
   255 	if (version > 2)
   256 		return NULL;
   257 
   258 	mm_vsp_callbacks.PrepForLoad(version);
   259 
   260 	return &mm_vsp_callbacks;
   261 }
   262