sourcemm/CSmmAPI.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 20 14:52:49 2005 +0000 (2005-10-20)
changeset 139 84948190c34b
parent 138 dfa3b31cc042
child 152 179336f55ff6
permissions -rw-r--r--
you can now see which plugin returned the iface
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #include "CSmmAPI.h"
    12 #include "sourcemm.h"
    13 #include "concommands.h"
    14 #include "CPlugin.h"
    15 
    16 /**
    17  * @brief Implementation of main API interface
    18  * @file CSmmAPI.cpp
    19  */
    20 
    21 using namespace SourceMM;
    22 
    23 CSmmAPI g_SmmAPI;
    24 
    25 CSmmAPI::CSmmAPI()
    26 {
    27 	m_ConPrintf = NULL;
    28 	m_Cache = false;
    29 }
    30 
    31 void CSmmAPI::LogMsg(ISmmPlugin *pl, const char *msg, ...)
    32 {
    33 	va_list ap;
    34 	static char buffer[2048];
    35 
    36 	buffer[0] = '\0';
    37 
    38 	va_start(ap, msg);
    39 	vsnprintf(buffer, sizeof(buffer)-1, msg, ap);
    40 	va_end(ap);
    41 
    42 	LogMessage("[%s] %s", pl->GetLogTag(), buffer);
    43 }
    44 
    45 CreateInterfaceFn CSmmAPI::engineFactory(bool syn)
    46 {
    47 	if (syn)
    48 		return EngineFactory;
    49 	return g_Engine.engineFactory;
    50 }
    51 
    52 CreateInterfaceFn CSmmAPI::physicsFactory(bool syn)
    53 {
    54 	if (syn)
    55 		return PhysicsFactory;
    56 	return g_Engine.physicsFactory;
    57 }
    58 
    59 CreateInterfaceFn CSmmAPI::fileSystemFactory(bool syn)
    60 {
    61 	if (syn)
    62 		return FileSystemFactory;
    63 	return g_Engine.fileSystemFactory;
    64 }
    65 
    66 CreateInterfaceFn CSmmAPI::serverFactory(bool syn)
    67 {
    68 	if (syn)
    69 		return CreateInterface;
    70 	return g_GameDll.factory;
    71 }
    72 
    73 CGlobalVars *CSmmAPI::pGlobals()
    74 {
    75 	return g_Engine.pGlobals;
    76 }
    77 
    78 void CSmmAPI::SetLastMetaReturn(META_RES res)
    79 {
    80 	m_Res = res;
    81 }
    82 
    83 META_RES CSmmAPI::GetLastMetaReturn()
    84 {
    85 	return m_Res;
    86 }
    87 
    88 IConCommandBaseAccessor *CSmmAPI::GetCvarBaseAccessor()
    89 {
    90 	return static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor);
    91 }
    92 
    93 bool CSmmAPI::RegisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
    94 {
    95 	if (pCommand->IsCommand())
    96 	{
    97 		g_PluginMngr.AddPluginCmd(plugin, pCommand);
    98 	} else {
    99 		g_PluginMngr.AddPluginCvar(plugin, pCommand);
   100 	}
   101 
   102 	return g_SMConVarAccessor.Register(pCommand);
   103 }
   104 
   105 void CSmmAPI::UnregisterConCmdBase(ISmmPlugin *plugin, ConCommandBase *pCommand)
   106 {
   107 	if (pCommand->IsCommand())
   108 	{
   109 		g_PluginMngr.RemovePluginCmd(plugin, pCommand);
   110 	} else {
   111 		g_PluginMngr.RemovePluginCvar(plugin, pCommand);
   112 	}
   113 
   114 	g_SMConVarAccessor.Unregister(pCommand);
   115 }
   116 
   117 void CSmmAPI::ConPrint(const char *fmt)
   118 {
   119 	(m_ConPrintf)("%s", fmt);
   120 }
   121 
   122 void CSmmAPI::ConPrintf(const char *fmt, ...)
   123 {
   124 	va_list ap;
   125 	static char buf[4096];
   126 	va_start(ap, fmt);
   127 	vsnprintf(buf, sizeof(buf)-1, fmt, ap);
   128 	ConPrint(buf);
   129 	va_end(ap);
   130 }
   131 
   132 void CSmmAPI::AddListener(ISmmPlugin *plugin, IMetamodListener *pListener)
   133 {
   134 	CPluginManager::CPlugin *pl = g_PluginMngr.FindByAPI(plugin);
   135 
   136 	pl->m_Events.push_back(pListener);
   137 }
   138 
   139 void *CSmmAPI::MetaFactory(const char *iface, int *_ret, PluginId *id)
   140 {
   141 	if (id)
   142 		*id = 0;
   143 
   144 	if (!iface)
   145 		return NULL;
   146 
   147 	//first check ours... we get first chance!
   148 	if (strcmp(iface, MMIFACE_SOURCEHOOK)==0)
   149 	{
   150 		if (_ret)
   151 			*_ret = IFACE_OK;
   152 		return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
   153 	} else if (strcmp(iface, MMIFACE_PLMANAGER)==0) {
   154 		if (_ret)
   155 			*_ret = IFACE_OK;
   156 		return static_cast<void *>(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
   157 	}
   158 
   159 	CPluginManager::CPlugin *pl;
   160 	SourceHook::List<IMetamodListener *>::iterator event;
   161 	IMetamodListener *api;
   162 	int ret = 0;
   163 	void *val = NULL;
   164 
   165 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
   166 	{
   167 		pl = (*iter);
   168 		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
   169 		{
   170 			api = (*event);
   171 			ret = IFACE_FAILED;
   172 			if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
   173 			{
   174 				if (_ret)
   175 					*_ret = ret;
   176 				if (id)
   177 					*id = pl->m_Id;
   178 				return val;
   179 			}
   180 		}
   181 	}
   182 
   183 	if (_ret)
   184 		*_ret = IFACE_FAILED;
   185 
   186 	return NULL;
   187 }
   188 
   189 //////////////////////////////////////////////////////////////////////////
   190 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
   191 // (note: you can find the offset by looking for the text               //
   192 //   "Echo text to console", you'll find the callback cmd pushed on the //
   193 //   stack.)                                                            //
   194 //////////////////////////////////////////////////////////////////////////
   195 
   196 #define SIGLEN			8
   197 #define ENGINE486_SIG	"\x55\x89\xE5\x53\x83\xEC\x14\xBB"
   198 #define ENGINE486_OFFS	40
   199 #define ENGINE686_SIG	"\x53\x83\xEC\x08\xBB\x01\x00\x00"
   200 #define ENGINE686_OFFS	50
   201 #define	ENGINEAMD_SIG	"\x53\x51\xBB\x01\x00\x00\x00\x51"
   202 #define	ENGINEAMD_OFFS	47
   203 #define ENGINEW32_SIG	"\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
   204 #define ENGINEW32_OFFS	38
   205 #define IA32_CALL		0xE8
   206 
   207 bool vcmp(void *_addr1, void *_addr2, size_t len)
   208 {
   209 	unsigned char *addr1 = (unsigned char *)_addr1;
   210 	unsigned char *addr2 = (unsigned char *)_addr2;
   211 
   212 	for (size_t i=0; i<len; i++)
   213 	{
   214 		if (addr2[i] == '*')
   215 			continue;
   216 		if (addr1[i] != addr2[i])
   217 			return false;
   218 	}
   219 
   220 	return true;
   221 }
   222 
   223 //Thanks to fysh for the idea of extracting info from "echo" and for
   224 // having the original offsets at hand!
   225 bool CSmmAPI::CacheCmds()
   226 {
   227 	ICvar *pCvar = g_Engine.icvar;
   228 
   229 	ConCommandBase *pBase = pCvar->GetCommands();
   230 	unsigned char *ptr = NULL;
   231 	FnCommandCallback callback = NULL;
   232 	int offs = 0;
   233 
   234 	while (pBase)
   235 	{
   236 		if ( strcmp(pBase->GetName(), "echo") == 0 )
   237 		{
   238 			//callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
   239 			callback = ((ConCommand *)pBase)->GetCallback();
   240 			ptr = (unsigned char *)callback;
   241 			if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
   242 			{
   243 				offs = ENGINE486_OFFS;
   244 			} else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
   245 				offs = ENGINE686_OFFS;
   246 			} else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
   247 				offs = ENGINEAMD_OFFS;
   248 			} else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
   249 				offs = ENGINEW32_OFFS;
   250 			}
   251 			if (!offs || ptr[offs-1] != IA32_CALL)
   252 			{
   253 				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   254 				return false;
   255 			}
   256 			//get the relative offset
   257 			m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
   258 			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
   259 			m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
   260 
   261 			m_Cache = true;
   262 
   263 			return true;
   264 		}
   265 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   266 	}
   267 
   268 	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   269 
   270 	return false;
   271 }
   272 
   273 bool CSmmAPI::CacheSuccessful()
   274 {
   275 	return m_Cache;
   276 }
   277 
   278 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
   279 {
   280 	major = SM_VERS_API_MAJOR;
   281 	minor = SM_VERS_API_MINOR;
   282 	plvers = PLAPI_VERSION;
   283 	plmin = PLAPI_MIN_VERSION;
   284 }
   285 
   286 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
   287 {
   288 	shvers = SH_IFACE_VERSION;
   289 	shimpl = SH_IMPL_VERSION;
   290 }