core/systems/PluginSys.h
author David Anderson <dvander@alliedmods.net>
Tue Jun 26 17:08:07 2007 +0000 (2007-06-26)
changeset 1018 e466e7319a68
parent 978 54782a83a1ef
child 1089 275107f9ca95
permissions -rw-r--r--
- sourcemod can now unload properly without crashing
- extensions now unload, dependencies are resolved
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
dvander@370
     3
 * ===============================================================
dvander@370
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     5
 * ===============================================================
dvander@370
     6
 *
dvander@370
     7
 * This file is not open source and may not be copied without explicit
dvander@370
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     9
 * in whole or significant part.
dvander@370
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    11
 *
dvander@370
    12
 * Version: $Id$
dvander@370
    13
 */
dvander@370
    14
dvander@173
    15
#ifndef _INCLUDE_SOURCEMOD_PLUGINSYSTEM_H_
dvander@173
    16
#define _INCLUDE_SOURCEMOD_PLUGINSYSTEM_H_
dvander@173
    17
dvander@211
    18
#include <time.h>
faluco@333
    19
#include <errno.h>
faluco@333
    20
#include <sys/stat.h>
dvander@173
    21
#include <IPluginSys.h>
dvander@220
    22
#include <IHandleSys.h>
ds@611
    23
#include <IForwardSys.h>
dvander@174
    24
#include <sh_list.h>
dvander@174
    25
#include <sh_stack.h>
faluco@389
    26
#include <sh_vector.h>
dvander@601
    27
#include <sh_string.h>
dvander@173
    28
#include "sm_globals.h"
dvander@262
    29
#include "vm/sp_vm_basecontext.h"
dvander@203
    30
#include "PluginInfoDatabase.h"
dvander@211
    31
#include "sm_trie.h"
dvander@220
    32
#include "sourcemod.h"
dvander@309
    33
#include <IRootConsoleMenu.h>
ds@457
    34
#include "convar_sm.h"
dvander@173
    35
dvander@174
    36
using namespace SourceHook;
dvander@174
    37
dvander@209
    38
/**
dvander@209
    39
 * NOTES:
dvander@209
    40
 *
dvander@209
    41
 *  Currently this system needs a lot of work but it's good skeletally.  Plugin creation 
dvander@209
    42
 * is done without actually compiling anything.  This is done by Load functions in the 
dvander@209
    43
 * manager.  This will need a rewrite when we add context switching.
dvander@209
    44
 *
dvander@209
    45
 *  The plugin object itself has a few things to note.  The most important is that it stores
dvander@209
    46
 * a table of function objects.  The manager marshals allocation and freeing of these objects.
dvander@209
    47
 * The plugin object can be in erroneous states, they are:
dvander@209
    48
 *   Plugin_Error   --> Some error occurred any time during or after compilation.
dvander@209
    49
 *						This error can be cleared since the plugin itself is valid.
dvander@209
    50
 *						However, the state itself being set prevents any runtime action.
dvander@209
    51
 *   Plugin_BadLoad	--> The plugin failed to load entirely and nothing can be done to save it.
dvander@209
    52
 *
dvander@209
    53
 *  If a plugin fails to load externally, it is never added to the internal tracker.  However, 
dvander@209
    54
 * plugins that failed to load from the internal loading mechanism are always tracked.  This 
dvander@209
    55
 * allows users to see which automatically loaded plugins failed, and makes the interface a bit
dvander@209
    56
 * more flexible.
dvander@211
    57
 *
dvander@211
    58
 *  Once a plugin is compiled, it sets its own state to Plugin_Created.  This state is still invalid
dvander@211
    59
 * for execution.  SourceMod is a two pass system, and even though the second pass is not implemented
dvander@211
    60
 * yet, it is structured so Plugin_Created must be switched to Plugin_Running in the second pass.  When
dvander@211
    61
 * implemented, a Created plugin will be switched to Error in the second pass if it not loadable.
dvander@211
    62
 *
dvander@211
    63
 *  The two pass loading mechanism is described below.  Modules/natives are not implemented yet.
dvander@211
    64
 * PASS ONE: All loadable plugins are found and have the following steps performed:
dvander@211
    65
 *			 1.  Loading and compilation is attempted.
dvander@211
    66
 *			 2.  If successful, all natives from Core are added.
dvander@211
    67
 *			 3.  OnPluginLoad() is called.
dvander@211
    68
 *			 4.  If failed, any user natives are scrapped and the process halts here.
dvander@211
    69
 *			 5.  If successful, the plugin is ready for Pass 2.
dvander@211
    70
 * INTERMEDIATE:
dvander@211
    71
 *			 1.  All forced modules are loaded.
dvander@211
    72
 * PASS TWO: All loaded plugins are found and have these steps performed:
dvander@211
    73
 *			 1. Any modules referenced in the plugin that are not already loaded, are loaded.
dvander@211
    74
 *			 2. If any module fails to load and the plugin requires it, load fails and jump to step 6.
dvander@211
    75
 *			 3. If any natives are unresolved, check if they are found in the user-natives pool.
dvander@211
    76
 *			 4. If yes, load succeeds.  If not, natives are passed through a native acceptance filter.
dvander@211
    77
 *			 5. If the filter fails, the plugin is marked as failed.
dvander@211
    78
 *			 6. If the plugin has failed to load at this point, any dynamic natives it has added are scrapped.
dvander@211
    79
 *			    Furthermore, any plugin that referenced these natives must now have pass 2 re-ran.
dvander@211
    80
 * PASS THREE (not a real pass):
ds@426
    81
 *			 7. Once all plugins are deemed to be loaded, OnPluginStart() is called
dvander@209
    82
 */
