1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/loader/serverplugin.cpp Sat Nov 15 18:29:24 2008 -0600
1.3 @@ -0,0 +1,248 @@
1.4 +#include <stdio.h>
1.5 +#include <assert.h>
1.6 +#include <stddef.h>
1.7 +#include "loader.h"
1.8 +#include <sh_memfuncinfo.h>
1.9 +#include <sh_memory.h>
1.10 +#include "serverplugin.h"
1.11 +
1.12 +typedef enum
1.13 +{
1.14 + PLUGIN_CONTINUE = 0,
1.15 + PLUGIN_OVERRIDE,
1.16 + PLUGIN_STOP,
1.17 +} PLUGIN_RESULT;
1.18 +
1.19 +typedef enum
1.20 +{
1.21 + eQueryCvarValueStatus_ValueIntact=0,
1.22 + eQueryCvarValueStatus_CvarNotFound=1,
1.23 + eQueryCvarValueStatus_NotACvar=2,
1.24 + eQueryCvarValueStatus_CvarProtected=3
1.25 +} EQueryCvarValueStatus;
1.26 +
1.27 +typedef int QueryCvarCookie_t;
1.28 +class CCommand;
1.29 +class IServerPluginCallbacks;
1.30 +struct edict_t;
1.31 +
1.32 +#if defined WIN32
1.33 +#define LIBRARY_EXT ".dll"
1.34 +#else
1.35 +#define LIBRARY_EXT "_i486.so"
1.36 +#endif
1.37 +
1.38 +class IRandomThings
1.39 +{
1.40 +public:
1.41 + virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
1.42 + {
1.43 + return PLUGIN_CONTINUE;
1.44 + }
1.45 +};
1.46 +
1.47 +/**
1.48 + * The vtable must match the general layout for ISPC. We modify the vtable
1.49 + * based on what we get back.
1.50 + */
1.51 +class ServerPlugin
1.52 +{
1.53 + IVspBridge* bridge;
1.54 + unsigned int vsp_version;
1.55 + bool load_allowed;
1.56 +public:
1.57 + ServerPlugin()
1.58 + {
1.59 + load_allowed = false;
1.60 + }
1.61 + virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
1.62 + {
1.63 + MetamodBackend backend = MMBackend_UNKNOWN;
1.64 +
1.65 + if (!load_allowed)
1.66 + return false;
1.67 +
1.68 + load_allowed = false;
1.69 +
1.70 + /* Check for L4D */
1.71 + if (engineFactory("VEngineServer022", NULL) != NULL &&
1.72 + engineFactory("VEngineCvar007", NULL) != NULL)
1.73 + {
1.74 + backend = MMBackend_Left4Dead;
1.75 + }
1.76 + else if (engineFactory("VEngineServer021", NULL) != NULL)
1.77 + {
1.78 + /* Check for OB */
1.79 + if (engineFactory("VEngineCvar004", NULL) != NULL &&
1.80 + engineFactory("VModelInfoServer002", NULL) != NULL)
1.81 + {
1.82 + backend = MMBackend_Episode2;
1.83 + }
1.84 + /* Check for EP1 */
1.85 + else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
1.86 + (engineFactory("VEngineCvar003", NULL) != NULL ||
1.87 + engineFactory("VEngineCvar002", NULL) != NULL))
1.88 + {
1.89 + backend = MMBackend_Episode1;
1.90 + }
1.91 + }
1.92 +
1.93 + if (backend == MMBackend_UNKNOWN)
1.94 + {
1.95 + mm_LogFatal("Could not detect engine version, spewing stats:");
1.96 + return false;
1.97 + }
1.98 + else if (backend >= MMBackend_Episode2)
1.99 + {
1.100 + /* We need to insert the right type of call into this vtable */
1.101 + void **vtable_src;
1.102 + void **vtable_dest;
1.103 + IRandomThings sample;
1.104 + SourceHook::MemFuncInfo mfp_dest, mfp_src;
1.105 +
1.106 + mfp_dest.isVirtual = false;
1.107 + mfp_src.isVirtual = false;
1.108 +
1.109 + SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
1.110 + SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
1.111 +
1.112 + assert(mfp_dest.isVirtual);
1.113 + assert(mfp_dest.thisptroffs == 0);
1.114 + assert(mfp_dest.vtbloffs == 0);
1.115 + assert(mfp_src.isVirtual);
1.116 + assert(mfp_src.thisptroffs == 0);
1.117 + assert(mfp_src.vtbloffs == 0);
1.118 +
1.119 + vtable_src = (void **)*(void **)&sample;
1.120 + vtable_dest = (void **)*(void **)this;
1.121 + SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
1.122 + sizeof(void*),
1.123 + SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
1.124 + vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
1.125 + }
1.126 +
1.127 + char error[255];
1.128 + if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
1.129 + {
1.130 + mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
1.131 + return false;
1.132 + }
1.133 +
1.134 + typedef IVspBridge *(*GetVspBridge)();
1.135 + GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
1.136 + if (get_bridge == NULL)
1.137 + {
1.138 + mm_UnloadMetamodLibrary();
1.139 + mm_LogFatal("Detected engine %d but could not find GetVspBridge callback.");
1.140 + return false;
1.141 + }
1.142 +
1.143 + bridge = get_bridge();
1.144 +
1.145 + vsp_bridge_info info;
1.146 +
1.147 + info.engineFactory = engineFactory;
1.148 + info.gsFactory = gsFactory;
1.149 + info.vsp_callbacks = (IServerPluginCallbacks*)this;
1.150 + info.vsp_version = vsp_version;
1.151 +
1.152 + if (!bridge->Load(&info))
1.153 + {
1.154 + mm_UnloadMetamodLibrary();
1.155 + return false;
1.156 + }
1.157 +
1.158 + return true;
1.159 + }
1.160 + virtual void Unload()
1.161 + {
1.162 + if (bridge == NULL)
1.163 + return;
1.164 + bridge->Unload();
1.165 + }
1.166 + virtual void Pause()
1.167 + {
1.168 + }
1.169 + virtual void UnPause()
1.170 + {
1.171 + }
1.172 + virtual const char *GetPluginDescription()
1.173 + {
1.174 + if (bridge == NULL)
1.175 + return "Metamod:Source Loader Shim";
1.176 + return bridge->GetDescription();
1.177 + }
1.178 + virtual void LevelInit(char const *pMapName)
1.179 + {
1.180 + }
1.181 + virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
1.182 + {
1.183 + }
1.184 + virtual void GameFrame(bool simulating)
1.185 + {
1.186 + }
1.187 + virtual void LevelShutdown()
1.188 + {
1.189 + }
1.190 + virtual void ClientActive(edict_t *pEntity)
1.191 + {
1.192 + }
1.193 + virtual void ClientDisconnect(edict_t *pEntity)
1.194 + {
1.195 + }
1.196 + virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
1.197 + {
1.198 + }
1.199 + virtual void SetCommandClient(int index)
1.200 + {
1.201 + }
1.202 + virtual void ClientSettingsChanged(edict_t *pEdict)
1.203 + {
1.204 + }
1.205 + virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
1.206 + edict_t *pEntity,
1.207 + const char *pszName,
1.208 + const char *pszAddress,
1.209 + char *reject,
1.210 + int maxrejectlen)
1.211 + {
1.212 + return PLUGIN_CONTINUE;
1.213 + }
1.214 + virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
1.215 + {
1.216 + return PLUGIN_CONTINUE;
1.217 + }
1.218 + virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
1.219 + {
1.220 + return PLUGIN_CONTINUE;
1.221 + }
1.222 + virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
1.223 + edict_t *pPlayerEntity,
1.224 + EQueryCvarValueStatus eStatus,
1.225 + const char *pCvarName,
1.226 + const char *pCvarValue)
1.227 + {
1.228 + }
1.229 + void PrepForLoad(unsigned int version)
1.230 + {
1.231 + vsp_version = version;
1.232 + load_allowed = true;
1.233 + }
1.234 + bool IsLoaded()
1.235 + {
1.236 + return bridge != NULL;
1.237 + }
1.238 +};
1.239 +
1.240 +ServerPlugin mm_vsp_callbacks;
1.241 +
1.242 +void *mm_GetVspCallbacks(unsigned int version)
1.243 +{
1.244 + if (mm_vsp_callbacks.IsLoaded())
1.245 + return NULL;
1.246 +
1.247 + mm_vsp_callbacks.PrepForLoad(version);
1.248 +
1.249 + return &mm_vsp_callbacks;
1.250 +}
1.251 +