core/ExtensionSys.h
author Kyle Sanderson <kyle.leet@gmail.com>
Sun Mar 17 21:58:56 2013 +0000 (2013-03-17)
changeset 3837 f86a51ee4eb4
parent 3511 c9fd86b6bec3
permissions -rw-r--r--
Check for cloned handles when dumping (bug 5245, r=asherkin).
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 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@2086
    46
#include "NativeOwner.h"
dvander@298
    47
voidedweasel@3511
    48
class CPlayer;
voidedweasel@3511
    49
dvander@298
    50
using namespace SourceMod;
dvander@298
    51
using namespace SourceHook;
dvander@298
    52
dvander@1391
    53
class CExtension;
dvander@1391
    54
dvander@1391
    55
struct sm_extnative_t
dvander@1391
    56
{
dvander@1391
    57
	CExtension *owner;
dvander@1391
    58
	const sp_nativeinfo_t *info;
dvander@1391
    59
};
dvander@1391
    60
dvander@2086
    61
class CExtension : 
dvander@2086
    62
	public IExtension,
dvander@2086
    63
	public CNativeOwner
dvander@298
    64
{
dvander@304
    65
	friend class CExtensionManager;
dvander@298
    66
public:
dvander@1648
    67
	virtual ~CExtension();
dvander@298
    68
public: //IExtension
dvander@298
    69
	IExtensionInterface *GetAPI();
dvander@298
    70
	const char *GetFilename();
skamonkey@3402
    71
	const char *GetPath() const;
dvander@298
    72
	IdentityToken_t *GetIdentity();
dvander@304
    73
	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
dvander@304
    74
	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
dvander@304
    75
	void FreeDependencyIterator(ITERATOR *iter);
dvander@315
    76
	bool IsRunning(char *error, size_t maxlength);
dvander@298
    77
public:
dvander@298
    78
	void SetError(const char *error);
dvander@2134
    79
	void AddDependency(const IfaceInfo *pInfo);
dvander@1150
    80
	void AddChildDependent(CExtension *pOther, SMInterface *iface);
dvander@304
    81
	void AddInterface(SMInterface *pInterface);
dvander@2086
    82
	void AddPlugin(CPlugin *pPlugin);
dvander@315
    83
	void MarkAllLoaded();
dvander@1572
    84
	void AddLibrary(const char *library);
dvander@1648
    85
public:
dvander@1648
    86
	virtual bool Load(char *error, size_t maxlength);
dvander@1648
    87
	virtual bool IsLoaded() =0;
dvander@1648
    88
	virtual void Unload() =0;
kornholijo@2778
    89
	virtual bool Reload(char *error, size_t maxlength) =0;
dvander@2782
    90
	virtual bool IsSameFile(const char* file) =0;
dvander@1648
    91
protected:
dvander@1648
    92
	void Initialize(const char *filename, const char *path);
dvander@1648
    93
	bool PerformAPICheck(char *error, size_t maxlength);
dvander@1648
    94
	void CreateIdentity();
dvander@1648
    95
	void DestroyIdentity();
dvander@1648
    96
protected:
dvander@298
    97
	IdentityToken_t *m_pIdentToken;
dvander@298
    98
	IExtensionInterface *m_pAPI;
dvander@298
    99
	String m_File;
dvander@2782
   100
	String m_RealFile;
dvander@941
   101
	String m_Path;
dvander@298
   102
	String m_Error;
dvander@1150
   103
	List<IfaceInfo> m_Deps;			/** Dependencies */
dvander@1150
   104
	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
dvander@304
   105
	List<SMInterface *> m_Interfaces;
dvander@1572
   106
	List<String> m_Libraries;
dvander@1648
   107
	unsigned int unload_code;
dvander@1648
   108
	bool m_bFullyLoaded;
dvander@1648
   109
};
dvander@1648
   110
dvander@1648
   111
