core/logic/ShareSys.cpp
author David Anderson <dvander@alliedmods.net>
Fri Aug 30 10:14:18 2013 -0700 (2013-08-30)
changeset 4108 dabaea5ab45c
parent 4106 a39557bde4ac
child 4109 10eb68d668af
permissions -rw-r--r--
Simplify NativeEntry state (bug 5852 part 3, r=ds).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "ShareSys.h"
    33 #include "ExtensionSys.h"
    34 #include <ILibrarySys.h>
    35 #include "common_logic.h"
    36 #include "PluginSys.h"
    37 #include "HandleSys.h"
    38 #include <assert.h>
    39 
    40 ShareSystem g_ShareSys;
    41 static unsigned int g_mark_serial = 0;
    42 
    43 ShareSystem::ShareSystem()
    44 {
    45 	m_IdentRoot.ident = 0;
    46 	m_TypeRoot = 0;
    47 	m_IfaceType = 0;
    48 	m_CoreType = 0;
    49 }
    50 
    51 IdentityToken_t *ShareSystem::CreateCoreIdentity()
    52 {
    53 	if (!m_CoreType)
    54 	{
    55 		m_CoreType = CreateIdentType("CORE");
    56 	}
    57 
    58 	return CreateIdentity(m_CoreType, this);
    59 }
    60 
    61 void ShareSystem::Initialize()
    62 {
    63 	TypeAccess sec;
    64 
    65 	handlesys->InitAccessDefaults(&sec, NULL);
    66 	sec.ident = GetIdentRoot();
    67 
    68 	m_TypeRoot = handlesys->CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
    69 	m_IfaceType = handlesys->CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
    70 
    71 	/* Initialize our static identity handle */
    72 	m_IdentRoot.ident = handlesys->CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
    73 
    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);
    77 }
    78 
    79 void ShareSystem::OnSourceModShutdown()
    80 {
    81 	if (m_CoreType)
    82 	{
    83 		handlesys->RemoveType(m_CoreType, GetIdentRoot());
    84 	}
    85 
    86 	handlesys->RemoveType(m_IfaceType, GetIdentRoot());
    87 	handlesys->RemoveType(m_TypeRoot, GetIdentRoot());
    88 }
    89 
    90 IdentityType_t ShareSystem::FindIdentType(const char *name)
    91 {
    92 	HandleType_t type;
    93 
    94 	if (handlesys->FindHandleType(name, &type))
    95 	{
    96 		if (g_HandleSys.TypeCheck(type, m_TypeRoot))
    97 		{
    98 			return type;
    99 		}
   100 	}
   101 
   102 	return 0;
   103 }
   104 
   105 IdentityType_t ShareSystem::CreateIdentType(const char *name)
   106 {
   107 	if (!m_TypeRoot)
   108 	{
   109 		return 0;
   110 	}
   111 
   112 	return handlesys->CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
   113 }
   114 
   115 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
   116 {
   117 	/* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
   118 }
   119 
   120 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
   121 {
   122 	if (!m_TypeRoot)
   123 	{
   124 		return 0;
   125 	}
   126 
   127 	/* :TODO: Cache? */
   128 	IdentityToken_t *pToken = new IdentityToken_t;
   129 
   130 	HandleSecurity sec;
   131 	sec.pOwner = sec.pIdentity = GetIdentRoot();
   132 
   133 	pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
   134 	pToken->ptr = ptr;
   135 	pToken->type = type;
   136 
   137 	return pToken;
   138 }
   139 
   140 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
   141 {
   142 	if (!iface)
   143 	{
   144 		return false;
   145 	}
   146 
   147 	IfaceInfo info;
   148 
   149 	info.owner = myself;
   150 	info.iface = iface;
   151 	
   152 	m_Interfaces.push_back(info);
   153 
   154 	return true;
   155 }
   156 
   157 bool ShareSystem::RequestInterface(const char *iface_name, 
   158 								   unsigned int iface_vers, 
   159 								   IExtension *myself, 
   160 								   SMInterface **pIface)
   161 {
   162 	/* See if the interface exists */
   163 	List<IfaceInfo>::iterator iter;
   164 	SMInterface *iface;
   165 	IExtension *iface_owner;
   166 	bool found = false;
   167 	for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
   168 	{
   169 		IfaceInfo &info = (*iter);
   170 		iface = info.iface;
   171 		if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
   172 		{
   173 			if (iface->GetInterfaceVersion() == iface_vers
   174 				|| iface->IsVersionCompatible(iface_vers))
   175 			{
   176 				iface_owner = info.owner;
   177 				found = true;
   178 				break;
   179 			}
   180 		}
   181 	}
   182 
   183 	if (!found)
   184 	{
   185 		return false;
   186 	}
   187 
   188 	/* Add a dependency node */
   189 	if (iface_owner)
   190 	{
   191 		IfaceInfo info;
   192 		info.iface = iface;
   193 		info.owner = iface_owner;
   194 		g_Extensions.BindDependency(myself, &info);
   195 	}
   196 
   197 	if (pIface)
   198 	{
   199 		*pIface = iface;
   200 	}
   201 
   202 	return true;
   203 }
   204 
   205 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   206 {
   207 	CNativeOwner *pOwner;
   208 
   209 	pOwner = g_Extensions.GetNativeOwner(myself);
   210 
   211 	pOwner->AddNatives(natives);
   212 }
   213 
   214 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
   215 {
   216 	HandleSecurity sec;
   217 
   218 	sec.pOwner = GetIdentRoot();
   219 	sec.pIdentity = GetIdentRoot();
   220 
   221 	handlesys->FreeHandle(identity->ident, &sec);
   222 	delete identity;
   223 }
   224 
   225 void ShareSystem::DestroyIdentType(IdentityType_t type)
   226 {
   227 	handlesys->RemoveType(type, GetIdentRoot());
   228 }
   229 
   230 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
   231 {
   232 	List<IfaceInfo>::iterator iter = m_Interfaces.begin();
   233 
   234 	while (iter != m_Interfaces.end())
   235 	{
   236 		if ((*iter).owner == pExtension)
   237 		{
   238 			iter = m_Interfaces.erase(iter);
   239 		}
   240 		else
   241 		{
   242 			iter++;
   243 		}
   244 	}
   245 }
   246 
   247 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
   248 {
   249 	g_Extensions.AddDependency(myself, filename, require, autoload);
   250 }
   251 
   252 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
   253 {
   254 	g_Extensions.AddLibrary(myself, name);
   255 }
   256 
   257 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   258 {
   259 	assert(false);
   260 }
   261 
   262 NativeEntry *ShareSystem::FindNative(const char *name)
   263 {
   264 	NativeEntry *entry;
   265 	if (!m_NtvCache.retrieve(name, &entry))
   266 		return NULL;
   267 	return entry;
   268 }
   269 
   270 void ShareSystem::BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly)
   271 {
   272 	sp_native_t *native;
   273 	uint32_t i, native_count;
   274 	IPluginContext *pContext;
   275 
   276 	pContext = pPlugin->GetBaseContext();
   277 
   278 	/* Generate a new serial ID, mark our dependencies with it. */
   279 	g_mark_serial++;
   280 	pPlugin->PropagateMarkSerial(g_mark_serial);
   281 
   282 	native_count = pContext->GetNativesNum();
   283 	for (i = 0; i < native_count; i++)
   284 	{
   285 		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
   286 			continue;
   287 
   288 		// If we're already bound, no need to do anything else.
   289 		if (native->status == SP_NATIVE_BOUND)
   290 			continue;
   291 
   292 		/* Otherwise, the native must be in our cache. */
   293 		NativeEntry *pEntry = FindNative(native->name);
   294 		if (!pEntry)
   295 			continue;
   296 
   297 		if (bCoreOnly && pEntry->owner != &g_CoreNatives)
   298 			continue;
   299 
   300 		BindNativeToPlugin(pPlugin, native, i, pEntry);
   301 	}
   302 }
   303 
   304 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, NativeEntry *pEntry)
   305 {
   306 	uint32_t i;
   307 	sp_native_t *native;
   308 	IPluginContext *pContext;
   309 
   310 	pContext = pPlugin->GetBaseContext();
   311 
   312 	if (pContext->FindNativeByName(pEntry->name(), &i) != SP_ERROR_NONE)
   313 	{
   314 		return;
   315 	}
   316 	if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
   317 	{
   318 		return;
   319 	}
   320 
   321 	if (native->status == SP_NATIVE_BOUND)
   322 	{
   323 		return;
   324 	}
   325 
   326 	BindNativeToPlugin(pPlugin, native, i, pEntry);
   327 }
   328 
   329 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, 
   330 									 sp_native_t *native,
   331 									 uint32_t index,
   332 									 NativeEntry *pEntry)
   333 {
   334 	/* Mark as bound... we do the rest next. */
   335 	native->status = SP_NATIVE_BOUND;
   336 	native->user = pEntry;
   337 
   338 	/* Perform a bind. */
   339 	native->pfn = pEntry->func();
   340 
   341 	/* We don't bother with dependency crap if the owner is Core. */
   342 	if (pEntry->owner != &g_CoreNatives)
   343 	{
   344 		/* The native is optional, this is a special case */
   345 		if ((native->flags & SP_NTVFLAG_OPTIONAL) == SP_NTVFLAG_OPTIONAL)
   346 		{
   347 			/* Only add if there is a valid owner. */
   348 			if (pEntry->owner != NULL)
   349 			{
   350 				pEntry->owner->AddWeakRef(WeakNative(pPlugin, index));
   351 			}
   352 			else
   353 			{
   354 				native->status = SP_NATIVE_UNBOUND;
   355 			}
   356 		}
   357 		/* Otherwise, we're a strong dependent and not a weak one */
   358 		else
   359 		{
   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.
   363 			 */
   364 			if (pEntry->owner != pPlugin->ToNativeOwner() 
   365 				&& pEntry->owner->GetMarkSerial() != g_mark_serial)
   366 			{
   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);
   371 			}
   372 		}
   373 	}
   374 }
   375 
   376 NativeEntry *ShareSystem::AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv)
   377 {
   378 	NativeEntry *pEntry;
   379 
   380 	if ((pEntry = FindNative(ntv->name)) == NULL)
   381 	{
   382 		pEntry = new NativeEntry(pOwner, ntv);
   383 		m_NtvCache.insert(ntv->name, pEntry);
   384 		return pEntry;
   385 	}
   386 
   387 	if (pEntry->owner != NULL)
   388 		return NULL;
   389 
   390 	pEntry->owner = pOwner;
   391 	pEntry->native = NULL;
   392 	return pEntry;
   393 }
   394 
   395 FakeNative::~FakeNative()
   396 {
   397 	g_pSourcePawn2->DestroyFakeNative(gate);
   398 }
   399 
   400 void ShareSystem::ClearNativeFromCache(CNativeOwner *pOwner, const char *name)
   401 {
   402 	NativeEntry *pEntry;
   403 
   404 	if ((pEntry = FindNative(name)) == NULL)
   405 		return;
   406 
   407 	if (pEntry->owner != pOwner)
   408 		return;
   409 
   410 	pEntry->fake = NULL;
   411 	pEntry->native = NULL;
   412 	pEntry->owner = NULL;
   413 }
   414 
   415 NativeEntry *ShareSystem::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func)
   416 {
   417 	NativeEntry *pEntry;
   418 
   419 	if ((pEntry = FindNative(name)) != NULL && pEntry->owner != NULL)
   420 		return NULL;
   421 
   422 	ke::AutoPtr<FakeNative> fake(new FakeNative(name, pFunc));
   423 
   424 	fake->gate = g_pSourcePawn2->CreateFakeNative(func, fake);
   425 	if (!fake->gate)
   426 		return NULL;
   427 
   428 	CNativeOwner *owner = g_PluginSys.GetPluginByCtx(fake->ctx->GetContext());
   429 
   430 	if (!pEntry)
   431 	{
   432 		pEntry = new NativeEntry(owner, fake.take());
   433 		m_NtvCache.insert(name, pEntry);
   434 	} else {
   435 		pEntry->owner = owner;
   436 		pEntry->fake = fake.take();
   437 	}
   438 
   439 	return pEntry;
   440 }
   441 
   442 void ShareSystem::AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
   443 		                                const char *name)
   444 {
   445 	if (m_caps.contains(name))
   446 		return;
   447 
   448 	Capability cap;
   449 	cap.ext = myself;
   450 	cap.provider = provider;
   451 
   452 	m_caps.insert(name, cap);
   453 }
   454 
   455 void ShareSystem::DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
   456 		                                 const char *name)
   457 {
   458 	StringHashMap<Capability>::Result r = m_caps.find(name);
   459 	if (!r.found())
   460 		return;
   461 	if (r->value.ext != myself || r->value.provider != provider)
   462 		return;
   463 
   464 	m_caps.remove(r);
   465 }
   466 
   467 FeatureStatus ShareSystem::TestFeature(IPluginRuntime *pRuntime, FeatureType feature, 
   468                                        const char *name)
   469 {
   470 	switch (feature)
   471 	{
   472 	case FeatureType_Native:
   473 		return TestNative(pRuntime, name);
   474 	case FeatureType_Capability:
   475 		return TestCap(name);
   476 	default:
   477 		break;
   478 	}
   479 
   480 	return FeatureStatus_Unknown;
   481 }
   482 
   483 FeatureStatus ShareSystem::TestNative(IPluginRuntime *pRuntime, const char *name)
   484 {
   485 	uint32_t index;
   486 
   487 	if (pRuntime->FindNativeByName(name, &index) == SP_ERROR_NONE)
   488 	{
   489 		sp_native_t *native;
   490 		if (pRuntime->GetNativeByIndex(index, &native) == SP_ERROR_NONE)
   491 		{
   492 			if (native->status == SP_NATIVE_BOUND)
   493 				return FeatureStatus_Available;
   494 			else
   495 				return FeatureStatus_Unknown;
   496 		}
   497 	}
   498 
   499 	NativeEntry *entry = FindNative(name);
   500 	if (!entry)
   501 		return FeatureStatus_Unknown;
   502 
   503 	if (entry->owner)
   504 		return FeatureStatus_Available;
   505 
   506 	return FeatureStatus_Unavailable;
   507 }
   508 
   509 FeatureStatus ShareSystem::TestCap(const char *name)
   510 {
   511 	StringHashMap<Capability>::Result r = m_caps.find(name);
   512 	if (!r.found())
   513 		return FeatureStatus_Unknown;
   514 
   515 	return r->value.provider->GetFeatureStatus(FeatureType_Capability, name);
   516 }