public/sample_ext/smsdk_ext.h
author David Anderson <dvander@alliedmods.net>
Mon Apr 09 03:05:53 2007 +0000 (2007-04-09)
changeset 676 1ed65fdc194a
parent 376 02c9ad824677
permissions -rw-r--r--
GET_V_IFACE_* will work from SDK_OnMetamodLoad now
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     9  * or modified under the Terms and Conditions of its License Agreement, which is found 
    10  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    11  * may change at any time.  To view the latest information, see:
    12  *   http://www.sourcemod.net/license.php
    13  *
    14  * Version: $Id$
    15  */
    16 
    17 #ifndef _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_
    18 #define _INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_
    19 
    20 /**
    21  * @file smsdk_ext.h
    22  * @brief Contains wrappers for making Extensions easier to write.
    23  */
    24 
    25 #include "smsdk_config.h"
    26 #include <IExtensionSys.h>
    27 #include <IHandleSys.h>
    28 #include <sp_vm_api.h>
    29 #include <sm_platform.h>
    30 #include <ISourceMod.h>
    31 #include <IForwardSys.h>
    32 
    33 #if defined SMEXT_CONF_METAMOD
    34 #include <ISmmPlugin.h>
    35 #include <eiface.h>
    36 #endif
    37 
    38 using namespace SourceMod;
    39 using namespace SourcePawn;
    40 
    41 class SDKExtension : 
    42 #if defined SMEXT_CONF_METAMOD
    43 	public ISmmPlugin,
    44 #endif
    45 	public IExtensionInterface
    46 {
    47 public:
    48 	/** Constructor */
    49 	SDKExtension();
    50 public:
    51 	/**
    52 	 * @brief This is called after the initial loading sequence has been processed.
    53 	 *
    54 	 * @param error		Error message buffer.
    55 	 * @param err_max	Size of error message buffer.
    56 	 * @param late		Whether or not the module was loaded after map load.
    57 	 * @return			True to succeed loading, false to fail.
    58 	 */
    59 	virtual bool SDK_OnLoad(char *error, size_t err_max, bool late);
    60 	
    61 	/**
    62 	 * @brief This is called right before the extension is unloaded.
    63 	 */
    64 	virtual void SDK_OnUnload();
    65 
    66 	/**
    67 	 * @brief This is called once all known extensions have been loaded.
    68 	 */
    69 	virtual void SDK_OnAllLoaded();
    70 
    71 	/**
    72 	 * @brief Called when the pause state is changed.
    73 	 */
    74 	virtual void SDK_OnPauseChange(bool paused);
    75 
    76 #if defined SMEXT_CONF_METAMOD
    77 	/**
    78 	 * @brief Called when Metamod is attached, before the extension version is called.
    79 	 *
    80 	 * @param error			Error buffer.
    81 	 * @param err_max		Maximum size of error buffer.
    82 	 * @param late			Whether or not Metamod considers this a late load.
    83 	 * @return				True to succeed, false to fail.
    84 	 */
    85 	virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late);
    86 
    87 	/**
    88 	 * @brief Called when Metamod is detaching, after the extension version is called.
    89 	 * NOTE: By default this is blocked unless sent from SourceMod.
    90 	 *
    91 	 * @param error			Error buffer.
    92 	 * @param err_max		Maximum size of error buffer.
    93 	 * @return				True to succeed, false to fail.
    94 	 */
    95 	virtual bool SDK_OnMetamodUnload(char *error, size_t err_max);
    96 
    97 	/**
    98 	 * @brief Called when Metamod's pause state is changing.
    99 	 * NOTE: By default this is blocked unless sent from SourceMod.
   100 	 *
   101 	 * @param paused		Pause state being set.
   102 	 * @param error			Error buffer.
   103 	 * @param err_max		Maximum size of error buffer.
   104 	 * @return				True to succeed, false to fail.
   105 	 */
   106 	virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t err_max);
   107 #endif
   108 
   109 public: //IExtensionInterface
   110 	virtual bool OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, size_t err_max, bool late);
   111 	virtual void OnExtensionUnload();
   112 	virtual void OnExtensionsAllLoaded();
   113 
   114 	/** Returns whether or not this is a Metamod-based extension */
   115 	virtual bool IsMetamodExtension();
   116 
   117 	/**
   118 	 * @brief Called when the pause state changes.
   119 	 * 
   120 	 * @param state			True if being paused, false if being unpaused.
   121 	 */
   122 	virtual void OnExtensionPauseChange(bool state);
   123 
   124 	/** Returns name */
   125 	virtual const char *GetExtensionName();
   126 	/** Returns URL */
   127 	virtual const char *GetExtensionURL();
   128 	/** Returns log tag */
   129 	virtual const char *GetExtensionTag();
   130 	/** Returns author */
   131 	virtual const char *GetExtensionAuthor();
   132 	/** Returns version string */
   133 	virtual const char *GetExtensionVerString();
   134 	/** Returns description string */
   135 	virtual const char *GetExtensionDescription();
   136 	/** Returns date string */
   137 	virtual const char *GetExtensionDateString();
   138 #if defined SMEXT_CONF_METAMOD
   139 public: //ISmmPlugin
   140 	/** Called when the extension is attached to Metamod. */
   141 	virtual bool Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlength, bool late);
   142 	/** Returns the author to MM */
   143 	virtual const char *GetAuthor();
   144 	/** Returns the name to MM */
   145 	virtual const char *GetName();
   146 	/** Returns the description to MM */
   147 	virtual const char *GetDescription();
   148 	/** Returns the URL to MM */
   149 	virtual const char *GetURL();
   150 	/** Returns the license to MM */
   151 	virtual const char *GetLicense();
   152 	/** Returns the version string to MM */
   153 	virtual const char *GetVersion();
   154 	/** Returns the date string to MM */
   155 	virtual const char *GetDate();
   156 	/** Returns the logtag to MM */
   157 	virtual const char *GetLogTag();
   158 	/** Called on unload */
   159 	virtual bool Unload(char *error, size_t maxlen);
   160 	/** Called on pause */
   161 	virtual bool Pause(char *error, size_t maxlen);
   162 	/** Called on unpause */
   163 	virtual bool Unpause(char *error, size_t maxlen);
   164 private:
   165 	bool m_SourceMMLoaded;
   166 	bool m_WeAreUnloaded;
   167 	bool m_WeGotPauseChange;
   168 #endif
   169 };
   170 
   171 extern SDKExtension *g_pExtensionIface;
   172 
   173 extern IShareSys *g_pShareSys;
   174 extern IExtension *myself;
   175 extern IHandleSys *g_pHandleSys;
   176 extern ISourceMod *g_pSM;
   177 extern IForwardManager *g_pForwards;
   178 
   179 #if defined SMEXT_CONF_METAMOD
   180 PLUGIN_GLOBALVARS();
   181 extern IVEngineServer *engine;
   182 extern IServerGameDLL *gamedll;
   183 #endif
   184 
   185 /** Creates a SourceMod interface macro pair */
   186 #define SM_MKIFACE(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
   187 /** Automates retrieving SourceMod interfaces */
   188 #define SM_GET_IFACE(prefix,addr) \
   189 	if (!g_pShareSys->RequestInterface(SM_MKIFACE(prefix), myself, (SMInterface **)&addr)) { \
   190 	 if (error) { \
   191 		snprintf(error, err_max, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
   192 	 } \
   193 	 return false; \
   194 	}
   195 
   196 #endif //_INCLUDE_SOURCEMOD_EXTENSION_BASESDK_H_