core/systems/PluginSys.h
author Borja Ferrer <faluco@alliedmods.net>
Thu Jul 12 02:57:43 2007 +0000 (2007-07-12)
changeset 1089 275107f9ca95
parent 1018 e466e7319a68
child 1112 152ceef17866
permissions -rw-r--r--
added experimental optional natives (part 1)
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);
faluco@1089
   237
	inline bool IsInAskPluginLoad()
faluco@1089
   238
	{
faluco@1089
   239
		return m_IsInAskPluginLoad;
faluco@1089
   240
	}
dvander@173
   241
protected:
dvander@173
   242
	void UpdateInfo();
faluco@333
   243
	void SetTimeStamp(time_t t);
dvander@605
   244
	void DependencyDropped(CPlugin *pOwner);
dvander@173
   245
private:
dvander@209
   246
	ContextPair m_ctx;
dvander@174
   247
	PluginType m_type;
ds@652
   248
	char m_filename[PLATFORM_MAX_PATH];
dvander@173
   249
	PluginStatus m_status;
dvander@173
   250
	unsigned int m_serial;
dvander@173
   251
	sm_plugininfo_t m_info;
dvander@173
   252
	sp_plugin_t *m_plugin;
dvander@209
   253
	char m_errormsg[256];
dvander@211
   254
	time_t m_LastAccess;
dvander@229
   255
	IdentityToken_t *m_ident;
dvander@220
   256
	Handle_t m_handle;
dvander@282
   257
	bool m_WasRunning;
faluco@389
   258
	CVector<unsigned int> m_PhraseFiles;
dvander@605
   259
	List<CPlugin *> m_dependents;
dvander@605
   260
	List<CPlugin *> m_dependsOn;
dvander@605
   261
	List<FakeNative *> m_fakeNatives;
dvander@486
   262
	Trie *m_pProps;
dvander@605
   263
	bool m_FakeNativesMissing;
dvander@978
   264
	CVector<AutoConfig *> m_configs;
faluco@1089
   265
	bool m_IsInAskPluginLoad;
dvander@601
   266
};
dvander@601
   267
dvander@220
   268
class CPluginManager : 
dvander@220
   269
	public IPluginManager,
dvander@220
   270
	public SMGlobalClass,
dvander@309
   271
	public IHandleTypeDispatch,
dvander@309
   272
	public IRootConsoleCommand