class CLocalExtension : public CExtension
dvander@1648
   112
{
dvander@1648
   113
public:
dvander@1648
   114
	CLocalExtension(const char *filename);
dvander@1648
   115
public:
dvander@1648
   116
	bool Load(char *error, size_t maxlength);
dvander@1648
   117
	bool IsLoaded();
dvander@1648
   118
	void Unload();
kornholijo@2778
   119
	bool Reload(char *error, size_t maxlength);
dvander@1648
   120
	bool IsExternal();
dvander@2782
   121
	bool IsSameFile(const char *file);
dvander@1648
   122
private:
dvander@304
   123
	PluginId m_PlId;
dvander@1648
   124
	ILibrary *m_pLib;
dvander@1648
   125
};
dvander@1648
   126
dvander@1648
   127
class CRemoteExtension : public CExtension
dvander@1648
   128
{
dvander@1648
   129
public:
dvander@1648
   130
	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
dvander@1648
   131
public:
dvander@1648
   132
	bool Load(char *error, size_t maxlength);
dvander@1648
   133
	bool IsLoaded();
dvander@1648
   134
	void Unload();
kornholijo@2778
   135
	bool Reload(char *error, size_t maxlength);
dvander@1648
   136
	bool IsExternal();
dvander@2782
   137
	bool IsSameFile(const char *file);
dvander@298
   138
};
dvander@298
   139
dvander@298
   140
class CExtensionManager : 
dvander@298
   141
	public IExtensionManager,
dvander@305
   142
	public SMGlobalClass,
dvander@310
   143
	public IPluginsListener,
dvander@310
   144
	public IRootConsoleCommand
dvander@298
   145
{
dvander@1391
   146
public:
dvander@1391
   147
	CExtensionManager();
dvander@1391
   148
	~CExtensionManager();
dvander@298
   149
public: //SMGlobalClass
dvander@298
   150
	void OnSourceModAllInitialized();
dvander@298
   151
	void OnSourceModShutdown();
dvander@298
   152
public: //IExtensionManager
dvander@298
   153
	IExtension *LoadExtension(const char *path, 
dvander@298
   154
		char *error,
ds@755
   155
		size_t maxlength);
dvander@298
   156
	bool UnloadExtension(IExtension *pExt);
dvander@298
   157
	IExtension *FindExtensionByFile(const char *file);
dvander@298
   158
	IExtension *FindExtensionByName(const char *ext);
dvander@1648
   159
	IExtension *LoadExternal(IExtensionInterface *pInterface,
dvander@1648
   160
		const char *filepath,
dvander@1648
   161
		const char *filename,
dvander@1648
   162
		char *error,
dvander@1648
   163
		size_t maxlength);
dvander@305
   164
public: //IPluginsListener
dvander@305
   165
	void OnPluginDestroyed(IPlugin *plugin);
dvander@310
   166
public: //IRootConsoleCommand
faluco@1541
   167
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@298
   168
public:
skamonkey@3402
   169
	IExtension *LoadAutoExtension(const char *path, bool bErrorOnMissing=true);
dvander@304
   170
	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
dvander@304
   171
	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
dvander@2086
   172
	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
dvander@315
   173
	void MarkAllLoaded();
dvander@324
   174
	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
dvander@397
   175
	void TryAutoload();
dvander@1572
   176
	void AddLibrary(IExtension *pSource, const char *library);
dvander@1573
   177
	bool LibraryExists(const char *library);
faluco@1993
   178
	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
dvander@2134
   179
	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
dvander@868
   180
public:
voidedweasel@3511
   181
	void ListExtensionsToClient(CPlayer *player, const CCommand &args);
voidedweasel@3511
   182
public:
dvander@868
   183
	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
dvander@1018
   184
	void Shutdown();
dvander@2086
   185
	CNativeOwner *GetNativeOwner(IExtension *pExt)
dvander@2086
   186
	{
dvander@2086
   187
		CExtension *p = (CExtension *)pExt;
dvander@2086
   188
		return p;
dvander@2086
   189
	}
dvander@298
   190
private:
dvander@311
   191
	CExtension *FindByOrder(unsigned int num);
dvander@311
   192
private:
dvander@298
   193
	List<CExtension *> m_Libs;
dvander@298
   194
};
dvander@298
   195
dvander@298
   196
extern CExtensionManager g_Extensions;
dvander@298
   197
dvander@298
   198
#endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_