public/IShareSys.h
author Fyren <fyrenmoo@gmail.com>
Fri Oct 30 14:34:34 2009 +1300 (2009-10-30)
changeset 2851 87b5dcbedcab
parent 2841 dde3dd0b4b8b
child 3872 4c93f74cae51
permissions -rw-r--r--
Updated GuessSDKVersion
     1 /**
     2  * vim: set ts=4 :
     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 #ifndef _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
    33 #define _INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_
    34 
    35 /**
    36  * @file IShareSys.h
    37  * @brief Defines the Share System, responsible for shared resources and dependencies.
    38  */
    39 
    40 #include <sp_vm_types.h>
    41 
    42 
    43 namespace SourceMod
    44 {
    45 	class IExtension;
    46 	struct IdentityToken_t;
    47 
    48 	/** Forward declaration from IHandleSys.h */
    49 	typedef unsigned int		HandleType_t;
    50 
    51 	/** Forward declaration from IHandleSys.h */
    52 	typedef HandleType_t		IdentityType_t;
    53 
    54 	/**
    55 	 * @brief Types of features.
    56 	 */
    57 	enum FeatureType
    58 	{
    59 		FeatureType_Native,      /**< Native functions for plugins. */
    60 		FeatureType_Capability   /**< Named capabilities. */
    61 	};
    62 
    63 	/**
    64 	 * @brief Feature presence status codes.
    65 	 */
    66 	enum FeatureStatus
    67 	{
    68 		FeatureStatus_Available = 0,  /**< Feature is available for use. */
    69 		FeatureStatus_Unavailable,    /**< Feature is unavailable, but known. */
    70 		FeatureStatus_Unknown         /**< Feature is not known. */
    71 	};
    72 
    73 	/**
    74 	 * @brief Provides a capability feature.
    75 	 */
    76 	class IFeatureProvider
    77 	{
    78 	public:
    79 		/**
    80 		 * @brief Must return whether a capability is present.
    81 		 *
    82 		 * @param type          Feature type (FeatureType_Capability right now).
    83 		 * @param name          Feature name.
    84 		 * @return              Feature status code.
    85 		 */
    86 		virtual FeatureStatus GetFeatureStatus(FeatureType type, const char *name) = 0;
    87 	};
    88 
    89 	/**
    90 	 * @brief Defines the base functionality required by a shared interface.
    91 	 */
    92 	class SMInterface
    93 	{
    94 	public:
    95 		/**
    96 		 * @brief Must return an integer defining the interface's version.
    97 		 */
    98 		virtual unsigned int GetInterfaceVersion() =0;
    99 
   100 		/**
   101 		 * @brief Must return a string defining the interface's unique name.
   102 		 */
   103 		virtual const char *GetInterfaceName() =0;
   104 
   105 		/**
   106 		 * @brief Must return whether the requested version number is backwards compatible.
   107 		 * Note: This can be overridden for breaking changes or custom versioning.
   108 		 * 
   109 		 * @param version		Version number to compare against.
   110 		 * @return				True if compatible, false otherwise.
   111 		 */
   112 		virtual bool IsVersionCompatible(unsigned int version)
   113 		{
   114 			if (version > GetInterfaceVersion())
   115 			{
   116 				return false;
   117 			}
   118 
   119 			return true;
   120 		}
   121 	};
   122 
   123 	/**
   124 	 * @brief Tracks dependencies and fires dependency listeners.
   125 	 */
   126 	class IShareSys
   127 	{
   128 	public:
   129 		/**
   130 		 * @brief Adds an interface to the global interface system.
   131 		 *
   132 		 * @param myself		Object adding this interface, in order to track dependencies.
   133 		 * @param iface			Interface pointer (must be unique).
   134 		 * @return				True on success, false otherwise.
   135 		 */
   136 		virtual bool AddInterface(IExtension *myself, SMInterface *iface) =0;
   137 
   138 		/**
   139 		 * @brief Requests an interface from the global interface system.
   140 		 * If found, the interface's internal reference count will be increased.
   141 		 *
   142 		 * @param iface_name	Interface name.
   143 		 * @param iface_vers	Interface version to attempt to match.
   144 		 * @param myself		Object requesting this interface, in order to track dependencies.
   145 		 * @param pIface		Pointer to store the return value in.
   146 		 */
   147 		virtual bool RequestInterface(const char *iface_name, 
   148 										unsigned int iface_vers,
   149 										IExtension *myself,
   150 										SMInterface **pIface) =0;
   151 
   152 		/**
   153 		 * @brief Adds a list of natives to the global native pool, to be 
   154 		 * bound on plugin load.
   155 		 *
   156 		 * Adding natives does not bind them to any loaded plugins; the 
   157 		 * plugins must be reloaded for new natives to take effect.  
   158 		 * 
   159 		 * @param myself		Identity token of parent object.
   160 		 * @param natives		Array of natives to add.  The last entry in 
   161 		 *						the array must be filled with NULLs to 
   162 		 *						terminate the array.  The array must be static 
   163 		 *						as Core will cache the pointer for the 
   164 		 *						lifetime of the extension.
   165 		 */
   166 		virtual void AddNatives(IExtension *myself, const sp_nativeinfo_t *natives) =0;
   167 
   168 		/**
   169 		 * @brief Creates a new identity type.
   170 		 * NOTE: Module authors should never need to use this.  Due to the current implementation,
   171 		 * there is a hardcoded limit of 15 types.  Core uses up a few, so think carefully!
   172 		 *
   173 		 * @param name			String containing type name.  Must not be empty or NULL.
   174 		 * @return				A new HandleType_t identifier, or 0 on failure.
   175 		 */
   176 		virtual IdentityType_t CreateIdentType(const char *name) =0;
   177 
   178 		/**
   179 		 * @brief Finds an identity type by name.
   180 		 * DEFAULT IDENTITY TYPES:
   181 		 *  "PLUGIN"	- An IPlugin object.
   182 		 *  "MODULE"	- An IModule object.
   183 		 *  "CORE"		- An SMGlobalClass or other singleton.
   184 		 * 
   185 		 * @param name			String containing type name to search for.
   186 		 * @return				A HandleType_t identifier if found, 0 otherwise.
   187 		 */
   188 		virtual IdentityType_t FindIdentType(const char *name) =0;
   189 
   190 		/**
   191 		 * @brief Creates a new identity token.  This token is guaranteed to be 
   192 		 * unique amongst all other open identities.
   193 		 *
   194 		 * @param type			Identity type.
   195 		 * @param ptr			Private data pointer (cannot be NULL).
   196 		 * @return				A new IdentityToken_t pointer, or NULL on failure.
   197 		 */
   198 		virtual IdentityToken_t *CreateIdentity(IdentityType_t type, void *ptr) =0;
   199 
   200 		/** 
   201 		 * @brief Destroys an identity type.  Note that this will delete any identities
   202 		 * that are under this type.  
   203 		 *
   204 		 * @param type			Identity type.
   205 		 */
   206 		virtual void DestroyIdentType(IdentityType_t type) =0;
   207 
   208 		/**
   209 		 * @brief Destroys an identity token.  Any handles being owned by this token, or
   210 		 * any handles being 
   211 		 *
   212 		 * @param identity		Identity to remove.
   213 		 */
   214 		virtual void DestroyIdentity(IdentityToken_t *identity) =0;
   215 
   216 		/**
   217 		 * @brief Requires an extension.  This tells SourceMod that without this extension,
   218 		 * your extension should not be loaded.  The name should not include the ".dll" or
   219 		 * the ".so" part of the file name.
   220 		 *
   221 		 * @param myself		IExtension pointer to yourself.
   222 		 * @param filename		File of extension to require.
   223 		 * @param require		Whether or not this extension is a required dependency.
   224 		 * @param autoload		Whether or not to autoload this extension.
   225 		 */
   226 		virtual void AddDependency(IExtension *myself, const char *filename, bool require, bool autoload) =0;
   227 
   228 		/**
   229 		 * @brief Registers a library name to an extension.
   230 		 *
   231 		 * @param myself		Extension to register library to.
   232 		 * @param name			Library name.
   233 		 */
   234 		virtual void RegisterLibrary(IExtension *myself, const char *name) =0;
   235 
   236 		/**
   237 		 * @brief Adds natives that will override Core natives when called.
   238 		 *
   239 		 * A Core version of each native must exist.  If one does not, then 
   240 		 * Core will simply ignore that entry.  No more than one override 
   241 		 * can exist on a given native.
   242 		 *
   243 		 * Override natives represent a weak coupling.  If the extension is 
   244 		 * unloaded, the native will be re-bound to the Core version.  If 
   245 		 * the extension is loaded after plugins are loaded, the override 
   246 		 * will not take effect until those plugins are reloaded.
   247 		 * 
   248 		 * @param myself		Identity token of parent object.
   249 		 * @param natives		Array of natives to add.  The last entry in 
   250 		 *						the array must be filled with NULLs to 
   251 		 *						terminate the array.  The array must be static 
   252 		 *						as Core will cache the pointer for the 
   253 		 *						lifetime of the extension.
   254 		 */
   255 		virtual void OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives) =0;
   256 
   257 		/**
   258 		 * @brief Adds a capability provider. Feature providers are used by
   259 		 * plugins to determine if a feature exists at runtime. This is
   260 		 * distinctly different from checking for a native, because natives
   261 		 * may be backed by underlying functionality which is not available.
   262 		 *
   263 		 * @param myself        Extension.
   264 		 * @param provider      Feature provider implementation.
   265 		 * @param name          Capibility name.
   266 		 */
   267 		virtual void AddCapabilityProvider(IExtension *myself,
   268 		                                   IFeatureProvider *provider,
   269 		                                   const char *name) =0;
   270 
   271 		/**
   272 		 * @brief Drops a previously added cap provider.
   273 		 *
   274 		 * @param myself        Extension.
   275 		 * @param provider      Feature provider implementation.
   276 		 * @param name          Capibility name.
   277 		 */
   278 		virtual void DropCapabilityProvider(IExtension *myself,
   279 		                                    IFeatureProvider *provider,
   280 		                                    const char *name) =0;
   281 	};
   282 }
   283 
   284 #endif //_INCLUDE_SOURCEMOD_IFACE_SHARE_SYS_H_