Initial sketch of new unified loader (vsp only currently).
5 #include <sh_memfuncinfo.h>
7 #include "serverplugin.h"
18 eQueryCvarValueStatus_ValueIntact=0,
19 eQueryCvarValueStatus_CvarNotFound=1,
20 eQueryCvarValueStatus_NotACvar=2,
21 eQueryCvarValueStatus_CvarProtected=3
22 } EQueryCvarValueStatus;
24 typedef int QueryCvarCookie_t;
26 class IServerPluginCallbacks;
30 #define LIBRARY_EXT ".dll"
32 #define LIBRARY_EXT "_i486.so"
38 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity, const CCommand& args)
40 return PLUGIN_CONTINUE;
45 * The vtable must match the general layout for ISPC. We modify the vtable
46 * based on what we get back.
51 unsigned int vsp_version;
58 virtual bool Load(QueryValveInterface engineFactory, QueryValveInterface gsFactory)
60 MetamodBackend backend = MMBackend_UNKNOWN;
68 if (engineFactory("VEngineServer022", NULL) != NULL &&
69 engineFactory("VEngineCvar007", NULL) != NULL)
71 backend = MMBackend_Left4Dead;
73 else if (engineFactory("VEngineServer021", NULL) != NULL)
76 if (engineFactory("VEngineCvar004", NULL) != NULL &&
77 engineFactory("VModelInfoServer002", NULL) != NULL)
79 backend = MMBackend_Episode2;
82 else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
83 (engineFactory("VEngineCvar003", NULL) != NULL ||
84 engineFactory("VEngineCvar002", NULL) != NULL))
86 backend = MMBackend_Episode1;
90 if (backend == MMBackend_UNKNOWN)
92 mm_LogFatal("Could not detect engine version, spewing stats:");
95 else if (backend >= MMBackend_Episode2)
97 /* We need to insert the right type of call into this vtable */
100 IRandomThings sample;
101 SourceHook::MemFuncInfo mfp_dest, mfp_src;
103 mfp_dest.isVirtual = false;
104 mfp_src.isVirtual = false;
106 SourceHook::GetFuncInfo(&ServerPlugin::ClientCommand, mfp_dest);
107 SourceHook::GetFuncInfo(&IRandomThings::ClientCommand, mfp_src);
109 assert(mfp_dest.isVirtual);
110 assert(mfp_dest.thisptroffs == 0);
111 assert(mfp_dest.vtbloffs == 0);
112 assert(mfp_src.isVirtual);
113 assert(mfp_src.thisptroffs == 0);
114 assert(mfp_src.vtbloffs == 0);
116 vtable_src = (void **)*(void **)&sample;
117 vtable_dest = (void **)*(void **)this;
118 SourceHook::SetMemAccess(&vtable_dest[mfp_dest.vtblindex],
120 SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
121 vtable_dest[mfp_dest.vtblindex] = vtable_src[mfp_src.vtblindex];
125 if (!mm_LoadMetamodLibrary(backend, error, sizeof(error)))
127 mm_LogFatal("Detected engine %d but could not load: %s", backend, error);
131 typedef IVspBridge *(*GetVspBridge)();
132 GetVspBridge get_bridge = (GetVspBridge)mm_GetProcAddress("GetVspBridge");
133 if (get_bridge == NULL)
135 mm_UnloadMetamodLibrary();
136 mm_LogFatal("Detected engine %d but could not find GetVspBridge callback.");
140 bridge = get_bridge();
142 vsp_bridge_info info;
144 info.engineFactory = engineFactory;
145 info.gsFactory = gsFactory;
146 info.vsp_callbacks = (IServerPluginCallbacks*)this;
147 info.vsp_version = vsp_version;
149 if (!bridge->Load(&info))
151 mm_UnloadMetamodLibrary();
157 virtual void Unload()
166 virtual void UnPause()
169 virtual const char *GetPluginDescription()
172 return "Metamod:Source Loader Shim";
173 return bridge->GetDescription();
175 virtual void LevelInit(char const *pMapName)
178 virtual void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
181 virtual void GameFrame(bool simulating)
184 virtual void LevelShutdown()
187 virtual void ClientActive(edict_t *pEntity)
190 virtual void ClientDisconnect(edict_t *pEntity)
193 virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
196 virtual void SetCommandClient(int index)
199 virtual void ClientSettingsChanged(edict_t *pEdict)
202 virtual PLUGIN_RESULT ClientConnect(bool *bAllowConnect,
205 const char *pszAddress,
209 return PLUGIN_CONTINUE;
211 virtual PLUGIN_RESULT ClientCommand(edict_t *pEntity)
213 return PLUGIN_CONTINUE;
215 virtual PLUGIN_RESULT NetworkIDValidated(const char *pszUserName, const char *pszNetworkID)
217 return PLUGIN_CONTINUE;
219 virtual void OnQueryCvarValueFinished(QueryCvarCookie_t iCookie,
220 edict_t *pPlayerEntity,
221 EQueryCvarValueStatus eStatus,
222 const char *pCvarName,
223 const char *pCvarValue)
226 void PrepForLoad(unsigned int version)
228 vsp_version = version;
233 return bridge != NULL;
237 ServerPlugin mm_vsp_callbacks;
239 void *mm_GetVspCallbacks(unsigned int version)
241 if (mm_vsp_callbacks.IsLoaded())
244 mm_vsp_callbacks.PrepForLoad(version);
246 return &mm_vsp_callbacks;