Initial import of new GameDLL loading code
authorDavid Anderson <dvander@alliedmods.net>
Thu Oct 06 00:59:01 2005 +0000 (2005-10-06)
changeset 1227c36121e2dfe
parent 121 158b860440c0
child 123 53e819ac032a
Initial import of new GameDLL loading code
sourcemm/CHLTVDirector.h
sourcemm/CPlugin.cpp
sourcemm/CPlugin.h
sourcemm/CServerGameClients.h
sourcemm/CServerGameDLL.h
sourcemm/CServerGameEnts.h
sourcemm/IPluginManager.h
sourcemm/ISmmPlugin.h
sourcemm/oslink.cpp
sourcemm/oslink.h
sourcemm/sourcemm.cpp
sourcemm/sourcemm.h
sourcemm/sourcemm.vcproj
sourcemm/util.cpp
sourcemm/util.h
     1.1 --- a/sourcemm/CHLTVDirector.h	Wed Oct 05 21:51:02 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,41 +0,0 @@
     1.4 -/* ======== SourceMM ========
     1.5 -* Copyright (C) 2004-2005 Metamod:Source Development Team
     1.6 -* No warranties of any kind
     1.7 -*
     1.8 -* License: zlib/libpng
     1.9 -*
    1.10 -* Author(s): David "BAILOPAN" Anderson
    1.11 -* ============================
    1.12 -*/
    1.13 -
    1.14 -#ifndef _INCLUDE_HLTVDIRECTOR_H
    1.15 -#define _INCLUDE_HLTVDIRECTOR_H
    1.16 -
    1.17 -/**
    1.18 - * @brief Dummy class for IHLTVDirector
    1.19 - * @file CHLTVDirector.
    1.20 - */
    1.21 -
    1.22 -#include <eiface.h>
    1.23 -#include <ihltvdirector.h>
    1.24 -
    1.25 -class CHLTVDirector : IHLTVDirector
    1.26 -{
    1.27 -	IHLTVDirector *m_pOrig;
    1.28 -public:
    1.29 -	CHLTVDirector() : m_pOrig(0) {}
    1.30 -	void SetOrig(IHLTVDirector *pOrig)
    1.31 -	{
    1.32 -		m_pOrig = pOrig;
    1.33 -	}
    1.34 -	virtual bool IsActive() { return m_pOrig->IsActive(); }
    1.35 -	virtual void SetHLTVServer(IHLTVServer *hltv) { m_pOrig->SetHLTVServer(hltv); }
    1.36 -	virtual IHLTVServer	*GetHLTVServer() { return m_pOrig->GetHLTVServer(); }
    1.37 -	virtual int	GetDirectorTick() { return m_pOrig->GetDirectorTick(); }
    1.38 -	virtual int	GetPVSEntity() { return m_pOrig->GetPVSEntity(); }
    1.39 -	virtual Vector GetPVSOrigin() { return m_pOrig->GetPVSOrigin(); }
    1.40 -	virtual float GetDelay() { return m_pOrig->GetDelay(); }
    1.41 -	virtual const char**GetModEvents() { return m_pOrig->GetModEvents(); }
    1.42 -};
    1.43 -
    1.44 -#endif //_INCLUDE_HLTVDIRECTOR_H
     2.1 --- a/sourcemm/CPlugin.cpp	Wed Oct 05 21:51:02 2005 +0000
     2.2 +++ b/sourcemm/CPlugin.cpp	Thu Oct 06 00:59:01 2005 +0000
     2.3 @@ -260,7 +260,7 @@
     2.4  							snprintf(error, maxlen, "Plugin API %d is newer than internal version (%d)", api, PLAPI_VERSION);
     2.5  						pl->m_Status = Pl_Error;
     2.6  					} else {
     2.7 -						if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), &(pl->fac_list), error, maxlen))
     2.8 +						if (pl->m_API->Load(pl->m_Id, static_cast<ISmmAPI *>(&g_SmmAPI), &(pl->fac_list), error, maxlen, m_AllLoaded))
     2.9  						{
    2.10  							pl->m_Status = Pl_Running;
    2.11  							if (m_AllLoaded)
    2.12 @@ -424,6 +424,20 @@
    2.13  	return true;
    2.14  }
    2.15  
    2.16 +bool CPluginManager::QueryRunning(PluginId id, char *error, size_t maxlength)
    2.17 +{
    2.18 +	CPlugin *pl = FindById(id);
    2.19 +
    2.20 +	if (!pl || !pl->m_API)
    2.21 +	{
    2.22 +		if (error)
    2.23 +			snprintf(error, maxlength, "Plugin not valid");
    2.24 +		return false;
    2.25 +	}
    2.26 +
    2.27 +	return pl->m_API->QueryRunning(error, maxlength);
    2.28 +}
    2.29 +
    2.30  PluginIter CPluginManager::_begin()
    2.31  {
    2.32  	return m_Plugins.begin();
     3.1 --- a/sourcemm/CPlugin.h	Wed Oct 05 21:51:02 2005 +0000
     3.2 +++ b/sourcemm/CPlugin.h	Thu Oct 06 00:59:01 2005 +0000
     3.3 @@ -33,6 +33,8 @@
     3.4   *    5: Bumped version for SourceHook V4 (2005-05-01)
     3.5   *    6: Added functions for console printing (2005-05-26)
     3.6   * MC 7: Changed template libraries (2005-08-11)
     3.7 + *		 New loading structure mechanism
     3.8 + *	     New SourceHook version
     3.9   */
    3.10  
    3.11  #define PLAPI_MIN_VERSION	7
    3.12 @@ -75,6 +77,7 @@
    3.13  		bool UnloadAll();
    3.14  	public:
    3.15  		bool Query(PluginId id, const char *&file, factories *&list, Pl_Status &status, PluginId &source);
    3.16 +		bool QueryRunning(PluginId id, char *error, size_t maxlength);
    3.17  
    3.18  		void AddPluginCvar(ISmmPlugin *api, ConCommandBase *pCvar);
    3.19  		void AddPluginCmd(ISmmPlugin *api, ConCommandBase *pCmd);
     4.1 --- a/sourcemm/CServerGameClients.h	Wed Oct 05 21:51:02 2005 +0000
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,77 +0,0 @@
     4.4 -/* ======== SourceMM ========
     4.5 -* Copyright (C) 2004-2005 Metamod:Source Development Team
     4.6 -* No warranties of any kind
     4.7 -*
     4.8 -* License: zlib/libpng
     4.9 -*
    4.10 -* Author(s): David "BAILOPAN" Anderson
    4.11 -* ============================
    4.12 -*/
    4.13 -
    4.14 -#ifndef _INCLUDE_CSERVERGAMECLIENTS_H
    4.15 -#define _INCLUDE_CSERVERGAMECLIENTS_H
    4.16 -
    4.17 -/**
    4.18 - * @brief Dummy class for IServerGameClients
    4.19 - * @file CServerGameClients.h
    4.20 - */
    4.21 -
    4.22 -#include <eiface.h>
    4.23 -
    4.24 -class CServerGameClients : public IServerGameClients
    4.25 -{
    4.26 -	IServerGameClients *m_pOrig;
    4.27 -public:
    4.28 -	CServerGameClients() : m_pOrig(0) {}
    4.29 -	void SetOrig(IServerGameClients *pOrig)
    4.30 -	{
    4.31 -		m_pOrig = pOrig;
    4.32 -	}
    4.33 -
    4.34 -	virtual void GetPlayerLimits(int& minplayers, int& maxplayers, int &defaultMaxPlayers) const
    4.35 -	{ m_pOrig->GetPlayerLimits(minplayers, maxplayers, defaultMaxPlayers); }
    4.36 -
    4.37 -	virtual bool ClientConnect(edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen)
    4.38 -	{ return m_pOrig->ClientConnect(pEntity, pszName, pszAddress, reject, maxrejectlen); }
    4.39 -
    4.40 -	virtual void ClientActive(edict_t *pEntity, bool bLoadGame)
    4.41 -	{ m_pOrig->ClientActive(pEntity, bLoadGame); }
    4.42 -
    4.43 -	virtual void ClientDisconnect(edict_t *pEntity)
    4.44 -	{ m_pOrig->ClientDisconnect(pEntity); }
    4.45 -
    4.46 -	virtual void ClientPutInServer(edict_t *pEntity, char const *playername)
    4.47 -	{ m_pOrig->ClientPutInServer(pEntity, playername); }
    4.48 -
    4.49 -	virtual void ClientCommand(edict_t *pEntity)
    4.50 -	{ m_pOrig->ClientCommand(pEntity); }
    4.51 -
    4.52 -	virtual void SetCommandClient(int index)
    4.53 -	{ m_pOrig->SetCommandClient(index); }
    4.54 -
    4.55 -	virtual void ClientSettingsChanged(edict_t *pEdict)
    4.56 -	{ m_pOrig->ClientSettingsChanged(pEdict); }
    4.57 -
    4.58 -	virtual void ClientSetupVisibility(edict_t *pViewEntity, edict_t *pClient, unsigned char *pvs, int pvssize)
    4.59 -	{ m_pOrig->ClientSetupVisibility(pViewEntity, pClient, pvs, pvssize); }
    4.60 -
    4.61 -	virtual float ProcessUsercmds(edict_t *player, bf_read *buf, int numcmds, int totalcmds, int dropped_packets, bool ignore, bool paused)
    4.62 -	{ return m_pOrig->ProcessUsercmds(player, buf, numcmds, totalcmds, dropped_packets, ignore, paused); }
    4.63 -
    4.64 -	virtual void PostClientMessagesSent()
    4.65 -	{ m_pOrig->PostClientMessagesSent(); }
    4.66 -
    4.67 -	virtual CPlayerState *GetPlayerState(edict_t *player)
    4.68 -	{ return m_pOrig->GetPlayerState(player); }
    4.69 -
    4.70 -	virtual void ClientEarPosition(edict_t *pEntity, Vector *pEarOrigin)
    4.71 -	{ m_pOrig->ClientEarPosition(pEntity, pEarOrigin); }
    4.72 -
    4.73 -	virtual int GetReplayDelay(edict_t *player)
    4.74 -	{ return m_pOrig->GetReplayDelay(player); }
    4.75 -
    4.76 -	virtual void GetBugReportInfo(char *buf, int buflen)
    4.77 -	{ m_pOrig->GetBugReportInfo(buf, buflen); }
    4.78 -};
    4.79 -
    4.80 -#endif //_INCLUDE_CSERVERGAMECLIENTS_H
     5.1 --- a/sourcemm/CServerGameDLL.h	Wed Oct 05 21:51:02 2005 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,95 +0,0 @@
     5.4 -/* ======== SourceMM ========
     5.5 -* Copyright (C) 2004-2005 Metamod:Source Development Team
     5.6 -* No warranties of any kind
     5.7 -*
     5.8 -* License: zlib/libpng
     5.9 -*
    5.10 -* Author(s): David "BAILOPAN" Anderson
    5.11 -* ============================
    5.12 -*/
    5.13 -
    5.14 -#ifndef _INCLUDE_CSERVER_GAMEDLL_H
    5.15 -#define _INCLUDE_CSERVER_GAMEDLL_H
    5.16 -
    5.17 -/**
    5.18 - * @brief Defines wrapper class for gamedll interfaces
    5.19 - * @file CServerGameDLL.h
    5.20 - */
    5.21 -
    5.22 -#include <eiface.h>
    5.23 -#include "oslink.h"
    5.24 -
    5.25 -/**
    5.26 - * @brief Empty class.  We only care about DLLInit.
    5.27 - */
    5.28 -class CServerGameDLL : public IServerGameDLL
    5.29 -{
    5.30 -	IServerGameDLL *m_pOrig;
    5.31 -	char m_GameDescBuffer[256];
    5.32 -public:
    5.33 -	CServerGameDLL() : m_pOrig(0)
    5.34 -	{
    5.35 -		strcpy(m_GameDescBuffer, "Metamod:Source");
    5.36 -	}
    5.37 -	void SetOrig(IServerGameDLL *pOrig)
    5.38 -	{
    5.39 -		m_pOrig = pOrig;
    5.40 -		snprintf(m_GameDescBuffer, 255, "%s", pOrig->GetGameDescription());
    5.41 -	}
    5.42 -
    5.43 -	virtual bool DLLInit(	CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals);
    5.44 -	virtual bool GameInit( void )
    5.45 -	{ return m_pOrig->GameInit(); }
    5.46 -	virtual bool LevelInit( char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background );
    5.47 -	virtual void ServerActivate( edict_t *pEdictList, int edictCount, int clientMax )
    5.48 -	{ m_pOrig->ServerActivate(pEdictList, edictCount, clientMax); }
    5.49 -	virtual void GameFrame( bool simulating )
    5.50 -	{ m_pOrig->GameFrame(simulating); }
    5.51 -	virtual void PreClientUpdate( bool simulating )
    5.52 -	{ m_pOrig->PreClientUpdate(simulating); }
    5.53 -	virtual void LevelShutdown( void );
    5.54 -	virtual void GameShutdown( void )
    5.55 -	{ m_pOrig->GameShutdown(); }
    5.56 -	virtual void DLLShutdown( void );
    5.57 -	virtual float GetTickInterval( void ) const
    5.58 -	{ return m_pOrig->GetTickInterval(); }
    5.59 -	virtual ServerClass *GetAllServerClasses( void )
    5.60 -	{ return m_pOrig->GetAllServerClasses(); }
    5.61 -	virtual const char *GetGameDescription( void );
    5.62 -	virtual void CreateNetworkStringTables( void )
    5.63 -	{ m_pOrig->CreateNetworkStringTables(); }
    5.64 -	virtual CSaveRestoreData  *SaveInit( int size )
    5.65 -	{ return m_pOrig->SaveInit(size); }
    5.66 -	virtual void SaveWriteFields( CSaveRestoreData *a1, const char *a2, void *a3, datamap_t *a4, typedescription_t *a5, int a6)
    5.67 -	{ m_pOrig->SaveWriteFields(a1, a2, a3, a4, a5, a6); }
    5.68 -	virtual void SaveReadFields( CSaveRestoreData *a1, const char *a2, void *a3, datamap_t *a4, typedescription_t *a5, int a6)
    5.69 -	{ m_pOrig->SaveReadFields(a1, a2, a3, a4, a5, a6); }
    5.70 -	virtual void SaveGlobalState( CSaveRestoreData *a1)
    5.71 -	{ m_pOrig->SaveGlobalState(a1); }
    5.72 -	virtual void RestoreGlobalState( CSaveRestoreData *a1)
    5.73 -	{ m_pOrig->RestoreGlobalState(a1); }
    5.74 -	virtual void PreSave( CSaveRestoreData *a1)
    5.75 -	{ m_pOrig->PreSave(a1); }
    5.76 -	virtual void Save( CSaveRestoreData *a1)
    5.77 -	{ m_pOrig->Save(a1); }
    5.78 -	virtual void GetSaveComment( char *comment, int maxlength )
    5.79 -	{ m_pOrig->GetSaveComment(comment, maxlength); }
    5.80 -	virtual void WriteSaveHeaders( CSaveRestoreData *a1)
    5.81 -	{ m_pOrig->WriteSaveHeaders(a1); }
    5.82 -	virtual void ReadRestoreHeaders( CSaveRestoreData *a1)
    5.83 -	{ m_pOrig->ReadRestoreHeaders(a1); }
    5.84 -	virtual void Restore( CSaveRestoreData *a1, bool a2)
    5.85 -	{ m_pOrig->Restore(a1, a2); }
    5.86 -	virtual bool IsRestoring()
    5.87 -	{ return m_pOrig->IsRestoring(); }
    5.88 -	virtual int CreateEntityTransitionList( CSaveRestoreData *a1, int a2)
    5.89 -	{ return m_pOrig->CreateEntityTransitionList(a1, a2); }
    5.90 -	virtual void BuildAdjacentMapList( void )
    5.91 -	{ m_pOrig->BuildAdjacentMapList(); }
    5.92 -	virtual bool GetUserMessageInfo( int msg_type, char *name, int maxnamelength, int& size )
    5.93 -	{ return m_pOrig->GetUserMessageInfo(msg_type, name, maxnamelength, size); }
    5.94 -	virtual CStandardSendProxies* GetStandardSendProxies()
    5.95 -	{ return m_pOrig->GetStandardSendProxies(); }
    5.96 -};
    5.97 -
    5.98 -#endif //_INCLUDE_CSERVER_GAMEDLL_H
     6.1 --- a/sourcemm/CServerGameEnts.h	Wed Oct 05 21:51:02 2005 +0000
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,45 +0,0 @@
     6.4 -/* ======== SourceMM ========
     6.5 -* Copyright (C) 2004-2005 Metamod:Source Development Team
     6.6 -* No warranties of any kind
     6.7 -*
     6.8 -* License: zlib/libpng
     6.9 -*
    6.10 -* Author(s): David "BAILOPAN" Anderson
    6.11 -* ============================
    6.12 -*/
    6.13 -
    6.14 -#ifndef _INCLUDE_CSERVER_GAMEENTS_H
    6.15 -#define _INCLUDE_CSERVER_GAMEENTS_H
    6.16 -
    6.17 -/**
    6.18 - * @brief Dummy class for IServerGameEnts
    6.19 - * @file CServerGameEnts.h
    6.20 - */
    6.21 -
    6.22 -#include <eiface.h>
    6.23 -
    6.24 -class CServerGameEnts : public IServerGameEnts
    6.25 -{
    6.26 -	IServerGameEnts *m_pOrig;
    6.27 -public:
    6.28 -	CServerGameEnts() : m_pOrig(0) {}
    6.29 -	void SetOrig(IServerGameEnts *pOrig)
    6.30 -	{
    6.31 -		m_pOrig = pOrig;
    6.32 -	}
    6.33 -
    6.34 -	virtual void SetDebugEdictBase(edict_t *base)
    6.35 -	{ m_pOrig->SetDebugEdictBase(base); }
    6.36 -	virtual void MarkEntitiesAsTouching(edict_t *e1, edict_t *e2)
    6.37 -	{ m_pOrig->MarkEntitiesAsTouching(e1, e2); }
    6.38 -	virtual void FreeContainingEntity(edict_t *e)
    6.39 -	{ m_pOrig->FreeContainingEntity(e); }
    6.40 -	virtual edict_t *BaseEntityToEdict(CBaseEntity *pEnt)
    6.41 -	{ return m_pOrig->BaseEntityToEdict(pEnt); }
    6.42 -	virtual CBaseEntity *EdictToBaseEntity(edict_t *pEdict)
    6.43 -	{ return m_pOrig->EdictToBaseEntity(pEdict); }
    6.44 -	virtual void CheckTransmit(CCheckTransmitInfo *pInfo, const unsigned short *pEdictIndices, int nEdicts)
    6.45 -	{ m_pOrig->CheckTransmit(pInfo, pEdictIndices, nEdicts); }
    6.46 -};
    6.47 -
    6.48 -#endif //_INCLUDE_CSERVER_GAMEENTS_H
     7.1 --- a/sourcemm/IPluginManager.h	Wed Oct 05 21:51:02 2005 +0000
     7.2 +++ b/sourcemm/IPluginManager.h	Thu Oct 06 00:59:01 2005 +0000
     7.3 @@ -19,6 +19,17 @@
     7.4  #include "ISmmPlugin.h"
     7.5  
     7.6  /**
     7.7 + * @brief Load sources
     7.8 + */
     7.9 +enum
    7.10 +{
    7.11 +	Pl_BadLoad=0,
    7.12 +	Pl_Console=-1,
    7.13 +	Pl_File=-2,
    7.14 +	Pl_MinId=1,
    7.15 +};
    7.16 +
    7.17 +/**
    7.18   * @brief Status of a plugin at runtime
    7.19   */
    7.20  enum Pl_Status
    7.21 @@ -30,17 +41,6 @@
    7.22  	Pl_Running=0,
    7.23  };
    7.24  
    7.25 -/**
    7.26 - * @brief Load sources
    7.27 - */
    7.28 -enum
    7.29 -{
    7.30 -	Pl_BadLoad=0,
    7.31 -	Pl_Console=-1,
    7.32 -	Pl_File=-2,
    7.33 -	Pl_MinId=1,
    7.34 -};
    7.35 -
    7.36  typedef int PluginId;
    7.37  struct factories;
    7.38  
    7.39 @@ -48,7 +48,9 @@
    7.40  {
    7.41  public:
    7.42  	/**
    7.43 -	 * @brief Loads a plugin and returns its id
    7.44 +	 * @brief Loads a plugin and returns its id.  If this is called before DLLInit(),
    7.45 +	 *  then the plugin is considered to be "hot" - it might refuse its own load later!
    7.46 +	 *  Also, a hot plugin might not have an error message.
    7.47  	 *
    7.48  	 * @param file String containing file name
    7.49  	 * @param source Specifies who loaded the plugin
    7.50 @@ -109,6 +111,16 @@
    7.51  	 * @return True on success, false if not found
    7.52  	 */
    7.53  	virtual bool Query(PluginId id, const char *&file, factories *&list, Pl_Status &status, PluginId &source) =0;
    7.54 +
    7.55 +	/** 
    7.56 +	 * @brief Checks another plugin's QueryRunning() status.
    7.57 +	 *
    7.58 +	 * @param id		Id of plugin
    7.59 +	 * @param error		Message buffer
    7.60 +	 * @param maxlen	Size of error buffer
    7.61 +	 * @return			Status value
    7.62 +	 */
    7.63 +	virtual bool QueryRunning(PluginId id, char *error, size_t maxlength) =0;
    7.64  };
    7.65  
    7.66  #endif //_INCLUDE_PLUGINMANAGER_H
     8.1 --- a/sourcemm/ISmmPlugin.h	Wed Oct 05 21:51:02 2005 +0000
     8.2 +++ b/sourcemm/ISmmPlugin.h	Thu Oct 06 00:59:01 2005 +0000
     8.3 @@ -38,42 +38,83 @@
     8.4  {
     8.5  public:
     8.6  	virtual int GetApiVersion() { return PLAPI_VERSION; }
     8.7 +	virtual ~ISmmPlugin() { }
     8.8  public:
     8.9  	/**
    8.10  	 * @brief Called on plugin load.
    8.11 +	 * NOTE - As of API 7, this is called as DLLInit() executes - after the parameters are known, but before 
    8.12 +	 *  the original GameDLL function is called.  Therefore, you cannot hook it, but you don't need to - 
    8.13 +	 *  Load() is basically your hook.
    8.14 +	 * NOTE - As of API 7, you can override factories before the engine and gamedll exchange them.
    8.15 +	 *  However, take care to note that if your plugin is unloaded, and the gamedll/engine have cached
    8.16 +	 *  an interface you've passed, something will definitely crash.  Be careful.
    8.17  	 *
    8.18 -	 * @param id Internal id of plugin.  Saved globally by PLUGIN_SAVEVARS()
    8.19 -	 * @param ismm External API for SourceMM.  Saved globally by PLUGIN_SAVEVARS()
    8.20 -	 * @param list Contains a list of factories.  Hook a factory call by setting one equal to your own function.
    8.21 +	 * @param id	Internal id of plugin.  Saved globally by PLUGIN_SAVEVARS()
    8.22 +	 * @param ismm	External API for SourceMM.  Saved globally by PLUGIN_SAVEVARS()
    8.23 +	 * @param list	Contains a list of factories.  Hook a factory call by setting one equal to your own function.
    8.24 +	 * @param late	Set to true if your plugin was loaded late (not at server load).
    8.25  	 * @param error Error message buffer
    8.26  	 * @param maxlen Size of error message buffer
    8.27 -	 * @return True on success, return false to request no load.
    8.28 +	 * @return		True if successful, return false to reject the load.
    8.29  	 */
    8.30 -	virtual bool Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlen) =0;
    8.31 +	virtual bool Load(PluginId id, ISmmAPI *ismm, factories *list, char *error, size_t maxlength, bool late) =0;
    8.32  
    8.33 -	/** @brief Called on plugin unload.
    8.34 +	/**
    8.35 +	 * @brief Called when your plugin is "queried".  This is useful for rejecting a loaded
    8.36 +	 *  state.  For example, if your plugin wants to stop operating, it can simply return
    8.37 +	 *  false and copy an error message.  This will notify other plugins or MM:S of something
    8.38 +	 *  bad that happened.  NOTE - MM:S will not cache the return state, so if you return false,
    8.39 +	 *  your plugin will not actually be paused or unloaded.  This callback will be called when:
    8.40 +	 *  - Another plugin requests it
    8.41 +	 *  - Someone types "meta list", it will show up as "REFUSED"
    8.42 +	 *  - When Metamod need to re-check the plugin's status
    8.43 +	 *  - If the plugin does something like overload a factory, Metamod will make sure the Query() returns true
    8.44 +	 *    before calling it.
    8.45 +	 *  Also note that this query will only override Metamod when the plugin is running and not paused.
    8.46  	 *
    8.47 -	 * @param error Error message buffer
    8.48 -	 * @param maxlen Size of error message buffer
    8.49 -	 * @return True on success, return false to request no unload.
    8.50 +	 * @param error		Buffer for error message.  This can be NULL!
    8.51 +	 * @param maxlen	Maximum length of error buffer.
    8.52 +	 * @return			Status code - true for okay, false for badness.
    8.53  	 */
    8.54 -	virtual bool Unload(char *error, size_t maxlen) =0;
    8.55 +	virtual bool QueryRunning(char *error, size_t maxlen)
    8.56 +	{
    8.57 +		return true;
    8.58 +	}
    8.59 +
    8.60 +	/** 
    8.61 +	 * @brief Called on plugin unload.  You can return false if you know your plugin
    8.62 +	 *  is not capable of restoring critical states it modifies.
    8.63 +	 *
    8.64 +	 * @param	error Error message buffer
    8.65 +	 * @param	maxlen Size of error message buffer
    8.66 +	 * @return	True on success, return false to request no unload.
    8.67 +	 */
    8.68 +	virtual bool Unload(char *error, size_t maxlen)
    8.69 +	{
    8.70 +		return true;
    8.71 +	}
    8.72  
    8.73  	/** @brief Called on plugin pause.
    8.74  	 *
    8.75 -	 * @param error Error message buffer
    8.76 -	 * @param maxlen Size of error message buffer
    8.77 -	 * @return True on success, return false to request no pause.
    8.78 +	 * @param	error Error message buffer
    8.79 +	 * @param	maxlen Size of error message buffer
    8.80 +	 * @return	True on success, return false to request no pause.
    8.81  	 */
    8.82 -	virtual bool Pause(char *error, size_t maxlen) =0;
    8.83 +	virtual bool Pause(char *error, size_t maxlen)
    8.84 +	{
    8.85 +		return true;
    8.86 +	}
    8.87  
    8.88  	/** @brief Called on plugin unpause.
    8.89  	 *
    8.90 -	 * @param error Error message buffer
    8.91 -	 * @param maxlen Size of error message buffer
    8.92 -	 * @return True on success, return false to request no unpause.
    8.93 +	 * @param	error Error message buffer
    8.94 +	 * @param	maxlen Size of error message buffer
    8.95 +	 * @return	True on success, return false to request no unpause.
    8.96  	 */
    8.97 -	virtual bool Unpause(char *error, size_t maxlen) =0;
    8.98 +	virtual bool Unpause(char *error, size_t maxlen)
    8.99 +	{
   8.100 +		return true;
   8.101 +	}
   8.102  public:
   8.103  	/** @brief Return author as string */
   8.104  	virtual const char *GetAuthor() =0;
   8.105 @@ -101,13 +142,16 @@
   8.106  public:
   8.107  	/**
   8.108  	 * @brief Called when all plugins have been loaded - API version 4
   8.109 -         *
   8.110 -         * Ths is useful for knowing when it's safe to request things another plugin might have.
   8.111 +	 *
   8.112 +	 * This is useful for knowing when it's safe to request things another plugin might have.
   8.113 +	 * NOTE for API 7 - This is called after DLLInit(). 
   8.114  	 */
   8.115 -	virtual void AllPluginsLoaded() =0;
   8.116 +	virtual void AllPluginsLoaded()
   8.117 +	{
   8.118 +	}
   8.119  };
   8.120  
   8.121 -#define PL_EXPOSURE	CreateInterface
   8.122 +#define PL_EXPOSURE		CreateInterface
   8.123  #define PL_EXPOSURE_C	"CreateInterface"
   8.124  
   8.125  #define PLUGIN_EXPOSE(name, var) \
   8.126 @@ -138,13 +182,13 @@
   8.127  	g_SMAPI->SetLastMetaReturn(mres); \
   8.128  	return value;
   8.129  
   8.130 -#define META_LOG		g_SMAPI->LogMsg
   8.131 +#define META_LOG				g_SMAPI->LogMsg
   8.132  #define META_REGCMD(name)		g_SMAPI->RegisterConCmdBase(g_PLAPI, name##_command)
   8.133  #define META_REGCVAR(var)		g_SMAPI->RegisterConCmdBase(g_PLAPI, var)
   8.134  #define META_UNREGCMD(name)		g_SMAPI->UnregisterConCmdBase(g_PLAPI, name##_command)
   8.135  #define META_UNREGCVAR(var)		g_SMAPI->UnregisterConCmdBase(g_PLAPI, var)
   8.136 -#define	META_CONPRINT	g_SMAPI->ConPrint
   8.137 -#define META_CONPRINTF	g_SMAPI->ConPrintf
   8.138 +#define	META_CONPRINT			g_SMAPI->ConPrint
   8.139 +#define META_CONPRINTF			g_SMAPI->ConPrintf
   8.140  
   8.141  //probably should use this up above someday
   8.142  #define CONCMD_VARNAME(name) name##_command
     9.1 --- a/sourcemm/oslink.cpp	Wed Oct 05 21:51:02 2005 +0000
     9.2 +++ b/sourcemm/oslink.cpp	Thu Oct 06 00:59:01 2005 +0000
     9.3 @@ -36,3 +36,25 @@
     9.4  	return errno;
     9.5  }
     9.6  #endif
     9.7 +
     9.8 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength)
     9.9 +{
    9.10 +#if defined WIN32 || defined _WIN32
    9.11 +	MEMORY_BASIC_INFORMATION mem;
    9.12 +	if (!VirtualQuery((void *)pAddr, &mem, sizeof(mem)))
    9.13 +		return false;
    9.14 +	if (mem.AllocationBase == NULL)
    9.15 +		return false;
    9.16 +	HMODULE dll = (HMODULE)mem.AllocationBase;
    9.17 +	GetModuleFileName(dll, (LPTSTR)buffer, maxlength);
    9.18 +#elif defined __linux__
    9.19 +	Dl_info info;
    9.20 +	if (!dladdr((void *)pAddr, &info))
    9.21 +		return false;
    9.22 +	if (!info.dli_fbase || !info.dli_fname)
    9.23 +		return false;
    9.24 +	const char *dllpath = info.dli_fname;
    9.25 +	snprintf(buffer, maxlength, "%s", dllpath);
    9.26 +#endif
    9.27 +	return true;
    9.28 +}
    10.1 --- a/sourcemm/oslink.h	Wed Oct 05 21:51:02 2005 +0000
    10.2 +++ b/sourcemm/oslink.h	Thu Oct 06 00:59:01 2005 +0000
    10.3 @@ -27,6 +27,10 @@
    10.4  	#define		dlsym(x, s)		GetProcAddress(x, s)
    10.5  	#define		dlclose(x)		FreeLibrary(x)
    10.6  	const char*	dlerror();
    10.7 +	#define	PATH_SEP_STR	"\\"
    10.8 +	#define PATH_SEP_CHAR	'\\'
    10.9 +	#define ALT_SEP_CHAR	'/'
   10.10 +	#define SERVER_DLL		"server.dll"
   10.11  #elif defined __linux__
   10.12  	#define OS_LINUX
   10.13  	#include <dlfcn.h>
   10.14 @@ -35,6 +39,11 @@
   10.15  	#include <dirent.h>
   10.16  	#define		dlmount(x)		dlopen(x,RTLD_NOW)
   10.17  	typedef		void*			HINSTANCE;
   10.18 +	#define	PATH_SEP_STR	"/"
   10.19 +	#define PATH_SEP_CHAR	'/'
   10.20 +	#define ALT_SEP_CHAR	'\\'
   10.21 +	#define	stricmp				strcasecmp
   10.22 +	#define SERVER_DLL		"server_i486.so"
   10.23  #endif
   10.24  
   10.25  #if defined __linux__
   10.26 @@ -42,6 +51,8 @@
   10.27  	int GetLastError();
   10.28  #endif
   10.29  
   10.30 +bool GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength);
   10.31 +
   10.32  #if defined __WIN32__ || defined _WIN32 || defined WIN32
   10.33  	#define SMM_API extern "C" __declspec(dllexport)
   10.34  #elif defined __GNUC__
    11.1 --- a/sourcemm/sourcemm.cpp	Wed Oct 05 21:51:02 2005 +0000
    11.2 +++ b/sourcemm/sourcemm.cpp	Thu Oct 06 00:59:01 2005 +0000
    11.3 @@ -12,10 +12,6 @@
    11.4  
    11.5  #include <interface.h>
    11.6  #include <eiface.h>
    11.7 -#include "CServerGameDLL.h"
    11.8 -#include "CServerGameEnts.h"
    11.9 -#include "CServerGameClients.h"
   11.10 -#include "CHLTVDirector.h"
   11.11  #include "sourcemm.h"
   11.12  #include "concommands.h"
   11.13  #include "CSmmAPI.h"
   11.14 @@ -26,262 +22,389 @@
   11.15   * @file sourcemm.cpp
   11.16   */
   11.17  
   11.18 -CServerGameDLL g_TempGameDLL;
   11.19 -CServerGameEnts g_TempGameEnts;
   11.20 -CServerGameClients g_TempGameClients;
   11.21 -CHLTVDirector g_TempDirector;
   11.22 -GameDllInfo g_GameDll = {false, NULL, NULL};
   11.23 -EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
   11.24 +SH_DECL_HOOK4(IServerGameDLL, DLLInit, SH_NOATTRIB, false, bool, CreateInterfaceFn, CreateInterfaceFn, CreateInterfaceFn, CGlobalVars *);
   11.25 +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, false);
   11.26 +SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
   11.27 +SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
   11.28 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals);
   11.29 +void DLLShutdown_handler();
   11.30 +void LevelShutdown_handler();
   11.31 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background);
   11.32 +
   11.33 +GameDllInfo g_GameDll = {false, NULL, NULL, NULL};
   11.34 +EngineInfo g_Engine;
   11.35  SourceHook::CSourceHookImpl g_SourceHook;
   11.36 -SourceHook::ISourceHook *g_SHPtr;
   11.37 +SourceHook::ISourceHook *g_SHPtr = &g_SourceHook;
   11.38  SourceHook::String g_ModPath;
   11.39  SourceHook::String g_BinPath;
   11.40  PluginId g_PLID = Pl_Console;		//Technically, SourceMM is the "Console" plugin... :p
   11.41  bool bInFirstLevel = true;
   11.42 +bool gParsedGameInfo = false;
   11.43 +SourceHook::List<GameDllInfo *> gamedll_list;
   11.44 +SourceHook::CallClass<IServerGameDLL> *dllExec;
   11.45 +
   11.46 +void ClearGamedllList();
   11.47  
   11.48  ///////////////////////////////////
   11.49  // Main code for HL2 Interaction //
   11.50  ///////////////////////////////////
   11.51  
   11.52 +//Initialize everything here
   11.53 +void InitMainStates()
   11.54 +{
   11.55 +	char full_path[260] = {0};
   11.56 +	GetFileOfAddress(g_GameDll.factory, full_path, sizeof(full_path)-1);
   11.57 +	g_BinPath.assign(full_path);
   11.58 +
   11.59 +	UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
   11.60 +
   11.61 +	//Like metamod, reload plugins at the end of the map.
   11.62 +	//This is so plugins can hook everything on load, BUT, new plugins will be reloaded
   11.63 +	// if the server is shut down (silly, but rare case).
   11.64 +	bInFirstLevel = true;
   11.65 +
   11.66 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLInit, g_GameDll.pGameDLL, DLLInit, false);
   11.67 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, g_GameDll.pGameDLL, DLLShutdown_handler, false);
   11.68 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelShutdown, g_GameDll.pGameDLL, LevelShutdown_handler, true);
   11.69 +	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, LevelInit, g_GameDll.pGameDLL, LevelInit_handler, true);
   11.70 +}
   11.71 +
   11.72 +bool DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn filesystemFactory, CGlobalVars *pGlobals)
   11.73 +{
   11.74 +	g_Engine.engineFactory = engineFactory;
   11.75 +	g_Engine.fileSystemFactory = filesystemFactory;
   11.76 +	g_Engine.physicsFactory = physicsFactory;
   11.77 +	g_Engine.pGlobals = pGlobals;
   11.78 +
   11.79 +	g_Engine.engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
   11.80 +	if (!g_Engine.engine)
   11.81 +	{
   11.82 +		Error("Could not find IVEngineServer! Metamod cannot load.");
   11.83 +		return false;
   11.84 +	}
   11.85 +	g_Engine.icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION , NULL));
   11.86 +	if (!g_Engine.icvar)
   11.87 +	{
   11.88 +		Error("Could not find ICvar! Metamod cannot load.");
   11.89 +		return false;
   11.90 +	}
   11.91 +
   11.92 +	g_Engine.loaded = true;
   11.93 +
   11.94 +	//Initialize our console hooks
   11.95 +	ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
   11.96 +
   11.97 +	if (!g_SmmAPI.CacheCmds())
   11.98 +	{
   11.99 +		LogMessage("[META] Warning: Failed to initialize Con_Printf.  Defaulting to Msg().");
  11.100 +		LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
  11.101 +	}
  11.102 +
  11.103 +	char full_path[260];
  11.104 +	UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
  11.105 +
  11.106 +	LoadPluginsFromFile(full_path);
  11.107 +
  11.108 +	g_PluginMngr.SetAllLoaded();
  11.109 +
  11.110 +	bInFirstLevel = true;
  11.111 +
  11.112 +	dllExec = SH_GET_CALLCLASS(g_GameDll.pGameDLL);
  11.113 +
  11.114 +	RETURN_META_VALUE(MRES_IGNORED, true);
  11.115 +}
  11.116 +
  11.117  //This is where the magic happens
  11.118  SMM_API void *CreateInterface(const char *name, int *ret)
  11.119  {
  11.120 -	if (!g_GameDll.loaded)
  11.121 +	if (!gParsedGameInfo)
  11.122  	{
  11.123 -		//State of the Mod:
  11.124 -		// Currently, HL2 Engine has loaded Metamod:Source
  11.125 -		// It is now asking it to get an interface.  We don't have one,
  11.126 -		//  so we're gonna try to give it a fake one to get the information we need.
  11.127 -		//  Then, the the interface will forward the calls to the original interface
  11.128 +		gParsedGameInfo = true;
  11.129 +		char curpath[260] = {0};
  11.130 +		char dllpath[260] = {0};
  11.131 +		getcwd(curpath, sizeof(curpath)-1);
  11.132 +		if (!GetFileOfAddress((void *)CreateInterface, dllpath, sizeof(dllpath)-1))
  11.133 +		{
  11.134 +			Error("GetFileOfAddress() failed! Metamod cannot load.");
  11.135 +			return NULL;
  11.136 +		}
  11.137 +		SourceHook::String s_dllpath(dllpath);
  11.138 +		//begin the heuristics for searching for the mod path (these are quite ugly).
  11.139 +		//for OS compatibility purposes, we're going to do case insensitivity on windows.
  11.140 +		size_t path_len = strlen(curpath);
  11.141 +		size_t dll_len = strlen(dllpath);
  11.142  
  11.143 -		if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
  11.144 +		//strip the dll path off
  11.145 +		//:TODO: with path stuff - in Linux, \ can exist in a file path as a non-seperator!
  11.146 +		for (size_t i=dll_len-1; i>0; i--)
  11.147  		{
  11.148 -			//We're in.  Give the server our fake class as bait.
  11.149 -			if (ret)
  11.150 -				*ret = IFACE_OK;
  11.151 -			return static_cast<void *>(&g_TempGameDLL);
  11.152 -		} else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
  11.153 -			if (ret)
  11.154 -				*ret = IFACE_OK;
  11.155 +			if (dllpath[i] == '\\' || dllpath[i] == '/')
  11.156 +			{
  11.157 +				if (i == dll_len-1)
  11.158 +					break;
  11.159 +				//save path by stripping off file name and ending terminator
  11.160 +				dllpath[i] = '\0';
  11.161 +				dll_len = i;
  11.162 +				break;
  11.163 +			}
  11.164 +		}
  11.165 +
  11.166 +		//strip absolute path terminators if any!
  11.167 +		if (curpath[path_len-1] == '/' || curpath[path_len-1] == '\\')
  11.168 +			curpath[--path_len] = '\0';
  11.169  
  11.170 -			return static_cast<void *>(&g_TempGameEnts);
  11.171 -		} else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
  11.172 -			if (ret)
  11.173 -				*ret = IFACE_OK;
  11.174 +		//if the base path doesn't fit into the module path, something is wrong!
  11.175 +		// ex: c:\games\srcds
  11.176 +		//     c:\gaben.dll
  11.177 +		if (path_len > dll_len)
  11.178 +		{
  11.179 +			Error("Could not detect GameDLL path! Metamod cannot load[1].");
  11.180 +			return NULL;
  11.181 +		}
  11.182  
  11.183 -			return static_cast<void *>(&g_TempGameClients);
  11.184 -		} else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
  11.185 -			if (ret)
  11.186 -				*ret = IFACE_OK;
  11.187 -
  11.188 -			return static_cast<void *>(&g_TempDirector);
  11.189 -		} else {
  11.190 -			if (ret)
  11.191 -				*ret = IFACE_FAILED;
  11.192 -
  11.193 +		//we are now in such a position that the two dir names SHOULD MATCH!
  11.194 +		typedef int (*STRNCMP)(const char *str1, const char *str2, size_t n);
  11.195 +		STRNCMP cmp = 
  11.196 +#ifdef WIN32
  11.197 +			strnicmp;
  11.198 +#else
  11.199 +			strncmp;
  11.200 +#endif
  11.201 +		//are they equal?
  11.202 +		if ( ((cmp)(curpath, dllpath, path_len)) != 0 )
  11.203 +		{
  11.204 +			//:TODO: In this case, we should read /proc/self/maps and find srcds!
  11.205 +			Error("Could not detect GameDLL path! Metamod cannot load[2].");
  11.206  			return NULL;
  11.207  		}
  11.208 -	} else {
  11.209 -		META_INTERFACE_MACRO(server, g_GameDll.factory);
  11.210 -	}
  11.211 -}
  11.212 +		//this will skip past the dir and its separator char
  11.213 +		char *ptr = &(dllpath[path_len+1]);
  11.214 +		path_len = strlen(ptr);
  11.215 +		for (size_t i=0; i<path_len; i++)
  11.216 +		{
  11.217 +			if (ptr[i] == '/' || ptr[i] == '\\')
  11.218 +			{
  11.219 +				if (i == 0)
  11.220 +				{
  11.221 +					Error("Could not detect GameDLL path! Metamod cannot load[3].");
  11.222 +					return NULL;
  11.223 +				} else {
  11.224 +					ptr[i] = '\0';
  11.225 +					path_len = i+1;
  11.226 +					break;
  11.227 +				}
  11.228 +			}
  11.229 +		}
  11.230 +		//WE NOW HAVE A GUESS AT THE MOD DIR.  OH MY GOD.
  11.231 +		char temp_path[260];
  11.232 +		snprintf(temp_path, sizeof(temp_path)-1, 
  11.233 +			"%s%s%s", 
  11.234 +			curpath, 
  11.235 +			PATH_SEP_STR, 
  11.236 +			ptr);
  11.237 +
  11.238 +		g_ModPath.assign(temp_path);
  11.239 +
  11.240 +		snprintf(temp_path, sizeof(temp_path)-1, 
  11.241 +			"%s%s%s", 
  11.242 +			g_ModPath.c_str(),
  11.243 +			PATH_SEP_STR,
  11.244 +			"gameinfo.txt");
  11.245  
  11.246 -bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
  11.247 -{
  11.248 -	if (m_pOrig)
  11.249 -		return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
  11.250 +		FILE *fp = fopen(temp_path, "rt");
  11.251 +		if (!fp)
  11.252 +		{
  11.253 +			Error("Unable to open gameinfo.txt!  Metamod cannot load.");
  11.254 +			return NULL;
  11.255 +		}
  11.256 +		char buffer[255];
  11.257 +		char key[128], val[128];
  11.258 +		size_t len = 0;
  11.259 +		const char *gameinfo = "|gameinfo_path|";
  11.260 +		size_t gameinfo_len = strlen(gameinfo);
  11.261 +		bool search = false;
  11.262 +		bool gamebin = false;
  11.263 +		const char *lptr;
  11.264 +		while (!feof(fp))
  11.265 +		{
  11.266 +			buffer[0] = '\0';
  11.267 +			fgets(buffer, sizeof(buffer)-1, fp);
  11.268 +			len = strlen(buffer);
  11.269 +			if (buffer[len-1] == '\n')
  11.270 +				buffer[--len] = '\0';
  11.271 +			UTIL_TrimComments(buffer);
  11.272 +			UTIL_TrimLeft(buffer);
  11.273 +			UTIL_TrimRight(buffer);
  11.274 +			if (stricmp(buffer, "SearchPaths") == 0)
  11.275 +				search = true;
  11.276 +			if (!search)
  11.277 +				continue;
  11.278 +			UTIL_KeySplit(buffer, key, sizeof(key)-1, val, sizeof(val)-1);
  11.279 +			if (stricmp(key, "Game") == 0 || stricmp(key, "GameBin") == 0)
  11.280 +			{
  11.281 +				if (stricmp(key, "Game") == 0)
  11.282 +					gamebin = false;
  11.283 +				else
  11.284 +					gamebin = true;
  11.285 +
  11.286 +				if (strncmp(val, gameinfo, gameinfo_len) == 0)
  11.287 +				{
  11.288 +					ptr = &(val[gameinfo_len]);
  11.289 +					if (ptr[0] == '.')
  11.290 +						ptr++;
  11.291 +					lptr = g_ModPath.c_str();
  11.292 +				} else {
  11.293 +					lptr = curpath;
  11.294 +					ptr = val;
  11.295 +				}
  11.296 +				size_t ptr_len = strlen(ptr);
  11.297 +				if (ptr[ptr_len] == '/' || ptr[ptr_len] == '\\')
  11.298 +					ptr[--ptr_len] = '\0';
  11.299 +				//no need to append "bin"
  11.300 +				if (gamebin)
  11.301 +				{
  11.302 +					UTIL_PathFmt(temp_path, sizeof(temp_path)-1, "%s/%s/%s", lptr, ptr, SERVER_DLL);
  11.303 +				} else {
  11.304 +					UTIL_PathFmt(temp_path, sizeof(temp_path)-1, "%s/%s/%s/%s", lptr, ptr, "bin", SERVER_DLL);
  11.305 +				}
  11.306 +				if (!UTIL_PathCmp(s_dllpath.c_str(), temp_path))
  11.307 +				{
  11.308 +					FILE *fp = fopen(temp_path, "rb");
  11.309 +					if (!fp)
  11.310 +						continue;
  11.311 +					//:TODO: Optimize this a bit!
  11.312 +					SourceHook::List<GameDllInfo *>::iterator iter;
  11.313 +					GameDllInfo *pCheck;
  11.314 +					bool found = false;
  11.315 +					for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
  11.316 +					{
  11.317 +						pCheck = (*iter);
  11.318 +						if (GetFileOfAddress(pCheck->factory, buffer, sizeof(buffer)-1))
  11.319 +						{
  11.320 +							if (UTIL_PathCmp(temp_path, buffer))
  11.321 +							{
  11.322 +								found = true;
  11.323 +								break;
  11.324 +							}
  11.325 +						}
  11.326 +					}
  11.327 +					if (found)
  11.328 +						continue;
  11.329 +					fclose(fp);
  11.330 +					HINSTANCE gamedll = dlmount(temp_path);
  11.331 +					if (gamedll == NULL)
  11.332 +						continue;
  11.333 +					CreateInterfaceFn fn = (CreateInterfaceFn)dlsym(gamedll, "CreateInterface");
  11.334 +					if (fn == NULL)
  11.335 +					{
  11.336 +						dlclose(gamedll);
  11.337 +						continue;
  11.338 +					}
  11.339 +					GameDllInfo *pInfo = new GameDllInfo;
  11.340 +					pInfo->factory = fn;
  11.341 +					pInfo->lib = gamedll;
  11.342 +					pInfo->loaded = true;
  11.343 +					pInfo->pGameDLL = NULL;
  11.344 +					gamedll_list.push_back(pInfo);
  11.345 +				}
  11.346 +			}
  11.347 +		}
  11.348 +		fclose(fp);
  11.349 +	}
  11.350  
  11.351  	if (!g_GameDll.loaded)
  11.352  	{
  11.353 -		//Initialize SourceHook
  11.354 -		g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
  11.355 -
  11.356 -		//The gamedll isn't loaded yet.  We need to find out where it's hiding.
  11.357 -		IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
  11.358 -		if (!ive)
  11.359 -		{
  11.360 -			Error("Metamod:Source could not load %s", INTERFACEVERSION_VENGINESERVER);
  11.361 -			return false;
  11.362 -		}
  11.363 -		
  11.364 -		//Guess the file name
  11.365 -		//We'll have better heurestics[sp?] later on
  11.366 -		char mod_path[128], full_path[255];
  11.367 -		ive->GetGameDir(mod_path, sizeof(mod_path)-1);
  11.368 -		g_ModPath.assign(mod_path);
  11.369 -#if defined WIN32 || defined _WIN32
  11.370 -		snprintf(full_path, sizeof(full_path)-1, "%s\\bin\\server.dll", mod_path);
  11.371 -#else
  11.372 -		snprintf(full_path, sizeof(full_path)-1, "%s/bin/server_i486.so", mod_path);
  11.373 -#endif
  11.374 -
  11.375 -		g_BinPath.assign(full_path);
  11.376 -
  11.377 -		//See if the file even exists
  11.378 -		FILE *fp = fopen(g_BinPath.c_str(), "r");
  11.379 -		if (!fp)
  11.380 -		{
  11.381 -			Error("Metamod:Source could not read %s", g_BinPath.c_str());
  11.382 -			return false;
  11.383 -		}
  11.384 -		fclose(fp);
  11.385 -		fp = NULL;
  11.386 +		const char *str = "ServerGameDLL";
  11.387 +		size_t len = strlen(str);
  11.388  
  11.389 -		//Load the DLL
  11.390 -		g_GameDll.lib = dlmount(g_BinPath.c_str());
  11.391 -		if (!g_GameDll.lib)
  11.392 +		if (strncmp(name, str, len) == 0)
  11.393  		{
  11.394 -			Error("Metamod:Source could not load GameDLL: %s", dlerror());
  11.395 -			return false;
  11.396 -		} else {
  11.397 -			//Find its factory
  11.398 -			g_GameDll.factory = (CreateInterfaceFn)(dlsym(g_GameDll.lib, "CreateInterface"));
  11.399 -			if (!g_GameDll.factory)
  11.400 +			//This is the interface we want!  Right now we support versions 3 and 4.
  11.401 +			int version = atoi(&(name[len]));
  11.402 +			if (version < MIN_GAMEDLL_VERSION || version > MAX_GAMEDLL_VERSION)
  11.403  			{
  11.404 -				Error("Metamod:Source could not find an entry point in GameDLL: %s", g_BinPath.c_str());
  11.405 -				dlclose(g_GameDll.lib);
  11.406 -				return false;
  11.407 -			}
  11.408 -
  11.409 -			//Find the new IServerGameDLL pointer
  11.410 -			IServerGameDLL *serverDll;
  11.411 -			serverDll = (IServerGameDLL *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEDLL, NULL));
  11.412 -			if (!serverDll)
  11.413 -			{
  11.414 -				Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEDLL, g_BinPath.c_str());
  11.415 -				dlclose(g_GameDll.lib);
  11.416 -				return false;
  11.417 -			}
  11.418 -
  11.419 -			//Set this information early in case our wrappers are called somehow
  11.420 -			g_Engine.engineFactory = engineFactory;
  11.421 -			g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
  11.422 -			if (!g_Engine.icvar)
  11.423 -			{
  11.424 -				Error("Metamod:Source could not find %s in engine!", VENGINE_CVAR_INTERFACE_VERSION);
  11.425 -				dlclose(g_GameDll.lib);
  11.426 -				return false;
  11.427 +				Error("GameDLL version %d is not supported by Metamod!", version);
  11.428 +				return NULL;
  11.429  			}
  11.430 -			g_Engine.fileSystemFactory = fileSystemFactory;
  11.431 -			g_Engine.pGlobals = pGlobals;
  11.432 -			g_Engine.physicsFactory = physicsFactory;
  11.433 -			g_Engine.engine = ive;
  11.434 -			
  11.435 -			//Attempt to load the GameDLL
  11.436 -			// Note that nothing will be intercepting yet.
  11.437 -			// This is the one and only call that plugins have no chance of seeing.
  11.438 -			// Likewise, you won't be able to trick the Server DLL into loading random things.
  11.439 -			// Luckily, because of SourceHook, this really isn't a problem - you can change
  11.440 -			//  the virtual interfaces in anything it requests.
  11.441 -			if (!serverDll->DLLInit(EngineFactory, PhysicsFactory, FileSystemFactory, pGlobals))
  11.442 +			SourceHook::List<GameDllInfo *>::iterator iter;
  11.443 +			GameDllInfo *pInfo = NULL;
  11.444 +			void *ptr;
  11.445 +			for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
  11.446  			{
  11.447 -				//For some reason, the GameDLL failed to load. 
  11.448 -				Error("Metamod:Source: GameDLL %s refused to load.", g_BinPath.c_str());
  11.449 -				dlclose(g_GameDll.lib);
  11.450 -				return false;
  11.451 -			}
  11.452 -
  11.453 -			//Retrieve the pointers we'll need from the GameDLL
  11.454 -			IServerGameEnts *serverEnts;
  11.455 -			IServerGameClients *serverClients;
  11.456 -			IHLTVDirector *serverHLTV;
  11.457 -
  11.458 -			serverEnts = (IServerGameEnts *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMEENTS, NULL));
  11.459 -			if (!serverEnts)
  11.460 -			{
  11.461 -				Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMEENTS, g_BinPath.c_str());
  11.462 -				dlclose(g_GameDll.lib);
  11.463 -				return false;
  11.464 -			}
  11.465 -
  11.466 -			serverClients = (IServerGameClients *)((g_GameDll.factory)(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
  11.467 -			if (!serverClients)
  11.468 -			{
  11.469 -				Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_SERVERGAMECLIENTS, g_BinPath.c_str());
  11.470 -				dlclose(g_GameDll.lib);
  11.471 -				return false;
  11.472 +				pInfo = (*iter);
  11.473 +				ptr = (pInfo->factory)(name, ret);
  11.474 +				if (ptr)
  11.475 +				{
  11.476 +					//this is our gamedll.  unload the others.
  11.477 +					gamedll_list.erase(iter);
  11.478 +					ClearGamedllList();
  11.479 +					pInfo->pGameDLL = static_cast<IServerGameDLL *>(ptr);
  11.480 +					g_GameDll = *pInfo;
  11.481 +					delete pInfo;
  11.482 +					break;
  11.483 +				}
  11.484  			}
  11.485 -
  11.486 -            serverHLTV = (IHLTVDirector *)((g_GameDll.factory)(INTERFACEVERSION_HLTVDIRECTOR, NULL));
  11.487 -			if (!serverHLTV)
  11.488 -			{
  11.489 -				Error("Metamod:Source could not find %s in GameDLL: %s", INTERFACEVERSION_HLTVDIRECTOR, g_BinPath.c_str());
  11.490 -				dlclose(g_GameDll.lib);
  11.491 -				return false;
  11.492 -			}
  11.493 -			
  11.494 -			// Now tell the global temp classes that they can call the original functions
  11.495 -			g_TempDirector.SetOrig(serverHLTV);
  11.496 -			g_TempGameClients.SetOrig(serverClients);
  11.497 -			g_TempGameEnts.SetOrig(serverEnts);
  11.498 -			g_TempGameDLL.SetOrig(serverDll);
  11.499 -
  11.500 -			//Everything's done.
  11.501 -			g_GameDll.loaded = true;
  11.502 -
  11.503 -			//Initialize our console hooks
  11.504 -			ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
  11.505 -
  11.506 -			if (!g_SmmAPI.CacheCmds())
  11.507 +			if (g_GameDll.loaded)
  11.508  			{
  11.509 -				LogMessage("[META] Warning: Failed to initialize Con_Printf.  Defaulting to Msg().");
  11.510 -				LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
  11.511 +				InitMainStates();
  11.512 +			} else {
  11.513 +				if (ret)
  11.514 +					*ret = IFACE_FAILED;
  11.515 +				return NULL;
  11.516  			}
  11.517 -            
  11.518 -			//Now it's safe to load plugins.
  11.519 -#if defined WIN32 || defined _WIN32
  11.520 -			snprintf(full_path, sizeof(full_path)-1, "%s\\%s", g_ModPath.c_str(), GetPluginsFile());
  11.521 -#else
  11.522 -			snprintf(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), GetPluginsFile());
  11.523 -#endif
  11.524 -
  11.525 -			LoadPluginsFromFile(full_path);
  11.526 -
  11.527 -			//All plugins are now loaded.
  11.528 -			g_PluginMngr.SetAllLoaded();
  11.529 -
  11.530 -			//Like metamod, reload plugins at the end of the map.
  11.531 -			//This is so plugins can hook everything on load, BUT, new plugins will be reloaded
  11.532 -			// if the server is shut down (silly, but rare case).
  11.533 -			bInFirstLevel = true;
  11.534 -
  11.535 -			return true;
  11.536 +		} else {
  11.537 +			//wtf do we do...
  11.538 +			//:TODO: .. something a bit more intelligent?
  11.539 +			Error("Engine requested unknown interface before GameDLL was known!");
  11.540 +			return NULL;
  11.541  		}
  11.542  	}
  11.543  
  11.544 -	//Somehow, the function got here.  This should be impossible.
  11.545 -	Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
  11.546 +		SetUnhandledExceptionFilter(NULL);
  11.547  
  11.548 -	return false;
  11.549 +	//if we got here, there's definitely a gamedll.
  11.550 +	//META_INTERFACE_MACRO(server, g_GameDll.factory);
  11.551 +	return (g_GameDll.factory)(name, ret);
  11.552  }
  11.553  
  11.554 -void Shutdown()
  11.555 +void ClearGamedllList()
  11.556 +{
  11.557 +	SourceHook::List<GameDllInfo *>::iterator iter;
  11.558 +
  11.559 +	GameDllInfo *pInfo;
  11.560 +	for (iter=gamedll_list.begin(); iter!=gamedll_list.end(); iter++)
  11.561 +	{
  11.562 +		pInfo = (*iter);
  11.563 +		dlclose(pInfo->lib);
  11.564 +		delete pInfo;
  11.565 +	}
  11.566 +
  11.567 +	gamedll_list.clear();
  11.568 +}
  11.569 +
  11.570 +void DLLShutdown_handler()
  11.571  {
  11.572  	//Unload plugins
  11.573  	g_PluginMngr.UnloadAll();
  11.574  
  11.575 -	// Shutdown sourcehook now
  11.576 -	g_SourceHook.CompleteShutdown();
  11.577 -
  11.578  	// Add the FCVAR_GAMEDLL flag to our cvars so the engine removes them properly
  11.579  	g_SMConVarAccessor.MarkCommandsAsGameDLL();
  11.580 -}
  11.581 +	g_SMConVarAccessor.UnregisterGameDLLCommands();
  11.582  
  11.583 -void CServerGameDLL::DLLShutdown()
  11.584 -{
  11.585 -	Shutdown();
  11.586 +	SH_CALL(dllExec, &IServerGameDLL::DLLShutdown)();
  11.587  
  11.588 -	//Call the original function
  11.589 -	m_pOrig->DLLShutdown();
  11.590 +	SH_RELEASE_CALLCLASS(dllExec);
  11.591 +	dllExec = NULL;
  11.592  
  11.593 -	//Unregister all commands marked as GameDLL now
  11.594 -	//This prevents crashes when the engine tries to unregister them once we have already unloaded the gamedll
  11.595 -	//(we used to unload the gamedll in a __attribute__((destructor)) function but I've had problems with that (crashes in dlclose)
  11.596 -	g_SMConVarAccessor.UnregisterGameDLLCommands();
  11.597 +	//right now this will crash when the function returns!
  11.598 +	// :TODO: remove this warning once PM fixes it.
  11.599 +	g_SourceHook.CompleteShutdown();
  11.600  
  11.601  	if (g_GameDll.lib && g_GameDll.loaded)
  11.602  		dlclose(g_GameDll.lib);
  11.603  	memset(&g_GameDll, 0, sizeof(GameDllInfo));
  11.604 +
  11.605 +	RETURN_META(MRES_SUPERCEDE);
  11.606  }
  11.607  
  11.608  int LoadPluginsFromFile(const char *file)
  11.609 @@ -345,11 +468,7 @@
  11.610  				ext = "";
  11.611  			}
  11.612  			//Format the new path
  11.613 -#if defined WIN32 || defined _WIN32
  11.614 -			snprintf(full_path, sizeof(full_path)-1, "%s\\%s%s", g_ModPath.c_str(), buffer, ext);
  11.615 -#else
  11.616 -			snprintf(full_path, sizeof(full_path)-1, "%s/%s%s", g_ModPath.c_str(), buffer, ext);
  11.617 -#endif
  11.618 +			UTIL_PathFmt(full_path, sizeof(full_path)-1, "%s/%s", g_ModPath.c_str(), buffer, ext);
  11.619  			id = g_PluginMngr.Load(full_path, Pl_File, already, error, sizeof(error)-1);
  11.620  			if (id < Pl_MinId || g_PluginMngr.FindById(id)->m_Status < Pl_Paused)
  11.621  			{
  11.622 @@ -407,13 +526,12 @@
  11.623  	strcat(buffer, "\n");
  11.624  	va_end(ap);
  11.625  
  11.626 -	g_Engine.engine->LogPrint(buffer);
  11.627 -}
  11.628 -
  11.629 -void CServerGameDLL::LevelShutdown(void)
  11.630 -{
  11.631 -	LevelShutdown_handler();
  11.632 -	m_pOrig->LevelShutdown();
  11.633 +	if (!g_Engine.engine)
  11.634 +	{
  11.635 +		fprintf(stdout, "%s", buffer);
  11.636 +	} else {
  11.637 +		g_Engine.engine->LogPrint(buffer);
  11.638 +	}
  11.639  }
  11.640  
  11.641  void LevelShutdown_handler(void)
  11.642 @@ -432,7 +550,7 @@
  11.643  	}
  11.644  }
  11.645  
  11.646 -bool CServerGameDLL::LevelInit( char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background )
  11.647 +bool LevelInit_handler(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
  11.648  { 
  11.649  	if (!g_SmmAPI.CacheSuccessful())
  11.650  	{
  11.651 @@ -440,19 +558,7 @@
  11.652  		LogMessage("[META] Warning: Console messages will not be redirected to rcon console.");
  11.653  	}
  11.654  
  11.655 -	return m_pOrig->LevelInit(pMapName, pMapEntities, pOldLevel, pLandmarkName, loadGame, background);
  11.656 -}
  11.657 -
  11.658 -const char *CServerGameDLL::GetGameDescription()
  11.659 -{
  11.660 -	if (m_pOrig)
  11.661 -	{
  11.662 -		const char *game = m_pOrig->GetGameDescription();
  11.663 -		if (game)
  11.664 -			strcpy(m_GameDescBuffer, game);
  11.665 -	}
  11.666 -
  11.667 -	return m_GameDescBuffer;
  11.668 +	RETURN_META_VALUE(MRES_IGNORED, false);
  11.669  }
  11.670  
  11.671  #if defined __GNUC__ && (__GNUC__ == 3)
    12.1 --- a/sourcemm/sourcemm.h	Wed Oct 05 21:51:02 2005 +0000
    12.2 +++ b/sourcemm/sourcemm.h	Thu Oct 06 00:59:01 2005 +0000
    12.3 @@ -29,6 +29,9 @@
    12.4  #define		SOURCEMM_VERSION	"1.10"
    12.5  #define		SOURCEMM_DATE		__DATE__
    12.6  
    12.7 +#define	MIN_GAMEDLL_VERSION	3
    12.8 +#define	MAX_GAMEDLL_VERSION	4
    12.9 +
   12.10  /**
   12.11   * @brief Entry point for HL2 Engine
   12.12   */
   12.13 @@ -55,11 +58,17 @@
   12.14  	bool loaded;
   12.15  	HINSTANCE lib;
   12.16  	CreateInterfaceFn factory;
   12.17 +	IServerGameDLL *pGameDLL;
   12.18  };
   12.19  
   12.20  /** @brief Stores information about the HL2 Engine pointers */
   12.21  struct EngineInfo
   12.22  {
   12.23 +	EngineInfo() : loaded(false), 
   12.24 +		engineFactory(NULL), physicsFactory(NULL), fileSystemFactory(NULL),
   12.25 +		pGlobals(NULL), icvar(NULL), engine(NULL)
   12.26 +	{ };
   12.27 +	bool loaded;
   12.28  	CreateInterfaceFn engineFactory;
   12.29  	CreateInterfaceFn physicsFactory;
   12.30  	CreateInterfaceFn fileSystemFactory;
   12.31 @@ -89,7 +98,4 @@
   12.32  /** @brief We have our own internal plugin id... */
   12.33  extern PluginId g_PLID;
   12.34  
   12.35 -/** @brief Hook for LevelShutdown */
   12.36 -void LevelShutdown_handler(void);
   12.37 -
   12.38  #endif //_INCLUDE_SOURCEMM_H
    13.1 --- a/sourcemm/sourcemm.vcproj	Wed Oct 05 21:51:02 2005 +0000
    13.2 +++ b/sourcemm/sourcemm.vcproj	Thu Oct 06 00:59:01 2005 +0000
    13.3 @@ -155,33 +155,18 @@
    13.4  			Filter="h;hpp;hxx;hm;inl;inc;xsd"
    13.5  			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
    13.6  			<File
    13.7 -				RelativePath=".\CHLTVDirector.h">
    13.8 -			</File>
    13.9 -			<File
   13.10  				RelativePath=".\concommands.h">
   13.11  			</File>
   13.12  			<File
   13.13  				RelativePath=".\CPlugin.h">
   13.14  			</File>
   13.15  			<File
   13.16 -				RelativePath=".\CServerGameClients.h">
   13.17 -			</File>
   13.18 -			<File
   13.19 -				RelativePath=".\CServerGameDLL.h">
   13.20 -			</File>
   13.21 -			<File
   13.22 -				RelativePath=".\CServerGameEnts.h">
   13.23 -			</File>
   13.24 -			<File
   13.25  				RelativePath=".\CSmmAPI.h">
   13.26  			</File>
   13.27  			<File
   13.28  				RelativePath=".\oslink.h">
   13.29  			</File>
   13.30  			<File
   13.31 -				RelativePath=".\resource.h">
   13.32 -			</File>
   13.33 -			<File
   13.34  				RelativePath=".\sourcemm.h">
   13.35  			</File>
   13.36  			<File
    14.1 --- a/sourcemm/util.cpp	Wed Oct 05 21:51:02 2005 +0000
    14.2 +++ b/sourcemm/util.cpp	Thu Oct 06 00:59:01 2005 +0000
    14.3 @@ -10,7 +10,10 @@
    14.4  
    14.5  #include <ctype.h>
    14.6  #include <string.h>
    14.7 +#include <stdarg.h>
    14.8 +#include <stdio.h>
    14.9  #include "util.h"
   14.10 +#include "oslink.h"
   14.11  
   14.12  /**
   14.13   * @brief Utility functons
   14.14 @@ -55,11 +58,43 @@
   14.15  			i++;
   14.16  
   14.17  		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
   14.18 +		// :TODO: change this to not use memcpy()!
   14.19  		if (i != buffer)
   14.20  			memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
   14.21  	}
   14.22  }
   14.23  
   14.24 +//:TODO: this should skip string literals
   14.25 +void UTIL_TrimComments(char *buffer)
   14.26 +{
   14.27 +	int num_sc = 0;
   14.28 +	size_t len = strlen(buffer);
   14.29 +	if (buffer)
   14.30 +	{
   14.31 +		for (int i=(int)len-1; i>=0; i--)
   14.32 +		{
   14.33 +			if (buffer[i] == '/')
   14.34 +			{
   14.35 +				if (++num_sc >= 2 && i==0)
   14.36 +				{
   14.37 +					buffer[i] = '\0';
   14.38 +					return;
   14.39 +				}
   14.40 +			} else {
   14.41 +				if (num_sc >= 2)
   14.42 +				{
   14.43 +					buffer[i] = '\0';
   14.44 +					return;
   14.45 +				}
   14.46 +				num_sc = 0;
   14.47 +			}
   14.48 +			//size_t won't go below 0, manually break out
   14.49 +			if (i == 0)
   14.50 +				break;
   14.51 +		}
   14.52 +	}
   14.53 +}
   14.54 +
   14.55  /* UTIL_TrimLeft
   14.56   * Removes whitespace characters from right side of string
   14.57   */
   14.58 @@ -69,7 +104,7 @@
   14.59  	if (buffer)
   14.60  	{
   14.61  		// Loop through buffer backwards while replacing whitespace chars with null chars
   14.62 -		for (unsigned int i = strlen(buffer) - 1; i >= 0; i--)
   14.63 +		for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
   14.64  		{
   14.65  			if (isspace(buffer[i]))
   14.66  				buffer[i] = '\0';
   14.67 @@ -79,3 +114,110 @@
   14.68  	}
   14.69  }
   14.70  
   14.71 +/* UTIL_KeySplit
   14.72 + * Breaks a string at the first space until it reaches a nonspace
   14.73 + */
   14.74 +void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   14.75 +{
   14.76 +	size_t start;
   14.77 +	size_t len = strlen(str);
   14.78 +
   14.79 +	for (start=0; start<len; start++)
   14.80 +	{
   14.81 +		if (!isspace(str[start]))
   14.82 +			break;
   14.83 +	}
   14.84 +
   14.85 +	size_t end;
   14.86 +	for (end=start; end<len; end++)
   14.87 +	{
   14.88 +		if (isspace(str[end]))
   14.89 +			break;
   14.90 +	}
   14.91 +	
   14.92 +	size_t i, c=0;
   14.93 +	for (i=start; i<end; i++,c++)
   14.94 +	{
   14.95 +		if (c >= len1)
   14.96 +			break;
   14.97 +		buf1[c] = str[i];
   14.98 +	}
   14.99 +	buf1[c] = '\0';
  14.100 +
  14.101 +	for (start=end; start<len; start++)
  14.102 +	{
  14.103 +		if (!isspace(str[start]))
  14.104 +			break;
  14.105 +	}
  14.106 +
  14.107 +	for (c=0; start<len; start++,c++)
  14.108 +	{
  14.109 +		if (c >= len2)
  14.110 +			break;
  14.111 +		buf2[c] = str[start];
  14.112 +	}
  14.113 +	buf2[c] = '\0';
  14.114 +}
  14.115 +
  14.116 +/** 
  14.117 + * Formats a path name for an OS
  14.118 + */
  14.119 +void UTIL_PathFmt(char *buffer, size_t len, const char *fmt, ...)
  14.120 +{
  14.121 +	va_list ap;
  14.122 +	va_start(ap,fmt);
  14.123 +	size_t mylen = vsnprintf(buffer, len, fmt, ap);
  14.124 +	va_end(ap);
  14.125 +
  14.126 +	for (size_t i=0; i<mylen; i++)
  14.127 +	{
  14.128 +		if (buffer[i] == ALT_SEP_CHAR)
  14.129 +			buffer[i] = PATH_SEP_CHAR;
  14.130 +	}
  14.131 +}
  14.132 +
  14.133 +bool UTIL_PathCmp(const char *path1, const char *path2)
  14.134 +{
  14.135 +	size_t len1=strlen(path1);
  14.136 +	size_t len2=strlen(path2);
  14.137 +	size_t pos1=0,pos2=0;
  14.138 +
  14.139 +	while (true)
  14.140 +	{
  14.141 +		if (path1[pos1] == '\0' || path2[pos2] == '\0')
  14.142 +		{
  14.143 +			return (path1[pos1] == path2[pos2]);
  14.144 +		}
  14.145 +
  14.146 +		if (path1[pos1] == PATH_SEP_CHAR)
  14.147 +		{
  14.148 +			if (path2[pos2] != PATH_SEP_CHAR)
  14.149 +				return false;
  14.150 +			//look for extra path chars
  14.151 +			while (path1[++pos1])
  14.152 +			{
  14.153 +				if (path1[pos1] != PATH_SEP_CHAR)
  14.154 +					break;
  14.155 +			}
  14.156 +			while (path2[++pos2])
  14.157 +			{
  14.158 +				if (path2[pos2] != PATH_SEP_CHAR)
  14.159 +					break;
  14.160 +			}
  14.161 +			continue;
  14.162 +		}
  14.163 +
  14.164 +		//if we're at a different non-alphanumeric, the next character MUST Match
  14.165 +		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
  14.166 +			return false;
  14.167 +
  14.168 +#ifdef WIN32
  14.169 +		if (toupper(path1[pos1]) != toupper(path2[pos2]))
  14.170 +#else
  14.171 +		if (path1[pos1] != path2[pos2])
  14.172 +#endif
  14.173 +			return false;
  14.174 +		pos1++;
  14.175 +		pos2++;
  14.176 +	}
  14.177 +}
    15.1 --- a/sourcemm/util.h	Wed Oct 05 21:51:02 2005 +0000
    15.2 +++ b/sourcemm/util.h	Thu Oct 06 00:59:01 2005 +0000
    15.3 @@ -17,8 +17,12 @@
    15.4   */
    15.5  
    15.6  const char *UTIL_GetExtension(const char *file);
    15.7 +void UTIL_TrimComments(char *buffer);
    15.8  void UTIL_TrimLeft(char *buffer);
    15.9  void UTIL_TrimRight(char *buffer);
   15.10 +void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2);
   15.11 +void UTIL_PathFmt(char *buffer, size_t len, const char *fmt, ...);
   15.12 +bool UTIL_PathCmp(const char *path1, const char *path2);
   15.13  
   15.14  #define META_INTERFACE_MACRO(type, final) \
   15.15  	PluginIter i; \
   15.16 @@ -39,12 +43,15 @@
   15.17  	} \
   15.18  	if (high == MRES_OVERRIDE) \
   15.19  	{ \
   15.20 -		(final)(name, ret); \
   15.21 +		if (final) \
   15.22 +			(final)(name, ret); \
   15.23  		return mret; \
   15.24  	} else if (high == MRES_SUPERCEDE) { \
   15.25  		return mret; \
   15.26  	} else { \
   15.27 -		return (final)(name, ret); \
   15.28 +		if (final) \
   15.29 +			return (final)(name, ret); \
   15.30 +		return NULL; \
   15.31  	} 
   15.32  
   15.33  #endif //_INCLUDE_UTIL_H