core/ExtensionSys.h
author "David Anderson <dvander@alliedmods.net>
Thu May 14 11:38:50 2009 -0400 (2009-05-14)
changeset 2730 a2ff0a7a1c53
parent 2411 8dfaa3d730b5
child 2778 4a089866eb3e
permissions -rw-r--r--
Moved smn_functions to logic binary.
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
dvander@298
    48
using namespace SourceMod;
dvander@298
    49
using namespace SourceHook;
dvander@298
    50
dvander@1391
    51
class CExtension;
dvander@1391
    52
dvander@1391
    53
struct sm_extnative_t
dvander@1391
    54
{
dvander@1391
    55
	CExtension *owner;
dvander@1391
    56
	const sp_nativeinfo_t *info;
dvander@1391
    57
};
dvander@1391
    58
dvander@2086
    59
class CExtension : 
dvander@2086
    60
	public IExtension,
dvander@2086
    61
	public CNativeOwner
dvander@298
    62
{
dvander@304
    63
	friend class CExtensionManager;
dvander@298
    64
public:
dvander@1648
    65
	virtual ~CExtension();
dvander@298
    66
public: //IExtension
dvander@298
    67
	IExtensionInterface *GetAPI();
dvander@298
    68
	const char *GetFilename();
dvander@298
    69
	IdentityToken_t *GetIdentity();
dvander@304
    70
	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
dvander@304
    71
	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
dvander@304
    72
	void FreeDependencyIterator(ITERATOR *iter);
dvander@315
    73
	bool IsRunning(char *error, size_t maxlength);
dvander@298
    74
public:
dvander@298
    75
	void SetError(const char *error);
dvander@2134
    76
	void AddDependency(const IfaceInfo *pInfo);
dvander@1150
    77
	void AddChildDependent(CExtension *pOther, SMInterface *iface);
dvander@304
    78
	void AddInterface(SMInterface *pInterface);
dvander@2086
    79
	void AddPlugin(CPlugin *pPlugin);
dvander@315
    80
	void MarkAllLoaded();
dvander@1572
    81
	void AddLibrary(const char *library);
dvander@1648
    82
public:
dvander@1648
    83
	virtual bool Load(char *error, size_t maxlength);
dvander@1648
    84
	virtual bool IsLoaded() =0;
dvander@1648
    85
	virtual void Unload() =0;
dvander@1648
    86
protected:
dvander@1648
    87
	void Initialize(const char *filename, const char *path);
dvander@1648
    88
	bool PerformAPICheck(char *error, size_t maxlength);
dvander@1648
    89
	void CreateIdentity();
dvander@1648
    90
	void DestroyIdentity();
dvander@1648
    91
protected:
dvander@298
    92
	IdentityToken_t *m_pIdentToken;
dvander@298
    93
	IExtensionInterface *m_pAPI;
dvander@298
    94
	String m_File;
dvander@941
    95
	String m_Path;
dvander@298
    96
	String m_Error;
dvander@1150
    97
	List<IfaceInfo> m_Deps;			/** Dependencies */
dvander@1150
    98
	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
dvander@304
    99
	List<SMInterface *> m_Interfaces;
dvander@1572
   100
	List<String> m_Libraries;
dvander@1648
   101
	unsigned int unload_code;
dvander@1648
   102
	bool m_bFullyLoaded;
dvander@1648
   103
};
dvander@1648
   104
dvander@1648
   105
class CLocalExtension : public CExtension
dvander@1648
   106
{
dvander@1648
   107
public:
dvander@1648
   108
	CLocalExtension(const char *filename);
dvander@1648
   109
public:
dvander@1648
   110
	bool Load(char *error, size_t maxlength);
dvander@1648
   111
	bool IsLoaded();
dvander@1648
   112
	void Unload();
dvander@1648
   113
	bool IsExternal();
dvander@1648
   114
private:
dvander@304
   115
	PluginId m_PlId;
dvander@1648
   116
	ILibrary *m_pLib;
dvander@1648
   117
};
dvander@1648
   118
dvander@1648
   119
class CRemoteExtension : public CExtension
dvander@1648
   120
{
dvander@1648
   121
public:
dvander@1648
   122
	CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path);
dvander@1648
   123
public:
dvander@1648
   124
	bool Load(char *error, size_t maxlength);
dvander@1648
   125
	bool IsLoaded();
dvander@1648
   126
	void Unload();
dvander@1648
   127
	bool IsExternal();
dvander@298
   128
};
dvander@298
   129
dvander@298
   130
class CExtensionManager : 
dvander@298
   131
	public IExtensionManager,
dvander@305
   132
	public SMGlobalClass,
dvander@310
   133
	public IPluginsListener,
dvander@310
   134
	public IRootConsoleCommand
dvander@298
   135
{
dvander@1391
   136
public:
dvander@1391
   137
	CExtensionManager();
dvander@1391
   138
	~CExtensionManager();
dvander@298
   139
public: //SMGlobalClass
dvander@298
   140
	void OnSourceModAllInitialized();
dvander@298
   141
	void OnSourceModShutdown();
dvander@298
   142
public: //IExtensionManager
dvander@298
   143
	IExtension *LoadExtension(const char *path, 
dvander@298
   144
		char *error,
ds@755
   145
		size_t maxlength);
dvander@298
   146
	bool UnloadExtension(IExtension *pExt);
dvander@298
   147
	IExtension *FindExtensionByFile(const char *file);
dvander@298
   148
	IExtension *FindExtensionByName(const char *ext);
dvander@1648
   149
	IExtension *LoadExternal(IExtensionInterface *pInterface,
dvander@1648
   150
		const char *filepath,
dvander@1648
   151
		const char *filename,
dvander@1648
   152
		char *error,
dvander@1648
   153
		size_t maxlength);
dvander@305
   154
public: //IPluginsListener
dvander@305
   155
	void OnPluginDestroyed(IPlugin *plugin);
dvander@310
   156
public: //IRootConsoleCommand
faluco@1541
   157
	void OnRootConsoleCommand(const char *cmdname, const CCommand &command);
dvander@298
   158
public:
dvander@298
   159
	IExtension *LoadAutoExtension(const char *path);
dvander@304
   160
	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
dvander@304
   161
	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
dvander@2086
   162
	void BindChildPlugin(IExtension *pParent, CPlugin *pPlugin);
dvander@315
   163
	void MarkAllLoaded();
dvander@324
   164
	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
dvander@397
   165
	void TryAutoload();
dvander@1572
   166
	void AddLibrary(IExtension *pSource, const char *library);
dvander@1573
   167
	bool LibraryExists(const char *library);
faluco@1993
   168
	void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax);
dvander@2134
   169
	void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface);
dvander@868
   170
public:
dvander@868
   171
	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
dvander@1018
   172
	void Shutdown();
dvander@2086
   173
	CNativeOwner *GetNativeOwner(IExtension *pExt)
dvander@2086
   174
	{
dvander@2086
   175
		CExtension *p = (CExtension *)pExt;
dvander@2086
   176
		return p;
dvander@2086
   177
	}
dvander@298
   178
private:
dvander@311
   179
	CExtension *FindByOrder(unsigned int num);
dvander@311
   180
private:
dvander@298
   181
	List<CExtension *> m_Libs;
dvander@298
   182
};
dvander@298
   183
dvander@298
   184
extern CExtensionManager g_Extensions;
dvander@298
   185
dvander@298
   186
#endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_