sourcemm/CSmmAPI.cpp
author David Anderson <dvander@alliedmods.net>
Thu Oct 20 14:37:47 2005 +0000 (2005-10-20)
changeset 138 dfa3b31cc042
parent 124 b04971393ee4
child 139 84948190c34b
permissions -rw-r--r--
initial import of new events system
     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)
   140 {
   141 	if (!iface)
   142 		return NULL;
   143 
   144 	//first check ours... we get first chance!
   145 	if (strcmp(iface, MMIFACE_SOURCEHOOK)==0)
   146 	{
   147 		if (_ret)
   148 			*_ret = IFACE_OK;
   149 		return static_cast<void *>(static_cast<SourceHook::ISourceHook *>(&g_SourceHook));
   150 	} else if (strcmp(iface, MMIFACE_PLMANAGER)==0) {
   151 		if (_ret)
   152 			*_ret = IFACE_OK;
   153 		return static_cast<void *>(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
   154 	}
   155 
   156 	CPluginManager::CPlugin *pl;
   157 	SourceHook::List<IMetamodListener *>::iterator event;
   158 	IMetamodListener *api;
   159 	int ret = 0;
   160 	void *val = NULL;
   161 
   162 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++)
   163 	{
   164 		pl = (*iter);
   165 		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++)
   166 		{
   167 			api = (*event);
   168 			ret = IFACE_FAILED;
   169 			if ( (val=api->OnMetamodQuery(iface, &ret)) != NULL )
   170 			{
   171 				if (_ret)
   172 					*_ret = ret;
   173 				return val;
   174 			}
   175 		}
   176 	}
   177 
   178 	if (_ret)
   179 		*_ret = IFACE_FAILED;
   180 
   181 	return NULL;
   182 }
   183 
   184 //////////////////////////////////////////////////////////////////////////
   185 //THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
   186 // (note: you can find the offset by looking for the text               //
   187 //   "Echo text to console", you'll find the callback cmd pushed on the //
   188 //   stack.)                                                            //
   189 //////////////////////////////////////////////////////////////////////////
   190 
   191 #define SIGLEN			8
   192 #define ENGINE486_SIG	"\x55\x89\xE5\x53\x83\xEC\x14\xBB"
   193 #define ENGINE486_OFFS	40
   194 #define ENGINE686_SIG	"\x53\x83\xEC\x08\xBB\x01\x00\x00"
   195 #define ENGINE686_OFFS	50
   196 #define	ENGINEAMD_SIG	"\x53\x51\xBB\x01\x00\x00\x00\x51"
   197 #define	ENGINEAMD_OFFS	47
   198 #define ENGINEW32_SIG	"\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
   199 #define ENGINEW32_OFFS	38
   200 #define IA32_CALL		0xE8
   201 
   202 bool vcmp(void *_addr1, void *_addr2, size_t len)
   203 {
   204 	unsigned char *addr1 = (unsigned char *)_addr1;
   205 	unsigned char *addr2 = (unsigned char *)_addr2;
   206 
   207 	for (size_t i=0; i<len; i++)
   208 	{
   209 		if (addr2[i] == '*')
   210 			continue;
   211 		if (addr1[i] != addr2[i])
   212 			return false;
   213 	}
   214 
   215 	return true;
   216 }
   217 
   218 //Thanks to fysh for the idea of extracting info from "echo" and for
   219 // having the original offsets at hand!
   220 bool CSmmAPI::CacheCmds()
   221 {
   222 	ICvar *pCvar = g_Engine.icvar;
   223 
   224 	ConCommandBase *pBase = pCvar->GetCommands();
   225 	unsigned char *ptr = NULL;
   226 	FnCommandCallback callback = NULL;
   227 	int offs = 0;
   228 
   229 	while (pBase)
   230 	{
   231 		if ( strcmp(pBase->GetName(), "echo") == 0 )
   232 		{
   233 			//callback = //*((FnCommandCallback *)((char *)pBase + offsetof(ConCommand, m_fnCommandCallback)));
   234 			callback = ((ConCommand *)pBase)->GetCallback();
   235 			ptr = (unsigned char *)callback;
   236 			if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
   237 			{
   238 				offs = ENGINE486_OFFS;
   239 			} else if (vcmp(ptr, ENGINE686_SIG, SIGLEN)) {
   240 				offs = ENGINE686_OFFS;
   241 			} else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN)) {
   242 				offs = ENGINEAMD_OFFS;
   243 			} else if (vcmp(ptr, ENGINEW32_SIG, SIGLEN)) {
   244 				offs = ENGINEW32_OFFS;
   245 			}
   246 			if (!offs || ptr[offs-1] != IA32_CALL)
   247 			{
   248 				m_ConPrintf = (CONPRINTF_FUNC)Msg;
   249 				return false;
   250 			}
   251 			//get the relative offset
   252 			m_ConPrintf = *((CONPRINTF_FUNC *)(ptr + offs));
   253 			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
   254 			m_ConPrintf = (CONPRINTF_FUNC)((unsigned long)m_ConPrintf + (unsigned long)(ptr + offs) + 4);
   255 
   256 			m_Cache = true;
   257 
   258 			return true;
   259 		}
   260 		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   261 	}
   262 
   263 	m_ConPrintf = (CONPRINTF_FUNC)Msg;
   264 
   265 	return false;
   266 }
   267 
   268 bool CSmmAPI::CacheSuccessful()
   269 {
   270 	return m_Cache;
   271 }
   272 
   273 void CSmmAPI::GetApiVersions(int &major, int &minor, int &plvers, int &plmin)
   274 {
   275 	major = SM_VERS_API_MAJOR;
   276 	minor = SM_VERS_API_MINOR;
   277 	plvers = PLAPI_VERSION;
   278 	plmin = PLAPI_MIN_VERSION;
   279 }
   280 
   281 void CSmmAPI::GetShVersions(int &shvers, int &shimpl)
   282 {
   283 	shvers = SH_IFACE_VERSION;
   284 	shimpl = SH_IMPL_VERSION;
   285 }