Update gamedata for CS:S update.
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"
42 ShareSystem g_ShareSys;
43 static unsigned int g_mark_serial = 0;
45 ShareSystem::ShareSystem()
47 m_IdentRoot.ident = 0;
53 IdentityToken_t *ShareSystem::CreateCoreIdentity()
57 m_CoreType = CreateIdentType("CORE");
60 return CreateIdentity(m_CoreType, this);
63 void ShareSystem::Initialize()
67 handlesys->InitAccessDefaults(&sec, NULL);
68 sec.ident = GetIdentRoot();
70 m_TypeRoot = handlesys->CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
71 m_IfaceType = handlesys->CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
73 /* Initialize our static identity handle */
74 m_IdentRoot.ident = handlesys->CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
76 /* Add the Handle System and others... they are too innocent and pure to do it themselves */
77 AddInterface(NULL, handlesys);
78 AddInterface(NULL, libsys);
81 void ShareSystem::OnSourceModShutdown()
85 handlesys->RemoveType(m_CoreType, GetIdentRoot());
88 handlesys->RemoveType(m_IfaceType, GetIdentRoot());
89 handlesys->RemoveType(m_TypeRoot, GetIdentRoot());
92 IdentityType_t ShareSystem::FindIdentType(const char *name)
96 if (handlesys->FindHandleType(name, &type))
98 if (g_HandleSys.TypeCheck(type, m_TypeRoot))
107 IdentityType_t ShareSystem::CreateIdentType(const char *name)
114 return handlesys->CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
117 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
119 /* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
122 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
130 IdentityToken_t *pToken = new IdentityToken_t;
133 sec.pOwner = sec.pIdentity = GetIdentRoot();
135 pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
142 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
154 m_Interfaces.push_back(info);
159 bool ShareSystem::RequestInterface(const char *iface_name,
160 unsigned int iface_vers,
162 SMInterface **pIface)
164 /* See if the interface exists */
165 List<IfaceInfo>::iterator iter;
167 IExtension *iface_owner;
169 for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
171 IfaceInfo &info = (*iter);
173 if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
175 if (iface->GetInterfaceVersion() == iface_vers
176 || iface->IsVersionCompatible(iface_vers))
178 iface_owner = info.owner;
190 /* Add a dependency node */
195 info.owner = iface_owner;
196 g_Extensions.BindDependency(myself, &info);
207 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
209 CNativeOwner *pOwner;
211 pOwner = g_Extensions.GetNativeOwner(myself);
213 pOwner->AddNatives(natives);
216 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
220 sec.pOwner = GetIdentRoot();
221 sec.pIdentity = GetIdentRoot();
223 handlesys->FreeHandle(identity->ident, &sec);
227 void ShareSystem::DestroyIdentType(IdentityType_t type)
229 handlesys->RemoveType(type, GetIdentRoot());
232 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
234 List<IfaceInfo>::iterator iter = m_Interfaces.begin();
236 while (iter != m_Interfaces.end())
238 if ((*iter).owner == pExtension)
240 iter = m_Interfaces.erase(iter);
249 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
251 g_Extensions.AddDependency(myself, filename, require, autoload);
254 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
256 g_Extensions.AddLibrary(myself, name);
259 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
264 PassRef<Native> ShareSystem::FindNative(const char *name)
266 NativeCache::Result r = m_NtvCache.find(name);
272 void ShareSystem::BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly)
275 uint32_t i, native_count;
276 IPluginContext *pContext;
278 pContext = pPlugin->GetBaseContext();
280 /* Generate a new serial ID, mark our dependencies with it. */
282 pPlugin->PropagateMarkSerial(g_mark_serial);
284 native_count = pContext->GetNativesNum();
285 for (i = 0; i < native_count; i++)
287 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
290 // If we're already bound, no need to do anything else.
291 if (native->status == SP_NATIVE_BOUND)
294 /* Otherwise, the native must be in our cache. */
295 Ref<Native> pEntry = FindNative(native->name);
299 if (bCoreOnly && pEntry->owner != &g_CoreNatives)
302 BindNativeToPlugin(pPlugin, native, i, pEntry);
306 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, const Ref<Native> &entry)
311 IPluginContext *pContext = pPlugin->GetBaseContext();
314 if (pContext->FindNativeByName(entry->name(), &i) != SP_ERROR_NONE)
318 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
321 if (native->status == SP_NATIVE_BOUND)
324 BindNativeToPlugin(pPlugin, native, i, entry);
327 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, sp_native_t *native, uint32_t index,
328 const Ref<Native> &pEntry)
330 /* Mark as bound... we do the rest next. */
331 native->status = SP_NATIVE_BOUND;
332 native->pfn = pEntry->func();
336 /* This native is not necessarily optional, but we don't guarantee
337 * that its address is long-lived. */
338 native->flags |= SP_NTVFLAG_EPHEMERAL;
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. */
349 pEntry->owner->AddWeakRef(WeakNative(pPlugin, index));
351 native->status = SP_NATIVE_UNBOUND;
353 /* Otherwise, we're a strong dependent and not a weak one */
356 /* See if this has already been marked as a dependent.
357 * If it has, it means this relationship has already occurred,
358 * and there is no reason to do it again.
360 if (pEntry->owner != pPlugin->ToNativeOwner()
361 && pEntry->owner->GetMarkSerial() != g_mark_serial)
363 /* This has not been marked as a dependency yet */
364 //pPlugin->AddDependency(pEntry->owner);
365 pEntry->owner->AddDependent(pPlugin);
366 pEntry->owner->SetMarkSerial(g_mark_serial);
372 PassRef<Native> ShareSystem::AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv)
374 NativeCache::Insert i = m_NtvCache.findForAdd(ntv->name);
378 Ref<Native> entry = Newborn<Native>(new Native(pOwner, ntv));
379 m_NtvCache.insert(ntv->name, entry);
383 FakeNative::~FakeNative()
385 g_pSourcePawn2->DestroyFakeNative(gate);
388 void ShareSystem::ClearNativeFromCache(CNativeOwner *pOwner, const char *name)
390 NativeCache::Result r = m_NtvCache.find(name);
394 Ref<Native> entry(*r);
395 if (entry->owner != pOwner)
398 // Clear out the owner bit as a sanity measure.
401 m_NtvCache.remove(r);
404 PassRef<Native> ShareSystem::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func)
406 Ref<Native> entry(FindNative(name));
410 AutoPtr<FakeNative> fake(new FakeNative(name, pFunc));
412 fake->gate = g_pSourcePawn2->CreateFakeNative(func, fake);
416 CNativeOwner *owner = g_PluginSys.GetPluginByCtx(fake->ctx->GetContext());
418 entry = Newborn<Native>(new Native(owner, fake.take()));
419 m_NtvCache.insert(name, entry);
424 void ShareSystem::AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
427 if (m_caps.contains(name))
432 cap.provider = provider;
434 m_caps.insert(name, cap);
437 void ShareSystem::DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
440 StringHashMap<Capability>::Result r = m_caps.find(name);
443 if (r->value.ext != myself || r->value.provider != provider)
449 FeatureStatus ShareSystem::TestFeature(IPluginRuntime *pRuntime, FeatureType feature,
454 case FeatureType_Native:
455 return TestNative(pRuntime, name);
456 case FeatureType_Capability:
457 return TestCap(name);
462 return FeatureStatus_Unknown;
465 FeatureStatus ShareSystem::TestNative(IPluginRuntime *pRuntime, const char *name)
469 if (pRuntime->FindNativeByName(name, &index) == SP_ERROR_NONE)
472 if (pRuntime->GetNativeByIndex(index, &native) == SP_ERROR_NONE)
474 if (native->status == SP_NATIVE_BOUND)
475 return FeatureStatus_Available;
477 return FeatureStatus_Unknown;
481 Ref<Native> entry = FindNative(name);
483 return FeatureStatus_Unknown;
485 return FeatureStatus_Unavailable;
488 FeatureStatus ShareSystem::TestCap(const char *name)
490 StringHashMap<Capability>::Result r = m_caps.find(name);
492 return FeatureStatus_Unknown;
494 return r->value.provider->GetFeatureStatus(FeatureType_Capability, name);