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