dvander@209
    83
dvander@174
    84
#define SM_CONTEXTVAR_MYSELF	0
dvander@174
    85
dvander@173
    86
struct ContextPair
dvander@173
    87
{
dvander@209
    88
	ContextPair() : base(NULL), ctx(NULL), co(NULL)
dvander@173
    89
	{
dvander@173
    90
	};
dvander@262
    91
	BaseContext *base;
dvander@173
    92
	sp_context_t *ctx;
dvander@209
    93
	ICompilation *co;
dvander@209
    94
	IVirtualMachine *vm;
dvander@173
    95
};
dvander@173
    96
dvander@605
    97
struct FakeNative
dvander@605
    98
{
dvander@605
    99
	IPluginContext *ctx;
dvander@605
   100
	IPluginFunction *call;
dvander@605
   101
	String name;
dvander@605
   102
	SPVM_NATIVE_FUNC func;
dvander@605
   103
};
dvander@605
   104
faluco@383
   105
enum LoadRes
faluco@383
   106
{
faluco@383
   107
	LoadRes_Successful,
faluco@383
   108
	LoadRes_AlreadyLoaded,
faluco@383
   109
	LoadRes_Failure
faluco@383
   110
};
faluco@383
   111
dvander@978
   112
struct AutoConfig
dvander@978
   113
{
dvander@978
   114
	String autocfg;
dvander@978
   115
	String folder;
dvander@978
   116
	bool create;
dvander@978
   117
};
dvander@978
   118
dvander@173
   119
