2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include <engine/kernel.h>
|
|
|
|
|
|
|
|
class CKernel : public IKernel
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
MAX_INTERFACES = 32,
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
class CInterfaceInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CInterfaceInfo()
|
|
|
|
{
|
|
|
|
m_aName[0] = 0;
|
|
|
|
m_pInterface = 0x0;
|
2017-07-21 17:46:31 +00:00
|
|
|
m_AutoDestroy = false;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
char m_aName[64];
|
|
|
|
IInterface *m_pInterface;
|
2017-07-21 17:46:31 +00:00
|
|
|
bool m_AutoDestroy;
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CInterfaceInfo m_aInterfaces[MAX_INTERFACES];
|
|
|
|
int m_NumInterfaces;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CInterfaceInfo *FindInterfaceInfo(const char *pName)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < m_NumInterfaces; i++)
|
|
|
|
{
|
|
|
|
if(str_comp(pName, m_aInterfaces[i].m_aName) == 0)
|
|
|
|
return &m_aInterfaces[i];
|
|
|
|
}
|
|
|
|
return 0x0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
CKernel()
|
|
|
|
{
|
|
|
|
m_NumInterfaces = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:46:31 +00:00
|
|
|
virtual ~CKernel()
|
|
|
|
{
|
|
|
|
// delete interfaces in reverse order just the way it would happen to objects on the stack
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int i = m_NumInterfaces - 1; i >= 0; i--)
|
2017-07-21 17:46:31 +00:00
|
|
|
{
|
2017-07-28 18:44:03 +00:00
|
|
|
if(m_aInterfaces[i].m_AutoDestroy)
|
2017-07-21 17:46:31 +00:00
|
|
|
{
|
|
|
|
delete m_aInterfaces[i].m_pInterface;
|
|
|
|
m_aInterfaces[i].m_pInterface = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
virtual bool RegisterInterfaceImpl(const char *pName, IInterface *pInterface, bool Destroy)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
// TODO: More error checks here
|
2010-08-05 18:34:16 +00:00
|
|
|
if(!pInterface)
|
|
|
|
{
|
|
|
|
dbg_msg("kernel", "ERROR: couldn't register interface %s. null pointer given", pName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_NumInterfaces == MAX_INTERFACES)
|
|
|
|
{
|
|
|
|
dbg_msg("kernel", "ERROR: couldn't register interface '%s'. maximum of interfaces reached", pName);
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(FindInterfaceInfo(pName) != 0)
|
|
|
|
{
|
2010-08-05 18:34:16 +00:00
|
|
|
dbg_msg("kernel", "ERROR: couldn't register interface '%s'. interface already exists", pName);
|
2010-05-29 07:25:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pInterface->m_pKernel = this;
|
|
|
|
m_aInterfaces[m_NumInterfaces].m_pInterface = pInterface;
|
|
|
|
str_copy(m_aInterfaces[m_NumInterfaces].m_aName, pName, sizeof(m_aInterfaces[m_NumInterfaces].m_aName));
|
2020-10-27 17:57:14 +00:00
|
|
|
m_aInterfaces[m_NumInterfaces].m_AutoDestroy = Destroy;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_NumInterfaces++;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ReregisterInterfaceImpl(const char *pName, IInterface *pInterface)
|
|
|
|
{
|
|
|
|
if(FindInterfaceInfo(pName) == 0)
|
|
|
|
{
|
2017-07-26 01:58:00 +00:00
|
|
|
dbg_msg("kernel", "ERROR: couldn't reregister interface '%s'. interface doesn't exist", pName);
|
2010-05-29 07:25:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pInterface->m_pKernel = this;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
virtual IInterface *RequestInterfaceImpl(const char *pName)
|
|
|
|
{
|
|
|
|
CInterfaceInfo *pInfo = FindInterfaceInfo(pName);
|
|
|
|
if(!pInfo)
|
|
|
|
{
|
|
|
|
dbg_msg("kernel", "failed to find interface with the name '%s'", pName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return pInfo->m_pInterface;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IKernel *IKernel::Create() { return new CKernel; }
|