core/systems/ExtensionSys.h
author David Anderson <dvander@alliedmods.net>
Sun Oct 28 08:02:24 2007 +0000 (2007-10-28)
changeset 1648 3c8256da48b9
parent 1573 979f2ccfee5a
child 1689 451c11ef473a
permissions -rw-r--r--
added experimental extension interface for loading external extensions from mm:s
hard-bumped the extension api
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@370
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@370
    28
 *
dvander@370
    29
 * Version: $Id$
dvander@370
    30
 */
dvander@370
    31
dvander@298
    32
#ifndef _INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_
dvander@298
    33
#define _INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_
dvander@298
    34
dvander@298
    35
#include <IExtensionSys.h>
dvander@298
    36
#include <ILibrarySys.h>
dvander@298
    37
#include <sh_list.h>
dvander@298
    38
#include <sh_string.h>
dvander@1391
    39
#include <sm_trie_tpl.h>
dvander@298
    40
#include "sm_globals.h"
dvander@304
    41
#include "ShareSys.h"
dvander@304
    42
#include <ISmmAPI.h>
dvander@305
    43
#include <IPluginSys.h>
dvander@310
    44
#include <IRootConsoleMenu.h>
faluco@1112
    45
#include "PluginSys.h"
dvander@298
    46
dvander@298
    47
using namespace SourceMod;
dvander@298
    48
using namespace SourceHook;
dvander@298
    49
dvander@1391
    50
class CExtension;
dvander@1391
    51
dvander@1391
    52
struct sm_extnative_t
dvander@1391
    53
{
dvander@1391
    54
	CExtension *owner;
dvander@1391
    55
	const sp_nativeinfo_t *info;
dvander@1391
    56
};
dvander@1391
    57
dvander@298
    58
class CExtension : public IExtension
dvander@298
    59
{
dvander@304
    60
	friend class CExtensionManager;
dvander@298
    61
public:
dvander@1648
    62
	virtual ~CExtension();
dvander@298
    63
public: //IExtension
dvander@298
    64
	IExtensionInterface *GetAPI();
dvander@298
    65
	const char *GetFilename();
dvander@298
    66
	IdentityToken_t *GetIdentity();
dvander@304
    67
	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
dvander@304
    68
	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
dvander@304
    69
	void FreeDependencyIterator(ITERATOR *iter);
dvander@315
    70
	bool IsRunning(char *error, size_t maxlength);
dvander@298
    71
public:
dvander@298
    72
	void SetError(const char *error);
dvander@304
    73
	void AddDependency(IfaceInfo *pInfo);
dvander@1150
    74
	void AddChildDependent(CExtension *pOther, SMInterface *iface);
dvander@304
    75
	void AddInterface(SMInterface *pInterface);
dvander@305
    76
	void AddPlugin(IPlugin *pPlugin);
dvander@305
    77
	void RemovePlugin(IPlugin *pPlugin);
dvander@315
    78
	void MarkAllLoaded();
dvander@1572
    79
	void AddLibrary(const char *library);
dvander@1648
    80
public:
dvander@1648
    81
	virtual bool Load(char *error, size_t maxlength);
dvander@1648
    82
	virtual bool IsLoaded() =0;
dvander@1648
    83
	virtual void Unload() =0;
dvander@1648
    84
protected:
dvander@1648
    85
	void Initialize(const char *filename, const char *path);
dvander@1648
    86
	bool PerformAPICheck(char *error, size_t maxlength);
dvander@1648
    87
	void CreateIdentity();
dvander@1648
    88
	void DestroyIdentity();
dvander@1648
    89
protected:
dvander@298
    90
	IdentityToken_t *m_pIdentToken;
dvander@298
    91
	IExtensionInterface *m_pAPI;
dvander@298
    92
	String m_File;
dvander@941
    93
	String m_Path;
dvander@298
    94
	String m_Error;
dvander@1150
    95
	List<IfaceInfo> m_Deps;			/** Dependencies */
dvander@1150
    96
	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
dvander@304
    97
	List<SMInterface *> m_Interfaces;
dvander@305
    98
	List<IPlugin *> m_Plugins;
dvander@315
    99
	List<const sp_nativeinfo_t *> m_Natives;
dvander@1121
   100
	List<WeakNative> m_WeakNatives;
dvander@1572
   101
	List<String> m_Libraries;
dvander@1648
   102
	unsigned int unload_code;
dvander@1648
   103
	bool m_bFullyLoaded;
dvander@1648
   104
};
dvander@1648
   105
