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