dvander@174
   273
{
dvander@176
   274
	friend class CPlugin;
dvander@174
   275
public:
dvander@174
   276
	CPluginManager();
dvander@176
   277
	~CPluginManager();
dvander@174
   278
public:
dvander@203
   279
	/* Implements iterator class */
dvander@174
   280
	class CPluginIterator : public IPluginIterator
dvander@174
   281
	{
dvander@174
   282
	public:
dvander@213
   283
		CPluginIterator(List<CPlugin *> *mylist);
dvander@174
   284
		virtual ~CPluginIterator();
dvander@174
   285
		virtual bool MorePlugins();
dvander@174
   286
		virtual IPlugin *GetPlugin();
dvander@174
   287
		virtual void NextPlugin();
dvander@282
   288
		void Release();
dvander@174
   289
	public:
dvander@174
   290
		void Reset();
dvander@174
   291
	private:
dvander@213
   292
		List<CPlugin *> *mylist;
dvander@213
   293
		List<CPlugin *>::iterator current;
dvander@174
   294
	};
dvander@174
   295
	friend class CPluginManager::CPluginIterator;
dvander@203
   296
public: //IPluginManager
dvander@282
   297
	IPlugin *LoadPlugin(const char *path, 
dvander@174
   298
								bool debug,
dvander@174
   299
								PluginType type,
dvander@174
   300
								char error[],
ds@755
   301
								size_t maxlength,
faluco@383
   302
								bool *wasloaded);
dvander@282
   303
	bool UnloadPlugin(IPlugin *plugin);
dvander@282
   304
	IPlugin *FindPluginByContext(const sp_context_t *ctx);
dvander@282
   305
	unsigned int GetPluginCount();
dvander@282
   306
	IPluginIterator *GetPluginIterator();
dvander@282
   307
	void AddPluginsListener(IPluginsListener *listener);
dvander@282
   308
	void RemovePluginsListener(IPluginsListener *listener);
dvander@220
   309
public: //SMGlobalClass
dvander@282
   310
	void OnSourceModAllInitialized();
dvander@282
   311
	void OnSourceModShutdown();
dvander@220
   312
public: //IHandleTypeDispatch
dvander@282
   313
	void OnHandleDestroy(HandleType_t type, void *object);
dvander@309
   314
public: //IRootConsoleCommand
dvander@309
   315
	void OnRootConsoleCommand(const char *command, unsigned int argcount);
dvander@190
   316
public:
dvander@203
   317
	/**
dvander@211
   318
	 * Loads all plugins not yet loaded
dvander@203
   319
	 */
dvander@211
   320
	void LoadAll_FirstPass(const char *config, const char *basedir);
dvander@211
   321
dvander@211
   322
	/**
dvander@211
   323
	 * Runs the second loading pass for all plugins
dvander@211
   324
	 */
dvander@211
   325
	void LoadAll_SecondPass();
dvander@203
   326
dvander@203
   327
	/**
dvander@203
   328
	 * Tests a plugin file mask against a local folder.
dvander@203
   329
	 * The alias is searched backwards from localdir - i.e., given this input:
dvander@203
   330
	 *   csdm/ban        csdm/ban
dvander@203
   331
	 *   ban             csdm/ban
dvander@203
   332
	 *   csdm/ban        optional/csdm/ban
dvander@203
   333
	 * All of these will return true for an alias match.  
dvander@203
   334
	 * Wildcards are allowed in the filename.
dvander@203
   335
	 */
dvander@209
   336
	bool TestAliasMatch(const char *alias, const char *localdir);
dvander@211
   337
dvander@211
   338
	/** 
dvander@211
   339
	 * Returns whether anything loaded will be a late load.
dvander@211
   340
	 */
faluco@290
   341
	bool IsLateLoadTime() const;
dvander@233
   342
dvander@233
   343
	/**
dvander@233
   344
	 * Adds natives from core into the native pool.
dvander@233
   345
	 */
dvander@233
   346
	void RegisterNativesFromCore(sp_nativeinfo_t *natives);
dvander@247
   347
dvander@247
   348
	/**
dvander@247
   349
	 * Converts a Handle to an IPlugin if possible.
dvander@247
   350
	 */
dvander@247
   351
	IPlugin *PluginFromHandle(Handle_t handle, HandleError *err);
faluco@290
   352
faluco@290
   353
	/**
dvander@309
   354
	 * Finds a plugin based on its index. (starts on index 1)
dvander@309
   355
	 */
faluco@290
   356
	CPlugin *GetPluginByOrder(int num);
dvander@309
   357
ds@457
   358
	/** 
ds@457
   359
	 * Internal version of FindPluginByContext()
ds@457
   360
	 */
ds@457
   361
	inline CPlugin *GetPluginByCtx(const sp_context_t *ctx)
ds@457
   362
	{
ds@457
   363
		return reinterpret_cast<CPlugin *>(ctx->user[SM_CONTEXTVAR_MYSELF]);
ds@457
   364
	}
ds@457
   365
dvander@309
   366
	/**
dvander@309
   367
	 * Gets status text for a status code 
dvander@309
   368
	 */
dvander@309
   369
	const char *GetStatusText(PluginStatus status);
dvander@315
   370
faluco@333
   371
	/**
faluco@333
   372
	* Reload or update plugins on level shutdown.
faluco@333
   373
	*/
faluco@333
   374
	void ReloadOrUnloadPlugins();
faluco@333
   375
ds@611
   376
	/**
ds@611
   377
	 * Add public functions from all running or paused
ds@611
   378
	 * plugins to the specified forward if the names match.
ds@611
   379
	 */
ds@611
   380
	void AddFunctionsToForward(const char *name, IChangeableForward *pForward);
ds@611
   381
dvander@900
   382
	CPlugin *GetPluginFromIdentity(IdentityToken_t *pToken);
dvander@900
   383
dvander@1018
   384
	void Shutdown();
dvander@1018
   385
dvander@209
   386
private:
ds@755
   387
	LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength);
