core/smn_filesystem.cpp
author David Anderson <dvander@alliedmods.net>
Mon Mar 19 01:44:09 2007 +0000 (2007-03-19)
changeset 647 1e29dcc9ca97
parent 638 58d588da18c1
child 652 d62a08ba6318
permissions -rw-r--r--
fixed a potential bug with kicking players during authorization
dvander@370
     1
/**
dvander@370
     2
 * ===============================================================
dvander@370
     3
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     4
 * ===============================================================
dvander@370
     5
 *
dvander@370
     6
 * This file is not open source and may not be copied without explicit
dvander@370
     7
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     8
 * in whole or significant part.
dvander@370
     9
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    10
 *
dvander@370
    11
 * Version: $Id$
dvander@370
    12
 */
dvander@370
    13
faluco@274
    14
#include <sys/stat.h>
dvander@252
    15
#include "sm_globals.h"
dvander@252
    16
#include "HandleSys.h"
dvander@252
    17
#include "LibrarySys.h"
faluco@274
    18
#include "sm_stringutil.h"
dvander@592
    19
#include "Logger.h"
dvander@347
    20
#include "PluginSys.h"
dvander@347
    21
#include "sourcemm_api.h"
dvander@252
    22
dvander@252
    23
HandleType_t g_FileType;
dvander@252
    24
HandleType_t g_DirType;
dvander@252
    25
dvander@252
    26
class FileNatives : 
dvander@252
    27
	public SMGlobalClass,
dvander@252
    28
	public IHandleTypeDispatch
dvander@252
    29
{
dvander@252
    30
public:
dvander@252
    31
	virtual void OnSourceModAllInitialized()
dvander@252
    32
	{
dvander@262
    33
		g_FileType = g_HandleSys.CreateType("File", this, 0, NULL, NULL, g_pCoreIdent, NULL);
dvander@262
    34
		g_DirType = g_HandleSys.CreateType("Directory", this, 0, NULL, NULL, g_pCoreIdent, NULL);
dvander@252
    35
	}
dvander@252
    36
	virtual void OnSourceModShutdown()
dvander@252
    37
	{
dvander@252
    38
		g_HandleSys.RemoveType(g_DirType, g_pCoreIdent);
dvander@252
    39
		g_HandleSys.RemoveType(g_FileType, g_pCoreIdent);
dvander@252
    40
		g_DirType = 0;
dvander@252
    41
		g_FileType = 0;
dvander@252
    42
	}
dvander@252
    43
	virtual void OnHandleDestroy(HandleType_t type, void *object)
dvander@252
    44
	{
dvander@252
    45
		if (type == g_FileType)
dvander@252
    46
		{
dvander@252
    47
			FILE *fp = (FILE *)object;
dvander@252
    48
			fclose(fp);
dvander@252
    49
		} else if (type == g_DirType) {
dvander@252
    50
			IDirectory *pDir = (IDirectory *)object;
dvander@252
    51
			g_LibSys.CloseDirectory(pDir);
dvander@252
    52
		}
dvander@252
    53
	}
dvander@252
    54
};
dvander@252
    55
dvander@252
    56
faluco@274
    57
static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
dvander@252
    58
{
dvander@252
    59
	char *path;
dvander@252
    60
	int err;
dvander@252
    61
	if ((err=pContext->LocalToString(params[1], &path)) != SP_ERROR_NONE)
dvander@252
    62
	{
dvander@252
    63
		pContext->ThrowNativeErrorEx(err, NULL);
dvander@252
    64
		return 0;
dvander@252
    65
	}
dvander@252
    66
dvander@255
    67
	char realpath[PLATFORM_MAX_PATH+1];
dvander@317
    68
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", path);
dvander@255
    69
dvander@255
    70
	IDirectory *pDir = g_LibSys.OpenDirectory(realpath);
dvander@252
    71
	if (!pDir)
dvander@252
    72
	{
dvander@252
    73
		return 0;
dvander@252
    74
	}
dvander@252
    75
dvander@262
    76
	return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL);
dvander@252
    77
}
dvander@252
    78
faluco@274
    79