class CPlugin : public IPlugin
dvander@173
   120
{
dvander@174
   121
	friend class CPluginManager;
dvander@176
   122
	friend class CFunction;
dvander@173
   123
public:
dvander@209
   124
	CPlugin(const char *file);
dvander@209
   125
	~CPlugin();
dvander@209
   126
public:
faluco@641
   127
	PluginType GetType();
faluco@641
   128
	SourcePawn::IPluginContext *GetBaseContext();
faluco@641
   129
	sp_context_t *GetContext();
faluco@641
   130
	const sm_plugininfo_t *GetPublicInfo();
faluco@641
   131
	const char *GetFilename();
faluco@641
   132
	bool IsDebugging();
faluco@641
   133
	PluginStatus GetStatus();
faluco@641
   134
	bool SetPauseState(bool paused);
faluco@641
   135
	unsigned int GetSerial();
faluco@641
   136
	const sp_plugin_t *GetPluginStructure();
faluco@641
   137
	IdentityToken_t *GetIdentity();
faluco@641
   138
	bool SetProperty(const char *prop, void *ptr);
faluco@641
   139
	bool GetProperty(const char *prop, void **ptr, bool remove=false);
dvander@173
   140
public:
dvander@209
   141
	/**
dvander@209
   142
	 * Creates a plugin object with default values.
dvander@209
   143
	 *   If an error buffer is specified, and an error occurs, the error will be copied to the buffer
dvander@209
   144
	 * and NULL will be returned.
dvander@209
   145
	 *   If an error buffer is not specified, the error will be copied to an internal buffer and 
dvander@209
   146
	 * a valid (but error-stated) CPlugin will be returned.
dvander@209
   147
	 */
dvander@209
   148
	static CPlugin *CreatePlugin(const char *file, char *error, size_t maxlength);
dvander@209
   149
public:
dvander@209
   150
	/**
dvander@209
   151
	 * Starts the initial compilation of a plugin.
dvander@209
   152
	 * Returns false if another compilation exists or there is a current context set.
dvander@209
   153
	 */
dvander@209
   154
	ICompilation *StartMyCompile(IVirtualMachine *vm);
dvander@209
   155
	/** 
dvander@209
   156
	 * Finalizes a compilation.  If error buffer is NULL, the error is saved locally.
dvander@209
   157
	 */
dvander@209
   158
	bool FinishMyCompile(char *error, size_t maxlength);
dvander@209
   159
	void CancelMyCompile();
dvander@229
   160
dvander@209
   161
	/**
dvander@209
   162
	 * Sets an error state on the plugin
dvander@209
   163
	 */
dvander@209
   164
	void SetErrorState(PluginStatus status, const char *error_fmt, ...);
dvander@211
   165
dvander@211
   166
	/**
dvander@229
   167
	 * Initializes the plugin's identity information
dvander@229
   168
	 */
dvander@229
   169
	void InitIdentity();
dvander@229
   170
dvander@229
   171
	/**
dvander@211
   172
	 * Calls the OnPluginLoad function, and sets any failed states if necessary.
dvander@211
   173
	 * NOTE: Valid pre-states are: Plugin_Created
dvander@282
   174
	 * NOTE: If validated, plugin state is changed to Plugin_Loaded
dvander@211
   175
	 *
dvander@211
   176
	 * If the error buffer is NULL, the error message is cached locally.
dvander@211
   177
	 */
dvander@211
   178
	bool Call_AskPluginLoad(char *error, size_t maxlength);
dvander@211
   179
dvander@211
   180
	/**
ds@426
   181
	 * Calls the OnPluginStart function.
dvander@211
   182
	 * NOTE: Valid pre-states are: Plugin_Created
dvander@282
   183
	 * NOTE: Post-state will be Plugin_Running
dvander@211
   184
	 */
ds@426
   185
	void Call_OnPluginStart();
dvander@282
   186
dvander@282
   187
	/**
ds@426
   188
	 * Calls the OnPluginEnd function.
dvander@282
   189
	 */
ds@426
   190
	void Call_OnPluginEnd();
faluco@290
   191
faluco@290
   192
	/**
dvander@315
   193
	 * Toggles debug mode in the plugin
dvander@315
   194
	 */
dvander@309
   195
	bool ToggleDebugMode(bool debug, char *error, size_t maxlength);
faluco@290
   196
faluco@290
   197
	/**
dvander@315
   198
	 * Returns true if a plugin is usable.
dvander@315
   199
	 */
dvander@615
   200
	bool IsRunnable();
faluco@389
   201
faluco@389
   202
	/**
dvander@536
   203
	 * Adds a language file index to the plugin's list.
dvander@536
   204
	 */
faluco@389
   205
	void AddLangFile(unsigned int index);
faluco@389
   206
faluco@389
   207
	/**
dvander@536
   208
	 * Get language file count for this plugin.
dvander@536
   209
	 */
dvander@615
   210
	size_t GetLangFileCount();
faluco@389
   211
faluco@389
   212
	/**
dvander@536
   213
	 * Get language file index based on the vector index.
dvander@536
   214
	 */
dvander@615
   215
	unsigned int GetLangFileByIndex(unsigned int index);
dvander@211
   216
public:
faluco@333
   217
	/**
dvander@536
   218
	 * Returns the modification time during last plugin load.
dvander@536
   219
	 */
dvander@615
   220
	time_t GetTimeStamp();
faluco@333
   221
faluco@333
   222
	/**
dvander@536
   223
	 * Returns the current modification time of the plugin file.
dvander@536
   224
	 */
faluco@333
   225
	time_t GetFileTimeStamp();
dvander@282
   226
dvander@282
   227
	/** 
dvander@282
   228
	 * Returns true if the plugin was running, but is now invalid.
dvander@282
   229
	 */
dvander@282
   230
	bool WasRunning();
dvander@536
   231
dvander@536
   232
	Handle_t GetMyHandle();
dvander@978
   233
dvander@978
   234
	void AddConfig(bool autoCreate, const char *cfg, const char *folder);
dvander@978
   235
	unsigned int GetConfigCount();
dvander@978
   236
	AutoConfig *GetConfig(unsigned int i);
dvander@173
   237
protected:
dvander@173
   238
	void UpdateInfo();
faluco@333
   239
	void SetTimeStamp(time_t t);
dvander@605
   240
	void DependencyDropped(CPlugin *pOwner);
dvander@173
   241
private:
dvander@209
   242
	ContextPair m_ctx;
dvander@174
   243
	PluginType m_type;
ds@652
   244
	char m_filename[PLATFORM_MAX_PATH];
dvander@173
   245
	PluginStatus m_status;
dvander@173
   246
	unsigned int m_serial;
dvander@173
   247
	sm_plugininfo_t m_info;
dvander@173
   248
	sp_plugin_t *m_plugin;
dvander@209
   249
	char m_errormsg[256];
dvander@211
   250
	time_t m_LastAccess;
dvander@229
   251
	IdentityToken_t *m_ident;
dvander@220
   252
	Handle_t m_handle;
dvander@282
   253
	bool m_WasRunning;
faluco@389
   254
	CVector<unsigned int> m_PhraseFiles;
dvander@605
   255
	List<CPlugin *> m_dependents;
dvander@605
   256
	List<CPlugin *> m_dependsOn;
dvander@605
   257
	List<FakeNative *> m_fakeNatives;
dvander@486
   258
	Trie *m_pProps;
dvander@605
   259
	bool m_FakeNativesMissing;
dvander@978
   260
	CVector<AutoConfig *> m_configs;
dvander@601
   261
};
dvander@601
   262
