Fixed VSP listener on Linux when loaded via gameinfo.txt on Left 4 Dead 2 (
bug 4113, r=dvander).
This moves the actual VSP listener to the loader and so "plugin_load" is given the path to the loader rather than one of the engine specific binaries.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * ======================================================
5 * Copyright (C) 2004-2009 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
31 #include <sh_memfuncinfo.h>
32 #include <sh_memory.h>
33 #include "serverplugin.h"
45 eQueryCvarValueStatus_ValueIntact=0,
46 eQueryCvarValueStatus_CvarNotFound=1,
47 eQueryCvarValueStatus_NotACvar=2,
48 eQueryCvarValueStatus_CvarProtected=3
49 } EQueryCvarValueStatus;
51 typedef int QueryCvarCookie_t;
53 class IServerPluginCallbacks;
59 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
61 return PLUGIN_CONTINUE;
65 IVspBridge *vsp_bridge = NULL;
68 * The vtable must match the general layout for ISPC. We modify the vtable
69 * based on what we get back.
73 const char *game_name;
74 unsigned int vsp_version;
81 virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
88 /* Backend should already filled in if loaded as gamedll */
89 if (gamedll_bridge == NULL)
91 if ((game_name = mm_GetGameName()) == NULL)
96 mm_backend = mm_DetermineBackend(engineFactory, game_name);
99 if (mm_backend == MMBackend_UNKNOWN)
101 mm_LogFatal("Could not detect engine version");
104 else if (mm_backend >= MMBackend_Episode2)
106 /* We need to insert the right type of call into this vtable */
109 IRandomThings sample;
110 SourceHook::MemFuncInfo mfp_dest, mfp_src;
112 mfp_dest.isVirtual = false;
113 mfp_src.isVirtual = false;
115 SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
116 SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
118 assert(mfp_dest.isVirtual);
119 assert(mfp_dest.thisptroffs == 0);
120 assert(mfp_dest.vtbloffs == 0);
121 assert(mfp_src.isVirtual);
122 assert(mfp_src.thisptroffs == 0);
123 assert(mfp_src.vtbloffs == 0);
125 vtable_src = (void **)*(void **)&sample;
126 vtable_dest = (void **)*(void **)this;
127 SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
129 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
130 vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
134 if (gamedll_bridge == NULL)
136 if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
138 mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
143 typedef IVspBridge *(*GetVspBridge)();
144 GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
145 if (get_bridge == NULL)
147 if (gamedll_bridge == NULL)
149 mm_UnloadMetamodLibrary();
151 mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", mm_backend);
155 vsp_bridge = get_bridge();
157 vsp_bridge_info info;
159 info.engineFactory = engineFactory;
160 info.gsFactory = gsFactory;
161 info.vsp_callbacks = (IServerPluginCallbacks*)this;
162 info.vsp_version = vsp_version;
164 strcpy(error, "Unknown error");
165 if (!vsp_bridge->Load(&info, error, sizeof(error)))
168 if (gamedll_bridge == NULL)
170 mm_UnloadMetamodLibrary();
172 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
178 virtual void Unload()
180 if (vsp_bridge == NULL)
182 vsp_bridge->Unload();
184 if (gamedll_bridge == NULL)
186 mm_UnloadMetamodLibrary();
192 virtual void UnPause()
195 virtual const char *GetPluginDescription()
197 if (vsp_bridge == NULL)
198 return "Metamod:Source Loader Shim";
199 return vsp_bridge->GetDescription();
201 virtual void LevelInit(char const *pMapName)
204 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
207 virtual void GameFrame(bool simulating)
210 virtual void LevelShutdown()
213 virtual void ClientActive(edict_t *pEntity)
216 virtual void ClientDisconnect(edict_t *pEntity)
219 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
222 virtual void SetCommandClient(int index)
225 virtual void ClientSettingsChanged(edict_t *pEdict)
228 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
231 const char *pszAddress,
235 return PLUGIN_CONTINUE;
237 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
239 return PLUGIN_CONTINUE;
241 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
243 return PLUGIN_CONTINUE;
245 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
246 edict_t *pPlayerEntity,
247 EQueryCvarValueStatus eStatus,
248 const char *pCvarName,
249 const char *pCvarValue)
252 void PrepForLoad(unsigned int version)
254 vsp_version = version;
259 ServerPlugin mm_vsp_callbacks;
261 void *mm_GetVspCallbacks(unsigned int version)
263 if (vsp_bridge != NULL)
266 /* Only support versions 1 or 2 right now */
270 mm_vsp_callbacks.PrepForLoad(version);
272 return &mm_vsp_callbacks;