static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
dvander@252
    80
{
dvander@252
    81
	Handle_t hndl = static_cast<Handle_t>(params[1]);
dvander@252
    82
	IDirectory *pDir;
dvander@252
    83
	HandleError herr;
dvander@262
    84
	HandleSecurity sec;
dvander@252
    85
	int err;
dvander@262
    86
dvander@262
    87
	sec.pOwner = NULL;
dvander@262
    88
	sec.pIdentity = g_pCoreIdent;
dvander@262
    89
dvander@262
    90
	if ((herr=g_HandleSys.ReadHandle(hndl, g_DirType, &sec, (void **)&pDir))
dvander@252
    91
		!= HandleError_None)
dvander@252
    92
	{
dvander@252
    93
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
dvander@252
    94
	}
dvander@252
    95
dvander@252
    96
	if (!pDir->MoreFiles())
dvander@252
    97
	{
dvander@252
    98
		return false;
dvander@252
    99
	}
dvander@252
   100
dvander@252
   101
	cell_t *filetype;
dvander@252
   102
	if ((err=pContext->LocalToPhysAddr(params[4], &filetype)) != SP_ERROR_NONE)
dvander@252
   103
	{
dvander@252
   104
		pContext->ThrowNativeErrorEx(err, NULL);
dvander@252
   105
		return 0;
dvander@252
   106
	}
dvander@252
   107
dvander@252
   108
	if (pDir->IsEntryDirectory())
dvander@252
   109
	{
dvander@252
   110
		*filetype = 1;
dvander@252
   111
	} else if (pDir->IsEntryFile()) {
dvander@252
   112
		*filetype = 2;
dvander@252
   113
	} else {
dvander@252
   114
		*filetype = 0;
dvander@252
   115
	}
dvander@252
   116
dvander@252
   117
	const char *path = pDir->GetEntryName();
dvander@255
   118
	if ((err=pContext->StringToLocalUTF8(params[2], params[3], path, NULL))
dvander@252
   119
		!= SP_ERROR_NONE)
dvander@252
   120
	{
dvander@252
   121
		pContext->ThrowNativeErrorEx(err, NULL);
dvander@252
   122
		return 0;
dvander@252
   123
	}
dvander@252
   124
dvander@255
   125
	pDir->NextEntry();
dvander@255
   126
dvander@252
   127
	return true;
dvander@252
   128
}
dvander@252
   129
faluco@274
   130
static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   131
{
faluco@274
   132
	char *name, *mode;
faluco@274
   133
	int err;
faluco@274
   134
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   135
	{
faluco@274
   136
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   137
		return 0;
faluco@274
   138
	}
faluco@274
   139
	if ((err=pContext->LocalToString(params[2], &mode)) != SP_ERROR_NONE)
faluco@274
   140
	{
faluco@274
   141
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   142
		return 0;
faluco@274
   143
	}
faluco@274
   144
faluco@274
   145
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   146
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   147
faluco@274
   148
	FILE *pFile = fopen(realpath, mode);
faluco@274
   149
	if (!pFile)
faluco@274
   150
	{
faluco@274
   151
		return 0;
faluco@274
   152
	}
faluco@274
   153
faluco@274
   154
	return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
faluco@274
   155
}
faluco@274
   156
faluco@274
   157
static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   158
{
faluco@274
   159
	char *name;
faluco@274
   160
	int err;
faluco@274
   161
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   162
	{
faluco@274
   163
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   164
		return 0;
faluco@274
   165
	}
faluco@274
   166
faluco@274
   167
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   168
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   169
faluco@274
   170
	return (unlink(realpath)) ? 0 : 1;
faluco@274
   171
}
faluco@274
   172
faluco@274
   173
static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
faluco@274
   174
{
faluco@274
   175
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   176
	HandleError herr;
faluco@274
   177
	HandleSecurity sec;
faluco@274
   178
	FILE *pFile;
faluco@274
   179
	int err;
faluco@274
   180
faluco@274
   181
	sec.pOwner = NULL;
faluco@274
   182
	sec.pIdentity = g_pCoreIdent;
faluco@274
   183
faluco@274
   184
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   185
		!= HandleError_None)
faluco@274
   186
	{
faluco@274
   187
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   188
	}
faluco@274
   189
faluco@274
   190
	char *buf;
faluco@274
   191
	if ((err=pContext->LocalToString(params[2], &buf)) != SP_ERROR_NONE)
faluco@274
   192
	{
faluco@274
   193
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   194
		return 0;
faluco@274
   195
	}
