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