core/ExtensionSys.h
author Nicholas Hastings <skamonkey@gmail.com>
Thu Mar 11 17:43:46 2010 -0500 (2010-03-11)
changeset 2937 f5016767bfdf
parent 2782 57d431498ac3
child 3402 a459183ce707
permissions -rw-r--r--
Added missing core voting cvars to sourcemod.cfg (bug 4224, r=pred)
     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_EXTENSION_SYSTEM_H_
    33 #define _INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_
    34 
    35 #include <IExtensionSys.h>
    36 #include <ILibrarySys.h>
    37 #include <sh_list.h>
    38 #include <sh_string.h>
    39 #include <sm_trie_tpl.h>
    40 #include "sm_globals.h"
    41 #include "ShareSys.h"
    42 #include <ISmmAPI.h>
    43 #include <IPluginSys.h>
    44 #include <IRootConsoleMenu.h>
    45 #include "PluginSys.h"
    46 #include "NativeOwner.h"
    47 
    48 using namespace SourceMod;
    49 using namespace SourceHook;
    50 
    51 class CExtension;
    52 
    53 struct sm_extnative_t
    54 {
    55 	CExtension *owner;
    56 	const sp_nativeinfo_t *info;
    57 };
    58 
    59 class CExtension : 
    60 	public IExtension,
    61 	public CNativeOwner
    62 {
    63 	friend class CExtensionManager;
    64 public:
    65 	virtual ~CExtension();
    66 public: //IExtension
    67 	IExtensionInterface *GetAPI();
    68 	const char *GetFilename();
    69 	IdentityToken_t *GetIdentity();
    70 	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
    71 	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
    72 	void FreeDependencyIterator(ITERATOR *iter);
    73 	bool IsRunning(char *error, size_t maxlength);
    74 public:
    75 	void SetError(const char *error);
    76 	void AddDependency(const IfaceInfo *pInfo);
    77 	void AddChildDependent(CExtension *pOther, SMInterface *iface);
    78 	void AddInterface(SMInterface *pInterface);
    79 	void AddPlugin(CPlugin *pPlugin);
    80 	void MarkAllLoaded();
    81 	void AddLibrary(const char *library);
    82 public:
    83 	virtual bool Load(char *error, size_t maxlength);
    84 	virtual bool IsLoaded() =0;
    85 	virtual void Unload() =0;
    86 	virtual bool Reload(char *error, size_t maxlength) =0;
    87 	virtual bool IsSameFile(const char* file) =0;
    88 protected:
    89 	void Initialize(const char *filename, const char *path);
    90 	bool PerformAPICheck(char *error, size_t maxlength);
    91 	void CreateIdentity();
    92 	void DestroyIdentity();
    93 protected:
    94 	IdentityToken_t *m_pIdentToken;
    95 	IExtensionInterface *m_pAPI;
    96 	String m_File;
    97 	String m_RealFile;
    98 	String m_Path;
    99 	String m_Error;
   100 	List<IfaceInfo> m_Deps;			/** Dependencies */
   101 	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
   102 	List<SMInterface *> m_Interfaces;
   103 	List<String> m_Libraries;
   104 	unsigned int unload_code;
   105 	bool m_bFullyLoaded;
   106 };
   107 
   108 class CLocalExtension : public CExtension
   109 {
   110 public:
   111 	CLocalExtension(const char *filename);
   112 public:
   113 	bool Load(char *error, size_t maxlength);
   114 	bool IsLoaded();
   115 	void Unload();
   116 	bool Reload(char *error, size_t maxlength);
   117 	bool IsExternal();
   118 	bool IsSameFile(const char *file);
   119 private:
   120 	PluginId m_PlId;
   121 	ILibrary *m_pLib;
   122 };
   123 
   124 class CRemoteExtension : public CExtension
   125 {
   126 public:
   127 	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
   128 public:
   129 	bool Load(char *error, size_t maxlength);
   130 	bool IsLoaded();
   131 	void Unload();
   132 	bool Reload(char *error, size_t maxlength);
   133 	bool IsExternal();
   134 	bool IsSameFile(const char *file);
   135 };
   136 
   137 class CExtensionManager : 
   138 	public IExtensionManager,
   139 	public SMGlobalClass,
   140 	public IPluginsListener,
   141 	public IRootConsoleCommand
   142 {
   143 public:
   144 	CExtensionManager();
   145 	~CExtensionManager();
   146 public: //SMGlobalClass
   147 	void OnSourceModAllInitialized();
   148 	void OnSourceModShutdown();
   149 public: //IExtensionManager
   150 	IExtension *LoadExtension(const char *path, 
   151 		char *error,
   152 		size_t maxlength);
   153 	bool UnloadExtension(IExtension *pExt);
   154 	IExtension *FindExtensionByFile(const char *file);
   155 	IExtension *FindExtensionByName(const char *ext);
   156 	IExtension *LoadExternal(IExtensionInterface *pInterface,
   157 		const char *filepath,
   158 		const char *filename,
   159 		char *error,
   160 		size_t maxlength);
   161 public: //IPluginsListener
   162 	void OnPluginDestroyed(IPlugin *plugin);
   163 public: //IRootConsoleCommand
   164 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   165 public:
   166 	IExtension *LoadAutoExtension(const char *path);
   167 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   168 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   169 	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
   170 	void MarkAllLoaded();
   171 	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
   172 	void TryAutoload();
   173 	void AddLibrary(IExtension *pSource, const char *library);
   174 	bool LibraryExists(const char *library);
   175 	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
   176 	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
   177 public:
   178 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   179 	void Shutdown();
   180 	CNativeOwner *GetNativeOwner(IExtension *pExt)
   181 	{
   182 		CExtension *p = (CExtension *)pExt;
   183 		return p;
   184 	}
   185 private:
   186 	CExtension *FindByOrder(unsigned int num);
   187 private:
   188 	List<CExtension *> m_Libs;
   189 };
   190 
   191 extern CExtensionManager g_Extensions;
   192 
   193 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_