6 #include <sh_memfuncinfo.h>
8 #include "serverplugin.h"
19 eQueryCvarValueStatus_ValueIntact=0,
20 eQueryCvarValueStatus_CvarNotFound=1,
21 eQueryCvarValueStatus_NotACvar=2,
22 eQueryCvarValueStatus_CvarProtected=3
23 } EQueryCvarValueStatus;
25 typedef int QueryCvarCookie_t;
27 class IServerPluginCallbacks;
31 #define LIBRARY_EXT ".dll"
33 #define LIBRARY_EXT "_i486.so"
39 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
41 return PLUGIN_CONTINUE;
46 * The vtable must match the general layout for ISPC. We modify the vtable
47 * based on what we get back.
52 unsigned int vsp_version;
59 virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
61 MetamodBackend backend = MMBackend_UNKNOWN;
69 if (engineFactory("VEngineServer022", NULL) != NULL &&
70 engineFactory("VEngineCvar007", NULL) != NULL)
72 backend = MMBackend_Left4Dead;
74 else if (engineFactory("VEngineServer021", NULL) != NULL)
77 if (engineFactory("VEngineCvar004", NULL) != NULL &&
78 engineFactory("VModelInfoServer002", NULL) != NULL)
80 backend = MMBackend_Episode2;
83 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
84 (engineFactory("VEngineCvar003", NULL) != NULL ||
85 engineFactory("VEngineCvar002", NULL) != NULL))
87 backend = MMBackend_Episode1;
91 if (backend == MMBackend_UNKNOWN)
93 mm_LogFatal("Could not detect engine version, spewing stats:");
96 else if (backend >= MMBackend_Episode2)
98 /* We need to insert the right type of call into this vtable */
101 IRandomThings sample;
102 SourceHook::MemFuncInfo mfp_dest, mfp_src;
104 mfp_dest.isVirtual = false;
105 mfp_src.isVirtual = false;
107 SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
108 SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
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);
117 vtable_src = (void **)*(void **)&sample;
118 vtable_dest = (void **)*(void **)this;
119 SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
121 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
122 vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
126 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
128 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
132 typedef IVspBridge *(*GetVspBridge)();
133 GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
134 if (get_bridge == NULL)
136 mm_UnloadMetamodLibrary();
137 mm_LogFatal("Detected engine %d but could not find GetVspBridge callback.", backend);
141 bridge = get_bridge();
143 vsp_bridge_info info;
145 info.engineFactory = engineFactory;
146 info.gsFactory = gsFactory;
147 info.vsp_callbacks = (IServerPluginCallbacks*)this;
148 info.vsp_version = vsp_version;
150 strcpy(error, "Unknown error");
151 if (!bridge->Load(&info, error, sizeof(error)))
153 mm_UnloadMetamodLibrary();
154 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
160 virtual void Unload()
165 mm_UnloadMetamodLibrary();
170 virtual void UnPause()
173 virtual const char *GetPluginDescription()
176 return "Metamod:Source Loader Shim";
177 return bridge->GetDescription();
179 virtual void LevelInit(char const *pMapName)
182 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
185 virtual void GameFrame(bool simulating)
188 virtual void LevelShutdown()
191 virtual void ClientActive(edict_t *pEntity)
194 virtual void ClientDisconnect(edict_t *pEntity)
197 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
200 virtual void SetCommandClient(int index)
203 virtual void ClientSettingsChanged(edict_t *pEdict)
206 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
209 const char *pszAddress,
213 return PLUGIN_CONTINUE;
215 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
217 return PLUGIN_CONTINUE;
219 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
221 return PLUGIN_CONTINUE;
223 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
224 edict_t *pPlayerEntity,
225 EQueryCvarValueStatus eStatus,
226 const char *pCvarName,
227 const char *pCvarValue)
230 void PrepForLoad(unsigned int version)
232 vsp_version = version;
237 return bridge != NULL;
241 ServerPlugin mm_vsp_callbacks;
243 void *mm_GetVspCallbacks(unsigned int version)
245 if (mm_vsp_callbacks.IsLoaded())
248 /* Only support versions 1 or 2 right now */
252 mm_vsp_callbacks.PrepForLoad(version);
254 return &mm_vsp_callbacks;