core/systems/ExtensionSys.h
author Matt Woodrow <pred@alliedmods.net>
Tue Mar 11 23:52:56 2008 +0000 (2008-03-11)
changeset 1921 f993e100e5a3
parent 1689 451c11ef473a
child 1947 e73a1e51833b
permissions -rw-r--r--
Fixed bug in GetAdjustedTime()
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@1689
    58
/* Replacement native */
dvander@1689
    59
struct sm_repnative_t
dvander@1689
    60
{
dvander@1689
    61
	CExtension *owner;
dvander@1689
    62
	sp_nativeinfo_t info;
dvander@1689
    63
	SPVM_NATIVE_FUNC original;
dvander@1689
    64
};
dvander@1689
    65
dvander@298
    66
class CExtension : public IExtension
dvander@298
    67
{
dvander@304
    68
	friend class CExtensionManager;
dvander@298
    69
public:
dvander@1648
    70
	virtual ~CExtension();
dvander@298
    71
public: //IExtension
dvander@298
    72
	IExtensionInterface *GetAPI();
dvander@298
    73
	const char *GetFilename();
dvander@298
    74
	IdentityToken_t *GetIdentity();
dvander@304
    75
	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
dvander@304
    76
	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
dvander@304
    77
	void FreeDependencyIterator(ITERATOR *iter);
dvander@315
    78
	bool IsRunning(char *error, size_t maxlength);
dvander@298
    79
public:
dvander@298
    80
	void SetError(const char *error);
dvander@304
    81
	void AddDependency(IfaceInfo *pInfo);
dvander@1150
    82
	void AddChildDependent(CExtension *pOther, SMInterface *iface);
dvander@304
    83
	void AddInterface(SMInterface *pInterface);
dvander@305
    84
	void AddPlugin(IPlugin *pPlugin);
dvander@305
    85
	void RemovePlugin(IPlugin *pPlugin);
dvander@315
    86
	void MarkAllLoaded();
dvander@1572
    87
	void AddLibrary(const char *library);
dvander@1648
    88
public:
dvander@1648
    89
	virtual bool Load(char *error, size_t maxlength);
dvander@1648
    90
	virtual bool IsLoaded() =0;
dvander@1648
    91
	virtual void Unload() =0;
dvander@1648
    92
protected:
dvander@1648
    93
	void Initialize(const char *filename, const char *path);
dvander@1648
    94
	bool PerformAPICheck(char *error, size_t maxlength);
dvander@1648
    95
	void CreateIdentity();
dvander@1648
    96
	void DestroyIdentity();
dvander@1648
    97
protected:
dvander@298
    98
	IdentityToken_t *m_pIdentToken;
dvander@298
    99
	IExtensionInterface *m_pAPI;
dvander@298
   100
	String m_File;
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@305
   106
	List<IPlugin *> m_Plugins;
dvander@315
   107
	List<const sp_nativeinfo_t *> m_Natives;
dvander@1121
   108
	List<WeakNative> m_WeakNatives;
dvander@1689
   109
	List<WeakNative> m_ReplacedNatives;
dvander@1572
   110
	List<String> m_Libraries;
dvander@1648
   111
	unsigned int unload_code;
dvander@1648
   112
	bool m_bFullyLoaded;
dvander@1648
   113
};
dvander@1648
   114
dvander@1648
   115
class CLocalExtension : public CExtension
dvander@1648
   116
{
dvander@1648
   117
public:
dvander@1648
   118
	CLocalExtension(const char *filename);
dvander@1648
   119
public:
dvander@1648
   120
	bool Load(char *error, size_t maxlength);
dvander@1648
   121
	bool IsLoaded();
dvander@1648
   122
	void Unload();
dvander@1648
   123
	bool IsExternal();
dvander@1648
   124
private:
dvander@304
   125
	PluginId m_PlId;
dvander@1648
   126
	ILibrary *m_pLib;
dvander@1648
   127
};
dvander@1648
   128
dvander@1648
   129
class CRemoteExtension : public CExtension
dvander@1648
   130
{
dvander@1648
   131
public:
dvander@1648
   132
	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
dvander@1648
   133
public:
dvander@1648
   134
	bool Load(char *error, size_t maxlength);
dvander@1648
   135
	bool IsLoaded();
dvander@1648
   136
	void Unload();
dvander@1648
   137
	bool IsExternal();
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:
dvander@298
   169
	IExtension *LoadAutoExtension(const char *path);
dvander@304
   170
	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
dvander@304
   171
	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
dvander@305
   172
	void BindChildPlugin(IExtension *pParent, IPlugin *pPlugin);
dvander@315
   173
	void AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives);
dvander@315
   174
	void BindAllNativesToPlugin(IPlugin *pPlugin);
dvander@315
   175
	void MarkAllLoaded();
dvander@324
   176
	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
dvander@397
   177
	void TryAutoload();
dvander@1572
   178
	void AddLibrary(IExtension *pSource, const char *library);
dvander@1573
   179
	bool LibraryExists(const char *library);
dvander@1689
   180
	void OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives);
dvander@868
   181
public:
dvander@868
   182
	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
dvander@1018
   183
	void Shutdown();
dvander@298
   184
private:
dvander@311
   185
	CExtension *FindByOrder(unsigned int num);
dvander@311
   186
private:
dvander@298
   187
	List<CExtension *> m_Libs;
dvander@1689
   188
	List<sm_repnative_t> m_RepNativeList;
dvander@1689
   189
	KTrie<sm_repnative_t> m_RepNatives;
dvander@1391
   190
	KTrie<sm_extnative_t> m_ExtNatives;
dvander@298
   191
};
dvander@298
   192
dvander@298
   193
extern CExtensionManager g_Extensions;
dvander@298
   194
dvander@298
   195
#endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_