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.
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 (!mm_GetGameName(game_name, sizeof(game_name)))
96 mm_backend = mm_DetermineBackend(engineFactory, game_name);
99 if (mm_backend == MMBackend_UNKNOWN)
101 mm_LogFatal("Could not detect engine version");
106 this_vtable = (void **)*(void **)this;
108 if (mm_backend != MMBackend_Episode1
109 && mm_backend != MMBackend_DarkMessiah)
111 /* We need to insert the right type of call into this vtable */
113 IRandomThings sample;
114 SourceHook::MemFuncInfo mfp_dest, mfp_src;
116 mfp_dest.isVirtual = false;
117 mfp_src.isVirtual = false;
119 SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
120 SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
122 assert(mfp_dest.isVirtual);
123 assert(mfp_dest.thisptroffs == 0);
124 assert(mfp_dest.vtbloffs == 0);
125 assert(mfp_src.isVirtual);
126 assert(mfp_src.thisptroffs == 0);
127 assert(mfp_src.vtbloffs == 0);
129 vtable_src = (void **)*(void **)&sample;
130 SourceHook::SetMemAccess(&this_vtable[mfp_dest.vtblindex],
132 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
133 this_vtable[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
136 /* AS inserted ClientFullyConnect into vtable, so move entries up on older engines */
137 if (mm_backend != MMBackend_AlienSwarm
138 && mm_backend != MMBackend_Portal2
139 && mm_backend != MMBackend_Blade
140 && mm_backend != MMBackend_CSGO
141 && mm_backend != MMBackend_DOTA)
143 SourceHook::MemFuncInfo mfp_fconnect;
144 mfp_fconnect.isVirtual = false;
146 SourceHook::GetFuncInfo(&ServerPlugin::ClientFullyConnect, mfp_fconnect);
148 assert(mfp_fconnect.isVirtual);
149 assert(mfp_fconnect.thisptroffs == 0);
150 assert(mfp_fconnect.vtbloffs == 0);
152 /* Shifting ClientDisconnect through OnQueryCvarValueFinished up into slot for
153 * ClientFullyConnect (8 entries)
155 SourceHook::SetMemAccess(&this_vtable[mfp_fconnect.vtblindex],
157 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
158 memmove(&this_vtable[mfp_fconnect.vtblindex],
159 &this_vtable[mfp_fconnect.vtblindex + 1],
164 if (gamedll_bridge == NULL)
166 if (!mm_LoadMetamodLibrary(mm_backend, error, sizeof(error)))
168 mm_LogFatal("Detected engine %d but could not load: %s", mm_backend, error);
173 typedef IVspBridge *(*GetVspBridge)();
174 GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
175 if (get_bridge == NULL)
177 if (gamedll_bridge == NULL)
179 mm_UnloadMetamodLibrary();
181 mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", mm_backend);
185 vsp_bridge = get_bridge();
187 vsp_bridge_info info;
189 info.engineFactory = engineFactory;
190 info.gsFactory = gsFactory;
191 info.vsp_callbacks = (IServerPluginCallbacks*)this;
192 info.vsp_version = vsp_version;
194 strcpy(error, "Unknown error");
195 if (!vsp_bridge->Load(&info, error, sizeof(error)))
198 if (gamedll_bridge == NULL)
200 mm_UnloadMetamodLibrary();
202 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", mm_backend, error);
208 virtual void Unload()
210 if (vsp_bridge == NULL)
212 vsp_bridge->Unload();
214 if (gamedll_bridge == NULL)
216 mm_UnloadMetamodLibrary();
222 virtual void UnPause()
225 virtual const char *GetPluginDescription()
227 if (vsp_bridge == NULL)
228 return "Metamod:Source Loader Shim";
229 return vsp_bridge->GetDescription();
231 virtual void LevelInit(char const *pMapName)
234 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
237 virtual void GameFrame(bool simulating)
240 virtual void LevelShutdown()
243 virtual void ClientActive(edict_t *pEntity)
246 virtual void ClientFullyConnect(edict_t *pEntity)
249 virtual void ClientDisconnect(edict_t *pEntity)
252 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
255 virtual void SetCommandClient(int index)
258 virtual void ClientSettingsChanged(edict_t *pEdict)
261 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
264 const char *pszAddress,
268 return PLUGIN_CONTINUE;
270 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
272 return PLUGIN_CONTINUE;
274 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
276 return PLUGIN_CONTINUE;
278 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
279 edict_t *pPlayerEntity,
280 EQueryCvarValueStatus eStatus,
281 const char *pCvarName,
282 const char *pCvarValue)
285 void PrepForLoad(unsigned int version)
287 vsp_version = version;
292 ServerPlugin mm_vsp_callbacks;
294 void *mm_GetVspCallbacks(unsigned int version)
296 if (vsp_bridge != NULL)
299 /* Only support versions 1 or 2 right now */
303 mm_vsp_callbacks.PrepForLoad(version);
305 return &mm_vsp_callbacks;