core/systems/ShareSys.cpp
changeset 1952 b7aa2b626807
child 2086 debb2ab2c782
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/systems/ShareSys.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,250 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "ShareSys.h"
    1.36 +#include "HandleSys.h"
    1.37 +#include "ExtensionSys.h"
    1.38 +#include "LibrarySys.h"
    1.39 +
    1.40 +ShareSystem g_ShareSys;
    1.41 +
    1.42 +ShareSystem::ShareSystem()
    1.43 +{
    1.44 +	m_IdentRoot.ident = 0;
    1.45 +	m_TypeRoot = 0;
    1.46 +	m_IfaceType = 0;
    1.47 +	m_CoreType = 0;
    1.48 +}
    1.49 +
    1.50 +IdentityToken_t *ShareSystem::CreateCoreIdentity()
    1.51 +{
    1.52 +	if (!m_CoreType)
    1.53 +	{
    1.54 +		m_CoreType = CreateIdentType("CORE");
    1.55 +	}
    1.56 +
    1.57 +	return CreateIdentity(m_CoreType, this);
    1.58 +}
    1.59 +
    1.60 +void ShareSystem::Initialize()
    1.61 +{
    1.62 +	TypeAccess sec;
    1.63 +
    1.64 +	g_HandleSys.InitAccessDefaults(&sec, NULL);
    1.65 +	sec.ident = GetIdentRoot();
    1.66 +
    1.67 +	m_TypeRoot = g_HandleSys.CreateType("Identity", this, 0, &sec, NULL, NULL, NULL);
    1.68 +	m_IfaceType = g_HandleSys.CreateType("Interface", this, 0, NULL, NULL, GetIdentRoot(), NULL);
    1.69 +
    1.70 +	/* Initialize our static identity handle */
    1.71 +	m_IdentRoot.ident = g_HandleSys.CreateHandle(m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
    1.72 +
    1.73 +	/* Add the Handle System and others... they are too innocent and pure to do it themselves */
    1.74 +	AddInterface(NULL, &g_HandleSys);
    1.75 +	AddInterface(NULL, &g_LibSys);
    1.76 +}
    1.77 +
    1.78 +void ShareSystem::OnSourceModShutdown()
    1.79 +{
    1.80 +	if (m_CoreType)
    1.81 +	{
    1.82 +		g_HandleSys.RemoveType(m_CoreType, GetIdentRoot());
    1.83 +	}
    1.84 +
    1.85 +	g_HandleSys.RemoveType(m_IfaceType, GetIdentRoot());
    1.86 +	g_HandleSys.RemoveType(m_TypeRoot, GetIdentRoot());
    1.87 +}
    1.88 +
    1.89 +IdentityType_t ShareSystem::FindIdentType(const char *name)
    1.90 +{
    1.91 +	HandleType_t type;
    1.92 +
    1.93 +	if (g_HandleSys.FindHandleType(name, &type))
    1.94 +	{
    1.95 +		if (g_HandleSys.TypeCheck(type, m_TypeRoot))
    1.96 +		{
    1.97 +			return type;
    1.98 +		}
    1.99 +	}
   1.100 +
   1.101 +	return 0;
   1.102 +}
   1.103 +
   1.104 +IdentityType_t ShareSystem::CreateIdentType(const char *name)
   1.105 +{
   1.106 +	if (!m_TypeRoot)
   1.107 +	{
   1.108 +		return 0;
   1.109 +	}
   1.110 +
   1.111 +	return g_HandleSys.CreateType(name, this, m_TypeRoot, NULL, NULL, GetIdentRoot(), NULL);
   1.112 +}
   1.113 +
   1.114 +void ShareSystem::OnHandleDestroy(HandleType_t type, void *object)
   1.115 +{
   1.116 +	/* THIS WILL NEVER BE CALLED FOR ANYTHING WITH THE IDENTITY TYPE */
   1.117 +}
   1.118 +
   1.119 +IdentityToken_t *ShareSystem::CreateIdentity(IdentityType_t type, void *ptr)
   1.120 +{
   1.121 +	if (!m_TypeRoot)
   1.122 +	{
   1.123 +		return 0;
   1.124 +	}
   1.125 +
   1.126 +	/* :TODO: Cache? */
   1.127 +	IdentityToken_t *pToken = new IdentityToken_t;
   1.128 +
   1.129 +	HandleSecurity sec;
   1.130 +	sec.pOwner = sec.pIdentity = GetIdentRoot();
   1.131 +
   1.132 +	pToken->ident = g_HandleSys.CreateHandleInt(type, NULL, &sec, NULL, NULL, true);
   1.133 +	pToken->ptr = ptr;
   1.134 +	pToken->type = type;
   1.135 +
   1.136 +	return pToken;
   1.137 +}
   1.138 +
   1.139 +bool ShareSystem::AddInterface(IExtension *myself, SMInterface *iface)
   1.140 +{
   1.141 +	if (!iface)
   1.142 +	{
   1.143 +		return false;
   1.144 +	}
   1.145 +
   1.146 +	IfaceInfo info;
   1.147 +
   1.148 +	info.owner = myself;
   1.149 +	info.iface = iface;
   1.150 +	
   1.151 +	m_Interfaces.push_back(info);
   1.152 +
   1.153 +	return true;
   1.154 +}
   1.155 +
   1.156 +bool ShareSystem::RequestInterface(const char *iface_name, 
   1.157 +								   unsigned int iface_vers, 
   1.158 +								   IExtension *myself, 
   1.159 +								   SMInterface **pIface)
   1.160 +{
   1.161 +	/* See if the interface exists */
   1.162 +	List<IfaceInfo>::iterator iter;
   1.163 +	SMInterface *iface;
   1.164 +	IExtension *iface_owner;
   1.165 +	bool found = false;
   1.166 +	for (iter=m_Interfaces.begin(); iter!=m_Interfaces.end(); iter++)
   1.167 +	{
   1.168 +		IfaceInfo &info = (*iter);
   1.169 +		iface = info.iface;
   1.170 +		if (strcmp(iface->GetInterfaceName(), iface_name) == 0)
   1.171 +		{
   1.172 +			if (iface->GetInterfaceVersion() == iface_vers
   1.173 +				|| iface->IsVersionCompatible(iface_vers))
   1.174 +			{
   1.175 +				iface_owner = info.owner;
   1.176 +				found = true;
   1.177 +				break;
   1.178 +			}
   1.179 +		}
   1.180 +	}
   1.181 +
   1.182 +	if (!found)
   1.183 +	{
   1.184 +		return false;
   1.185 +	}
   1.186 +
   1.187 +	/* Add a dependency node */
   1.188 +	if (iface_owner)
   1.189 +	{
   1.190 +		IfaceInfo info;
   1.191 +		info.iface = iface;
   1.192 +		info.owner = iface_owner;
   1.193 +		g_Extensions.BindDependency(myself, &info);
   1.194 +	}
   1.195 +
   1.196 +	if (pIface)
   1.197 +	{
   1.198 +		*pIface = iface;
   1.199 +	}
   1.200 +
   1.201 +	return true;
   1.202 +}
   1.203 +
   1.204 +void ShareSystem::AddNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   1.205 +{
   1.206 +	g_Extensions.AddNatives(myself, natives);
   1.207 +}
   1.208 +
   1.209 +void ShareSystem::DestroyIdentity(IdentityToken_t *identity)
   1.210 +{
   1.211 +	HandleSecurity sec;
   1.212 +
   1.213 +	sec.pOwner = GetIdentRoot();
   1.214 +	sec.pIdentity = GetIdentRoot();
   1.215 +
   1.216 +	g_HandleSys.FreeHandle(identity->ident, &sec);
   1.217 +	delete identity;
   1.218 +}
   1.219 +
   1.220 +void ShareSystem::DestroyIdentType(IdentityType_t type)
   1.221 +{
   1.222 +	g_HandleSys.RemoveType(type, GetIdentRoot());
   1.223 +}
   1.224 +
   1.225 +void ShareSystem::RemoveInterfaces(IExtension *pExtension)
   1.226 +{
   1.227 +	List<IfaceInfo>::iterator iter = m_Interfaces.begin();
   1.228 +
   1.229 +	while (iter != m_Interfaces.end())
   1.230 +	{
   1.231 +		if ((*iter).owner == pExtension)
   1.232 +		{
   1.233 +			iter = m_Interfaces.erase(iter);
   1.234 +		} else {
   1.235 +			iter++;
   1.236 +		}
   1.237 +	}
   1.238 +}
   1.239 +
   1.240 +void ShareSystem::AddDependency(IExtension *myself, const char *filename, bool require, bool autoload)
   1.241 +{
   1.242 +	g_Extensions.AddDependency(myself, filename, require, autoload);
   1.243 +}
   1.244 +
   1.245 +void ShareSystem::RegisterLibrary(IExtension *myself, const char *name)
   1.246 +{
   1.247 +	g_Extensions.AddLibrary(myself, name);
   1.248 +}
   1.249 +
   1.250 +void ShareSystem::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
   1.251 +{
   1.252 +	g_Extensions.OverrideNatives(myself, natives);
   1.253 +}