added new path format functions

exposed helpers as ISourceMod

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40322
This commit is contained in:
David Anderson 2007-01-19 02:14:50 +00:00
parent 2ea877f14b
commit 5c6eebcebf
12 changed files with 181 additions and 39 deletions

View File

@ -34,7 +34,7 @@ void CLogger::_NewMapFile()
while (true)
{
g_LibSys.PathFormat(_filename, sizeof(_filename), "%s/logs/L%02d%02d%03d.log", g_SourceMod.GetSMBaseDir(), curtime->tm_mon + 1, curtime->tm_mday, i);
g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i);
FILE *fp = fopen(_filename, "r");
if (!fp)
{
@ -104,7 +104,7 @@ void CLogger::InitLogger(LoggingMode mode, bool startlogging)
m_CurDay = curtime->tm_mday;
char _filename[256];
g_LibSys.PathFormat(_filename, sizeof(_filename), "%s/logs/errors_%02d%02d%02d.log", g_SourceMod.GetSMBaseDir(), curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%02d%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
m_ErrFileName.assign(_filename);
switch (m_mode)
@ -119,7 +119,7 @@ void CLogger::InitLogger(LoggingMode mode, bool startlogging)
}
case LoggingMode_Daily:
{
g_LibSys.PathFormat(_filename, sizeof(_filename), "%s/logs/L%02d%02d.log", g_SourceMod.GetSMBaseDir(), curtime->tm_mon + 1, curtime->tm_mday);
g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
m_NrmFileName.assign(_filename);
m_DailyPrintHdr = true;
break;
@ -183,7 +183,7 @@ void CLogger::LogMessage(const char *vafmt, ...)
if (m_CurDay != curtime->tm_mday)
{
char _filename[256];
g_LibSys.PathFormat(_filename, sizeof(_filename), "%s/logs/L%02d%02d.log", g_SourceMod.GetSMBaseDir(), curtime->tm_mon + 1, curtime->tm_mday);
g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
m_NrmFileName.assign(_filename);
m_CurDay = curtime->tm_mday;
m_DailyPrintHdr = true;
@ -228,7 +228,7 @@ void CLogger::LogError(const char *vafmt, ...)
if (curtime->tm_mday != m_CurDay)
{
char _filename[256];
g_LibSys.PathFormat(_filename, sizeof(_filename), "%s/logs/errors_%02d%02d%02d.log", g_SourceMod.GetSMBaseDir(), curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%02d%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
m_ErrFileName.assign(_filename);
m_CurDay = curtime->tm_mday;
m_ErrMapStart = false;

View File

@ -87,7 +87,7 @@ void CPhraseFile::ReparseFile()
SMCParseError err;
char path[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s/translations/%s", g_SourceMod.GetSMBaseDir(), m_File.c_str());
g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "translations/%s", m_File.c_str());
unsigned int line=0, col=0;
if ((err=g_TextParser.ParseFile_SMC(path, this, &line, &col)) != SMCParse_Okay)

View File

@ -148,7 +148,7 @@ namespace SourceMod
* @param pathfmt Format string of path.
* @param ... Format string arguments.
*/
virtual void PathFormat(char *buffer, size_t maxlength, const char *pathfmt, ...) =0;
virtual size_t PathFormat(char *buffer, size_t maxlength, const char *pathfmt, ...) =0;
};
};

View File

@ -0,0 +1,74 @@
#ifndef _INCLUDE_SOURCEMOD_MAIN_HELPER_INTERFACE_H_
#define _INCLUDE_SOURCEMOD_MAIN_HELPER_INTERFACE_H_
#include <IShareSys.h>
#define SMINTERFACE_SOURCEMOD_NAME "ISourceMod"
#define SMINTERFACE_SOURCEMOD_VERSION 1
namespace SourceMod
{
enum PathType
{
Path_None = 0,
Path_Game,
Path_SM,
};
class ISourceMod : public SMInterface
{
public:
virtual const char *GetInterfaceName()
{
return SMINTERFACE_SOURCEMOD_NAME;
}
virtual unsigned int GetInterfaceVersion()
{
return SMINTERFACE_SOURCEMOD_VERSION;
}
public:
/**
* @brief Returns the full path to the mod directory.
*
* @return A string containing the full mod path.
*/
virtual const char *GetModPath() =0;
/**
* @brief Returns the full path to the SourceMod directory.
*
* @return A string containing the full SourceMod path.
*/
virtual const char *GetSourceModPath() =0;
/**
* @brief Builds a platform path for a specific target base path.
*
* @param type Type of path to use as a base.
* @param buffer Buffer to write to.
* @param maxlength Size of buffer.
* @param format Format string.
* @param ... Format arguments.
* @return Number of bytes written.
*/
virtual size_t BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...) =0;
/**
* @brief Logs a message to the SourceMod logs.
*
* @param format Message format.
* @param ... Message format parameters.
*/
virtual void LogMessage(IExtension *pExt, const char *format, ...) =0;
/**
* @brief Logs a message to the SourceMod error logs.
*
* @param format Message format.
* @param ... Message format parameters.
*/
virtual void LogError(IExtension *pExt, const char *format, ...) =0;
};
};
#endif //_INCLUDE_SOURCEMOD_MAIN_HELPER_INTERFACE_H_

View File

@ -329,6 +329,10 @@
RelativePath="..\interfaces\IRootConsoleMenu.h"
>
</File>
<File
RelativePath="..\interfaces\ISourceMod.h"
>
</File>
<File
RelativePath="..\interfaces\ITextParsers.h"
>

View File

@ -49,7 +49,7 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), path);
g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
IDirectory *pDir = g_LibSys.OpenDirectory(realpath);
if (!pDir)
@ -127,7 +127,7 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
FILE *pFile = fopen(realpath, mode);
if (!pFile)
@ -149,7 +149,7 @@ static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
return (unlink(realpath)) ? 0 : 1;
}
@ -253,7 +253,7 @@ static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
#ifdef PLATFORM_WINDOWS
struct _stat s;
if (_stat(realpath, &s) != 0)
@ -295,9 +295,9 @@ static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
}
char new_realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(new_realpath, sizeof(new_realpath), "%s/%s", g_SourceMod.GetBaseDir(), newpath);
g_SourceMod.BuildPath(Path_SM, new_realpath, sizeof(new_realpath), "%s", newpath);
char old_realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(old_realpath, sizeof(old_realpath), "%s/%s", g_SourceMod.GetBaseDir(), oldpath);
g_SourceMod.BuildPath(Path_SM, old_realpath, sizeof(old_realpath), "%s", oldpath);
#ifdef PLATFORM_WINDOWS
return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0;
@ -317,7 +317,7 @@ static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
#ifdef PLATFORM_WINDOWS
struct _stat s;
if (_stat(realpath, &s) != 0)
@ -354,7 +354,7 @@ static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
#ifdef PLATFORM_WINDOWS
struct _stat s;
if (_stat(realpath, &s) != 0)
@ -391,7 +391,7 @@ static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params)
}
char realpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
return (rmdir(realpath)) ? 0 : 1;
}

