dvander@370: /** ds@653: * vim: set ts=4 : dvander@370: * =============================================================== dvander@370: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. dvander@370: * =============================================================== dvander@370: * dvander@370: * This file is not open source and may not be copied without explicit dvander@370: * written permission of AlliedModders LLC. This file may not be redistributed dvander@370: * in whole or significant part. dvander@370: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php dvander@370: * dvander@370: * Version: $Id$ dvander@370: */ dvander@370: faluco@274: #include dvander@252: #include "sm_globals.h" dvander@252: #include "HandleSys.h" dvander@252: #include "LibrarySys.h" faluco@274: #include "sm_stringutil.h" dvander@592: #include "Logger.h" dvander@347: #include "PluginSys.h" dvander@347: #include "sourcemm_api.h" dvander@252: dvander@252: HandleType_t g_FileType; dvander@252: HandleType_t g_DirType; dvander@252: dvander@252: class FileNatives : dvander@252: public SMGlobalClass, dvander@252: public IHandleTypeDispatch dvander@252: { dvander@252: public: dvander@252: virtual void OnSourceModAllInitialized() dvander@252: { dvander@262: g_FileType = g_HandleSys.CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL); dvander@262: g_DirType = g_HandleSys.CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL); dvander@252: } dvander@252: virtual void OnSourceModShutdown() dvander@252: { dvander@252: g_HandleSys.RemoveType(g_DirType, g_pCoreIdent); dvander@252: g_HandleSys.RemoveType(g_FileType, g_pCoreIdent); dvander@252: g_DirType = 0; dvander@252: g_FileType = 0; dvander@252: } dvander@252: virtual void OnHandleDestroy(HandleType_t type, void *object) dvander@252: { dvander@252: if (type == g_FileType) dvander@252: { dvander@252: FILE *fp = (FILE *)object; dvander@252: fclose(fp); dvander@252: } else if (type == g_DirType) { dvander@252: IDirectory *pDir = (IDirectory *)object; dvander@252: g_LibSys.CloseDirectory(pDir); dvander@252: } dvander@252: } dvander@252: }; dvander@252: dvander@252: faluco@274: static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) dvander@252: { dvander@252: char *path; dvander@252: int err; dvander@252: if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE) dvander@252: { dvander@252: pContext->ThrowNativeErrorEx(err, NULL); dvander@252: return 0; dvander@252: } dvander@252: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@317: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path); dvander@255: dvander@255: IDirectory *pDir = g_LibSys.OpenDirectory(realpath); dvander@252: if (!pDir) dvander@252: { dvander@252: return 0; dvander@252: } dvander@252: dvander@262: return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL); dvander@252: } dvander@252: faluco@274: static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params) dvander@252: { dvander@252: Handle_t hndl = static_cast(params[1]); dvander@252: IDirectory *pDir; dvander@252: HandleError herr; dvander@262: HandleSecurity sec; dvander@252: int err; dvander@262: dvander@262: sec.pOwner = NULL; dvander@262: sec.pIdentity = g_pCoreIdent; dvander@262: dvander@262: if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, &sec, (void **)&pDir)) dvander@252: != HandleError_None) dvander@252: { dvander@252: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); dvander@252: } dvander@252: dvander@252: if (!pDir->MoreFiles()) dvander@252: { dvander@252: return false; dvander@252: } dvander@252: dvander@252: cell_t *filetype; dvander@252: if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE) dvander@252: { dvander@252: pContext->ThrowNativeErrorEx(err, NULL); dvander@252: return 0; dvander@252: } dvander@252: dvander@252: if (pDir->IsEntryDirectory()) dvander@252: { dvander@252: *filetype = 1; dvander@252: } else if (pDir->IsEntryFile()) { dvander@252: *filetype = 2; dvander@252: } else { dvander@252: *filetype = 0; dvander@252: } dvander@252: dvander@252: const char *path = pDir->GetEntryName(); dvander@255: if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL)) dvander@252: != SP_ERROR_NONE) dvander@252: { dvander@252: pContext->ThrowNativeErrorEx(err, NULL); dvander@252: return 0; dvander@252: } dvander@252: dvander@255: pDir->NextEntry(); dvander@255: dvander@252: return true; dvander@252: } dvander@252: faluco@274: static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name, *mode; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: if ((err=pContext->LocalToString(params[2], &mode)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: faluco@274: FILE *pFile = fopen(realpath, mode); faluco@274: if (!pFile) faluco@274: { faluco@274: return 0; faluco@274: } faluco@274: faluco@274: return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL); faluco@274: } faluco@274: faluco@274: static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: faluco@274: return (unlink(realpath)) ? 0 : 1; faluco@274: } faluco@274: faluco@274: static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: Handle_t hndl = static_cast(params[1]); faluco@274: HandleError herr; faluco@274: HandleSecurity sec; faluco@274: FILE *pFile; faluco@274: int err; faluco@274: faluco@274: sec.pOwner = NULL; faluco@274: sec.pIdentity = g_pCoreIdent; faluco@274: faluco@274: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) faluco@274: != HandleError_None) faluco@274: { faluco@274: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); faluco@274: } faluco@274: faluco@274: char *buf; faluco@274: if ((err=pContext->LocalToString(params[2], &buf)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: dvander@638: if (fgets(buf, params[3], pFile) == NULL) dvander@638: { dvander@638: return 0; dvander@638: } faluco@274: faluco@274: return 1; faluco@274: } faluco@274: faluco@274: static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: Handle_t hndl = static_cast(params[1]); faluco@274: HandleError herr; faluco@274: HandleSecurity sec; faluco@274: FILE *pFile; faluco@274: faluco@274: sec.pOwner = NULL; faluco@274: sec.pIdentity = g_pCoreIdent; faluco@274: faluco@274: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) faluco@274: != HandleError_None) faluco@274: { faluco@274: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); faluco@274: } faluco@274: faluco@274: return (feof(pFile)) ? 1 : 0; faluco@274: } faluco@274: faluco@274: static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: Handle_t hndl = static_cast(params[1]); faluco@274: HandleError herr; faluco@274: HandleSecurity sec; faluco@274: FILE *pFile; faluco@274: faluco@274: sec.pOwner = NULL; faluco@274: sec.pIdentity = g_pCoreIdent; faluco@274: faluco@274: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) faluco@274: != HandleError_None) faluco@274: { faluco@274: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); faluco@274: } faluco@274: faluco@274: fseek(pFile, params[2], params[3]); faluco@274: faluco@274: return 1; faluco@274: } faluco@274: faluco@274: static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: Handle_t hndl = static_cast(params[1]); faluco@274: HandleError herr; faluco@274: HandleSecurity sec; faluco@274: FILE *pFile; faluco@274: faluco@274: sec.pOwner = NULL; faluco@274: sec.pIdentity = g_pCoreIdent; faluco@274: faluco@274: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) faluco@274: != HandleError_None) faluco@274: { faluco@274: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); faluco@274: } faluco@274: faluco@274: return ftell(pFile); faluco@274: } faluco@274: faluco@274: static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: #ifdef PLATFORM_WINDOWS faluco@274: struct _stat s; faluco@274: if (_stat(realpath, &s) != 0) faluco@274: { faluco@274: return 0; faluco@274: } faluco@274: if (s.st_mode & S_IFREG) faluco@274: { faluco@274: return 1; faluco@274: } faluco@274: return 0; dvander@351: #elif defined PLATFORM_POSIX faluco@274: struct stat s; faluco@274: if (stat(realpath, &s) != 0) faluco@274: { faluco@274: return 0; faluco@274: } faluco@274: if (S_ISREG(s.st_mode)) faluco@274: { faluco@274: return 1; faluco@274: } faluco@274: return 0; faluco@274: #endif faluco@274: } faluco@274: faluco@274: static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *newpath, *oldpath; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char new_realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath); ds@652: char old_realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath); faluco@274: faluco@274: #ifdef PLATFORM_WINDOWS faluco@274: return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0; dvander@351: #elif defined PLATFORM_POSIX faluco@274: return (rename(old_realpath, new_realpath)) ? 0 : 1; faluco@274: #endif faluco@274: } faluco@274: faluco@274: static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: #ifdef PLATFORM_WINDOWS faluco@274: struct _stat s; faluco@274: if (_stat(realpath, &s) != 0) faluco@274: { faluco@274: return 0; faluco@274: } faluco@274: if (s.st_mode & S_IFDIR) faluco@274: { faluco@274: return 1; faluco@274: } faluco@274: return 0; dvander@351: #elif defined PLATFORM_POSIX faluco@274: struct stat s; faluco@274: if (stat(realpath, &s) != 0) faluco@274: { faluco@274: return 0; faluco@274: } faluco@274: if (S_ISDIR(s.st_mode)) faluco@274: { faluco@274: return 1; faluco@274: } faluco@274: return 0; faluco@274: #endif faluco@274: } faluco@274: faluco@274: static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return -1; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: #ifdef PLATFORM_WINDOWS faluco@274: struct _stat s; faluco@274: if (_stat(realpath, &s) != 0) faluco@274: { faluco@274: return -1; faluco@274: } faluco@274: if (s.st_mode & S_IFREG) faluco@274: { faluco@274: return static_cast(s.st_size); faluco@274: } faluco@274: return -1; dvander@351: #elif defined PLATFORM_POSIX faluco@274: struct stat s; faluco@274: if (stat(realpath, &s) != 0) faluco@274: { faluco@274: return -1; faluco@274: } faluco@274: if (S_ISREG(s.st_mode)) faluco@274: { faluco@274: return static_cast(s.st_size); faluco@274: } faluco@274: return -1; faluco@274: #endif faluco@274: } faluco@274: faluco@274: static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: char *name; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: ds@652: char realpath[PLATFORM_MAX_PATH]; dvander@636: g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name); faluco@274: faluco@274: return (rmdir(realpath)) ? 0 : 1; faluco@274: } faluco@274: faluco@274: static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params) faluco@274: { faluco@274: Handle_t hndl = static_cast(params[1]); faluco@274: HandleError herr; faluco@274: HandleSecurity sec; faluco@274: FILE *pFile; faluco@274: faluco@274: sec.pOwner = NULL; faluco@274: sec.pIdentity = g_pCoreIdent; faluco@274: faluco@274: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) faluco@274: != HandleError_None) faluco@274: { faluco@274: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); faluco@274: } faluco@274: faluco@274: char *fmt; faluco@274: int err; faluco@274: if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE) faluco@274: { faluco@274: pContext->ThrowNativeErrorEx(err, NULL); faluco@274: return 0; faluco@274: } faluco@274: faluco@274: char buffer[2048]; faluco@274: int arg = 3; faluco@274: atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg); faluco@274: fprintf(pFile, "%s\n", buffer); faluco@274: faluco@274: return 1; faluco@274: } faluco@274: dvander@821: static cell_t sm_FlushFile(IPluginContext *pContext, const cell_t *params) dvander@821: { dvander@821: Handle_t hndl = static_cast(params[1]); dvander@821: HandleError herr; dvander@821: HandleSecurity sec; dvander@821: FILE *pFile; dvander@821: dvander@821: sec.pOwner = NULL; dvander@821: sec.pIdentity = g_pCoreIdent; dvander@821: dvander@821: if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile)) dvander@821: != HandleError_None) dvander@821: { dvander@821: return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr); dvander@821: } dvander@821: dvander@821: return (fflush(pFile) == 0) ? 1 : 0; dvander@821: } dvander@821: dvander@345: static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params) dvander@345: { dvander@345: char path[PLATFORM_MAX_PATH], *fmt, *buffer; ds@711: int arg = 5; dvander@345: pContext->LocalToString(params[2], &buffer); dvander@345: pContext->LocalToString(params[4], &fmt); dvander@345: dvander@345: atcprintf(path, sizeof(path), fmt, pContext, params, &arg); dvander@345: dvander@345: return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path); dvander@345: } dvander@345: dvander@347: static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params) dvander@347: { dvander@347: char buffer[1024]; dvander@347: size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); dvander@347: dvander@347: if (len >= sizeof(buffer)-2) dvander@347: { dvander@347: buffer[1022] = '\n'; dvander@347: buffer[1023] = '\0'; dvander@347: } else { dvander@347: buffer[len++] = '\n'; dvander@347: buffer[len] = '\0'; dvander@347: } dvander@347: dvander@347: engine->LogPrint(buffer); dvander@347: dvander@347: return 1; dvander@347: } dvander@347: dvander@347: static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params) dvander@347: { dvander@347: char buffer[1024]; dvander@351: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); dvander@347: dvander@347: IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext()); dvander@347: g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer); dvander@347: dvander@347: return 1; dvander@347: } dvander@347: dvander@347: static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params) dvander@347: { dvander@347: char buffer[1024]; dvander@351: g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1); dvander@347: dvander@347: IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext()); dvander@347: g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer); dvander@347: dvander@347: return 1; dvander@347: } dvander@347: dvander@255: static FileNatives s_FileNatives; dvander@252: dvander@252: REGISTER_NATIVES(filesystem) dvander@252: { dvander@252: {"OpenDirectory", sm_OpenDirectory}, dvander@252: {"ReadDirEntry", sm_ReadDirEntry}, faluco@274: {"OpenFile", sm_OpenFile}, faluco@274: {"DeleteFile", sm_DeleteFile}, faluco@274: {"ReadFileLine", sm_ReadFileLine}, faluco@274: {"IsEndOfFile", sm_IsEndOfFile}, faluco@274: {"FileSeek", sm_FileSeek}, faluco@274: {"FilePosition", sm_FilePosition}, faluco@274: {"FileExists", sm_FileExists}, faluco@274: {"RenameFile", sm_RenameFile}, faluco@274: {"DirExists", sm_DirExists}, faluco@274: {"FileSize", sm_FileSize}, faluco@274: {"RemoveDir", sm_RemoveDir}, faluco@274: {"WriteFileLine", sm_WriteFileLine}, dvander@345: {"BuildPath", sm_BuildPath}, dvander@636: {"LogToGame", sm_LogToGame}, dvander@636: {"LogMessage", sm_LogMessage}, dvander@347: {"LogError", sm_LogError}, dvander@821: {"FlushFile", sm_FlushFile}, dvander@252: {NULL, NULL}, dvander@252: };