Merge.
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 "HandleSys.h"
34 #include "ExtensionSys.h"
35 #include "LibrarySys.h"
36 #include "PluginSys.h"
37 #include "sm_stringutil.h"
39 ShareSystem g_ShareSys;
40 static unsigned int g_mark_serial = 0;
42 ShareSystem::ShareSystem()
44 m_IdentRoot.ident = 0;
50 IdentityToken_t *ShareSystem::CreateCoreIdentity()
54 m_CoreType = CreateIdentType("CORE");
57 return CreateIdentity(m_CoreType, this);
60 void ShareSystem::Initialize()
64 g_HandleSys.InitAccessDefaults(&sec, NULL);
65 sec.ident = GetIdentRoot();
67 m_TypeRoot = g_HandleSys.CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
68 m_IfaceType = g_HandleSys.CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
70 /* Initialize our static identity handle */
71 m_IdentRoot.ident = g_HandleSys.CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
73 /* Add the Handle System and others... they are too innocent and pure to do it themselves */
74 AddInterface(NULL, &g_HandleSys);
75 AddInterface(NULL, &g_LibSys);
78 void ShareSystem::OnSourceModShutdown()
82 g_HandleSys.RemoveType(m_CoreType, GetIdentRoot());
85 g_HandleSys.RemoveType(m_IfaceType, GetIdentRoot());
86 g_HandleSys.RemoveType(m_TypeRoot, GetIdentRoot());
89 IdentityType_t ShareSystem::FindIdentType(const char *name)
93 if (g_HandleSys.FindHandleType(name, &type))
95 if (g_HandleSys.TypeCheck(type, m_TypeRoot))
104 IdentityType_t ShareSystem::CreateIdentType(const char *name)
111 return g_HandleSys.CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
114 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
116 /* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
119 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
127 IdentityToken_t *pToken = new IdentityToken_t;
130 sec.pOwner = sec.pIdentity = GetIdentRoot();
132 pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
139 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
151 m_Interfaces.push_back(info);
156 bool ShareSystem::RequestInterface(const char *iface_name,
157 unsigned int iface_vers,
159 SMInterface **pIface)
161 /* See if the interface exists */
162 List<IfaceInfo>::iterator iter;
164 IExtension *iface_owner;
166 for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
168 IfaceInfo &info = (*iter);
170 if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
172 if (iface->GetInterfaceVersion() == iface_vers
173 || iface->IsVersionCompatible(iface_vers))
175 iface_owner = info.owner;
187 /* Add a dependency node */
192 info.owner = iface_owner;
193 g_Extensions.BindDependency(myself, &info);
204 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
206 CNativeOwner *pOwner;
208 pOwner = g_Extensions.GetNativeOwner(myself);
210 pOwner->AddNatives(natives);
213 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
217 sec.pOwner = GetIdentRoot();
218 sec.pIdentity = GetIdentRoot();
220 g_HandleSys.FreeHandle(identity->ident, &sec);
224 void ShareSystem::DestroyIdentType(IdentityType_t type)
226 g_HandleSys.RemoveType(type, GetIdentRoot());
229 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
231 List<IfaceInfo>::iterator iter = m_Interfaces.begin();
233 while (iter != m_Interfaces.end())
235 if ((*iter).owner == pExtension)
237 iter = m_Interfaces.erase(iter);
246 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
248 g_Extensions.AddDependency(myself, filename, require, autoload);
251 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
253 g_Extensions.AddLibrary(myself, name);
256 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
260 CNativeOwner *pOwner;
262 pOwner = g_Extensions.GetNativeOwner(myself);
264 for (i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
266 if ((pEntry = FindNative(natives[i].name)) == NULL)
271 if (pEntry->owner != g_pCoreNatives)
276 if (pEntry->replacement.owner != NULL)
281 /* Now it's safe to add the override */
282 pEntry->replacement.func = natives[i].func;
283 pEntry->replacement.owner = pOwner;
284 pOwner->AddReplacedNative(pEntry);
288 NativeEntry *ShareSystem::FindNative(const char *name)
290 NativeEntry **ppEntry;
292 if ((ppEntry = m_NtvCache.retrieve(name)) == NULL)
300 void ShareSystem::BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly)
304 uint32_t i, native_count;
305 IPluginContext *pContext;
307 pContext = pPlugin->GetBaseContext();
309 /* Generate a new serial ID, mark our dependencies with it. */
311 pPlugin->PropogateMarkSerial(g_mark_serial);
313 native_count = pContext->GetNativesNum();
314 for (i = 0; i < native_count; i++)
316 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
321 /* If we're bound, check if there is a replacement available.
322 * If not, this native is totally finalized.
324 if (native->status == SP_NATIVE_BOUND)
326 pEntry = (NativeEntry *)native->user;
327 assert(pEntry != NULL);
328 if (pEntry->replacement.owner == NULL
329 || (pEntry->replacement.owner != NULL
330 && pEntry->replacement.func == native->pfn))
335 /* Otherwise, the native must be in our cache. */
336 else if ((pEntry = FindNative(native->name)) == NULL)
341 if (bCoreOnly && pEntry->owner != g_pCoreNatives)
346 BindNativeToPlugin(pPlugin, native, i, pEntry);
350 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry)
354 IPluginContext *pContext;
356 pContext = pPlugin->GetBaseContext();
358 if (pContext->FindNativeByName(pEntry->name, &i) != SP_ERROR_NONE)
362 if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
367 if (native->status == SP_NATIVE_BOUND)
372 BindNativeToPlugin(pPlugin, native, i, pEntry);
375 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin,
380 /* Mark as bound... we do the rest next. */
381 native->status = SP_NATIVE_BOUND;
382 native->user = pEntry;
384 /* See if a replacement is available. */
385 if (pEntry->replacement.owner != NULL)
387 /* Perform a replacement bind. */
388 native->pfn = pEntry->replacement.func;
389 pEntry->replacement.owner->AddWeakRef(WeakNative(pPlugin, index, pEntry));
393 /* Perform a normal bind. */
394 native->pfn = pEntry->func;
396 /* We don't bother with dependency crap if the owner is Core. */
397 if (pEntry->owner != g_pCoreNatives)
399 /* The native is optional, this is a special case */
400 if ((native->flags & SP_NTVFLAG_OPTIONAL) == SP_NTVFLAG_OPTIONAL)
402 /* Only add if there is a valid owner. */
403 if (pEntry->owner != NULL)
405 pEntry->owner->AddWeakRef(WeakNative(pPlugin, index));
409 native->status = SP_NATIVE_UNBOUND;
412 /* Otherwise, we're a strong dependent and not a weak one */
415 /* See if this has already been marked as a dependent.
416 * If it has, it means this relationship has already occurred,
417 * and there is no reason to do it again.
419 if (pEntry->owner != pPlugin
420 && pEntry->owner->GetMarkSerial() != g_mark_serial)
422 /* This has not been marked as a dependency yet */
423 //pPlugin->AddDependency(pEntry->owner);
424 pEntry->owner->AddDependent(pPlugin);
425 pEntry->owner->SetMarkSerial(g_mark_serial);
432 NativeEntry *ShareSystem::AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv)
436 if ((pEntry = FindNative(ntv->name)) == NULL)
438 pEntry = new NativeEntry;
440 pEntry->owner = pOwner;
441 pEntry->name = ntv->name;
442 pEntry->func = ntv->func;
443 pEntry->replacement.func = NULL;
444 pEntry->replacement.owner = NULL;
447 m_NtvCache.insert(ntv->name, pEntry);
452 if (pEntry->owner != NULL)
457 pEntry->owner = pOwner;
458 pEntry->func = ntv->func;
459 pEntry->name = ntv->name;
464 void ShareSystem::ClearNativeFromCache(CNativeOwner *pOwner, const char *name)
468 if ((pEntry = FindNative(name)) == NULL)
473 if (pEntry->owner != pOwner)
478 if (pEntry->fake != NULL)
480 g_pSourcePawn2->DestroyFakeNative(pEntry->func);
487 pEntry->owner = NULL;
488 pEntry->replacement.func = NULL;
489 pEntry->replacement.owner = NULL;
492 NativeEntry *ShareSystem::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func)
496 SPVM_NATIVE_FUNC gate;
498 if ((pEntry = FindNative(name)) != NULL && pEntry->owner != NULL)
503 pFake = new FakeNative;
505 if ((gate = g_pSourcePawn2->CreateFakeNative(func, pFake)) == NULL)
513 pEntry = new NativeEntry;
514 m_NtvCache.insert(name, pEntry);
518 pFake->ctx = pFunc->GetParentContext();
519 strncopy(pFake->name, name, sizeof(pFake->name));
521 pEntry->fake = pFake;
523 pEntry->name = pFake->name;
524 pEntry->owner = g_PluginSys.GetPluginByCtx(pFake->ctx->GetContext());
525 pEntry->replacement.func = NULL;
526 pEntry->replacement.owner = NULL;