From 5f0a55057efb9bbe322486744d9115322f733492 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 16 Feb 2013 15:52:11 -0800 Subject: [PATCH] Move filesystem natives from Core to Logic (bug 5606, r=psychonic). --HG-- rename : core/smn_filesystem.cpp => core/logic/smn_filesystem.cpp --- core/AMBuilder | 1 - core/Logger.cpp | 30 ++++- core/Logger.h | 1 + core/logic/AMBuilder | 1 + core/logic/common_logic.cpp | 6 +- core/logic/intercom.h | 5 + core/{ => logic}/smn_filesystem.cpp | 183 +++++++++++----------------- core/logic_bridge.cpp | 48 ++++++-- 8 files changed, 149 insertions(+), 126 deletions(-) rename core/{ => logic}/smn_filesystem.cpp (73%) diff --git a/core/AMBuilder b/core/AMBuilder index 42b8b1c19..9d9480ed8 100644 --- a/core/AMBuilder +++ b/core/AMBuilder @@ -64,7 +64,6 @@ for i in SM.sdkInfo: 'sourcemm_api.cpp', 'ChatTriggers.cpp', 'NativeOwner.cpp', - 'smn_filesystem.cpp', 'smn_player.cpp', 'sourcemod.cpp', 'concmd_cleaner.cpp', diff --git a/core/Logger.cpp b/core/Logger.cpp index f0834198d..0291e91cc 100644 --- a/core/Logger.cpp +++ b/core/Logger.cpp @@ -37,9 +37,12 @@ #include "LibrarySys.h" #include "TimerSys.h" #include +#include "logic_bridge.h" Logger g_Logger; +SH_DECL_HOOK1_void(IVEngineServer, LogPrint, SH_NOATTRIB, false, const char *); + /** * :TODO: This should be creating the log folder if it doesn't exist */ @@ -91,14 +94,27 @@ ConfigResult Logger::OnSourceModConfigChanged(const char *key, return ConfigResult_Ignore; } +static void HookLogPrint(const char *message) +{ + g_in_game_log_hook = true; + bool stopped = logicore.OnLogPrint(message); + g_in_game_log_hook = false; + + if (stopped) + RETURN_META(MRES_SUPERCEDE); +} + void Logger::OnSourceModStartup(bool late) { InitLogger(m_Mode); + + SH_ADD_HOOK_STATICFUNC(IVEngineServer, LogPrint, engine, HookLogPrint, false); } void Logger::OnSourceModAllShutdown() { CloseLogger(); + SH_REMOVE_HOOK_STATICFUNC(IVEngineServer, LogPrint, engine, HookLogPrint, false); } void Logger::OnSourceModLevelChange(const char *mapName) @@ -305,6 +321,14 @@ void Logger::LogToFileOnlyEx(FILE *fp, const char *msg, va_list ap) } void Logger::LogMessage(const char *vafmt, ...) +{ + va_list ap; + va_start(ap, vafmt); + LogMessageEx(vafmt, ap); + va_end(ap); +} + +void Logger::LogMessageEx(const char *vafmt, va_list ap) { if (!m_Active) { @@ -313,10 +337,7 @@ void Logger::LogMessage(const char *vafmt, ...) if (m_Mode == LoggingMode_Game) { - va_list ap; - va_start(ap, vafmt); _PrintToGameLog(vafmt, ap); - va_end(ap); return; } @@ -364,10 +385,7 @@ void Logger::LogMessage(const char *vafmt, ...) strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime); fprintf(fp, "L %s: SourceMod log file session started (file \"L%04d%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday, SM_VERSION_STRING); } - va_list ap; - va_start(ap, vafmt); LogToOpenFileEx(fp, vafmt, ap); - va_end(ap); fclose(fp); } else { goto print_error; diff --git a/core/Logger.h b/core/Logger.h index 86677e870..8982631cb 100644 --- a/core/Logger.h +++ b/core/Logger.h @@ -74,6 +74,7 @@ public: void EnableLogging(); void DisableLogging(); void LogMessage(const char *msg, ...); + void LogMessageEx(const char *msg, va_list ap); void LogError(const char *msg, ...); void LogErrorEx(const char *msg, va_list ap); void LogFatal(const char *msg, ...); diff --git a/core/logic/AMBuilder b/core/logic/AMBuilder index 1e5af769c..69d2f56d8 100644 --- a/core/logic/AMBuilder +++ b/core/logic/AMBuilder @@ -38,6 +38,7 @@ files = [ 'MemoryUtils.cpp', 'smn_admin.cpp', 'smn_banning.cpp', + 'smn_filesystem.cpp', 'stringutil.cpp', 'Translator.cpp', 'PhraseCollection.cpp', diff --git a/core/logic/common_logic.cpp b/core/logic/common_logic.cpp index f2540196f..19bfc1ecd 100644 --- a/core/logic/common_logic.cpp +++ b/core/logic/common_logic.cpp @@ -70,6 +70,9 @@ static IGameConfig *GetCoreGameConfig() return g_pGameConf; } +// Defined in smn_filesystem.cpp. +extern bool OnLogPrint(const char *msg); + static sm_logic_t logic = { NULL, @@ -82,7 +85,8 @@ static sm_logic_t logic = UTIL_ReplaceAll, UTIL_ReplaceEx, UTIL_DecodeHexString, - GetCoreGameConfig + GetCoreGameConfig, + OnLogPrint }; static void logic_init(const sm_core_t* core, sm_logic_t* _logic) diff --git a/core/logic/intercom.h b/core/logic/intercom.h index d2c968d08..8b570621b 100644 --- a/core/logic/intercom.h +++ b/core/logic/intercom.h @@ -105,6 +105,10 @@ struct sm_core_t unsigned int (*strncopy)(char*, const char*, size_t); char * (*TrimWhitespace)(char *, size_t &); void (*LogError)(const char*, ...); + void (*Log)(const char*, ...); + void (*LogToFile)(FILE *fp, const char*, ...); + void (*LogToGame)(const char *message); + bool (*FileExists)(const char *path); const char * (*GetCvarString)(ConVar*); size_t (*Format)(char*, size_t, const char*, ...); void (*GenerateError)(IPluginContext *, cell_t, int, const char *, ...); @@ -136,6 +140,7 @@ struct sm_logic_t char *(*ReplaceEx)(char *, size_t, const char *, size_t, const char *, size_t, bool); size_t (*DecodeHexString)(unsigned char *, size_t, const char *); IGameConfig * (*GetCoreGameConfig)(); + bool (*OnLogPrint)(const char *msg); // true to supercede }; typedef void (*LogicInitFunction)(const sm_core_t *core, sm_logic_t *logic); diff --git a/core/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp similarity index 73% rename from core/smn_filesystem.cpp rename to core/logic/smn_filesystem.cpp index 935f7cdf1..802fe9c1d 100644 --- a/core/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -30,16 +30,13 @@ */ #include -#include "sm_globals.h" -#include "HandleSys.h" -#include "LibrarySys.h" -#include "sm_stringutil.h" -#include "Logger.h" -#include "PluginSys.h" -#include "sourcemm_api.h" -#include "ForwardSys.h" - -SH_DECL_HOOK1_void(IVEngineServer, LogPrint, SH_NOATTRIB, false, const char *); +#include +#include +#include +#include +#include +#include +#include "common_logic.h" HandleType_t g_FileType; HandleType_t g_DirType; @@ -53,26 +50,20 @@ class FileNatives : public: FileNatives() { - m_bIsLoggingHooked = false; } virtual void OnSourceModAllInitialized() { - g_FileType = g_HandleSys.CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL); - g_DirType = g_HandleSys.CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL); - g_pLogHook = g_Forwards.CreateForwardEx(NULL, ET_Hook, 1, NULL, Param_String); - g_PluginSys.AddPluginsListener(this); + g_FileType = handlesys->CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL); + g_DirType = handlesys->CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL); + g_pLogHook = forwardsys->CreateForwardEx(NULL, ET_Hook, 1, NULL, Param_String); + pluginsys->AddPluginsListener(this); } virtual void OnSourceModShutdown() { - g_PluginSys.RemovePluginsListener(this); - if (m_bIsLoggingHooked) - { - SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); - m_bIsLoggingHooked = false; - } - g_Forwards.ReleaseForward(g_pLogHook); - g_HandleSys.RemoveType(g_DirType, g_pCoreIdent); - g_HandleSys.RemoveType(g_FileType, g_pCoreIdent); + pluginsys->RemovePluginsListener(this); + forwardsys->ReleaseForward(g_pLogHook); + handlesys->RemoveType(g_DirType, g_pCoreIdent); + handlesys->RemoveType(g_FileType, g_pCoreIdent); g_DirType = 0; g_FileType = 0; } @@ -86,57 +77,31 @@ public: else if (type == g_DirType) { IDirectory *pDir = (IDirectory *)object; - g_LibSys.CloseDirectory(pDir); - } - } - virtual void OnPluginDestroyed(IPlugin *plugin) - { - if (m_bIsLoggingHooked && g_pLogHook->GetFunctionCount() == 0) - { - SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); - m_bIsLoggingHooked = false; + libsys->CloseDirectory(pDir); } } virtual void AddLogHook(IPluginFunction *pFunc) { - if (!m_bIsLoggingHooked) - { - SH_ADD_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); - m_bIsLoggingHooked = true; - } - g_pLogHook->AddFunction(pFunc); } virtual void RemoveLogHook(IPluginFunction *pFunc) { g_pLogHook->RemoveFunction(pFunc); - - if (m_bIsLoggingHooked && g_pLogHook->GetFunctionCount() == 0) - { - SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); - m_bIsLoggingHooked = false; - } } - virtual void LogPrint(const char *msg) + virtual bool LogPrint(const char *msg) { - cell_t result; - - result = 0; - - g_in_game_log_hook = true; + cell_t result = 0; g_pLogHook->PushString(msg); g_pLogHook->Execute(&result); - g_in_game_log_hook = false; - - if (result >= Pl_Handled) - { - RETURN_META(MRES_SUPERCEDE); - } + return result >= Pl_Handled; } -private: - bool m_bIsLoggingHooked; } s_FileNatives; +bool OnLogPrint(const char *msg) +{ + return s_FileNatives.LogPrint(msg); +} + static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) { char *path; @@ -148,15 +113,15 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); - IDirectory *pDir = g_LibSys.OpenDirectory(realpath); + IDirectory *pDir = libsys->OpenDirectory(realpath); if (!pDir) { return 0; } - return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL); + return handlesys->CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL); } static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params) @@ -170,7 +135,7 @@ static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, &sec, (void **)&pDir)) + if ((herr=handlesys->ReadHandle(hndl, g_DirType, &sec, (void **)&pDir)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -226,7 +191,7 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); FILE *pFile = fopen(realpath, mode); if (!pFile) @@ -234,7 +199,7 @@ static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) return 0; } - return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL); + return handlesys->CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL); } static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params) @@ -248,7 +213,7 @@ static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); return (unlink(realpath)) ? 0 : 1; } @@ -264,7 +229,7 @@ static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -295,7 +260,7 @@ static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -314,7 +279,7 @@ static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -335,7 +300,7 @@ static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -356,11 +321,11 @@ static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params) if (params[0] >= 2 && params[2] == 1) { - return basefilesystem->FileExists(name) ? 1 : 0; + return smcore.FileExists(name) ? 1 : 0; } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); #ifdef PLATFORM_WINDOWS struct _stat s; if (_stat(realpath, &s) != 0) @@ -402,9 +367,9 @@ static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params) } char new_realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath); + g_pSM->BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath); char old_realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath); + g_pSM->BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath); #ifdef PLATFORM_WINDOWS return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0; @@ -424,7 +389,7 @@ static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); #ifdef PLATFORM_WINDOWS struct _stat s; if (_stat(realpath, &s) != 0) @@ -461,7 +426,7 @@ static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); #ifdef PLATFORM_WINDOWS struct _stat s; if (_stat(realpath, &s) != 0) @@ -493,7 +458,7 @@ static cell_t sm_CreateDirectory(IPluginContext *pContext, const cell_t *params) char realpath[PLATFORM_MAX_PATH]; pContext->LocalToString(params[1], &name); - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); #if defined PLATFORM_WINDOWS return mkdir(realpath) == 0; @@ -513,7 +478,7 @@ static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params) } char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); return (rmdir(realpath)) ? 0 : 1; } @@ -528,7 +493,7 @@ static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -544,7 +509,7 @@ static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params) char buffer[2048]; int arg = 3; - atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg); + smcore.atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg); fprintf(pFile, "%s\n", buffer); return 1; @@ -560,7 +525,7 @@ static cell_t sm_FlushFile(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -576,17 +541,17 @@ static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params) pContext->LocalToString(params[2], &buffer); pContext->LocalToString(params[4], &fmt); - atcprintf(path, sizeof(path), fmt, pContext, params, &arg); + smcore.atcprintf(path, sizeof(path), fmt, pContext, params, &arg); - return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path); + return g_pSM->BuildPath(Path_SM_Rel, buffer, params[3], "%s", path); } static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params) { - g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); char buffer[1024]; - size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + size_t len = g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 1); if (pContext->GetLastNativeError() != SP_ERROR_NONE) { @@ -602,43 +567,43 @@ static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params) buffer[len] = '\0'; } - Engine_LogPrintWrapper(buffer); + smcore.LogToGame(buffer); return 1; } static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params) { - g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); char buffer[1024]; - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 1); if (pContext->GetLastNativeError() != SP_ERROR_NONE) { return 0; } - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); - g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer); + IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext->GetContext()); + smcore.Log("[%s] %s", pPlugin->GetFilename(), buffer); return 1; } static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params) { - g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); char buffer[1024]; - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); + g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 1); if (pContext->GetLastNativeError() != SP_ERROR_NONE) { return 0; } - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); - g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer); + IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext->GetContext()); + smcore.LogError("[%s] %s", pPlugin->GetFilename(), buffer); return 1; } @@ -655,9 +620,9 @@ static cell_t sm_GetFileTime(IPluginContext *pContext, const cell_t *params) time_t time_val; char realpath[PLATFORM_MAX_PATH]; - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); + g_pSM->BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); - if (!g_LibSys.FileTime(realpath, (FileTimeType)params[2], &time_val)) + if (!libsys->FileTime(realpath, (FileTimeType)params[2], &time_val)) { return -1; } @@ -675,23 +640,23 @@ static cell_t sm_LogToOpenFile(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); } char buffer[2048]; - g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2); + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); + g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 2); if (pContext->GetLastNativeError() != SP_ERROR_NONE) { return 0; } - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); - g_Logger.LogToOpenFile(pFile, "[%s] %s", pPlugin->GetFilename(), buffer); + IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext->GetContext()); + smcore.LogToFile(pFile, "[%s] %s", pPlugin->GetFilename(), buffer); return 1; } @@ -706,22 +671,22 @@ static cell_t sm_LogToOpenFileEx(IPluginContext *pContext, const cell_t *params) sec.pOwner = NULL; sec.pIdentity = g_pCoreIdent; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); } char buffer[2048]; - g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2); + g_pSM->SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); + g_pSM->FormatString(buffer, sizeof(buffer), pContext, params, 2); if (pContext->GetLastNativeError() != SP_ERROR_NONE) { return 0; } - g_Logger.LogToOpenFile(pFile, "%s", buffer); + smcore.LogToFile(pFile, "%s", buffer); return 1; } @@ -734,7 +699,7 @@ static cell_t sm_ReadFile(IPluginContext *pContext, const cell_t *params) FILE *pFile; size_t read = 0; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -793,7 +758,7 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params) FILE *pFile; cell_t num_read = 0; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -861,7 +826,7 @@ static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params) HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); FILE *pFile; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); @@ -915,7 +880,7 @@ static cell_t sm_WriteFileString(IPluginContext *pContext, const cell_t *params) HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); FILE *pFile; - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) + if ((herr=handlesys->ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) != HandleError_None) { return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); diff --git a/core/logic_bridge.cpp b/core/logic_bridge.cpp index bb4a10ba8..108c85975 100644 --- a/core/logic_bridge.cpp +++ b/core/logic_bridge.cpp @@ -51,19 +51,19 @@ #include "HalfLife2.h" #include "CoreConfig.h" -#if defined _WIN32 - #define MATCHMAKINGDS_SUFFIX "" - #define MATCHMAKINGDS_EXT "dll" -#elif defined __APPLE__ - #define MATCHMAKINGDS_SUFFIX "" - #define MATCHMAKINGDS_EXT "dylib" -#elif defined __linux__ +#if defined _WIN32 + #define MATCHMAKINGDS_SUFFIX "" + #define MATCHMAKINGDS_EXT "dll" +#elif defined __APPLE__ + #define MATCHMAKINGDS_SUFFIX "" + #define MATCHMAKINGDS_EXT "dylib" +#elif defined __linux__ #if SOURCE_ENGINE < SE_LEFT4DEAD2 #define MATCHMAKINGDS_SUFFIX "_i486" #else #define MATCHMAKINGDS_SUFFIX "" -#endif - #define MATCHMAKINGDS_EXT "so" +#endif + #define MATCHMAKINGDS_EXT "so" #endif static ILibrary *g_pLogic = NULL; @@ -109,6 +109,32 @@ static void log_error(const char *fmt, ...) va_end(ap); } +static void log_message(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + g_Logger.LogMessageEx(fmt, ap); + va_end(ap); +} + +static void log_to_file(FILE *fp, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + g_Logger.LogToOpenFileEx(fp, fmt, ap); + va_end(ap); +} + +static void log_to_game(const char *message) +{ + Engine_LogPrintWrapper(message); +} + +static bool file_exists(const char *path) +{ + return basefilesystem->FileExists(path); +} + static void generate_error(IPluginContext *ctx, cell_t func_idx, int err, const char *fmt, ...) { va_list ap; @@ -214,6 +240,10 @@ static sm_core_t core_bridge = strncopy, UTIL_TrimWhitespace, log_error, + log_message, + log_to_file, + log_to_game, + file_exists, get_cvar_string, UTIL_Format, generate_error,