dvander@220
   263
class CPluginManager : 
dvander@220
   264
	public IPluginManager,
dvander@220
   265
	public SMGlobalClass,
dvander@309
   266
	public IHandleTypeDispatch,
dvander@309
   267
	public IRootConsoleCommand
dvander@174
   268
{
dvander@176
   269
	friend class CPlugin;
dvander@174
   270
public:
dvander@174
   271
	CPluginManager();
dvander@176
   272
	~CPluginManager();
dvander@174
   273
public:
dvander@203
   274
	/* Implements iterator class */
dvander@174
   275
	class CPluginIterator : public IPluginIterator
dvander@174
   276
	{
dvander@174
   277
	public:
dvander@213
   278
		CPluginIterator(List<CPlugin *> *mylist);
dvander@174
   279
		virtual ~CPluginIterator();
dvander@174
   280
		virtual bool MorePlugins();
dvander@174
   281
		virtual IPlugin *GetPlugin();
dvander@174
   282
		virtual void NextPlugin();
dvander@282
   283
		void Release();
dvander@174
   284
	public:
dvander@174
   285
		void Reset();
dvander@174
   286
	private:
dvander@213
   287
		List<CPlugin *> *mylist;
dvander@213
   288
		List<CPlugin *>::iterator current;
dvander@174
   289
	};
dvander@174
   290
	friend class CPluginManager::CPluginIterator;
dvander@203
   291
public: //IPluginManager
dvander@282
   292
	IPlugin *LoadPlugin(const char *path, 
dvander@174
   293
								bool debug,
dvander@174
   294
								PluginType type,
dvander@174
   295
								char error[],
ds@755
   296
								size_t maxlength,
faluco@383
   297
								bool *wasloaded);
dvander@282
   298
	bool UnloadPlugin(IPlugin *plugin);
dvander@282
   299
	IPlugin *FindPluginByContext(const sp_context_t *ctx);
dvander@282
   300
	unsigned int GetPluginCount();
dvander@282
   301
	IPluginIterator *GetPluginIterator();
dvander@282
   302
	void AddPluginsListener(IPluginsListener *listener);
dvander@282
   303
	void RemovePluginsListener(IPluginsListener *listener);
dvander@220
   304
public: //SMGlobalClass
dvander@282
   305
	void OnSourceModAllInitialized();
dvander@282
   306
	void OnSourceModShutdown();
dvander@220
   307
public: //IHandleTypeDispatch
dvander@282
   308
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@309
   309
public: //IRootConsoleCommand
dvander@309
   310
	void OnRootConsoleCommand(const char *command, unsigned int argcount);
dvander@190
   311
public:
dvander@203
   312
	/**
dvander@211
   313
	 * Loads all plugins not yet loaded
dvander@203
   314
	 */
dvander@211
   315
	void LoadAll_FirstPass(const char *config, const char *basedir);
dvander@211
   316
dvander@211
   317
	/**
dvander@211
   318
	 * Runs the second loading pass for all plugins
dvander@211
   319
	 */
dvander@211
   320
	void LoadAll_SecondPass();
dvander@203
   321
dvander@203
   322
	/**
dvander@203
   323
	 * Tests a plugin file mask against a local folder.
dvander@203
   324
	 * The alias is searched backwards from localdir - i.e., given this input:
dvander@203
   325
	 *   csdm/ban        csdm/ban
dvander@203
   326
	 *   ban             csdm/ban
dvander@203
   327
	 *   csdm/ban        optional/csdm/ban
dvander@203
   328
	 * All of these will return true for an alias match.  
dvander@203
   329
	 * Wildcards are allowed in the filename.
dvander@203
   330
	 */
dvander@209
   331
	bool TestAliasMatch(const char *alias, const char *localdir);
dvander@211
   332
dvander@211
   333
	/** 
dvander@211
   334
	 * Returns whether anything loaded will be a late load.
dvander@211
   335
	 */
faluco@290
   336
	bool IsLateLoadTime() const;
dvander@233
   337
dvander@233
   338
	/**
dvander@233
   339
	 * Adds natives from core into the native pool.
dvander@233
   340
	 */
dvander@233
   341
	void RegisterNativesFromCore(sp_nativeinfo_t *natives);
dvander@247
   342
dvander@247
   343
	/**
dvander@247
   344
	 * Converts a Handle to an IPlugin if possible.
dvander@247
   345
	 */
dvander@247
   346
	IPlugin *PluginFromHandle(Handle_t handle, HandleError *err);
faluco@290
   347
faluco@290
   348
	/**
dvander@309
   349
	 * Finds a plugin based on its index. (starts on index 1)
dvander@309
   350
	 */
faluco@290
   351
	CPlugin *GetPluginByOrder(int num);
dvander@309
   352
ds@457
   353
	/** 
ds@457
   354
	 * Internal version of FindPluginByContext()
ds@457
   355
	 */
ds@457
   356
	inline CPlugin *GetPluginByCtx(const sp_context_t *ctx)
ds@457
   357
	{
ds@457
   358
		return reinterpret_cast<CPlugin *>(ctx->user[SM_CONTEXTVAR_MYSELF]);
ds@457
   359
	}
ds@457
   360
dvander@309
   361
	/**
dvander@309
   362
	 * Gets status text for a status code 
dvander@309
   363
	 */
dvander@309
   364
	const char *GetStatusText(PluginStatus status);
dvander@315
   365
faluco@333
   366
	/**
faluco@333
   367
	* Reload or update plugins on level shutdown.
faluco@333
   368
	*/
faluco@333
   369
	void ReloadOrUnloadPlugins();
faluco@333
   370
ds@611
   371
	/**
ds@611
   372
	 * Add public functions from all running or paused
ds@611
   373
	 * plugins to the specified forward if the names match.
ds@611
   374
	 */
ds@611
   375
	void AddFunctionsToForward(const char *name, IChangeableForward *pForward);
ds@611
   376
dvander@900
   377
	CPlugin *GetPluginFromIdentity(IdentityToken_t *pToken);
dvander@900
   378
dvander@1018
   379
	void Shutdown();
dvander@1018
   380
dvander@209
   381
private:
ds@755
   382
	LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength);
