loader/serverplugin.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue Nov 18 07:20:38 2008 -0600 (2008-11-18)
changeset 576 fc3600fed257
parent 555 22e39517e97b
child 579 be04fd67923b
permissions -rw-r--r--
Added unload logic for core and core-legacy; no crash on exit anymore
     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 		mm_UnloadMetamodLibrary();
   166 	}
   167 	virtual void Pause()
   168 	{
   169 	}
   170 	virtual void UnPause()
   171 	{
   172 	}
   173 	virtual const char *GetPluginDescription()
   174 	{
   175 		if (bridge == NULL)
   176 			return "Metamod:Source Loader Shim";
   177 		return bridge->GetDescription();
   178 	}
   179 	virtual void LevelInit(char const *pMapName)
   180 	{
   181 	}
   182 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   183 	{
   184 	}
   185 	virtual void GameFrame(bool simulating)
   186 	{
   187 	}
   188 	virtual void LevelShutdown()
   189 	{
   190 	}
   191 	virtual void ClientActive(edict_t *pEntity)
   192 	{
   193 	}
   194 	virtual void ClientDisconnect(edict_t *pEntity)
   195 	{
   196 	}
   197 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   198 	{
   199 	}
   200 	virtual void SetCommandClient(int index)
   201 	{
   202 	}
   203 	virtual void ClientSettingsChanged(edict_t *pEdict)
   204 	{
   205 	}
   206 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   207 										edict_t *pEntity,
   208 										const char *pszName,
   209 										const char *pszAddress,
   210 										char *reject,
   211 										int maxrejectlen) 
   212 	{
   213 		return PLUGIN_CONTINUE;
   214 	}
   215 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   216 	{
   217 		return PLUGIN_CONTINUE;
   218 	}
   219 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   220 	{
   221 		return PLUGIN_CONTINUE;
   222 	}
   223 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   224 										  edict_t *pPlayerEntity,
   225 										  EQueryCvarValueStatus eStatus,
   226 										  const char *pCvarName,
   227 										  const char *pCvarValue)
   228 	{
   229 	}
   230 	void PrepForLoad(unsigned int version)
   231 	{
   232 		vsp_version = version;
   233 		load_allowed = true;
   234 	}
   235 	bool IsLoaded()
   236 	{
   237 		return bridge != NULL;
   238 	}
   239 };
   240 
   241 ServerPlugin mm_vsp_callbacks;
   242 
   243 void *mm_GetVspCallbacks(unsigned int version)
   244 {
   245 	if (mm_vsp_callbacks.IsLoaded())
   246 		return NULL;
   247 
   248 	/* Only support versions 1 or 2 right now */
   249 	if (version > 2)
   250 		return NULL;
   251 
   252 	mm_vsp_callbacks.PrepForLoad(version);
   253 
   254 	return &mm_vsp_callbacks;
   255 }
   256