dvander@298
   388
dvander@298
   389
	void LoadAutoPlugin(const char *plugin);
dvander@298
   390
dvander@209
   391
	/**
dvander@209
   392
	 * Recursively loads all plugins in the given directory.
dvander@209
   393
	 */
dvander@209
   394
	void LoadPluginsFromDir(const char *basedir, const char *localdir);
dvander@209
   395
dvander@209
   396
	/**
dvander@211
   397
	 * Adds a plugin object.  This is wrapped by LoadPlugin functions.
dvander@211
   398
	 */
dvander@211
   399
	void AddPlugin(CPlugin *pPlugin);
dvander@211
   400
dvander@211
   401
	/**
dvander@211
   402
	 * Runs the second loading pass on a plugin.
dvander@209
   403
	 */
dvander@298
   404
	bool RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength);
dvander@211
   405
dvander@211
   406
	/**
dvander@211
   407
	 * Adds any globally registered natives to a plugin
dvander@211
   408
	 */
dvander@211
   409
	void AddCoreNativesToPlugin(CPlugin *pPlugin);
dvander@315
   410
dvander@315
   411
	/**
dvander@315
   412
	 * Runs an extension pass on a plugin.
dvander@315
   413
	 */
dvander@315
   414
	bool LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength);
faluco@430
   415
faluco@430
   416
	void _SetPauseState(CPlugin *pPlugin, bool pause);
dvander@978
   417
dvander@978
   418
	void ExecAndGenPluginConfs();
dvander@174
   419
protected:
dvander@209
   420
	/**
dvander@209
   421
	 * Caching internal objects
dvander@209
   422
	 */
dvander@174
   423
	void ReleaseIterator(CPluginIterator *iter);
dvander@900
   424
public:
dvander@229
   425
	inline IdentityToken_t *GetIdentity()
dvander@229
   426
	{
dvander@229
   427
		return m_MyIdent;
dvander@229
   428
	}
dvander@601
   429
public:
dvander@601
   430
	bool AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func);
dvander@601
   431
private:
dvander@601
   432
	void AddFakeNativesToPlugin(CPlugin *pPlugin);
dvander@605
   433
	void TryRefreshNatives(CPlugin *pOther);
dvander@174
   434
private:
dvander@174
   435
	List<IPluginsListener *> m_listeners;
dvander@213
   436
	List<CPlugin *> m_plugins;
dvander@211
   437
	List<sp_nativeinfo_t *> m_natives;
dvander@174
   438
	CStack<CPluginManager::CPluginIterator *> m_iters;
dvander@203
   439
	CPluginInfoDatabase m_PluginInfo;
dvander@211
   440
	Trie *m_LoadLookup;
dvander@211
   441
	bool m_AllPluginsLoaded;
dvander@229
   442
	IdentityToken_t *m_MyIdent;
dvander@601
   443
dvander@601
   444
	/* Dynamic native stuff */
dvander@601
   445
	List<FakeNative *> m_Natives;
dvander@601
   446
	Trie *m_pNativeLookup;
dvander@601
   447
dvander@174
   448
};
dvander@174
   449
dvander@211
   450
extern CPluginManager g_PluginSys;
dvander@174
   451
dvander@173
   452
#endif //_INCLUDE_SOURCEMOD_PLUGINSYSTEM_H_