dvander@298
   383
dvander@298
   384
	void LoadAutoPlugin(const char *plugin);
dvander@298
   385
dvander@209
   386
	/**
dvander@209
   387
	 * Recursively loads all plugins in the given directory.
dvander@209
   388
	 */
dvander@209
   389
	void LoadPluginsFromDir(const char *basedir, const char *localdir);
dvander@209
   390
dvander@209
   391
	/**
dvander@211
   392
	 * Adds a plugin object.  This is wrapped by LoadPlugin functions.
dvander@211
   393
	 */
dvander@211
   394
	void AddPlugin(CPlugin *pPlugin);
dvander@211
   395
dvander@211
   396
	/**
dvander@211
   397
	 * Runs the second loading pass on a plugin.
dvander@209
   398
	 */
dvander@298
   399
	bool RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength);
dvander@211
   400
dvander@211
   401
	/**
dvander@211
   402
	 * Adds any globally registered natives to a plugin
dvander@211
   403
	 */
dvander@211
   404
	void AddCoreNativesToPlugin(CPlugin *pPlugin);
dvander@315
   405
dvander@315
   406
	/**
dvander@315
   407
	 * Runs an extension pass on a plugin.
dvander@315
   408
	 */
dvander@315
   409
	bool LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength);
faluco@430
   410
