1.1 --- a/core/smn_filesystem.cpp Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,689 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include <sys/stat.h>
1.36 -#include "sm_globals.h"
1.37 -#include "HandleSys.h"
1.38 -#include "LibrarySys.h"
1.39 -#include "sm_stringutil.h"
1.40 -#include "Logger.h"
1.41 -#include "PluginSys.h"
1.42 -#include "sourcemm_api.h"
1.43 -
1.44 -HandleType_t g_FileType;
1.45 -HandleType_t g_DirType;
1.46 -
1.47 -class FileNatives :
1.48 - public SMGlobalClass,
1.49 - public IHandleTypeDispatch
1.50 -{
1.51 -public:
1.52 - virtual void OnSourceModAllInitialized()
1.53 - {
1.54 - g_FileType = g_HandleSys.CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.55 - g_DirType = g_HandleSys.CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.56 - }
1.57 - virtual void OnSourceModShutdown()
1.58 - {
1.59 - g_HandleSys.RemoveType(g_DirType, g_pCoreIdent);
1.60 - g_HandleSys.RemoveType(g_FileType, g_pCoreIdent);
1.61 - g_DirType = 0;
1.62 - g_FileType = 0;
1.63 - }
1.64 - virtual void OnHandleDestroy(HandleType_t type, void *object)
1.65 - {
1.66 - if (type == g_FileType)
1.67 - {
1.68 - FILE *fp = (FILE *)object;
1.69 - fclose(fp);
1.70 - } else if (type == g_DirType) {
1.71 - IDirectory *pDir = (IDirectory *)object;
1.72 - g_LibSys.CloseDirectory(pDir);
1.73 - }
1.74 - }
1.75 -} s_FileNatives;
1.76 -
1.77 -static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
1.78 -{
1.79 - char *path;
1.80 - int err;
1.81 - if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE)
1.82 - {
1.83 - pContext->ThrowNativeErrorEx(err, NULL);
1.84 - return 0;
1.85 - }
1.86 -
1.87 - char realpath[PLATFORM_MAX_PATH];
1.88 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
1.89 -
1.90 - IDirectory *pDir = g_LibSys.OpenDirectory(realpath);
1.91 - if (!pDir)
1.92 - {
1.93 - return 0;
1.94 - }
1.95 -
1.96 - return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.97 -}
1.98 -
1.99 -static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
1.100 -{
1.101 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.102 - IDirectory *pDir;
1.103 - HandleError herr;
1.104 - HandleSecurity sec;
1.105 - int err;
1.106 -
1.107 - sec.pOwner = NULL;
1.108 - sec.pIdentity = g_pCoreIdent;
1.109 -
1.110 - if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, &sec, (void **)&pDir))
1.111 - != HandleError_None)
1.112 - {
1.113 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.114 - }
1.115 -
1.116 - if (!pDir->MoreFiles())
1.117 - {
1.118 - return false;
1.119 - }
1.120 -
1.121 - cell_t *filetype;
1.122 - if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE)
1.123 - {
1.124 - pContext->ThrowNativeErrorEx(err, NULL);
1.125 - return 0;
1.126 - }
1.127 -
1.128 - if (pDir->IsEntryDirectory())
1.129 - {
1.130 - *filetype = 1;
1.131 - } else if (pDir->IsEntryFile()) {
1.132 - *filetype = 2;
1.133 - } else {
1.134 - *filetype = 0;
1.135 - }
1.136 -
1.137 - const char *path = pDir->GetEntryName();
1.138 - if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL))
1.139 - != SP_ERROR_NONE)
1.140 - {
1.141 - pContext->ThrowNativeErrorEx(err, NULL);
1.142 - return 0;
1.143 - }
1.144 -
1.145 - pDir->NextEntry();
1.146 -
1.147 - return true;
1.148 -}
1.149 -
1.150 -static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
1.151 -{
1.152 - char *name, *mode;
1.153 - int err;
1.154 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.155 - {
1.156 - pContext->ThrowNativeErrorEx(err, NULL);
1.157 - return 0;
1.158 - }
1.159 - if ((err=pContext->LocalToString(params[2], &mode)) != SP_ERROR_NONE)
1.160 - {
1.161 - pContext->ThrowNativeErrorEx(err, NULL);
1.162 - return 0;
1.163 - }
1.164 -
1.165 - char realpath[PLATFORM_MAX_PATH];
1.166 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.167 -
1.168 - FILE *pFile = fopen(realpath, mode);
1.169 - if (!pFile)
1.170 - {
1.171 - return 0;
1.172 - }
1.173 -
1.174 - return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.175 -}
1.176 -
1.177 -static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
1.178 -{
1.179 - char *name;
1.180 - int err;
1.181 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.182 - {
1.183 - pContext->ThrowNativeErrorEx(err, NULL);
1.184 - return 0;
1.185 - }
1.186 -
1.187 - char realpath[PLATFORM_MAX_PATH];
1.188 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.189 -
1.190 - return (unlink(realpath)) ? 0 : 1;
1.191 -}
1.192 -
1.193 -static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
1.194 -{
1.195 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.196 - HandleError herr;
1.197 - HandleSecurity sec;
1.198 - FILE *pFile;
1.199 - int err;
1.200 -
1.201 - sec.pOwner = NULL;
1.202 - sec.pIdentity = g_pCoreIdent;
1.203 -
1.204 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.205 - != HandleError_None)
1.206 - {
1.207 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.208 - }
1.209 -
1.210 - char *buf;
1.211 - if ((err=pContext->LocalToString(params[2], &buf)) != SP_ERROR_NONE)
1.212 - {
1.213 - pContext->ThrowNativeErrorEx(err, NULL);
1.214 - return 0;
1.215 - }
1.216 -
1.217 - if (fgets(buf, params[3], pFile) == NULL)
1.218 - {
1.219 - return 0;
1.220 - }
1.221 -
1.222 - return 1;
1.223 -}
1.224 -
1.225 -static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
1.226 -{
1.227 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.228 - HandleError herr;
1.229 - HandleSecurity sec;
1.230 - FILE *pFile;
1.231 -
1.232 - sec.pOwner = NULL;
1.233 - sec.pIdentity = g_pCoreIdent;
1.234 -
1.235 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.236 - != HandleError_None)
1.237 - {
1.238 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.239 - }
1.240 -
1.241 - return (feof(pFile)) ? 1 : 0;
1.242 -}
1.243 -
1.244 -static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
1.245 -{
1.246 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.247 - HandleError herr;
1.248 - HandleSecurity sec;
1.249 - FILE *pFile;
1.250 -
1.251 - sec.pOwner = NULL;
1.252 - sec.pIdentity = g_pCoreIdent;
1.253 -
1.254 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.255 - != HandleError_None)
1.256 - {
1.257 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.258 - }
1.259 -
1.260 - fseek(pFile, params[2], params[3]);
1.261 -
1.262 - return 1;
1.263 -}
1.264 -
1.265 -static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params)
1.266 -{
1.267 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.268 - HandleError herr;
1.269 - HandleSecurity sec;
1.270 - FILE *pFile;
1.271 -
1.272 - sec.pOwner = NULL;
1.273 - sec.pIdentity = g_pCoreIdent;
1.274 -
1.275 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.276 - != HandleError_None)
1.277 - {
1.278 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.279 - }
1.280 -
1.281 - return ftell(pFile);
1.282 -}
1.283 -
1.284 -static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
1.285 -{
1.286 - char *name;
1.287 - int err;
1.288 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.289 - {
1.290 - pContext->ThrowNativeErrorEx(err, NULL);
1.291 - return 0;
1.292 - }
1.293 -
1.294 - char realpath[PLATFORM_MAX_PATH];
1.295 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.296 -#ifdef PLATFORM_WINDOWS
1.297 - struct _stat s;
1.298 - if (_stat(realpath, &s) != 0)
1.299 - {
1.300 - return 0;
1.301 - }
1.302 - if (s.st_mode & S_IFREG)
1.303 - {
1.304 - return 1;
1.305 - }
1.306 - return 0;
1.307 -#elif defined PLATFORM_POSIX
1.308 - struct stat s;
1.309 - if (stat(realpath, &s) != 0)
1.310 - {
1.311 - return 0;
1.312 - }
1.313 - if (S_ISREG(s.st_mode))
1.314 - {
1.315 - return 1;
1.316 - }
1.317 - return 0;
1.318 -#endif
1.319 -}
1.320 -
1.321 -static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
1.322 -{
1.323 - char *newpath, *oldpath;
1.324 - int err;
1.325 - if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE)
1.326 - {
1.327 - pContext->ThrowNativeErrorEx(err, NULL);
1.328 - return 0;
1.329 - }
1.330 - if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE)
1.331 - {
1.332 - pContext->ThrowNativeErrorEx(err, NULL);
1.333 - return 0;
1.334 - }
1.335 -
1.336 - char new_realpath[PLATFORM_MAX_PATH];
1.337 - g_SourceMod.BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath);
1.338 - char old_realpath[PLATFORM_MAX_PATH];
1.339 - g_SourceMod.BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath);
1.340 -
1.341 -#ifdef PLATFORM_WINDOWS
1.342 - return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0;
1.343 -#elif defined PLATFORM_POSIX
1.344 - return (rename(old_realpath, new_realpath)) ? 0 : 1;
1.345 -#endif
1.346 -}
1.347 -
1.348 -static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
1.349 -{
1.350 - char *name;
1.351 - int err;
1.352 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.353 - {
1.354 - pContext->ThrowNativeErrorEx(err, NULL);
1.355 - return 0;
1.356 - }
1.357 -
1.358 - char realpath[PLATFORM_MAX_PATH];
1.359 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.360 -#ifdef PLATFORM_WINDOWS
1.361 - struct _stat s;
1.362 - if (_stat(realpath, &s) != 0)
1.363 - {
1.364 - return 0;
1.365 - }
1.366 - if (s.st_mode & S_IFDIR)
1.367 - {
1.368 - return 1;
1.369 - }
1.370 - return 0;
1.371 -#elif defined PLATFORM_POSIX
1.372 - struct stat s;
1.373 - if (stat(realpath, &s) != 0)
1.374 - {
1.375 - return 0;
1.376 - }
1.377 - if (S_ISDIR(s.st_mode))
1.378 - {
1.379 - return 1;
1.380 - }
1.381 - return 0;
1.382 -#endif
1.383 -}
1.384 -
1.385 -static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
1.386 -{
1.387 - char *name;
1.388 - int err;
1.389 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.390 - {
1.391 - pContext->ThrowNativeErrorEx(err, NULL);
1.392 - return -1;
1.393 - }
1.394 -
1.395 - char realpath[PLATFORM_MAX_PATH];
1.396 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.397 -#ifdef PLATFORM_WINDOWS
1.398 - struct _stat s;
1.399 - if (_stat(realpath, &s) != 0)
1.400 - {
1.401 - return -1;
1.402 - }
1.403 - if (s.st_mode & S_IFREG)
1.404 - {
1.405 - return static_cast<cell_t>(s.st_size);
1.406 - }
1.407 - return -1;
1.408 -#elif defined PLATFORM_POSIX
1.409 - struct stat s;
1.410 - if (stat(realpath, &s) != 0)
1.411 - {
1.412 - return -1;
1.413 - }
1.414 - if (S_ISREG(s.st_mode))
1.415 - {
1.416 - return static_cast<cell_t>(s.st_size);
1.417 - }
1.418 - return -1;
1.419 -#endif
1.420 -}
1.421 -
1.422 -static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params)
1.423 -{
1.424 - char *name;
1.425 - int err;
1.426 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.427 - {
1.428 - pContext->ThrowNativeErrorEx(err, NULL);
1.429 - return 0;
1.430 - }
1.431 -
1.432 - char realpath[PLATFORM_MAX_PATH];
1.433 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.434 -
1.435 - return (rmdir(realpath)) ? 0 : 1;
1.436 -}
1.437 -
1.438 -static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
1.439 -{
1.440 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.441 - HandleError herr;
1.442 - HandleSecurity sec;
1.443 - FILE *pFile;
1.444 -
1.445 - sec.pOwner = NULL;
1.446 - sec.pIdentity = g_pCoreIdent;
1.447 -
1.448 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.449 - != HandleError_None)
1.450 - {
1.451 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.452 - }
1.453 -
1.454 - char *fmt;
1.455 - int err;
1.456 - if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE)
1.457 - {
1.458 - pContext->ThrowNativeErrorEx(err, NULL);
1.459 - return 0;
1.460 - }
1.461 -
1.462 - char buffer[2048];
1.463 - int arg = 3;
1.464 - atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
1.465 - fprintf(pFile, "%s\n", buffer);
1.466 -
1.467 - return 1;
1.468 -}
1.469 -
1.470 -static cell_t sm_FlushFile(IPluginContext *pContext, const cell_t *params)
1.471 -{
1.472 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.473 - HandleError herr;
1.474 - HandleSecurity sec;
1.475 - FILE *pFile;
1.476 -
1.477 - sec.pOwner = NULL;
1.478 - sec.pIdentity = g_pCoreIdent;
1.479 -
1.480 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.481 - != HandleError_None)
1.482 - {
1.483 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.484 - }
1.485 -
1.486 - return (fflush(pFile) == 0) ? 1 : 0;
1.487 -}
1.488 -
1.489 -static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params)
1.490 -{
1.491 - char path[PLATFORM_MAX_PATH], *fmt, *buffer;
1.492 - int arg = 5;
1.493 - pContext->LocalToString(params[2], &buffer);
1.494 - pContext->LocalToString(params[4], &fmt);
1.495 -
1.496 - atcprintf(path, sizeof(path), fmt, pContext, params, &arg);
1.497 -
1.498 - return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path);
1.499 -}
1.500 -
1.501 -static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params)
1.502 -{
1.503 - g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.504 -
1.505 - char buffer[1024];
1.506 - size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
1.507 -
1.508 - if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.509 - {
1.510 - return 0;
1.511 - }
1.512 -
1.513 - if (len >= sizeof(buffer)-2)
1.514 - {
1.515 - buffer[1022] = '\n';
1.516 - buffer[1023] = '\0';
1.517 - } else {
1.518 - buffer[len++] = '\n';
1.519 - buffer[len] = '\0';
1.520 - }
1.521 -
1.522 - engine->LogPrint(buffer);
1.523 -
1.524 - return 1;
1.525 -}
1.526 -
1.527 -static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params)
1.528 -{
1.529 - g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.530 -
1.531 - char buffer[1024];
1.532 - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
1.533 -
1.534 - if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.535 - {
1.536 - return 0;
1.537 - }
1.538 -
1.539 - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.540 - g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer);
1.541 -
1.542 - return 1;
1.543 -}
1.544 -
1.545 -static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params)
1.546 -{
1.547 - g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.548 -
1.549 - char buffer[1024];
1.550 - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
1.551 -
1.552 - if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.553 - {
1.554 - return 0;
1.555 - }
1.556 -
1.557 - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.558 - g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer);
1.559 -
1.560 - return 1;
1.561 -}
1.562 -
1.563 -enum
1.564 -{
1.565 - FileTime_LastAccess = 0, /* Last access (not available on FAT) */
1.566 - FileTime_Created = 1, /* Creation (not available on FAT) */
1.567 - FileTime_LastChange = 2, /* Last modification */
1.568 -};
1.569 -
1.570 -static cell_t sm_GetFileTime(IPluginContext *pContext, const cell_t *params)
1.571 -{
1.572 - char *name;
1.573 - int err;
1.574 - if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.575 - {
1.576 - pContext->ThrowNativeErrorEx(err, NULL);
1.577 - return 0;
1.578 - }
1.579 -
1.580 - char realpath[PLATFORM_MAX_PATH];
1.581 - g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
1.582 -
1.583 -#ifdef PLATFORM_WINDOWS
1.584 - struct _stat s;
1.585 - if (_stat(realpath, &s) != 0)
1.586 -#elif defined PLATFORM_POSIX
1.587 - struct stat s;
1.588 - if (stat(realpath, &s) != 0)
1.589 -#endif
1.590 - {
1.591 - return -1;
1.592 - } else {
1.593 - if (params[2] == FileTime_LastAccess)
1.594 - {
1.595 - return (cell_t)s.st_atime;
1.596 - } else if (params[2] == FileTime_Created) {
1.597 - return (cell_t)s.st_ctime;
1.598 - } else if (params[2] == FileTime_LastChange) {
1.599 - return (cell_t)s.st_mtime;
1.600 - }
1.601 - }
1.602 -
1.603 - return -1;
1.604 -}
1.605 -
1.606 -static cell_t sm_LogToOpenFile(IPluginContext *pContext, const cell_t *params)
1.607 -{
1.608 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.609 - HandleError herr;
1.610 - HandleSecurity sec;
1.611 - FILE *pFile;
1.612 -
1.613 - sec.pOwner = NULL;
1.614 - sec.pIdentity = g_pCoreIdent;
1.615 -
1.616 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.617 - != HandleError_None)
1.618 - {
1.619 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.620 - }
1.621 -
1.622 - char buffer[2048];
1.623 - g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.624 - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.625 -
1.626 - if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.627 - {
1.628 - return 0;
1.629 - }
1.630 -
1.631 - CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
1.632 - g_Logger.LogToOpenFile(pFile, "[%s] %s", pPlugin->GetFilename(), buffer);
1.633 -
1.634 - return 1;
1.635 -}
1.636 -
1.637 -static cell_t sm_LogToOpenFileEx(IPluginContext *pContext, const cell_t *params)
1.638 -{
1.639 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.640 - HandleError herr;
1.641 - HandleSecurity sec;
1.642 - FILE *pFile;
1.643 -
1.644 - sec.pOwner = NULL;
1.645 - sec.pIdentity = g_pCoreIdent;
1.646 -
1.647 - if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.648 - != HandleError_None)
1.649 - {
1.650 - return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.651 - }
1.652 -
1.653 - char buffer[2048];
1.654 - g_SourceMod.SetGlobalTarget(LANG_SERVER);
1.655 - g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1.656 -
1.657 - if (pContext->GetContext()->n_err != SP_ERROR_NONE)
1.658 - {
1.659 - return 0;
1.660 - }
1.661 -
1.662 - g_Logger.LogToOpenFile(pFile, "%s", buffer);
1.663 -
1.664 - return 1;
1.665 -}
1.666 -
1.667 -REGISTER_NATIVES(filesystem)
1.668 -{
1.669 - {"OpenDirectory", sm_OpenDirectory},
1.670 - {"ReadDirEntry", sm_ReadDirEntry},
1.671 - {"OpenFile", sm_OpenFile},
1.672 - {"DeleteFile", sm_DeleteFile},
1.673 - {"ReadFileLine", sm_ReadFileLine},
1.674 - {"IsEndOfFile", sm_IsEndOfFile},
1.675 - {"FileSeek", sm_FileSeek},
1.676 - {"FilePosition", sm_FilePosition},
1.677 - {"FileExists", sm_FileExists},
1.678 - {"RenameFile", sm_RenameFile},
1.679 - {"DirExists", sm_DirExists},
1.680 - {"FileSize", sm_FileSize},
1.681 - {"RemoveDir", sm_RemoveDir},
1.682 - {"WriteFileLine", sm_WriteFileLine},
1.683 - {"BuildPath", sm_BuildPath},
1.684 - {"LogToGame", sm_LogToGame},
1.685 - {"LogMessage", sm_LogMessage},
1.686 - {"LogError", sm_LogError},
1.687 - {"FlushFile", sm_FlushFile},
1.688 - {"GetFileTime", sm_GetFileTime},
1.689 - {"LogToOpenFile", sm_LogToOpenFile},
1.690 - {"LogToOpenFileEx", sm_LogToOpenFileEx},
1.691 - {NULL, NULL},
1.692 -};