faluco@274
   196
dvander@638
   197
	if (fgets(buf, params[3], pFile) == NULL)
dvander@638
   198
	{
dvander@638
   199
		return 0;
dvander@638
   200
	}
faluco@274
   201
faluco@274
   202
	return 1;
faluco@274
   203
}
faluco@274
   204
faluco@274
   205
static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   206
{
faluco@274
   207
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   208
	HandleError herr;
faluco@274
   209
	HandleSecurity sec;
faluco@274
   210
	FILE *pFile;
faluco@274
   211
faluco@274
   212
	sec.pOwner = NULL;
faluco@274
   213
	sec.pIdentity = g_pCoreIdent;
faluco@274
   214
faluco@274
   215
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   216
		!= HandleError_None)
faluco@274
   217
	{
faluco@274
   218
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   219
	}
faluco@274
   220
faluco@274
   221
	return (feof(pFile)) ? 1 : 0;
faluco@274
   222
}
faluco@274
   223
faluco@274
   224
static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
faluco@274
   225
{
faluco@274
   226
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   227
	HandleError herr;
faluco@274
   228
	HandleSecurity sec;
faluco@274
   229
	FILE *pFile;
faluco@274
   230
faluco@274
   231
	sec.pOwner = NULL;
faluco@274
   232
	sec.pIdentity = g_pCoreIdent;
faluco@274
   233
faluco@274
   234
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   235
		!= HandleError_None)
faluco@274
   236
	{
faluco@274
   237
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   238
	}
faluco@274
   239
faluco@274
   240
	fseek(pFile, params[2], params[3]);
faluco@274
   241
	
faluco@274
   242
	return 1;
faluco@274
   243
}
faluco@274
   244
faluco@274
   245
static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params)
faluco@274
   246
{
faluco@274
   247
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   248
	HandleError herr;
faluco@274
   249
	HandleSecurity sec;
faluco@274
   250
	FILE *pFile;
faluco@274
   251
faluco@274
   252
	sec.pOwner = NULL;
faluco@274
   253
	sec.pIdentity = g_pCoreIdent;
faluco@274
   254
faluco@274
   255
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   256
		!= HandleError_None)
faluco@274
   257
	{
faluco@274
   258
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   259
	}
faluco@274
   260
faluco@274
   261
	return ftell(pFile);
faluco@274
   262
}
faluco@274
   263
faluco@274
   264
static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
faluco@274
   265
{
faluco@274
   266
	char *name;
faluco@274
   267
	int err;
faluco@274
   268
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   269
	{
faluco@274
   270
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   271
		return 0;
faluco@274
   272
	}
faluco@274
   273
faluco@274
   274
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   275
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   276
#ifdef PLATFORM_WINDOWS
faluco@274
   277
	struct _stat s;
faluco@274
   278
	if (_stat(realpath, &s) != 0)
faluco@274
   279
	{
faluco@274
   280
		return 0;
faluco@274
   281
	}
faluco@274
   282
	if (s.st_mode & S_IFREG)
faluco@274
   283
	{
faluco@274
   284
		return 1;
faluco@274
   285
	}
faluco@274
   286
	return 0;
dvander@351
   287
#elif defined PLATFORM_POSIX
faluco@274
   288
	struct stat s;
faluco@274
   289
	if (stat(realpath, &s) != 0)
faluco@274
   290
	{
faluco@274
   291
		return 0;
faluco@274
   292
	}
faluco@274
   293
	if (S_ISREG(s.st_mode))
faluco@274
   294
	{
faluco@274
   295
		return 1;
faluco@274
   296
	}
faluco@274
   297
	return 0;
faluco@274
   298
#endif
faluco@274
   299
}
faluco@274
   300
faluco@274
   301
static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   302
{
faluco@274
   303
	char *newpath, *oldpath;
faluco@274
   304
	int err;
faluco@274
   305
	if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE)
faluco@274
   306
	{
faluco@274
   307
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   308
		return 0;
faluco@274
   309
	}
faluco@274
   310
	if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE)
faluco@274
   311
	{
faluco@274
   312
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   313
		return 0;
faluco@274
   314
	}
faluco@274
   315
faluco@274
   316
	char new_realpath[PLATFORM_MAX_PATH+1];
dvander@636
   317
	g_SourceMod.BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath);
