3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "HandleSys.h"
34 #include "ExtensionSys.h"
35 #include "LibrarySys.h"
37 ShareSystem g_ShareSys;
39 ShareSystem::ShareSystem()
41 m_IdentRoot.ident = 0;
47 IdentityToken_t *ShareSystem::CreateCoreIdentity()
51 m_CoreType = CreateIdentType("CORE");
54 return CreateIdentity(m_CoreType, this);
57 void ShareSystem::Initialize()
61 g_HandleSys.InitAccessDefaults(&sec, NULL);
62 sec.ident = GetIdentRoot();
64 m_TypeRoot = g_HandleSys.CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
65 m_IfaceType = g_HandleSys.CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
67 /* Initialize our static identity handle */
68 m_IdentRoot.ident = g_HandleSys.CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
70 /* Add the Handle System and others... they are too innocent and pure to do it themselves */
71 AddInterface(NULL, &g_HandleSys);
72 AddInterface(NULL, &g_LibSys);
75 void ShareSystem::OnSourceModShutdown()
79 g_HandleSys.RemoveType(m_CoreType, GetIdentRoot());
82 g_HandleSys.RemoveType(m_IfaceType, GetIdentRoot());
83 g_HandleSys.RemoveType(m_TypeRoot, GetIdentRoot());
86 IdentityType_t ShareSystem::FindIdentType(const char *name)
90 if (g_HandleSys.FindHandleType(name, &type))
92 if (g_HandleSys.TypeCheck(type, m_TypeRoot))
101 IdentityType_t ShareSystem::CreateIdentType(const char *name)
108 return g_HandleSys.CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
111 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
113 /* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
116 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
124 IdentityToken_t *pToken = new IdentityToken_t;
127 sec.pOwner = sec.pIdentity = GetIdentRoot();
129 pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
136 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
148 m_Interfaces.push_back(info);
153 bool ShareSystem::RequestInterface(const char *iface_name,
154 unsigned int iface_vers,
156 SMInterface **pIface)
158 /* See if the interface exists */
159 List<IfaceInfo>::iterator iter;
161 IExtension *iface_owner;
163 for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
165 IfaceInfo &info = (*iter);
167 if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
169 if (iface->GetInterfaceVersion() == iface_vers
170 || iface->IsVersionCompatible(iface_vers))
172 iface_owner = info.owner;
184 /* Add a dependency node */
189 info.owner = iface_owner;
190 g_Extensions.BindDependency(myself, &info);
201 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
203 g_Extensions.AddNatives(myself, natives);
206 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
210 sec.pOwner = GetIdentRoot();
211 sec.pIdentity = GetIdentRoot();
213 g_HandleSys.FreeHandle(identity->ident, &sec);
217 void ShareSystem::DestroyIdentType(IdentityType_t type)
219 g_HandleSys.RemoveType(type, GetIdentRoot());
222 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
224 List<IfaceInfo>::iterator iter = m_Interfaces.begin();
226 while (iter != m_Interfaces.end())
228 if ((*iter).owner == pExtension)
230 iter = m_Interfaces.erase(iter);
237 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
239 g_Extensions.AddDependency(myself, filename, require, autoload);
242 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
244 g_Extensions.AddLibrary(myself, name);
247 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
249 g_Extensions.OverrideNatives(myself, natives);