1 #include "ExtensionSys.h"
2 #include "LibrarySys.h"
6 CExtensionManager g_Extensions;
7 IdentityType_t g_ExtType;
9 CExtension::CExtension(const char *filename, char *error, size_t err_max)
11 m_File.assign(filename);
15 char path[PLATFORM_MAX_PATH+1];
16 g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s/extensions/%s", g_SourceMod.GetSMBaseDir(), filename);
18 m_pLib = g_LibSys.OpenLibrary(path, error, err_max);
25 typedef IExtensionInterface *(*GETAPI)();
26 GETAPI pfnGetAPI = NULL;
28 if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
30 m_pLib->CloseLibrary();
32 snprintf(error, err_max, "Unable to find extension entry point");
37 if (!m_pAPI || m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
39 m_pLib->CloseLibrary();
41 snprintf(error, err_max, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
45 if (m_pAPI->IsMetamodExtension())
50 m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType);
52 if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, err_max, g_SourceMod.IsLateLoadInMap()))
54 if (m_pAPI->IsMetamodExtension())
59 m_pLib->CloseLibrary();
61 g_ShareSys.DestroyIdentity(m_pIdentToken);
67 CExtension::~CExtension()
71 m_pAPI->OnExtensionUnload();
76 g_ShareSys.DestroyIdentity(m_pIdentToken);
81 m_pLib->CloseLibrary();
85 void CExtension::SetError(const char *error)
87 m_Error.assign(error);
90 IExtensionInterface *CExtension::GetAPI()
95 const char *CExtension::GetFilename()
97 return m_File.c_str();
100 IdentityToken_t *CExtension::GetIdentity()
102 return m_pIdentToken;
105 bool CExtension::IsLoaded()
107 return (m_pLib != NULL);
110 void CExtensionManager::OnSourceModAllInitialized()
112 g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
115 void CExtensionManager::OnSourceModShutdown()
117 g_ShareSys.DestroyIdentType(g_ExtType);
120 IExtension *CExtensionManager::LoadAutoExtension(const char *path)
123 CExtension *p = new CExtension(path, error, sizeof(error));
127 g_Logger.LogError("[SOURCEMOD] Unable to load extension \"%s\": %s", path, error);
136 IExtension *CExtensionManager::FindExtensionByFile(const char *file)
138 List<CExtension *>::iterator iter;
141 /* Make sure the file direction is right */
142 char path[PLATFORM_MAX_PATH+1];
143 g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
145 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
148 if (strcmp(pExt->GetFilename(), path) == 0)
157 IExtension *CExtensionManager::FindExtensionByName(const char *ext)
159 List<CExtension *>::iterator iter;
161 IExtensionInterface *pAPI;
164 for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
167 if (!pExt->IsLoaded())
171 if ((pAPI = pExt->GetAPI()) == NULL)
175 name = pAPI->GetExtensionName();
180 if (strcmp(name, ext) == 0)
189 IExtension *CExtensionManager::LoadExtension(const char *file, ExtensionLifetime lifetime, char *error, size_t err_max)
191 CExtension *pExt = new CExtension(file, error, err_max);
193 /* :NOTE: lifetime is currently ignored */
195 if (!pExt->IsLoaded())
201 m_Libs.push_back(pExt);
206 bool CExtensionManager::UnloadExtension(IExtension *pExt)
208 /* :TODO: implement */
212 unsigned int CExtensionManager::NumberOfPluginDependents(IExtension *pExt, unsigned int *optional)
214 /* :TODO: implement */
218 bool CExtensionManager::IsExtensionUnloadable(IExtension *pExtension)
220 /* :TODO: implement */