faluco@274
   318
	char old_realpath[PLATFORM_MAX_PATH+1];
dvander@636
   319
	g_SourceMod.BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath);
faluco@274
   320
faluco@274
   321
#ifdef PLATFORM_WINDOWS
faluco@274
   322
	return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0;
dvander@351
   323
#elif defined PLATFORM_POSIX
faluco@274
   324
	return (rename(old_realpath, new_realpath)) ? 0 : 1;
faluco@274
   325
#endif
faluco@274
   326
}
faluco@274
   327
faluco@274
   328
static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
faluco@274
   329
{
faluco@274
   330
	char *name;
faluco@274
   331
	int err;
faluco@274
   332
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   333
	{
faluco@274
   334
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   335
		return 0;
faluco@274
   336
	}
faluco@274
   337
faluco@274
   338
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   339
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   340
#ifdef PLATFORM_WINDOWS
faluco@274
   341
	struct _stat s;
faluco@274
   342
	if (_stat(realpath, &s) != 0)
faluco@274
   343
	{
faluco@274
   344
		return 0;
faluco@274
   345
	}
faluco@274
   346
	if (s.st_mode & S_IFDIR)
faluco@274
   347
	{
faluco@274
   348
		return 1;
faluco@274
   349
	}
faluco@274
   350
	return 0;
dvander@351
   351
#elif defined PLATFORM_POSIX
faluco@274
   352
	struct stat s;
faluco@274
   353
	if (stat(realpath, &s) != 0)
faluco@274
   354
	{
faluco@274
   355
		return 0;
faluco@274
   356
	}
faluco@274
   357
	if (S_ISDIR(s.st_mode))
faluco@274
   358
	{
faluco@274
   359
		return 1;
faluco@274
   360
	}
faluco@274
   361
	return 0;
faluco@274
   362
#endif
faluco@274
   363
}
faluco@274
   364
faluco@274
   365
static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
faluco@274
   366
{
faluco@274
   367
	char *name;
faluco@274
   368
	int err;
faluco@274
   369
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   370
	{
faluco@274
   371
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   372
		return -1;
faluco@274
   373
	}
faluco@274
   374
faluco@274
   375
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   376
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   377
#ifdef PLATFORM_WINDOWS
faluco@274
   378
	struct _stat s;
faluco@274
   379
	if (_stat(realpath, &s) != 0)
faluco@274
   380
	{
faluco@274
   381
		return -1;
faluco@274
   382
	}
faluco@274
   383
	if (s.st_mode & S_IFREG)
faluco@274
   384
	{
faluco@274
   385
		return static_cast<cell_t>(s.st_size);
faluco@274
   386
	}
faluco@274
   387
	return -1;
dvander@351
   388
#elif defined PLATFORM_POSIX
faluco@274
   389
	struct stat s;
faluco@274
   390
	if (stat(realpath, &s) != 0)
faluco@274
   391
	{
faluco@274
   392
		return -1;
faluco@274
   393
	}
faluco@274
   394
	if (S_ISREG(s.st_mode))
faluco@274
   395
	{
faluco@274
   396
		return static_cast<cell_t>(s.st_size);
faluco@274
   397
	}
faluco@274
   398
	return -1;
faluco@274
   399
#endif
faluco@274
   400
}
faluco@274
   401
faluco@274
   402
static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params)
faluco@274
   403
{
faluco@274
   404
	char *name;
faluco@274
   405
	int err;
faluco@274
   406
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   407
	{
faluco@274
   408
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   409
		return 0;
faluco@274
   410
	}
faluco@274
   411
faluco@274
   412
	char realpath[PLATFORM_MAX_PATH+1];
dvander@636
   413
	g_SourceMod.BuildPath(Path_Game, realpath, sizeof(realpath), "%s", name);
faluco@274
   414
faluco@274
   415
	return (rmdir(realpath)) ? 0 : 1;
faluco@274
   416
}
faluco@274
   417
faluco@274
   418
static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
faluco@274
   419
{
faluco@274
   420
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   421
	HandleError herr;
faluco@274
   422
	HandleSecurity sec;
faluco@274
   423
	FILE *pFile;
faluco@274
   424
faluco@274
   425
	sec.pOwner = NULL;
faluco@274
   426
	sec.pIdentity = g_pCoreIdent;
faluco@274
   427
faluco@274
   428
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   429
		!= HandleError_None)
