loader/serverplugin.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 16 03:47:58 2008 -0600 (2008-11-16)
changeset 555 22e39517e97b
parent 547 d43e4c343426
child 576 fc3600fed257
permissions -rw-r--r--
Fixed loader responding to plugin api > 2.
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <assert.h>
     4 #include <stddef.h>
     5 #include "loader.h"
     6 #include <sh_memfuncinfo.h>
     7 #include <sh_memory.h>
     8 #include "serverplugin.h"
     9 
    10 typedef enum
    11 {
    12 	PLUGIN_CONTINUE = 0,
    13 	PLUGIN_OVERRIDE,
    14 	PLUGIN_STOP,
    15 } PLUGIN_RESULT;
    16 
    17 typedef enum
    18 {
    19     eQueryCvarValueStatus_ValueIntact=0,
    20     eQueryCvarValueStatus_CvarNotFound=1,
    21     eQueryCvarValueStatus_NotACvar=2,
    22     eQueryCvarValueStatus_CvarProtected=3
    23 } EQueryCvarValueStatus;
    24 
    25 typedef int QueryCvarCookie_t;
    26 class CCommand;
    27 class IServerPluginCallbacks;
    28 struct edict_t;
    29 
    30 #if defined WIN32
    31 #define LIBRARY_EXT		".dll"
    32 #else
    33 #define LIBRARY_EXT		"_i486.so"
    34 #endif
    35 
    36 class IRandomThings
    37 {
    38 public:
    39 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
    40 	{
    41 		return PLUGIN_CONTINUE;
    42 	}
    43 };
    44 
    45 /**
    46  * The vtable must match the general layout for ISPC.  We modify the vtable
    47  * based on what we get back.
    48  */
    49 class ServerPlugin
    50 {
    51 	IVspBridge* bridge;
    52 	unsigned int vsp_version;
    53 	bool load_allowed;
    54 public:
    55 	ServerPlugin()
    56 	{
    57 		load_allowed = false;
    58 	}
    59 	virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
    60 	{
    61 		MetamodBackend backend = MMBackend_UNKNOWN;
    62 
    63 		if (!load_allowed)
    64 			return false;
    65 
    66 		load_allowed = false;
    67 
    68 		/* Check for L4D */
    69 		if (engineFactory("VEngineServer022", NULL) != NULL &&
    70 			engineFactory("VEngineCvar007", NULL) != NULL)
    71 		{
    72 			backend = MMBackend_Left4Dead;
    73 		}
    74 		else if (engineFactory("VEngineServer021", NULL) != NULL)
    75 		{
    76 			/* Check for OB */
    77 			if (engineFactory("VEngineCvar004", NULL) != NULL &&
    78 				engineFactory("VModelInfoServer002", NULL) != NULL)
    79 			{
    80 				backend = MMBackend_Episode2;
    81 			}
    82 			/* Check for EP1 */
    83 			else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
    84 					 (engineFactory("VEngineCvar003", NULL) != NULL ||
    85 					  engineFactory("VEngineCvar002", NULL) != NULL))
    86 			{
    87 				backend = MMBackend_Episode1;
    88 			}
    89 		}
    90 
    91 		if (backend == MMBackend_UNKNOWN)
    92 		{
    93 			mm_LogFatal("Could not detect engine version, spewing stats:");
    94 			return false;
    95 		}
    96 		else if (backend >= MMBackend_Episode2)
    97 		{
    98 			/* We need to insert the right type of call into this vtable */
    99 			void **vtable_src;
   100 			void **vtable_dest;
   101 			IRandomThings sample;
   102 			SourceHook::MemFuncInfo mfp_dest, mfp_src;
   103 
   104 			mfp_dest.isVirtual = false;
   105 			mfp_src.isVirtual = false;
   106 
   107 			SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
   108 			SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
   109 
   110 			assert(mfp_dest.isVirtual);
   111 			assert(mfp_dest.thisptroffs == 0);
   112 			assert(mfp_dest.vtbloffs == 0);
   113 			assert(mfp_src.isVirtual);
   114 			assert(mfp_src.thisptroffs == 0);
   115 			assert(mfp_src.vtbloffs == 0);
   116 
   117 			vtable_src = (void **)*(void **)&sample;
   118 			vtable_dest = (void **)*(void **)this;
   119 			SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
   120 									 sizeof(void*),
   121 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   122 			vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
   123 		}
   124 
   125 		char error[255];
   126 		if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
   127 		{
   128 			mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
   129 			return false;
   130 		}
   131 
   132 		typedef IVspBridge *(*GetVspBridge)();
   133 		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
   134 		if (get_bridge == NULL)
   135 		{
   136 			mm_UnloadMetamodLibrary();
   137 			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback.", backend);
   138 			return false;
   139 		}
   140 
   141 		bridge = get_bridge();
   142 
   143 		vsp_bridge_info info;
   144 
   145 		info.engineFactory = engineFactory;
   146 		info.gsFactory = gsFactory;
   147 		info.vsp_callbacks = (IServerPluginCallbacks*)this;
   148 		info.vsp_version = vsp_version;
   149 
   150 		strcpy(error, "Unknown error");
   151 		if (!bridge->Load(&info, error, sizeof(error)))
   152 		{
   153 			mm_UnloadMetamodLibrary();
   154 			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);	
   155 			return false;
   156 		}
   157 
   158 		return true;
   159 	}
   160 	virtual void Unload()
   161 	{
   162 		if (bridge == NULL)
   163 			return;
   164 		bridge->Unload();
   165 	}
   166 	virtual void Pause()
   167 	{
   168 	}
   169 	virtual void UnPause()
   170 	{
   171 	}
   172 	virtual const char *GetPluginDescription()
   173 	{
   174 		if (bridge == NULL)
   175 			return "Metamod:Source Loader Shim";
   176 		return bridge->GetDescription();
   177 	}
   178 	virtual void LevelInit(char const *pMapName)
   179 	{
   180 	}
   181 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   182 	{
   183 	}
   184 	virtual void GameFrame(bool simulating)
   185 	{
   186 	}
   187 	virtual void LevelShutdown()
   188 	{
   189 	}
   190 	virtual void ClientActive(edict_t *pEntity)
   191 	{
   192 	}
   193 	virtual void ClientDisconnect(edict_t *pEntity)
   194 	{
   195 	}
   196 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   197 	{
   198 	}
   199 	virtual void SetCommandClient(int index)
   200 	{
   201 	}
   202 	virtual void ClientSettingsChanged(edict_t *pEdict)
   203 	{
   204 	}
   205 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   206 										edict_t *pEntity,
   207 										const char *pszName,
   208 										const char *pszAddress,
   209 										char *reject,
   210 										int maxrejectlen) 
   211 	{
   212 		return PLUGIN_CONTINUE;
   213 	}
   214 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   215 	{
   216 		return PLUGIN_CONTINUE;
   217 	}
   218 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   219 	{
   220 		return PLUGIN_CONTINUE;
   221 	}
   222 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   223 										  edict_t *pPlayerEntity,
   224 										  EQueryCvarValueStatus eStatus,
   225 										  const char *pCvarName,
   226 										  const char *pCvarValue)
   227 	{
   228 	}
   229 	void PrepForLoad(unsigned int version)
   230 	{
   231 		vsp_version = version;
   232 		load_allowed = true;
   233 	}
   234 	bool IsLoaded()
   235 	{
   236 		return bridge != NULL;
   237 	}
   238 };
   239 
   240 ServerPlugin mm_vsp_callbacks;
   241 
   242 void *mm_GetVspCallbacks(unsigned int version)
   243 {
   244 	if (mm_vsp_callbacks.IsLoaded())
   245 		return NULL;
   246 
   247 	/* Only support versions 1 or 2 right now */
   248 	if (version > 2)
   249 		return NULL;
   250 
   251 	mm_vsp_callbacks.PrepForLoad(version);
   252 
   253 	return &mm_vsp_callbacks;
   254 }
   255