View File

@ -50,7 +50,7 @@ bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
char file[PLATFORM_MAX_PATH];
char myerror[255];
g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
GetSMBaseDir(),
GetSourceModPath(),
PLATFORM_LIB_EXT
);
@ -150,6 +150,9 @@ void SourceModBase::StartSourceMod(bool late)
pBase->OnSourceModAllInitialized();
pBase = pBase->m_pGlobalClassNext;
}
/* Add us now... */
g_ShareSys.AddInterface(NULL, this);
}
bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
@ -175,14 +178,12 @@ void SourceModBase::DoGlobalPluginLoads()
char config_path[PLATFORM_MAX_PATH];
char plugins_path[PLATFORM_MAX_PATH];
g_SMAPI->PathFormat(config_path,
BuildPath(Path_SM, config_path,
sizeof(config_path),
"%s/configs/plugin_settings.cfg",
GetSMBaseDir());
g_SMAPI->PathFormat(plugins_path,
"configs/plugin_settings.cfg");
BuildPath(Path_SM, plugins_path,
sizeof(plugins_path),
"%s/plugins",
GetSMBaseDir());
"plugins");
/* Run the first pass */
g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
@ -197,6 +198,31 @@ void SourceModBase::DoGlobalPluginLoads()
g_Extensions.MarkAllLoaded();
}
size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
{
char _buffer[PLATFORM_MAX_PATH+1];
va_list ap;
va_start(ap, format);
vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
va_end(ap);
const char *base = NULL;
if (type == Path_Game)
{
base = GetModPath();
} else if (type == Path_SM) {
base = GetSourceModPath();
}
if (base)
{
return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
} else {
return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
}
}
void SourceModBase::CloseSourceMod()
{
/* Notify! */
@ -219,12 +245,50 @@ void SourceModBase::CloseSourceMod()
ShutdownJIT();
}
const char *SourceModBase::GetSMBaseDir()
void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
{
IExtensionInterface *pAPI = pExt->GetAPI();
const char *tag = pAPI->GetExtensionTag();
char buffer[2048];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
if (tag)
{
g_Logger.LogMessage("[%s] %s", tag, buffer);
} else {
g_Logger.LogMessage("%s", buffer);
}
}
void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
{
IExtensionInterface *pAPI = pExt->GetAPI();
const char *tag = pAPI->GetExtensionTag();
char buffer[2048];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
if (tag)
{
g_Logger.LogError("[%s] %s", tag, buffer);
} else {
g_Logger.LogError("%s", buffer);
}
}
const char *SourceModBase::GetSourceModPath()
{
return m_SMBaseDir;
}
const char *SourceModBase::GetBaseDir()
const char *SourceModBase::GetModPath()
{
return g_BaseDir.c_str();
}

