2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2008 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 "ExtensionSys.h"
34 #include <ILibrarySys.h>
35 #include "common_logic.h"
36 #include "PluginSys.h"
37 #include "HandleSys.h"
40 ShareSystem g_ShareSys;
41 static unsigned int g_mark_serial = 0;
43 ShareSystem::ShareSystem()
45 m_IdentRoot.ident = 0;
51 IdentityToken_t *ShareSystem::CreateCoreIdentity()
55 m_CoreType = CreateIdentType("CORE");
58 return CreateIdentity(m_CoreType, this);
61 void ShareSystem::Initialize()
65 handlesys->InitAccessDefaults(&sec, NULL);
66 sec.ident = GetIdentRoot();
68 m_TypeRoot = handlesys->CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
69 m_IfaceType = handlesys->CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
71 /* Initialize our static identity handle */
72 m_IdentRoot.ident = handlesys->CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
74 /* Add the Handle System and others... they are too innocent and pure to do it themselves */
75 AddInterface(NULL, handlesys);
76 AddInterface(NULL, libsys);
79 void ShareSystem::OnSourceModShutdown()
83 handlesys->RemoveType(m_CoreType, GetIdentRoot());
86 handlesys->RemoveType(m_IfaceType, GetIdentRoot());
87 handlesys->RemoveType(m_TypeRoot, GetIdentRoot());
90 IdentityType_t ShareSystem::FindIdentType(const char *name)
94 if (handlesys->FindHandleType(name, &type))
96 if (g_HandleSys.TypeCheck(type, m_TypeRoot))
105 IdentityType_t ShareSystem::CreateIdentType(const char *name)
112 return handlesys->CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
115 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
117 /* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
120 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
128 IdentityToken_t *pToken = new IdentityToken_t;
131 sec.pOwner = sec.pIdentity = GetIdentRoot();
133 pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
140 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
152 m_Interfaces.push_back(info);
157 bool ShareSystem::RequestInterface(const char *iface_name,
158 unsigned int iface_vers,
160 SMInterface **pIface)
162 /* See if the interface exists */
163 List<IfaceInfo>::iterator iter;
165 IExtension *iface_owner;
167 for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
169 IfaceInfo &info = (*iter);
171 if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
173 if (iface->GetInterfaceVersion() == iface_vers
174 || iface->IsVersionCompatible(iface_vers))
176 iface_owner = info.owner;
188 /* Add a dependency node */
193 info.owner = iface_owner;
194 g_Extensions.BindDependency(myself, &info);
205 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
207 CNativeOwner *pOwner;
209 pOwner = g_Extensions.GetNativeOwner(myself);
211 pOwner->AddNatives(natives);
214 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
218 sec.pOwner = GetIdentRoot();
219 sec.pIdentity = GetIdentRoot();
221 handlesys->FreeHandle(identity->ident, &sec);
225 void ShareSystem::DestroyIdentType(IdentityType_t type)
227 handlesys->RemoveType(type, GetIdentRoot());
230 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
232 List<IfaceInfo>::iterator iter = m_Interfaces.begin();
234 while (iter != m_Interfaces.end())
236 if ((*iter).owner == pExtension)
238 iter = m_Interfaces.erase(iter);
247 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
249 g_Extensions.AddDependency(myself, filename, require, autoload);
252 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
254 g_Extensions.AddLibrary(myself, name);
257 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
262 NativeEntry *ShareSystem::FindNative(const char *name)
265 if (!m_NtvCache.retrieve(name, &entry))
270 void ShareSystem::BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly)
273 uint32_t i, native_count;
274 IPluginContext *pContext;
276 pContext = pPlugin->GetBaseContext();
278 /* Generate a new serial ID, mark our dependencies with it. */
280 pPlugin->PropagateMarkSerial(g_mark_serial);
282 native_count = pContext->GetNativesNum();
283 for (i = 0; i < native_count; i++)
285 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
288 // If we're already bound, no need to do anything else.
289 if (native->status == SP_NATIVE_BOUND)
292 /* Otherwise, the native must be in our cache. */
293 NativeEntry *pEntry = FindNative(native->name);
297 if (bCoreOnly && pEntry->owner != &g_CoreNatives)
300 BindNativeToPlugin(pPlugin, native, i, pEntry);
304 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry)
308 IPluginContext *pContext;
310 pContext = pPlugin->GetBaseContext();
312 if (pContext->FindNativeByName(pEntry->name(), &i) != SP_ERROR_NONE)
316 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
321 if (native->status == SP_NATIVE_BOUND)
326 BindNativeToPlugin(pPlugin, native, i, pEntry);
329 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin,
334 /* Mark as bound... we do the rest next. */
335 native->status = SP_NATIVE_BOUND;
336 native->user = pEntry;
338 /* Perform a bind. */
339 native->pfn = pEntry->func();
341 /* We don't bother with dependency crap if the owner is Core. */
342 if (pEntry->owner != &g_CoreNatives)
344 /* The native is optional, this is a special case */
345 if ((native->flags & SP_NTVFLAG_OPTIONAL) == SP_NTVFLAG_OPTIONAL)
347 /* Only add if there is a valid owner. */
348 if (pEntry->owner != NULL)
350 pEntry->owner->AddWeakRef(WeakNative(pPlugin, index));
354 native->status = SP_NATIVE_UNBOUND;
357 /* Otherwise, we're a strong dependent and not a weak one */
360 /* See if this has already been marked as a dependent.
361 * If it has, it means this relationship has already occurred,
362 * and there is no reason to do it again.
364 if (pEntry->owner != pPlugin->ToNativeOwner()
365 && pEntry->owner->GetMarkSerial() != g_mark_serial)
367 /* This has not been marked as a dependency yet */
368 //pPlugin->AddDependency(pEntry->owner);
369 pEntry->owner->AddDependent(pPlugin);
370 pEntry->owner->SetMarkSerial(g_mark_serial);
376 NativeEntry *ShareSystem::AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv)
380 if ((pEntry = FindNative(ntv->name)) == NULL)
382 pEntry = new NativeEntry(pOwner, ntv);
383 m_NtvCache.insert(ntv->name, pEntry);
387 if (pEntry->owner != NULL)
390 pEntry->owner = pOwner;
391 pEntry->native = NULL;
395 FakeNative::~FakeNative()
397 g_pSourcePawn2->DestroyFakeNative(gate);
400 void ShareSystem::ClearNativeFromCache(CNativeOwner *pOwner, const char *name)
404 if ((pEntry = FindNative(name)) == NULL)
407 if (pEntry->owner != pOwner)
411 pEntry->native = NULL;
412 pEntry->owner = NULL;
415 NativeEntry *ShareSystem::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func)
419 if ((pEntry = FindNative(name)) != NULL && pEntry->owner != NULL)
422 ke::AutoPtr<FakeNative> fake(new FakeNative(name, pFunc));
424 fake->gate = g_pSourcePawn2->CreateFakeNative(func, fake);
428 CNativeOwner *owner = g_PluginSys.GetPluginByCtx(fake->ctx->GetContext());
432 pEntry = new NativeEntry(owner, fake.take());
433 m_NtvCache.insert(name, pEntry);
435 pEntry->owner = owner;
436 pEntry->fake = fake.take();
442 void ShareSystem::AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
445 if (m_caps.contains(name))
450 cap.provider = provider;
452 m_caps.insert(name, cap);
455 void ShareSystem::DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
458 StringHashMap<Capability>::Result r = m_caps.find(name);
461 if (r->value.ext != myself || r->value.provider != provider)
467 FeatureStatus ShareSystem::TestFeature(IPluginRuntime *pRuntime, FeatureType feature,
472 case FeatureType_Native:
473 return TestNative(pRuntime, name);
474 case FeatureType_Capability:
475 return TestCap(name);
480 return FeatureStatus_Unknown;
483 FeatureStatus ShareSystem::TestNative(IPluginRuntime *pRuntime, const char *name)
487 if (pRuntime->FindNativeByName(name, &index) == SP_ERROR_NONE)
490 if (pRuntime->GetNativeByIndex(index, &native) == SP_ERROR_NONE)
492 if (native->status == SP_NATIVE_BOUND)
493 return FeatureStatus_Available;
495 return FeatureStatus_Unknown;
499 NativeEntry *entry = FindNative(name);
501 return FeatureStatus_Unknown;
504 return FeatureStatus_Available;
506 return FeatureStatus_Unavailable;
509 FeatureStatus ShareSystem::TestCap(const char *name)
511 StringHashMap<Capability>::Result r = m_caps.find(name);
513 return FeatureStatus_Unknown;
515 return r->value.provider->GetFeatureStatus(FeatureType_Capability, name);