core/ExtensionSys.h
author Nicholas Hastings <skamonkey@gmail.com>
Sat Jun 02 10:07:25 2012 -0400 (2012-06-02)
changeset 3549 657ea0e26e7a
parent 3511 c9fd86b6bec3
permissions -rw-r--r--
Added missing CS:GO GameRulesProxy gamedata.
     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 class CPlayer;
    49 
    50 using namespace SourceMod;
    51 using namespace SourceHook;
    52 
    53 class CExtension;
    54 
    55 struct sm_extnative_t
    56 {
    57 	CExtension *owner;
    58 	const sp_nativeinfo_t *info;
    59 };
    60 
    61 class CExtension : 
    62 	public IExtension,
    63 	public CNativeOwner
    64 {
    65 	friend class CExtensionManager;
    66 public:
    67 	virtual ~CExtension();
    68 public: //IExtension
    69 	IExtensionInterface *GetAPI();
    70 	const char *GetFilename();
    71 	const char *GetPath() const;
    72 	IdentityToken_t *GetIdentity();
    73 	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
    74 	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
    75 	void FreeDependencyIterator(ITERATOR *iter);
    76 	bool IsRunning(char *error, size_t maxlength);
    77 public:
    78 	void SetError(const char *error);
    79 	void AddDependency(const IfaceInfo *pInfo);
    80 	void AddChildDependent(CExtension *pOther, SMInterface *iface);
    81 	void AddInterface(SMInterface *pInterface);
    82 	void AddPlugin(CPlugin *pPlugin);
    83 	void MarkAllLoaded();
    84 	void AddLibrary(const char *library);
    85 public:
    86 	virtual bool Load(char *error, size_t maxlength);
    87 	virtual bool IsLoaded() =0;
    88 	virtual void Unload() =0;
    89 	virtual bool Reload(char *error, size_t maxlength) =0;
    90 	virtual bool IsSameFile(const char* file) =0;
    91 protected:
    92 	void Initialize(const char *filename, const char *path);
    93 	bool PerformAPICheck(char *error, size_t maxlength);
    94 	void CreateIdentity();
    95 	void DestroyIdentity();
    96 protected:
    97 	IdentityToken_t *m_pIdentToken;
    98 	IExtensionInterface *m_pAPI;
    99 	String m_File;
   100 	String m_RealFile;
   101 	String m_Path;
   102 	String m_Error;
   103 	List<IfaceInfo> m_Deps;			/** Dependencies */
   104 	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
   105 	List<SMInterface *> m_Interfaces;
   106 	List<String> m_Libraries;
   107 	unsigned int unload_code;
   108 	bool m_bFullyLoaded;
   109 };
   110 
   111 class CLocalExtension : public CExtension
   112 {
   113 public:
   114 	CLocalExtension(const char *filename);
   115 public:
   116 	bool Load(char *error, size_t maxlength);
   117 	bool IsLoaded();
   118 	void Unload();
   119 	bool Reload(char *error, size_t maxlength);
   120 	bool IsExternal();
   121 	bool IsSameFile(const char *file);
   122 private:
   123 	PluginId m_PlId;
   124 	ILibrary *m_pLib;
   125 };
   126 
   127 class CRemoteExtension : public CExtension
   128 {
   129 public:
   130 	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
   131 public:
   132 	bool Load(char *error, size_t maxlength);
   133 	bool IsLoaded();
   134 	void Unload();
   135 	bool Reload(char *error, size_t maxlength);
   136 	bool IsExternal();
   137 	bool IsSameFile(const char *file);
   138 };
   139 
   140 class CExtensionManager : 
   141 	public IExtensionManager,
   142 	public SMGlobalClass,
   143 	public IPluginsListener,
   144 	public IRootConsoleCommand
   145 {
   146 public:
   147 	CExtensionManager();
   148 	~CExtensionManager();
   149 public: //SMGlobalClass
   150 	void OnSourceModAllInitialized();
   151 	void OnSourceModShutdown();
   152 public: //IExtensionManager
   153 	IExtension *LoadExtension(const char *path, 
   154 		char *error,
   155 		size_t maxlength);
   156 	bool UnloadExtension(IExtension *pExt);
   157 	IExtension *FindExtensionByFile(const char *file);
   158 	IExtension *FindExtensionByName(const char *ext);
   159 	IExtension *LoadExternal(IExtensionInterface *pInterface,
   160 		const char *filepath,
   161 		const char *filename,
   162 		char *error,
   163 		size_t maxlength);
   164 public: //IPluginsListener
   165 	void OnPluginDestroyed(IPlugin *plugin);
   166 public: //IRootConsoleCommand
   167 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   168 public:
   169 	IExtension *LoadAutoExtension(const char *path, bool bErrorOnMissing=true);
   170 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   171 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   172 	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
   173 	void MarkAllLoaded();
   174 	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
   175 	void TryAutoload();
   176 	void AddLibrary(IExtension *pSource, const char *library);
   177 	bool LibraryExists(const char *library);
   178 	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
   179 	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
   180 public:
   181 	void ListExtensionsToClient(CPlayer *player, const CCommand &args);
   182 public:
   183 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   184 	void Shutdown();
   185 	CNativeOwner *GetNativeOwner(IExtension *pExt)
   186 	{
   187 		CExtension *p = (CExtension *)pExt;
   188 		return p;
   189 	}
   190 private:
   191 	CExtension *FindByOrder(unsigned int num);
   192 private:
   193 	List<CExtension *> m_Libs;
   194 };
   195 
   196 extern CExtensionManager g_Extensions;
   197 
   198 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_