View File

@ -2,12 +2,13 @@
#define _INCLUDE_SOURCEMOD_GLOBALHEADER_H_
#include "sm_globals.h"
#include <ISourceMod.h>
/**
* @brief Implements SourceMod's global overall management, API, and logic
*/
class SourceModBase
class SourceModBase : public ISourceMod
{
public:
SourceModBase();
@ -37,15 +38,12 @@ public:
*/
bool IsMapLoading();
/**
* @brief Returns the base SourceMod folder.
*/
const char *GetSMBaseDir();
/**
* @brief Returns the base folder for file natives.
*/
const char *GetBaseDir();
public: //ISourceMod
const char *GetModPath();
const char *GetSourceModPath();
size_t BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...);
void LogMessage(IExtension *pExt, const char *format, ...);
void LogError(IExtension *pExt, const char *format, ...);
private:
/**
* @brief Loading plugins

View File

@ -20,7 +20,7 @@ CExtension::CExtension(const char *filename, char *error, size_t err_max)
m_FullyLoaded = false;
char path[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s/extensions/%s", g_SourceMod.GetSMBaseDir(), filename);
g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
m_pLib = g_LibSys.OpenLibrary(path, error, err_max);

View File

@ -282,7 +282,7 @@ ILibrary *LibrarySystem::OpenLibrary(const char *path, char *error, size_t err_m
return new CLibrary(lib);
}
void LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
size_t LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
@ -298,4 +298,6 @@ void LibrarySystem::PathFormat(char *buffer, size_t len, const char *fmt, ...)
buffer[i] = PLATFORM_SEP_CHAR;
}
}
return mylen;
}

View File

@ -59,7 +59,7 @@ public:
virtual bool IsPathFile(const char *path);
virtual bool IsPathDirectory(const char *path);
virtual void GetPlatformError(char *error, size_t err_max);
virtual void PathFormat(char *buffer, size_t len, const char *fmt, ...);
virtual size_t PathFormat(char *buffer, size_t len, const char *fmt, ...);
};
extern LibrarySystem g_LibSys;

View File

@ -100,7 +100,7 @@ void CPlugin::InitIdentity()
CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength)
{
char fullpath[PLATFORM_MAX_PATH+1];
g_LibSys.PathFormat(fullpath, sizeof(fullpath), "%s/plugins/%s", g_SourceMod.GetSMBaseDir(), file);
g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file);
FILE *fp = fopen(fullpath, "rb");
CPlugin *pPlugin = new CPlugin(file);