faluco@430
   411
	void _SetPauseState(CPlugin *pPlugin, bool pause);
dvander@978
   412
dvander@978
   413
	void ExecAndGenPluginConfs();
dvander@174
   414
protected:
dvander@209
   415
	/**
dvander@209
   416
	 * Caching internal objects
dvander@209
   417
	 */
dvander@174
   418
	void ReleaseIterator(CPluginIterator *iter);
dvander@900
   419
public:
dvander@229
   420
	inline IdentityToken_t *GetIdentity()
dvander@229
   421
	{
dvander@229
   422
		return m_MyIdent;
dvander@229
   423
	}
dvander@601
   424
public:
dvander@601
   425
	bool AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func);
dvander@601
   426
private:
dvander@601
   427
	void AddFakeNativesToPlugin(CPlugin *pPlugin);
dvander@605
   428
	void TryRefreshNatives(CPlugin *pOther);
dvander@174
   429
private:
dvander@174
   430
	List<IPluginsListener *> m_listeners;
dvander@213
   431
	List<CPlugin *> m_plugins;
dvander@211
   432
	List<sp_nativeinfo_t *> m_natives;
dvander@174
   433
	CStack<CPluginManager::CPluginIterator *> m_iters;
dvander@203
   434
	CPluginInfoDatabase m_PluginInfo;
dvander@211
   435
	Trie *m_LoadLookup;
dvander@211
   436
	bool m_AllPluginsLoaded;
dvander@229
   437
	IdentityToken_t *m_MyIdent;
dvander@601
   438
dvander@601
   439
	/* Dynamic native stuff */
dvander@601
   440
	List<FakeNative *> m_Natives;
dvander@601
   441
	Trie *m_pNativeLookup;
dvander@601
   442
dvander@174
   443
};
dvander@174
   444
dvander@211
   445
extern CPluginManager g_PluginSys;
dvander@174
   446
dvander@173
   447
#endif //_INCLUDE_SOURCEMOD_PLUGINSYSTEM_H_