dvander@1648
   106
class CLocalExtension : public CExtension
dvander@1648
   107
{
dvander@1648
   108
public:
dvander@1648
   109
	CLocalExtension(const char *filename);
dvander@1648
   110
public:
dvander@1648
   111
	bool Load(char *error, size_t maxlength);
dvander@1648
   112
	bool IsLoaded();
dvander@1648
   113
	void Unload();
dvander@1648
   114
	bool IsExternal();
dvander@1648
   115
private:
dvander@304
   116
	PluginId m_PlId;
dvander@1648
   117
	ILibrary *m_pLib;
dvander@1648
   118
};
dvander@1648
   119
dvander@1648
   120
class CRemoteExtension : public CExtension
dvander@1648
   121
{
dvander@1648
   122
public:
dvander@1648
   123
	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
dvander@1648
   124
public:
dvander@1648
   125
	bool Load(char *error, size_t maxlength);
dvander@1648
   126
	bool IsLoaded();
dvander@1648
   127
	void Unload();
dvander@1648
   128
	bool IsExternal();
dvander@298
   129
};
dvander@298
   130
dvander@298
   131
class CExtensionManager : 
dvander@298
   132
	public IExtensionManager,
dvander@305
   133
	public SMGlobalClass,
dvander@310
   134
	public IPluginsListener,
dvander@310
   135
	public IRootConsoleCommand
dvander@298
   136
{
dvander@1391
   137
public:
dvander@1391
   138
	CExtensionManager();
dvander@1391
   139
	~CExtensionManager();
dvander@298
   140
public: //SMGlobalClass
dvander@298
   141
	void OnSourceModAllInitialized();
dvander@298
   142
	void OnSourceModShutdown();
dvander@298
   143
public: //IExtensionManager
dvander@298
   144
	IExtension *LoadExtension(const char *path, 
dvander@298
   145
		char *error,
ds@755
   146
		size_t maxlength);
dvander@298
   147
	bool UnloadExtension(IExtension *pExt);
dvander@298
   148
	IExtension *FindExtensionByFile(const char *file);
dvander@298
   149
	IExtension *FindExtensionByName(const char *ext);
dvander@1648
   150
	IExtension *LoadExternal(IExtensionInterface *pInterface,
dvander@1648
   151
		const char *filepath,
dvander@1648
   152
		const char *filename,
dvander@1648
   153
		char *error,
dvander@1648
   154
		size_t maxlength);
dvander@305
   155
public: //IPluginsListener
dvander@305
   156
	void OnPluginDestroyed(IPlugin *plugin);
dvander@310
   157
public: //IRootConsoleCommand
faluco@1541
   158
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@298
   159
public:
dvander@298
   160
	IExtension *LoadAutoExtension(const char *path);
dvander@304
   161
	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
dvander@304
   162
	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
dvander@305
   163
	void BindChildPlugin(IExtension *pParent, IPlugin *pPlugin);
dvander@315
   164
	void AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives);
dvander@315
   165
	void BindAllNativesToPlugin(IPlugin *pPlugin);
dvander@315
   166
	void MarkAllLoaded();
dvander@324
   167
	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
dvander@397
   168
	void TryAutoload();
dvander@1572
   169
	void AddLibrary(IExtension *pSource, const char *library);
dvander@1573
   170
	bool LibraryExists(const char *library);
dvander@868
   171
public:
dvander@868
   172
	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
dvander@1018
   173
	void Shutdown();
dvander@298
   174
private:
dvander@311
   175
	CExtension *FindByOrder(unsigned int num);
dvander@311
   176
private:
dvander@298
   177
	List<CExtension *> m_Libs;
dvander@1391
   178
	KTrie<sm_extnative_t> m_ExtNatives;
dvander@298
   179
};
dvander@298
   180
dvander@298
   181
extern CExtensionManager g_Extensions;
dvander@298
   182
dvander@298
   183
#endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_