core/logic/ShareSys.cpp
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 15 21:22:24 2014 -0400 (2014-05-15)
changeset 4362 8a15e967d983
parent 4358 52aa5fab9866
permissions -rw-r--r--
Update gamedata for CS:S update.
     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 using namespace ke;
    41 
    42 ShareSystem g_ShareSys;
    43 static unsigned int g_mark_serial = 0;
    44 
    45 ShareSystem::ShareSystem()
    46 {
    47 	m_IdentRoot.ident = 0;
    48 	m_TypeRoot = 0;
    49 	m_IfaceType = 0;
    50 	m_CoreType = 0;
    51 }
    52 
    53 IdentityToken_t *ShareSystem::CreateCoreIdentity()
    54 {
    55 	if (!m_CoreType)
    56 	{
    57 		m_CoreType = CreateIdentType("CORE");
    58 	}
    59 
    60 	return CreateIdentity(m_CoreType, this);
    61 }
    62 
    63 void ShareSystem::Initialize()
    64 {
    65 	TypeAccess sec;
    66 
    67 	handlesys->InitAccessDefaults(&sec, NULL);
    68 	sec.ident = GetIdentRoot();
    69 
    70 	m_TypeRoot = handlesys->CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
    71 	m_IfaceType = handlesys->CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
    72 
    73 	/* Initialize our static identity handle */
    74 	m_IdentRoot.ident = handlesys->CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
    75 
    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);
    79 }
    80 
    81 void ShareSystem::OnSourceModShutdown()
    82 {
    83 	if (m_CoreType)
    84 	{
    85 		handlesys->RemoveType(m_CoreType, GetIdentRoot());
    86 	}
    87 
    88 	handlesys->RemoveType(m_IfaceType, GetIdentRoot());
    89 	handlesys->RemoveType(m_TypeRoot, GetIdentRoot());
    90 }
    91 
    92 IdentityType_t ShareSystem::FindIdentType(const char *name)
    93 {
    94 	HandleType_t type;
    95 
    96 	if (handlesys->FindHandleType(name, &type))
    97 	{
    98 		if (g_HandleSys.TypeCheck(type, m_TypeRoot))
    99 		{
   100 			return type;
   101 		}
   102 	}
   103 
   104 	return 0;
   105 }
   106 
   107 IdentityType_t ShareSystem::CreateIdentType(const char *name)
   108 {
   109 	if (!m_TypeRoot)
   110 	{
   111 		return 0;
   112 	}
   113 
   114 	return handlesys->CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
   115 }
   116 
   117 void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
   118 {
   119 	/* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
   120 }
   121 
   122 IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
   123 {
   124 	if (!m_TypeRoot)
   125 	{
   126 		return 0;
   127 	}
   128 
   129 	/* :TODO: Cache? */
   130 	IdentityToken_t *pToken = new IdentityToken_t;
   131 
   132 	HandleSecurity sec;
   133 	sec.pOwner = sec.pIdentity = GetIdentRoot();
   134 
   135 	pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
   136 	pToken->ptr = ptr;
   137 	pToken->type = type;
   138 
   139 	return pToken;
   140 }
   141 
   142 bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
   143 {
   144 	if (!iface)
   145 	{
   146 		return false;
   147 	}
   148 
   149 	IfaceInfo info;
   150 
   151 	info.owner = myself;
   152 	info.iface = iface;
   153 	
   154 	m_Interfaces.push_back(info);
   155 
   156 	return true;
   157 }
   158 
   159 bool ShareSystem::RequestInterface(const char *iface_name, 
   160 								   unsigned int iface_vers, 
   161 								   IExtension *myself, 
   162 								   SMInterface **pIface)
   163 {
   164 	/* See if the interface exists */
   165 	List<IfaceInfo>::iterator iter;
   166 	SMInterface *iface;
   167 	IExtension *iface_owner;
   168 	bool found = false;
   169 	for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
   170 	{
   171 		IfaceInfo &info = (*iter);
   172 		iface = info.iface;
   173 		if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
   174 		{
   175 			if (iface->GetInterfaceVersion() == iface_vers
   176 				|| iface->IsVersionCompatible(iface_vers))
   177 			{
   178 				iface_owner = info.owner;
   179 				found = true;
   180 				break;
   181 			}
   182 		}
   183 	}
   184 
   185 	if (!found)
   186 	{
   187 		return false;
   188 	}
   189 
   190 	/* Add a dependency node */
   191 	if (iface_owner)
   192 	{
   193 		IfaceInfo info;
   194 		info.iface = iface;
   195 		info.owner = iface_owner;
   196 		g_Extensions.BindDependency(myself, &info);
   197 	}
   198 
   199 	if (pIface)
   200 	{
   201 		*pIface = iface;
   202 	}
   203 
   204 	return true;
   205 }
   206 
   207 void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   208 {
   209 	CNativeOwner *pOwner;
   210 
   211 	pOwner = g_Extensions.GetNativeOwner(myself);
   212 
   213 	pOwner->AddNatives(natives);
   214 }
   215 
   216 void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
   217 {
   218 	HandleSecurity sec;
   219 
   220 	sec.pOwner = GetIdentRoot();
   221 	sec.pIdentity = GetIdentRoot();
   222 
   223 	handlesys->FreeHandle(identity->ident, &sec);
   224 	delete identity;
   225 }
   226 
   227 void ShareSystem::DestroyIdentType(IdentityType_t type)
   228 {
   229 	handlesys->RemoveType(type, GetIdentRoot());
   230 }
   231 
   232 void ShareSystem::RemoveInterfaces(IExtension *pExtension)
   233 {
   234 	List<IfaceInfo>::iterator iter = m_Interfaces.begin();
   235 
   236 	while (iter != m_Interfaces.end())
   237 	{
   238 		if ((*iter).owner == pExtension)
   239 		{
   240 			iter = m_Interfaces.erase(iter);
   241 		}
   242 		else
   243 		{
   244 			iter++;
   245 		}
   246 	}
   247 }
   248 
   249 void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
   250 {
   251 	g_Extensions.AddDependency(myself, filename, require, autoload);
   252 }
   253 
   254 void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
   255 {
   256 	g_Extensions.AddLibrary(myself, name);
   257 }
   258 
   259 void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   260 {
   261 	assert(false);
   262 }
   263 
   264 PassRef<Native> ShareSystem::FindNative(const char *name)
   265 {
   266 	NativeCache::Result r = m_NtvCache.find(name);
   267 	if (!r.found())
   268 		return NULL;
   269 	return *r;
   270 }
   271 
   272 void ShareSystem::BindNativesToPlugin(CPlugin *pPlugin, bool bCoreOnly)
   273 {
   274 	sp_native_t *native;
   275 	uint32_t i, native_count;
   276 	IPluginContext *pContext;
   277 
   278 	pContext = pPlugin->GetBaseContext();
   279 
   280 	/* Generate a new serial ID, mark our dependencies with it. */
   281 	g_mark_serial++;
   282 	pPlugin->PropagateMarkSerial(g_mark_serial);
   283 
   284 	native_count = pContext->GetNativesNum();
   285 	for (i = 0; i < native_count; i++)
   286 	{
   287 		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
   288 			continue;
   289 
   290 		// If we're already bound, no need to do anything else.
   291 		if (native->status == SP_NATIVE_BOUND)
   292 			continue;
   293 
   294 		/* Otherwise, the native must be in our cache. */
   295 		Ref<Native> pEntry = FindNative(native->name);
   296 		if (!pEntry)
   297 			continue;
   298 
   299 		if (bCoreOnly && pEntry->owner != &g_CoreNatives)
   300 			continue;
   301 
   302 		BindNativeToPlugin(pPlugin, native, i, pEntry);
   303 	}
   304 }
   305 
   306 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, const Ref<Native> &entry)
   307 {
   308 	if (!entry->owner)
   309 		return;
   310 
   311 	IPluginContext *pContext = pPlugin->GetBaseContext();
   312 
   313 	uint32_t i;
   314 	if (pContext->FindNativeByName(entry->name(), &i) != SP_ERROR_NONE)
   315 		return;
   316 
   317 	sp_native_t *native;
   318 	if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
   319 		return;
   320 
   321 	if (native->status == SP_NATIVE_BOUND)
   322 		return;
   323 
   324 	BindNativeToPlugin(pPlugin, native, i, entry);
   325 }
   326 
   327 void ShareSystem::BindNativeToPlugin(CPlugin *pPlugin, sp_native_t *native, uint32_t index,
   328                                      const Ref<Native> &pEntry)
   329 {
   330 	/* Mark as bound... we do the rest next. */
   331 	native->status = SP_NATIVE_BOUND;
   332 	native->pfn = pEntry->func();
   333 
   334 	if (pEntry->fake)
   335 	{
   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;
   339 	}
   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)
   349 				pEntry->owner->AddWeakRef(WeakNative(pPlugin, index));
   350 			else
   351 				native->status = SP_NATIVE_UNBOUND;
   352 		}
   353 		/* Otherwise, we're a strong dependent and not a weak one */
   354 		else
   355 		{
   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.
   359 			 */
   360 			if (pEntry->owner != pPlugin->ToNativeOwner() 
   361 				&& pEntry->owner->GetMarkSerial() != g_mark_serial)
   362 			{
   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);
   367 			}
   368 		}
   369 	}
   370 }
   371 
   372 PassRef<Native> ShareSystem::AddNativeToCache(CNativeOwner *pOwner, const sp_nativeinfo_t *ntv)
   373 {
   374 	NativeCache::Insert i = m_NtvCache.findForAdd(ntv->name);
   375 	if (i.found())
   376 		return NULL;
   377 
   378 	Ref<Native> entry = Newborn<Native>(new Native(pOwner, ntv));
   379 	m_NtvCache.insert(ntv->name, entry);
   380 	return entry;
   381 }
   382 
   383 FakeNative::~FakeNative()
   384 {
   385 	g_pSourcePawn2->DestroyFakeNative(gate);
   386 }
   387 
   388 void ShareSystem::ClearNativeFromCache(CNativeOwner *pOwner, const char *name)
   389 {
   390 	NativeCache::Result r = m_NtvCache.find(name);
   391 	if (!r.found())
   392 		return;
   393 
   394 	Ref<Native> entry(*r);
   395 	if (entry->owner != pOwner)
   396 		return;
   397 
   398 	// Clear out the owner bit as a sanity measure.
   399 	entry->owner = NULL;
   400 
   401 	m_NtvCache.remove(r);
   402 }
   403 
   404 PassRef<Native> ShareSystem::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func)
   405 {
   406 	Ref<Native> entry(FindNative(name));
   407 	if (entry)
   408 		return NULL;
   409 
   410 	AutoPtr<FakeNative> fake(new FakeNative(name, pFunc));
   411 
   412 	fake->gate = g_pSourcePawn2->CreateFakeNative(func, fake);
   413 	if (!fake->gate)
   414 		return NULL;
   415 
   416 	CNativeOwner *owner = g_PluginSys.GetPluginByCtx(fake->ctx->GetContext());
   417 
   418 	entry = Newborn<Native>(new Native(owner, fake.take()));
   419 	m_NtvCache.insert(name, entry);
   420 
   421 	return entry;
   422 }
   423 
   424 void ShareSystem::AddCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
   425 		                                const char *name)
   426 {
   427 	if (m_caps.contains(name))
   428 		return;
   429 
   430 	Capability cap;
   431 	cap.ext = myself;
   432 	cap.provider = provider;
   433 
   434 	m_caps.insert(name, cap);
   435 }
   436 
   437 void ShareSystem::DropCapabilityProvider(IExtension *myself, IFeatureProvider *provider,
   438 		                                 const char *name)
   439 {
   440 	StringHashMap<Capability>::Result r = m_caps.find(name);
   441 	if (!r.found())
   442 		return;
   443 	if (r->value.ext != myself || r->value.provider != provider)
   444 		return;
   445 
   446 	m_caps.remove(r);
   447 }
   448 
   449 FeatureStatus ShareSystem::TestFeature(IPluginRuntime *pRuntime, FeatureType feature, 
   450                                        const char *name)
   451 {
   452 	switch (feature)
   453 	{
   454 	case FeatureType_Native:
   455 		return TestNative(pRuntime, name);
   456 	case FeatureType_Capability:
   457 		return TestCap(name);
   458 	default:
   459 		break;
   460 	}
   461 
   462 	return FeatureStatus_Unknown;
   463 }
   464 
   465 FeatureStatus ShareSystem::TestNative(IPluginRuntime *pRuntime, const char *name)
   466 {
   467 	uint32_t index;
   468 
   469 	if (pRuntime->FindNativeByName(name, &index) == SP_ERROR_NONE)
   470 	{
   471 		sp_native_t *native;
   472 		if (pRuntime->GetNativeByIndex(index, &native) == SP_ERROR_NONE)
   473 		{
   474 			if (native->status == SP_NATIVE_BOUND)
   475 				return FeatureStatus_Available;
   476 			else
   477 				return FeatureStatus_Unknown;
   478 		}
   479 	}
   480 
   481 	Ref<Native> entry = FindNative(name);
   482 	if (!entry)
   483 		return FeatureStatus_Unknown;
   484 
   485 	return FeatureStatus_Unavailable;
   486 }
   487 
   488 FeatureStatus ShareSystem::TestCap(const char *name)
   489 {
   490 	StringHashMap<Capability>::Result r = m_caps.find(name);
   491 	if (!r.found())
   492 		return FeatureStatus_Unknown;
   493 
   494 	return r->value.provider->GetFeatureStatus(FeatureType_Capability, name);
   495 }