core/systems/ExtensionSys.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 01 18:04:37 2007 +0000 (2007-10-01)
changeset 1508 398538bce59b
parent 1391 ae2ae57ccccf
child 1541 e46ad208b9b4
permissions -rw-r--r--
- fixed a bug where CS:S Tools could be loaded more than once on mapchange
- fixed a bug where Core did not interpet some file paths properly
- fixed a crash bug where SourceMod could pass invalid data to ISmmAPI on shutdown
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@298
    62
	CExtension(const char *filename, char *error, size_t maxlen);
dvander@298
    63
	~CExtension();
dvander@298
    64
public: //IExtension
dvander@298
    65
	IExtensionInterface *GetAPI();
dvander@298
    66
	const char *GetFilename();
dvander@298
    67
	IdentityToken_t *GetIdentity();
dvander@298
    68
	bool IsLoaded();
dvander@304
    69
	ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface);
dvander@304
    70
	bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface);
dvander@304
    71
	void FreeDependencyIterator(ITERATOR *iter);
dvander@315
    72
	bool IsRunning(char *error, size_t maxlength);
dvander@298
    73
public:
dvander@298
    74
	void SetError(const char *error);
dvander@304
    75
	void AddDependency(IfaceInfo *pInfo);
dvander@1150
    76
	void AddChildDependent(CExtension *pOther, SMInterface *iface);
dvander@304
    77
	void AddInterface(SMInterface *pInterface);
dvander@305
    78
	void AddPlugin(IPlugin *pPlugin);
dvander@305
    79
	void RemovePlugin(IPlugin *pPlugin);
dvander@315
    80
	void MarkAllLoaded();
dvander@298
    81
private:
dvander@941
    82
	bool Load(char *error, size_t maxlength);
dvander@941
    83
private:
dvander@298
    84
	IdentityToken_t *m_pIdentToken;
dvander@298
    85
	IExtensionInterface *m_pAPI;
dvander@298
    86
	String m_File;
dvander@941
    87
	String m_Path;
dvander@298
    88
	ILibrary *m_pLib;
dvander@298
    89
	String m_Error;
dvander@1150
    90
	List<IfaceInfo> m_Deps;			/** Dependencies */
dvander@1150
    91
	List<IfaceInfo> m_ChildDeps;	/** Children who might depend on us */
dvander@304
    92
	List<SMInterface *> m_Interfaces;
dvander@305
    93
	List<IPlugin *> m_Plugins;
dvander@315
    94
	List<const sp_nativeinfo_t *> m_Natives;
dvander@1121
    95
	List<WeakNative> m_WeakNatives;
dvander@304
    96
	PluginId m_PlId;
dvander@311
    97
	unsigned int unload_code;
dvander@315
    98
	bool m_FullyLoaded;
dvander@298
    99
};
dvander@298
   100
dvander@298
   101
class CExtensionManager : 
dvander@298
   102
	public IExtensionManager,
dvander@305
   103
	public SMGlobalClass,
dvander@310
   104
	public IPluginsListener,
dvander@310
   105
	public IRootConsoleCommand
dvander@298
   106
{
dvander@1391
   107
public:
dvander@1391
   108
	CExtensionManager();
dvander@1391
   109
	~CExtensionManager();
dvander@298
   110
public: //SMGlobalClass
dvander@298
   111
	void OnSourceModAllInitialized();
dvander@298
   112
	void OnSourceModShutdown();
dvander@298
   113
public: //IExtensionManager
dvander@298
   114
	IExtension *LoadExtension(const char *path, 
dvander@298
   115
		ExtensionLifetime lifetime, 
dvander@298
   116
		char *error,
ds@755
   117
		size_t maxlength);
dvander@298
   118
	bool UnloadExtension(IExtension *pExt);
dvander@298
   119
	IExtension *FindExtensionByFile(const char *file);
dvander@298
   120
	IExtension *FindExtensionByName(const char *ext);
dvander@305
   121
public: //IPluginsListener
dvander@305
   122
	void OnPluginDestroyed(IPlugin *plugin);
dvander@310
   123
public: //IRootConsoleCommand
dvander@310
   124
	void OnRootConsoleCommand(const char *cmd, unsigned int argcount);
dvander@298
   125
public:
dvander@298
   126
	IExtension *LoadAutoExtension(const char *path);
dvander@304
   127
	void BindDependency(IExtension *pOwner, IfaceInfo *pInfo);
dvander@304
   128
	void AddInterface(IExtension *pOwner, SMInterface *pInterface);
dvander@305
   129
	void BindChildPlugin(IExtension *pParent, IPlugin *pPlugin);
dvander@315
   130
	void AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives);
dvander@315
   131
	void BindAllNativesToPlugin(IPlugin *pPlugin);
dvander@315
   132
	void MarkAllLoaded();
dvander@324
   133
	void AddDependency(IExtension *pSource, const char *file, bool required, bool autoload);
dvander@397
   134
	void TryAutoload();
dvander@868
   135
public:
dvander@868
   136
	CExtension *GetExtensionFromIdent(IdentityToken_t *ptr);
dvander@1018
   137
	void Shutdown();
dvander@298
   138
private:
dvander@311
   139
	CExtension *FindByOrder(unsigned int num);
dvander@311
   140
private:
dvander@298
   141
	List<CExtension *> m_Libs;
dvander@1391
   142
	KTrie<sm_extnative_t> m_ExtNatives;
dvander@298
   143
};
dvander@298
   144
dvander@298
   145
extern CExtensionManager g_Extensions;
dvander@298
   146
dvander@298
   147
#endif //_INCLUDE_SOURCEMOD_EXTENSION_SYSTEM_H_