loader/serverplugin.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Feb 18 01:38:23 2009 -0600 (2009-02-18)
changeset 637 b66489d8aedc
parent 608 30b546cc4e03
child 669 2ae87570fab2
permissions -rw-r--r--
Added support for Dark Messiah engine and game. (no bug, r=me).

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