core/logic/ExtensionSys.h
author Scott Ehlert <ds@alliedmods.net>
Sun Feb 09 22:50:20 2014 -0600 (2014-02-09)
changeset 4241 46fdb8cd9883
parent 4069 336e32fe098f
permissions -rw-r--r--
Use == instead of is operator for checking string equality in AMBuild scripts (bug 6043, 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 "common_logic.h"
    40 #include <IPluginSys.h>
    41 #include <IRootConsoleMenu.h>
    42 #include "NativeOwner.h"
    43 #include "ShareSys.h"
    44 
    45 class CPlayer;
    46 
    47 using namespace SourceMod;
    48 using namespace SourceHook;
    49 
    50 class CExtension;
    51 
    52 struct sm_extnative_t
    53 {
    54 	CExtension *owner;
    55 	const sp_nativeinfo_t *info;
    56 };
    57 
    58 class CExtension : 
    59 	public IExtension,
    60 	public CNativeOwner
    61 {
    62 	friend class CExtensionManager;
    63 public:
    64 	virtual ~CExtension();
    65 public: //IExtension
    66 	IExtensionInterface *GetAPI();
    67 	const char *GetFilename();
    68 	const char *GetPath() const;
    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 	int 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 IExtensionSys,
   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, bool bErrorOnMissing=true);
   167 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   168 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   169 	void BindChildPlugin(IExtension *pParent, SMPlugin *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 	const CVector<IExtension *> *ListExtensions();
   178 	void FreeExtensionList(const CVector<IExtension *> *list);
   179 public:
   180 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   181 	void Shutdown();
   182 	CNativeOwner *GetNativeOwner(IExtension *pExt)
   183 	{
   184 		CExtension *p = (CExtension *)pExt;
   185 		return p;
   186 	}
   187 private:
   188 	CExtension *FindByOrder(unsigned int num);
   189 private:
   190 	List<CExtension *> m_Libs;
   191 };
   192 
   193 extern CExtensionManager g_Extensions;
   194 
   195 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_