loader/serverplugin.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 23 21:21:49 2008 -0600 (2008-11-23)
changeset 579 be04fd67923b
parent 576 fc3600fed257
child 608 30b546cc4e03
permissions -rw-r--r--
Initial import of gameinfo loader.
     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 IVspBridge *vsp_bridge = NULL;
    46 
    47 /**
    48  * The vtable must match the general layout for ISPC.  We modify the vtable
    49  * based on what we get back.
    50  */
    51 class ServerPlugin
    52 {
    53 	unsigned int vsp_version;
    54 	bool load_allowed;
    55 public:
    56 	ServerPlugin()
    57 	{
    58 		load_allowed = false;
    59 	}
    60 	virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
    61 	{
    62 		if (!load_allowed)
    63 			return false;
    64 
    65 		load_allowed = false;
    66 
    67 		MetamodBackend backend = mm_DetermineBackend(engineFactory);
    68 
    69 		if (backend == MMBackend_UNKNOWN)
    70 		{
    71 			mm_LogFatal("Could not detect engine version");
    72 			return false;
    73 		}
    74 		else if (backend >= MMBackend_Episode2)
    75 		{
    76 			/* We need to insert the right type of call into this vtable */
    77 			void **vtable_src;
    78 			void **vtable_dest;
    79 			IRandomThings sample;
    80 			SourceHook::MemFuncInfo mfp_dest, mfp_src;
    81 
    82 			mfp_dest.isVirtual = false;
    83 			mfp_src.isVirtual = false;
    84 
    85 			SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
    86 			SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
    87 
    88 			assert(mfp_dest.isVirtual);
    89 			assert(mfp_dest.thisptroffs == 0);
    90 			assert(mfp_dest.vtbloffs == 0);
    91 			assert(mfp_src.isVirtual);
    92 			assert(mfp_src.thisptroffs == 0);
    93 			assert(mfp_src.vtbloffs == 0);
    94 
    95 			vtable_src = (void **)*(void **)&sample;
    96 			vtable_dest = (void **)*(void **)this;
    97 			SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
    98 									 sizeof(void*),
    99 									 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
   100 			vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
   101 		}
   102 
   103 		char error[255];
   104 		if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
   105 		{
   106 			mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
   107 			return false;
   108 		}
   109 
   110 		typedef IVspBridge *(*GetVspBridge)();
   111 		GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
   112 		if (get_bridge == NULL)
   113 		{
   114 			mm_UnloadMetamodLibrary();
   115 			mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", backend);
   116 			return false;
   117 		}
   118 
   119 		vsp_bridge = get_bridge();
   120 
   121 		vsp_bridge_info info;
   122 
   123 		info.engineFactory = engineFactory;
   124 		info.gsFactory = gsFactory;
   125 		info.vsp_callbacks = (IServerPluginCallbacks*)this;
   126 		info.vsp_version = vsp_version;
   127 
   128 		strcpy(error, "Unknown error");
   129 		if (!vsp_bridge->Load(&info, error, sizeof(error)))
   130 		{
   131 			vsp_bridge = NULL;
   132 			mm_UnloadMetamodLibrary();
   133 			mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);	
   134 			return false;
   135 		}
   136 
   137 		return true;
   138 	}
   139 	virtual void Unload()
   140 	{
   141 		if (vsp_bridge == NULL)
   142 			return;
   143 		vsp_bridge->Unload();
   144 		mm_UnloadMetamodLibrary();
   145 	}
   146 	virtual void Pause()
   147 	{
   148 	}
   149 	virtual void UnPause()
   150 	{
   151 	}
   152 	virtual const char *GetPluginDescription()
   153 	{
   154 		if (vsp_bridge == NULL)
   155 			return "Metamod:Source Loader Shim";
   156 		return vsp_bridge->GetDescription();
   157 	}
   158 	virtual void LevelInit(char const *pMapName)
   159 	{
   160 	}
   161 	virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
   162 	{
   163 	}
   164 	virtual void GameFrame(bool simulating)
   165 	{
   166 	}
   167 	virtual void LevelShutdown()
   168 	{
   169 	}
   170 	virtual void ClientActive(edict_t *pEntity)
   171 	{
   172 	}
   173 	virtual void ClientDisconnect(edict_t *pEntity)
   174 	{
   175 	}
   176 	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
   177 	{
   178 	}
   179 	virtual void SetCommandClient(int index)
   180 	{
   181 	}
   182 	virtual void ClientSettingsChanged(edict_t *pEdict)
   183 	{
   184 	}
   185 	virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
   186 										edict_t *pEntity,
   187 										const char *pszName,
   188 										const char *pszAddress,
   189 										char *reject,
   190 										int maxrejectlen) 
   191 	{
   192 		return PLUGIN_CONTINUE;
   193 	}
   194 	virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
   195 	{
   196 		return PLUGIN_CONTINUE;
   197 	}
   198 	virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
   199 	{
   200 		return PLUGIN_CONTINUE;
   201 	}
   202 	virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
   203 										  edict_t *pPlayerEntity,
   204 										  EQueryCvarValueStatus eStatus,
   205 										  const char *pCvarName,
   206 										  const char *pCvarValue)
   207 	{
   208 	}
   209 	void PrepForLoad(unsigned int version)
   210 	{
   211 		vsp_version = version;
   212 		load_allowed = true;
   213 	}
   214 };
   215 
   216 ServerPlugin mm_vsp_callbacks;
   217 
   218 void *mm_GetVspCallbacks(unsigned int version)
   219 {
   220 	if (vsp_bridge != NULL)
   221 		return NULL;
   222 
   223 	/* Only support versions 1 or 2 right now */
   224 	if (version > 2)
   225 		return NULL;
   226 
   227 	mm_vsp_callbacks.PrepForLoad(version);
   228 
   229 	return &mm_vsp_callbacks;
   230 }
   231