sourcemm/ISmmPlugin.h
author Scott Ehlert <ds@alliedmods.net>
Sun May 22 04:33:58 2005 +0000 (2005-05-22)
changeset 66 ddefc8bfb9fa
parent 51 7985801b5b42
child 67 1753f7a9b2f2
permissions -rw-r--r--
Added macro so that ConCommands created with the SDK's CON_COMMAND and CON_COMMAND_F macros can be properly registered and unregisterd
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #ifndef _INCLUDE_ISMM_PLUGIN_H
    12 #define _INCLUDE_ISMM_PLUGIN_H
    13 
    14 /**
    15  * @brief Plugin API interface
    16  * @file ISmmPlugin.h
    17  */
    18 
    19 #include <interface.h>
    20 #include <sourcehook/sourcehook.h>
    21 #include "ISmmAPI.h"
    22 
    23 #define PLAPI_VERSION	5
    24 #define PLAPI_NAME		"ISmmPlugin"
    25 
    26 struct factories
    27 {
    28 	CreateInterfaceFn engine;
    29 	CreateInterfaceFn server;
    30 	CreateInterfaceFn physics;
    31 	CreateInterfaceFn fileSystem;
    32 };
    33 
    34 class ISmmAPI;
    35 typedef int PluginId;
    36 
    37 class ISmmPlugin
    38 {
    39 public:
    40 	virtual int GetApiVersion() { return PLAPI_VERSION; }
    41 public:
    42 	/**
    43 	 * @brief Called on plugin load.
    44 	 *
    45 	 * @param id Internal id of plugin.  Saved globally by PLUGIN_SAVEVARS()
    46 	 * @param ismm External API for SourceMM.  Saved globally by PLUGIN_SAVEVARS()
    47 	 * @param list Contains a list of factories.  Hook a factory call by setting one equal to your own function.
    48 	 * @param error Error message buffer
    49 	 * @param maxlen Size of error message buffer
    50 	 * @return True on success, return false to request no load.
    51 	 */
    52 	virtual bool Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen) =0;
    53 
    54 	/** @brief Called on plugin unload.
    55 	 *
    56 	 * @param error Error message buffer
    57 	 * @param maxlen Size of error message buffer
    58 	 * @return True on success, return false to request no unload.
    59 	 */
    60 	virtual bool Unload(char *error, size_t maxlen) =0;
    61 
    62 	/** @brief Called on plugin pause.
    63 	 *
    64 	 * @param error Error message buffer
    65 	 * @param maxlen Size of error message buffer
    66 	 * @return True on success, return false to request no pause.
    67 	 */
    68 	virtual bool Pause(char *error, size_t maxlen) =0;
    69 
    70 	/** @brief Called on plugin unpause.
    71 	 *
    72 	 * @param error Error message buffer
    73 	 * @param maxlen Size of error message buffer
    74 	 * @return True on success, return false to request no unpause.
    75 	 */
    76 	virtual bool Unpause(char *error, size_t maxlen) =0;
    77 public:
    78 	/** @brief Return author as string */
    79 	virtual const char *GetAuthor() =0;
    80 
    81 	/** @brief Return plugin name as string */
    82 	virtual const char *GetName() =0;
    83 
    84 	/** @brief Return a description as string */
    85 	virtual const char *GetDescription() =0;
    86 
    87 	/** @brief Return a URL as string */
    88 	virtual const char *GetURL() =0;
    89 
    90 	/** @brief Return quick license code as string */
    91 	virtual const char *GetLicense() =0;
    92 
    93 	/** @brief Return version as string */
    94 	virtual const char *GetVersion() =0;
    95 
    96 	/** @brief Return author as string */
    97 	virtual const char *GetDate() =0;
    98 
    99 	/** @brief Return author as string */
   100 	virtual const char *GetLogTag() =0;
   101 public:
   102 	/**
   103 	 * @brief Called when all plugins have been loaded - API version 4
   104          *
   105          * Ths is useful for knowing when it's safe to request things another plugin might have.
   106 	 */
   107 	virtual void AllPluginsLoaded() =0;
   108 };
   109 
   110 #define PL_EXPOSURE	CreateInterface
   111 #define PL_EXPOSURE_C	"CreateInterface"
   112 
   113 #define PLUGIN_EXPOSE(name, var) \
   114 	ISmmAPI *g_SMAPI = NULL; \
   115 	ISmmPlugin *g_PLAPI = NULL; \
   116 	PluginId g_PLID = (PluginId)0; \
   117 	SourceHook::ISourceHook *g_SHPtr = NULL; \
   118 	SMM_API void *PL_EXPOSURE(const char *name, int *code) { \
   119 		if (name && !strcmp(name, PLAPI_NAME)) { \
   120 			return static_cast<void *>(&var); \
   121 		} \
   122 		return NULL; \
   123 	}
   124 
   125 #define PLUGIN_GLOBALVARS()	\
   126 	extern SourceHook::ISourceHook *g_SHPtr; \
   127 	extern ISmmAPI *g_SMAPI; \
   128 	extern ISmmPlugin *g_PLAPI; \
   129 	extern PluginId g_PLID; 
   130 
   131 #define PLUGIN_SAVEVARS() \
   132 	g_SMAPI = ismm; \
   133 	g_SHPtr = ismm->SourceHook(); \
   134 	g_PLAPI = static_cast<ISmmPlugin *>(this); \
   135 	g_PLID = id;
   136 
   137 #define FACTORY_RETURN(mres, value) \
   138 	g_SMAPI->SetLastMetaReturn(mres); \
   139 	return value;
   140 
   141 #define META_LOG	g_SMAPI->LogMsg
   142 
   143 #define CONCMD_VARNAME(name) name##_command
   144 
   145 #if !defined SMM_API
   146 #if defined __WIN32__ || defined _WIN32 || defined WIN32
   147 	#define SMM_API extern "C" __declspec(dllexport)
   148 #elif defined __GNUC__
   149 	#define SMM_API	extern "C"
   150 #endif
   151 #endif //!defined SMM_API
   152 
   153 #endif //_INCLUDE_ISMM_PLUGIN_H