Fixed loader responding to plugin api > 2.
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()
169 virtual void UnPause()
172 virtual const char *GetPluginDescription()
175 return "Metamod:Source Loader Shim";
176 return bridge->GetDescription();
178 virtual void LevelInit(char const *pMapName)
181 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
184 virtual void GameFrame(bool simulating)
187 virtual void LevelShutdown()
190 virtual void ClientActive(edict_t *pEntity)
193 virtual void ClientDisconnect(edict_t *pEntity)
196 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
199 virtual void SetCommandClient(int index)
202 virtual void ClientSettingsChanged(edict_t *pEdict)
205 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
208 const char *pszAddress,
212 return PLUGIN_CONTINUE;
214 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
216 return PLUGIN_CONTINUE;
218 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
220 return PLUGIN_CONTINUE;
222 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
223 edict_t *pPlayerEntity,
224 EQueryCvarValueStatus eStatus,
225 const char *pCvarName,
226 const char *pCvarValue)
229 void PrepForLoad(unsigned int version)
231 vsp_version = version;
236 return bridge != NULL;
240 ServerPlugin mm_vsp_callbacks;
242 void *mm_GetVspCallbacks(unsigned int version)
244 if (mm_vsp_callbacks.IsLoaded())
247 /* Only support versions 1 or 2 right now */
251 mm_vsp_callbacks.PrepForLoad(version);
253 return &mm_vsp_callbacks;