Updated changelog, added license headers.
3 * ======================================================
5 * Copyright (C) 2004-2008 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.
33 #include <sh_memfuncinfo.h>
34 #include <sh_memory.h>
35 #include "serverplugin.h"
46 eQueryCvarValueStatus_ValueIntact=0,
47 eQueryCvarValueStatus_CvarNotFound=1,
48 eQueryCvarValueStatus_NotACvar=2,
49 eQueryCvarValueStatus_CvarProtected=3
50 } EQueryCvarValueStatus;
52 typedef int QueryCvarCookie_t;
54 class IServerPluginCallbacks;
58 #define LIBRARY_EXT ".dll"
60 #define LIBRARY_EXT "_i486.so"
66 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
68 return PLUGIN_CONTINUE;
72 IVspBridge *vsp_bridge = NULL;
75 * The vtable must match the general layout for ISPC. We modify the vtable
76 * based on what we get back.
80 unsigned int vsp_version;
87 virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
94 MetamodBackend backend = mm_DetermineBackend(engineFactory);
96 if (backend == MMBackend_UNKNOWN)
98 mm_LogFatal("Could not detect engine version");
101 else if (backend >= MMBackend_Episode2)
103 /* We need to insert the right type of call into this vtable */
106 IRandomThings sample;
107 SourceHook::MemFuncInfo mfp_dest, mfp_src;
109 mfp_dest.isVirtual = false;
110 mfp_src.isVirtual = false;
112 SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
113 SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
115 assert(mfp_dest.isVirtual);
116 assert(mfp_dest.thisptroffs == 0);
117 assert(mfp_dest.vtbloffs == 0);
118 assert(mfp_src.isVirtual);
119 assert(mfp_src.thisptroffs == 0);
120 assert(mfp_src.vtbloffs == 0);
122 vtable_src = (void **)*(void **)&sample;
123 vtable_dest = (void **)*(void **)this;
124 SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
126 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
127 vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
131 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
133 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
137 typedef IVspBridge *(*GetVspBridge)();
138 GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
139 if (get_bridge == NULL)
141 mm_UnloadMetamodLibrary();
142 mm_LogFatal("Detected engine %d but could not find GetVspBridge callback", backend);
146 vsp_bridge = get_bridge();
148 vsp_bridge_info info;
150 info.engineFactory = engineFactory;
151 info.gsFactory = gsFactory;
152 info.vsp_callbacks = (IServerPluginCallbacks*)this;
153 info.vsp_version = vsp_version;
155 strcpy(error, "Unknown error");
156 if (!vsp_bridge->Load(&info, error, sizeof(error)))
159 mm_UnloadMetamodLibrary();
160 mm_LogFatal("Unknown error loading Metamod for engine %d: %s", backend, error);
166 virtual void Unload()
168 if (vsp_bridge == NULL)
170 vsp_bridge->Unload();
171 mm_UnloadMetamodLibrary();
176 virtual void UnPause()
179 virtual const char *GetPluginDescription()
181 if (vsp_bridge == NULL)
182 return "Metamod:Source Loader Shim";
183 return vsp_bridge->GetDescription();
185 virtual void LevelInit(char const *pMapName)
188 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
191 virtual void GameFrame(bool simulating)
194 virtual void LevelShutdown()
197 virtual void ClientActive(edict_t *pEntity)
200 virtual void ClientDisconnect(edict_t *pEntity)
203 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
206 virtual void SetCommandClient(int index)
209 virtual void ClientSettingsChanged(edict_t *pEdict)
212 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
215 const char *pszAddress,
219 return PLUGIN_CONTINUE;
221 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
223 return PLUGIN_CONTINUE;
225 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
227 return PLUGIN_CONTINUE;
229 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
230 edict_t *pPlayerEntity,
231 EQueryCvarValueStatus eStatus,
232 const char *pCvarName,
233 const char *pCvarValue)
236 void PrepForLoad(unsigned int version)
238 vsp_version = version;
243 ServerPlugin mm_vsp_callbacks;
245 void *mm_GetVspCallbacks(unsigned int version)
247 if (vsp_bridge != NULL)
250 /* Only support versions 1 or 2 right now */
254 mm_vsp_callbacks.PrepForLoad(version);
256 return &mm_vsp_callbacks;