core/systems/ExtensionSys.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 17:55:02 2007 +0000 (2007-10-14)
changeset 1545 4ceff5fa5fdd
parent 1541 e46ad208b9b4
child 1572 adbc41dc9cd0
permissions -rw-r--r--
fixed compat wrappers including core stuff
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 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 
    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 : public IExtension
    59 {
    60 	friend class CExtensionManager;
    61 public:
    62 	CExtension(const char *filename, char *error, size_t maxlen);
    63 	~CExtension();
    64 public: //IExtension
    65 	IExtensionInterface *GetAPI();
    66 	const char *GetFilename();
    67 	IdentityToken_t *GetIdentity();
    68 	bool IsLoaded();
    69 	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
    70 	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
    71 	void FreeDependencyIterator(ITERATOR *iter);
    72 	bool IsRunning(char *error, size_t maxlength);
    73 public:
    74 	void SetError(const char *error);
    75 	void AddDependency(IfaceInfo *pInfo);
    76 	void AddChildDependent(CExtension *pOther, SMInterface *iface);
    77 	void AddInterface(SMInterface *pInterface);
    78 	void AddPlugin(IPlugin *pPlugin);
    79 	void RemovePlugin(IPlugin *pPlugin);
    80 	void MarkAllLoaded();
    81 private:
    82 	bool Load(char *error, size_t maxlength);
    83 private:
    84 	IdentityToken_t *m_pIdentToken;
    85 	IExtensionInterface *m_pAPI;
    86 	String m_File;
    87 	String m_Path;
    88 	ILibrary *m_pLib;
    89 	String m_Error;
    90 	List<IfaceInfo> m_Deps;			/** Dependencies */
    91 	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
    92 	List<SMInterface *> m_Interfaces;
    93 	List<IPlugin *> m_Plugins;
    94 	List<const sp_nativeinfo_t *> m_Natives;
    95 	List<WeakNative> m_WeakNatives;
    96 	PluginId m_PlId;
    97 	unsigned int unload_code;
    98 	bool m_FullyLoaded;
    99 };
   100 
   101 class CExtensionManager : 
   102 	public IExtensionManager,
   103 	public SMGlobalClass,
   104 	public IPluginsListener,
   105 	public IRootConsoleCommand
   106 {
   107 public:
   108 	CExtensionManager();
   109 	~CExtensionManager();
   110 public: //SMGlobalClass
   111 	void OnSourceModAllInitialized();
   112 	void OnSourceModShutdown();
   113 public: //IExtensionManager
   114 	IExtension *LoadExtension(const char *path, 
   115 		ExtensionLifetime lifetime, 
   116 		char *error,
   117 		size_t maxlength);
   118 	bool UnloadExtension(IExtension *pExt);
   119 	IExtension *FindExtensionByFile(const char *file);
   120 	IExtension *FindExtensionByName(const char *ext);
   121 public: //IPluginsListener
   122 	void OnPluginDestroyed(IPlugin *plugin);
   123 public: //IRootConsoleCommand
   124 	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
   125 public:
   126 	IExtension *LoadAutoExtension(const char *path);
   127 	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
   128 	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
   129 	void BindChildPlugin(IExtension *pParent, IPlugin *pPlugin);
   130 	void AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives);
   131 	void BindAllNativesToPlugin(IPlugin *pPlugin);
   132 	void MarkAllLoaded();
   133 	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
   134 	void TryAutoload();
   135 public:
   136 	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
   137 	void Shutdown();
   138 private:
   139 	CExtension *FindByOrder(unsigned int num);
   140 private:
   141 	List<CExtension *> m_Libs;
   142 	KTrie<sm_extnative_t> m_ExtNatives;
   143 };
   144 
   145 extern CExtensionManager g_Extensions;
   146 
   147 #endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_