core/ExtensionSys.h
author Nicholas Hastings <skamonkey@gmail.com>
Fri Nov 04 10:01:33 2011 -0400 (2011-11-04)
changeset 3438 07a1b8a8a2ba
parent 3402 a459183ce707
child 3511 c9fd86b6bec3
permissions -rw-r--r--
Updated TF2_OnIsHolidayActive ret behavior to match doc (bug 5155, r=fyren).
     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 	const char *GetPath() const;
    70 	IdentityToken_t *GetIdentity();
    71 	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
    72 	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
    73 	void FreeDependencyIterator(ITERATOR *iter);
    74 	bool IsRunning(char *error, size_t maxlength);
    75 public:
    76 	void SetError(const char *error);
    77 	void AddDependency(const IfaceInfo *pInfo);
    78 	void AddChildDependent(CExtension *pOther, SMInterface *iface);
    79 	void AddInterface(SMInterface *pInterface);
    80 	void AddPlugin(CPlugin *pPlugin);
    81 	void MarkAllLoaded();
    82 	void AddLibrary(const char *library);
    83 public:
    84 	virtual bool Load(char *error, size_t maxlength);
    85 	virtual bool IsLoaded() =0;
    86 	virtual void Unload() =0;
    87 	virtual bool Reload(char *error, size_t maxlength) =0;
    88 	virtual bool IsSameFile(const char* file) =0;
    89 protected:
    90 	void Initialize(const char *filename, const char *path);
    91 	bool PerformAPICheck(char *error, size_t maxlength);
    92 	void CreateIdentity();
    93 	void DestroyIdentity();
    94 protected:
    95 	IdentityToken_t *m_pIdentToken;
    96 	IExtensionInterface *m_pAPI;
    97 	String m_File;
    98 	String m_RealFile;
    99 	String m_Path;
   100 	String m_Error;
   101 	List<IfaceInfo> m_Deps;			/** Dependencies */
   102 	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
   103 	List<SMInterface *> m_Interfaces;
   104 	List<String> m_Libraries;
   105 	unsigned int unload_code;
   106 	bool m_bFullyLoaded;
   107 };
   108 
   109 class CLocalExtension : public CExtension
   110 {
   111 public:
   112 	CLocalExtension(const char *filename);
   113 public:
   114 	bool Load(char *error, size_t maxlength);
   115 	bool IsLoaded();
   116 	void Unload();
   117 	bool Reload(char *error, size_t maxlength);
   118 	bool IsExternal();
   119 	bool IsSameFile(const char *file);
   120 private:
   121 	PluginId m_PlId;
   122 	ILibrary *m_pLib;
   123 };
   124 
   125 class CRemoteExtension : public CExtension
   126 {
   127 public:
   128 	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
   129 public:
   130 	bool Load(char *error, size_t maxlength);
   131 	bool IsLoaded();
   132 	void Unload();
   133 	bool Reload(char *error, size_t maxlength);
   134 	bool IsExternal();
   135 	bool IsSameFile(const char *file);
   136 };
   137 
   138 class CExtensionManager : 
   139 	public IExtensionManager,
   140 	public SMGlobalClass,
   141 	public IPluginsListener,
   142 	public IRootConsoleCommand
   143 {
   144 public:
   145 	CExtensionManager();
   146 	~CExtensionManager();
   147 public: //SMGlobalClass
   148 	void OnSourceModAllInitialized();
   149 	void OnSourceModShutdown();
   150 public: //IExtensionManager
   151 	IExtension *LoadExtension(const char *path, 
   152 		char *error,
   153 		size_t maxlength);
   154 	bool UnloadExtension(IExtension *pExt);
   155 	IExtension *FindExtensionByFile(const char *file);
   156 	IExtension *FindExtensionByName(const char *ext);
   157 	IExtension *LoadExternal(IExtensionInterface *pInterface,
   158 		const char *filepath,
   159 		const char *filename,
   160 		char *error,
   161 		size_t maxlength);
   162 public: //IPluginsListener
   163 	void OnPluginDestroyed(IPlugin *plugin);
   164 public: //IRootConsoleCommand
   165 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   166 public:
   167 	IExtension *LoadAutoExtension(const char *path, bool bErrorOnMissing=true);
   168 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   169 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   170 	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
   171 	void MarkAllLoaded();
   172 	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
   173 	void TryAutoload();
   174 	void AddLibrary(IExtension *pSource, const char *library);
   175 	bool LibraryExists(const char *library);
   176 	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
   177 	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
   178 public:
   179 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   180 	void Shutdown();
   181 	CNativeOwner *GetNativeOwner(IExtension *pExt)
   182 	{
   183 		CExtension *p = (CExtension *)pExt;
   184 		return p;
   185 	}
   186 private:
   187 	CExtension *FindByOrder(unsigned int num);
   188 private:
   189 	List<CExtension *> m_Libs;
   190 };
   191 
   192 extern CExtensionManager g_Extensions;
   193 
   194 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_