faluco@274
   430
	{
faluco@274
   431
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   432
	}
faluco@274
   433
faluco@274
   434
	char *fmt;
faluco@274
   435
	int err;
faluco@274
   436
	if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE)
faluco@274
   437
	{
faluco@274
   438
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   439
		return 0;
faluco@274
   440
	}
faluco@274
   441
faluco@274
   442
	char buffer[2048];
faluco@274
   443
	int arg = 3;
faluco@274
   444
	atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
faluco@274
   445
	fprintf(pFile, "%s\n", buffer);
faluco@274
   446
faluco@274
   447
	return 1;
faluco@274
   448
}
faluco@274
   449
dvander@345
   450
static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params)
dvander@345
   451
{
dvander@345
   452
	char path[PLATFORM_MAX_PATH], *fmt, *buffer;
dvander@345
   453
	int arg = 4;
dvander@345
   454
	pContext->LocalToString(params[2], &buffer);
dvander@345
   455
	pContext->LocalToString(params[4], &fmt);
dvander@345
   456
dvander@345
   457
	atcprintf(path, sizeof(path), fmt, pContext, params, &arg);
dvander@345
   458
dvander@345
   459
	return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path);
dvander@345
   460
}
dvander@345
   461
dvander@347
   462
static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params)
dvander@347
   463
{
dvander@347
   464
	char buffer[1024];
dvander@347
   465
	size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   466
dvander@347
   467
	if (len >= sizeof(buffer)-2)
dvander@347
   468
	{
dvander@347
   469
		buffer[1022] = '\n';
dvander@347
   470
		buffer[1023] = '\0';
dvander@347
   471
	} else {
dvander@347
   472
		buffer[len++] = '\n';
dvander@347
   473
		buffer[len] = '\0';
dvander@347
   474
	}
dvander@347
   475
dvander@347
   476
	engine->LogPrint(buffer);
dvander@347
   477
dvander@347
   478
	return 1;
dvander@347
   479
}
dvander@347
   480
dvander@347
   481
static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params)
dvander@347
   482
{
dvander@347
   483
	char buffer[1024];
dvander@351
   484
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   485
dvander@347
   486
	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
dvander@347
   487
	g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer);
dvander@347
   488
dvander@347
   489
	return 1;
dvander@347
   490
}
dvander@347
   491
dvander@347
   492
static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params)
dvander@347
   493
{
dvander@347
   494
	char buffer[1024];
dvander@351
   495
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   496
dvander@347
   497
	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
dvander@347
   498
	g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer);
dvander@347
   499
dvander@347
   500
	return 1;
dvander@347
   501
}
dvander@347
   502
dvander@255
   503
static FileNatives s_FileNatives;
dvander@252
   504
dvander@252
   505
REGISTER_NATIVES(filesystem)
dvander@252
   506
{
dvander@252
   507
	{"OpenDirectory",			sm_OpenDirectory},
dvander@252
   508
	{"ReadDirEntry",			sm_ReadDirEntry},
faluco@274
   509
	{"OpenFile",				sm_OpenFile},
faluco@274
   510
	{"DeleteFile",				sm_DeleteFile},
faluco@274
   511
	{"ReadFileLine",			sm_ReadFileLine},
faluco@274
   512
	{"IsEndOfFile",				sm_IsEndOfFile},
faluco@274
   513
	{"FileSeek",				sm_FileSeek},
faluco@274
   514
	{"FilePosition",			sm_FilePosition},
faluco@274
   515
	{"FileExists",				sm_FileExists},
faluco@274
   516
	{"RenameFile",				sm_RenameFile},
faluco@274
   517
	{"DirExists",				sm_DirExists},
faluco@274
   518
	{"FileSize",				sm_FileSize},
faluco@274
   519
	{"RemoveDir",				sm_RemoveDir},
faluco@274
   520
	{"WriteFileLine",			sm_WriteFileLine},
dvander@345
   521
	{"BuildPath",				sm_BuildPath},
dvander@636
   522
	{"LogToGame",				sm_LogToGame},
dvander@636
   523
	{"LogMessage",				sm_LogMessage},
dvander@347
   524
	{"LogError",				sm_LogError},
dvander@252
   525
	{NULL,						NULL},
dvander@252
   526
};