ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include ds@1952: #include "sm_globals.h" ds@1952: #include "HandleSys.h" ds@1952: #include "LibrarySys.h" ds@1952: #include "sm_stringutil.h" ds@1952: #include "Logger.h" ds@1952: #include "PluginSys.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "ForwardSys.h" ds@1952: ds@1952: SH_DECL_HOOK1_void(IVEngineServer, LogPrint, SH_NOATTRIB, false, const char *); ds@1952: ds@1952: HandleType_t g_FileType; ds@1952: HandleType_t g_DirType; ds@1952: IChangeableForward *g_pLogHook = NULL; ds@1952: ds@1952: class FileNatives : ds@1952: public SMGlobalClass, ds@1952: public IHandleTypeDispatch, ds@1952: public IPluginsListener ds@1952: { ds@1952: public: ds@1952: FileNatives() ds@1952: { ds@1952: m_bIsLoggingHooked = false; ds@1952: } ds@1952: virtual void OnSourceModAllInitialized() ds@1952: { ds@1952: g_FileType = g_HandleSys.CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL); ds@1952: g_DirType = g_HandleSys.CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL); ds@1952: g_pLogHook = g_Forwards.CreateForwardEx(NULL, ET_Hook, 1, NULL, Param_String); ds@1952: g_PluginSys.AddPluginsListener(this); ds@1952: } ds@1952: virtual void OnSourceModShutdown() ds@1952: { ds@1952: g_PluginSys.RemovePluginsListener(this); ds@1952: if (m_bIsLoggingHooked) ds@1952: { ds@1952: SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); ds@1952: m_bIsLoggingHooked = false; ds@1952: } ds@1952: g_Forwards.ReleaseForward(g_pLogHook); ds@1952: g_HandleSys.RemoveType(g_DirType, g_pCoreIdent); ds@1952: g_HandleSys.RemoveType(g_FileType, g_pCoreIdent); ds@1952: g_DirType = 0; ds@1952: g_FileType = 0; ds@1952: } ds@1952: virtual void OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: if (type == g_FileType) ds@1952: { ds@1952: FILE *fp = (FILE *)object; ds@1952: fclose(fp); ds@1952: } ds@1952: else if (type == g_DirType) ds@1952: { ds@1952: IDirectory *pDir = (IDirectory *)object; ds@1952: g_LibSys.CloseDirectory(pDir); ds@1952: } ds@1952: } ds@1952: virtual void OnPluginDestroyed(IPlugin *plugin) ds@1952: { ds@1952: if (m_bIsLoggingHooked && g_pLogHook->GetFunctionCount() == 0) ds@1952: { ds@1952: SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); ds@1952: m_bIsLoggingHooked = false; ds@1952: } ds@1952: } ds@1952: virtual void AddLogHook(IPluginFunction *pFunc) ds@1952: { ds@1952: if (!m_bIsLoggingHooked) ds@1952: { ds@1952: SH_ADD_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); ds@1952: m_bIsLoggingHooked = true; ds@1952: } ds@1952: ds@1952: g_pLogHook->AddFunction(pFunc); ds@1952: } ds@1952: virtual void RemoveLogHook(IPluginFunction *pFunc) ds@1952: { ds@1952: g_pLogHook->RemoveFunction(pFunc); ds@1952: ds@1952: if (m_bIsLoggingHooked && g_pLogHook->GetFunctionCount() == 0) ds@1952: { ds@1952: SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, LogPrint, engine, this, &FileNatives::LogPrint, false); ds@1952: m_bIsLoggingHooked = false; ds@1952: } ds@1952: } ds@1952: virtual void LogPrint(const char *msg) ds@1952: { ds@1952: cell_t result; ds@1952: ds@1952: result = 0; ds@1952: ds@1952: g_in_game_log_hook = true; ds@1952: g_pLogHook->PushString(msg); ds@1952: g_pLogHook->Execute(&result); ds@1952: g_in_game_log_hook = false; ds@1952: ds@1952: if (result >= Pl_Handled) ds@1952: { ds@1952: RETURN_META(MRES_SUPERCEDE); ds@1952: } ds@1952: } ds@1952: private: ds@1952: bool m_bIsLoggingHooked; ds@1952: } s_FileNatives; ds@1952: ds@1952: static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *path; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); ds@1952: ds@1952: IDirectory *pDir = g_LibSys.OpenDirectory(realpath); ds@1952: if (!pDir) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: IDirectory *pDir; ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: int err; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, &sec, (void **)&pDir)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (!pDir->MoreFiles()) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: cell_t *filetype; ds@1952: if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: if (pDir->IsEntryDirectory()) ds@1952: { ds@1952: *filetype = 1; ds@1952: } else if (pDir->IsEntryFile()) { ds@1952: *filetype = 2; ds@1952: } else { ds@1952: *filetype = 0; ds@1952: } ds@1952: ds@1952: const char *path = pDir->GetEntryName(); ds@1952: if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL)) ds@1952: != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: pDir->NextEntry(); ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name, *mode; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: if ((err=pContext->LocalToString(params[2], &mode)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: ds@1952: FILE *pFile = fopen(realpath, mode); ds@1952: if (!pFile) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: ds@1952: return (unlink(realpath)) ? 0 : 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: int err; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *buf; ds@1952: if ((err=pContext->LocalToString(params[2], &buf)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: if (fgets(buf, params[3], pFile) == NULL) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return (feof(pFile)) ? 1 : 0; ds@1952: } ds@1952: ds@1952: static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: fseek(pFile, params[2], params[3]); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return ftell(pFile); ds@1952: } ds@1952: ds@1952: static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: if (params[0] >= 2 && params[2] == 1) ds@1952: { ds@1952: return basefilesystem->FileExists(name) ? 1 : 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: #ifdef PLATFORM_WINDOWS ds@1952: struct _stat s; ds@1952: if (_stat(realpath, &s) != 0) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: if (s.st_mode & S_IFREG) ds@1952: { ds@1952: return 1; ds@1952: } ds@1952: return 0; ds@1952: #elif defined PLATFORM_POSIX ds@1952: struct stat s; ds@1952: if (stat(realpath, &s) != 0) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: if (S_ISREG(s.st_mode)) ds@1952: { ds@1952: return 1; ds@1952: } ds@1952: return 0; ds@1952: #endif ds@1952: } ds@1952: ds@1952: static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *newpath, *oldpath; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char new_realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath); ds@1952: char old_realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath); ds@1952: ds@1952: #ifdef PLATFORM_WINDOWS ds@1952: return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0; ds@1952: #elif defined PLATFORM_POSIX ds@1952: return (rename(old_realpath, new_realpath)) ? 0 : 1; ds@1952: #endif ds@1952: } ds@1952: ds@1952: static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: #ifdef PLATFORM_WINDOWS ds@1952: struct _stat s; ds@1952: if (_stat(realpath, &s) != 0) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: if (s.st_mode & S_IFDIR) ds@1952: { ds@1952: return 1; ds@1952: } ds@1952: return 0; ds@1952: #elif defined PLATFORM_POSIX ds@1952: struct stat s; ds@1952: if (stat(realpath, &s) != 0) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: if (S_ISDIR(s.st_mode)) ds@1952: { ds@1952: return 1; ds@1952: } ds@1952: return 0; ds@1952: #endif ds@1952: } ds@1952: ds@1952: static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return -1; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: #ifdef PLATFORM_WINDOWS ds@1952: struct _stat s; ds@1952: if (_stat(realpath, &s) != 0) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: if (s.st_mode & S_IFREG) ds@1952: { ds@1952: return static_cast(s.st_size); ds@1952: } ds@1952: return -1; ds@1952: #elif defined PLATFORM_POSIX ds@1952: struct stat s; ds@1952: if (stat(realpath, &s) != 0) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: if (S_ISREG(s.st_mode)) ds@1952: { ds@1952: return static_cast(s.st_size); ds@1952: } ds@1952: return -1; ds@1952: #endif ds@1952: } ds@1952: ds@1952: static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: ds@1952: return (rmdir(realpath)) ? 0 : 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *fmt; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: char buffer[2048]; ds@1952: int arg = 3; ds@1952: atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg); ds@1952: fprintf(pFile, "%s\n", buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_FlushFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: return (fflush(pFile) == 0) ? 1 : 0; ds@1952: } ds@1952: ds@1952: static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char path[PLATFORM_MAX_PATH], *fmt, *buffer; ds@1952: int arg = 5; ds@1952: pContext->LocalToString(params[2], &buffer); ds@1952: pContext->LocalToString(params[4], &fmt); ds@1952: ds@1952: atcprintf(path, sizeof(path), fmt, pContext, params, &arg); ds@1952: ds@1952: return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path); ds@1952: } ds@1952: ds@1952: static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params) ds@1952: { dvander@2042: g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); ds@1952: ds@1952: char buffer[1024]; ds@1952: size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); ds@1952: dvander@2193: if (pContext->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: if (len >= sizeof(buffer)-2) ds@1952: { ds@1952: buffer[1022] = '\n'; ds@1952: buffer[1023] = '\0'; ds@1952: } else { ds@1952: buffer[len++] = '\n'; ds@1952: buffer[len] = '\0'; ds@1952: } ds@1952: ds@1952: Engine_LogPrintWrapper(buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params) ds@1952: { dvander@2042: g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); ds@1952: ds@1952: char buffer[1024]; ds@1952: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); ds@1952: dvander@2193: if (pContext->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); ds@1952: g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params) ds@1952: { dvander@2042: g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); ds@1952: ds@1952: char buffer[1024]; ds@1952: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); ds@1952: dvander@2193: if (pContext->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); ds@1952: g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_GetFileTime(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: int err; ds@1952: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) ds@1952: { ds@1952: pContext->ThrowNativeErrorEx(err, NULL); ds@1952: return 0; ds@1952: } ds@1952: ds@1952: time_t time_val; ds@1952: char realpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); ds@1952: ds@1952: if (!g_LibSys.FileTime(realpath, (FileTimeType)params[2], &time_val)) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: ds@1952: return (cell_t)time_val; ds@1952: } ds@1952: ds@1952: static cell_t sm_LogToOpenFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char buffer[2048]; dvander@2042: g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); ds@1952: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2); ds@1952: dvander@2193: if (pContext->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); ds@1952: g_Logger.LogToOpenFile(pFile, "[%s] %s", pPlugin->GetFilename(), buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_LogToOpenFileEx(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec; ds@1952: FILE *pFile; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = g_pCoreIdent; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char buffer[2048]; dvander@2042: g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE); ds@1952: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2); ds@1952: dvander@2193: if (pContext->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: g_Logger.LogToOpenFile(pFile, "%s", buffer); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_ReadFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: FILE *pFile; ds@1952: size_t read = 0; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: if (params[4] != 1 && params[4] != 2 && params[4] != 4) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid size specifier (%d is not 1, 2, or 4)", params[4]); ds@1952: } ds@1952: ds@1952: cell_t *data; ds@1952: pContext->LocalToPhysAddr(params[2], &data); ds@1952: ds@1952: if (params[4] == 4) ds@1952: { ds@1952: read = fread(data, sizeof(cell_t), params[3], pFile); ds@1952: } ds@1952: else if (params[4] == 2) ds@1952: { ds@1952: int16_t val; ds@1952: for (cell_t i = 0; i < params[3]; i++) ds@1952: { ds@1952: if (fread(&val, sizeof(int16_t), 1, pFile) != 1) ds@1952: { ds@1952: break; ds@1952: } ds@1952: data[read++] = val; ds@1952: } ds@1952: } ds@1952: else if (params[4] == 1) ds@1952: { ds@1952: int8_t val; ds@1952: for (cell_t i = 0; i < params[3]; i++) ds@1952: { ds@1952: if (fread(&val, sizeof(int8_t), 1, pFile) != 1) ds@1952: { ds@1952: break; ds@1952: } ds@1952: data[read++] = val; ds@1952: } ds@1952: } ds@1952: ds@1952: if (read != (size_t)params[3] && ferror(pFile) != 0) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: ds@1952: return read; ds@1952: } ds@1952: ds@1952: static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: FILE *pFile; ds@1952: cell_t num_read = 0; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *buffer; ds@1952: pContext->LocalToString(params[2], &buffer); ds@1952: ds@1952: char val; ds@1952: while (1) ds@1952: { ds@1952: /* If we're in stop mode, break as soon as the buffer is full. */ ds@1952: if (params[4] && (params[3] == 0 || num_read >= params[3] - 1)) ds@1952: { ds@1952: break; ds@1952: } ds@1952: if (fread(&val, sizeof(val), 1, pFile) != 1) ds@1952: { ds@1952: if (ferror(pFile)) ds@1952: { ds@1952: return -1; ds@1952: } ds@1952: break; ds@1952: } ds@1952: if (val == '\0') ds@1952: { ds@1952: break; ds@1952: } ds@1952: if (params[3] > 0 && num_read < params[3] - 1) ds@1952: { ds@1952: buffer[num_read++] = val; ds@1952: } ds@1952: } ds@1952: ds@1952: if (params[3] > 0) ds@1952: { ds@1952: buffer[num_read] = '\0'; ds@1952: } ds@1952: ds@1952: return num_read; ds@1952: } ds@1952: ds@1952: static cell_t sm_WriteFile(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: FILE *pFile; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: cell_t *data; ds@1952: pContext->LocalToPhysAddr(params[2], &data); ds@1952: ds@1952: if (params[4] != 1 && params[4] != 2 && params[4] != 4) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid size specifier (%d is not 1, 2, or 4)", params[4]); ds@1952: } ds@1952: ds@1952: /* :NOTE: This really isn't compatible with big endian but we will never have to worry about that. */ ds@1952: ds@1952: if (params[4] == 4) ds@1952: { ds@1952: if (fwrite(data, sizeof(cell_t), params[3], pFile) != (size_t)params[3]) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: else if (params[4] == 2) ds@1952: { ds@1952: for (cell_t i = 0; i < params[3]; i++) ds@1952: { ds@1952: if (fwrite(&data[i], sizeof(int16_t), 1, pFile) != 1) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: else if (params[4] == 1) ds@1952: { ds@1952: for (cell_t i = 0; i < params[3]; i++) ds@1952: { ds@1952: if (fwrite(&data[i], sizeof(int8_t), 1, pFile) != 1) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_WriteFileString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError herr; ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: FILE *pFile; ds@1952: ds@1952: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); ds@1952: } ds@1952: ds@1952: char *buffer; ds@1952: pContext->LocalToString(params[2], &buffer); ds@1952: ds@1952: size_t len = strlen(buffer); ds@1952: ds@1952: if (params[3]) ds@1952: { ds@1952: len++; ds@1952: } ds@1952: ds@1952: return (fwrite(buffer, sizeof(char), len, pFile) == len) ? 1 : 0; ds@1952: } ds@1952: ds@1952: static cell_t sm_AddGameLogHook(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: if ((pFunction=pContext->GetFunctionById(params[1])) == NULL) ds@1952: { ds@1952: return pContext->ThrowNativeError("Function id %x is invalid", params[1]); ds@1952: } ds@1952: ds@1952: s_FileNatives.AddLogHook(pFunction); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_RemoveGameLogHook(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: if ((pFunction=pContext->GetFunctionById(params[1])) == NULL) ds@1952: { ds@1952: return pContext->ThrowNativeError("Function id %x is invalid", params[1]); ds@1952: } ds@1952: ds@1952: s_FileNatives.RemoveLogHook(pFunction); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: REGISTER_NATIVES(filesystem) ds@1952: { ds@1952: {"OpenDirectory", sm_OpenDirectory}, ds@1952: {"ReadDirEntry", sm_ReadDirEntry}, ds@1952: {"OpenFile", sm_OpenFile}, ds@1952: {"DeleteFile", sm_DeleteFile}, ds@1952: {"ReadFileLine", sm_ReadFileLine}, ds@1952: {"IsEndOfFile", sm_IsEndOfFile}, ds@1952: {"FileSeek", sm_FileSeek}, ds@1952: {"FilePosition", sm_FilePosition}, ds@1952: {"FileExists", sm_FileExists}, ds@1952: {"RenameFile", sm_RenameFile}, ds@1952: {"DirExists", sm_DirExists}, ds@1952: {"FileSize", sm_FileSize}, ds@1952: {"RemoveDir", sm_RemoveDir}, ds@1952: {"WriteFileLine", sm_WriteFileLine}, ds@1952: {"BuildPath", sm_BuildPath}, ds@1952: {"LogToGame", sm_LogToGame}, ds@1952: {"LogMessage", sm_LogMessage}, ds@1952: {"LogError", sm_LogError}, ds@1952: {"FlushFile", sm_FlushFile}, ds@1952: {"GetFileTime", sm_GetFileTime}, ds@1952: {"LogToOpenFile", sm_LogToOpenFile}, ds@1952: {"LogToOpenFileEx", sm_LogToOpenFileEx}, ds@1952: {"ReadFile", sm_ReadFile}, ds@1952: {"ReadFileString", sm_ReadFileString}, ds@1952: {"WriteFile", sm_WriteFile}, ds@1952: {"WriteFileString", sm_WriteFileString}, ds@1952: {"AddGameLogHook", sm_AddGameLogHook}, ds@1952: {"RemoveGameLogHook", sm_RemoveGameLogHook}, ds@1952: {NULL, NULL}, ds@1952: };