core/ExtensionSys.h
author Fyren <fyrenmoo@gmail.com>
Sun Mar 01 16:41:44 2009 -0500 (2009-03-01)
changeset 2637 ed7b03e3bf16
parent 2411 8dfaa3d730b5
child 2778 4a089866eb3e
permissions -rw-r--r--
Added case insensitivity search to ReplaceString (bug 3639, r=dvander).
     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 protected:
    87 	void Initialize(const char *filename, const char *path);
    88 	bool PerformAPICheck(char *error, size_t maxlength);
    89 	void CreateIdentity();
    90 	void DestroyIdentity();
    91 protected:
    92 	IdentityToken_t *m_pIdentToken;
    93 	IExtensionInterface *m_pAPI;
    94 	String m_File;
    95 	String m_Path;
    96 	String m_Error;
    97 	List<IfaceInfo> m_Deps;			/** Dependencies */
    98 	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
    99 	List<SMInterface *> m_Interfaces;
   100 	List<String> m_Libraries;
   101 	unsigned int unload_code;
   102 	bool m_bFullyLoaded;
   103 };
   104 
   105 class CLocalExtension : public CExtension
   106 {
   107 public:
   108 	CLocalExtension(const char *filename);
   109 public:
   110 	bool Load(char *error, size_t maxlength);
   111 	bool IsLoaded();
   112 	void Unload();
   113 	bool IsExternal();
   114 private:
   115 	PluginId m_PlId;
   116 	ILibrary *m_pLib;
   117 };
   118 
   119 class CRemoteExtension : public CExtension
   120 {
   121 public:
   122 	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
   123 public:
   124 	bool Load(char *error, size_t maxlength);
   125 	bool IsLoaded();
   126 	void Unload();
   127 	bool IsExternal();
   128 };
   129 
   130 class CExtensionManager : 
   131 	public IExtensionManager,
   132 	public SMGlobalClass,
   133 	public IPluginsListener,
   134 	public IRootConsoleCommand
   135 {
   136 public:
   137 	CExtensionManager();
   138 	~CExtensionManager();
   139 public: //SMGlobalClass
   140 	void OnSourceModAllInitialized();
   141 	void OnSourceModShutdown();
   142 public: //IExtensionManager
   143 	IExtension *LoadExtension(const char *path, 
   144 		char *error,
   145 		size_t maxlength);
   146 	bool UnloadExtension(IExtension *pExt);
   147 	IExtension *FindExtensionByFile(const char *file);
   148 	IExtension *FindExtensionByName(const char *ext);
   149 	IExtension *LoadExternal(IExtensionInterface *pInterface,
   150 		const char *filepath,
   151 		const char *filename,
   152 		char *error,
   153 		size_t maxlength);
   154 public: //IPluginsListener
   155 	void OnPluginDestroyed(IPlugin *plugin);
   156 public: //IRootConsoleCommand
   157 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   158 public:
   159 	IExtension *LoadAutoExtension(const char *path);
   160 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   161 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   162 	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
   163 	void MarkAllLoaded();
   164 	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
   165 	void TryAutoload();
   166 	void AddLibrary(IExtension *pSource, const char *library);
   167 	bool LibraryExists(const char *library);
   168 	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
   169 	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
   170 public:
   171 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   172 	void Shutdown();
   173 	CNativeOwner *GetNativeOwner(IExtension *pExt)
   174 	{
   175 		CExtension *p = (CExtension *)pExt;
   176 		return p;
   177 	}
   178 private:
   179 	CExtension *FindByOrder(unsigned int num);
   180 private:
   181 	List<CExtension *> m_Libs;
   182 };
   183 
   184 extern CExtensionManager g_Extensions;
   185 
   186 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_