core/smn_filesystem.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Feb 17 05:33:31 2007 +0000 (2007-02-17)
changeset 511 c29f8796bcf4
parent 370 d3a484f98f6d
child 592 c2a2c38f1331
permissions -rw-r--r--
Initial fixes to ensure static strings passed to new convars
Also fixed minor warning in smn_bitbuffer.cpp
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@347
    19
#include "CLogger.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@317
   146
	g_SourceMod.BuildPath(Path_SM, 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@317
   168
	g_SourceMod.BuildPath(Path_SM, 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
faluco@274
   197
	fgets(buf, params[3], pFile);
faluco@274
   198
faluco@274
   199
	return 1;
faluco@274
   200
}
faluco@274
   201
faluco@274
   202
static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   203
{
faluco@274
   204
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   205
	HandleError herr;
faluco@274
   206
	HandleSecurity sec;
faluco@274
   207
	FILE *pFile;
faluco@274
   208
faluco@274
   209
	sec.pOwner = NULL;
faluco@274
   210
	sec.pIdentity = g_pCoreIdent;
faluco@274
   211
faluco@274
   212
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   213
		!= HandleError_None)
faluco@274
   214
	{
faluco@274
   215
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   216
	}
faluco@274
   217
faluco@274
   218
	return (feof(pFile)) ? 1 : 0;
faluco@274
   219
}
faluco@274
   220
faluco@274
   221
static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
faluco@274
   222
{
faluco@274
   223
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   224
	HandleError herr;
faluco@274
   225
	HandleSecurity sec;
faluco@274
   226
	FILE *pFile;
faluco@274
   227
faluco@274
   228
	sec.pOwner = NULL;
faluco@274
   229
	sec.pIdentity = g_pCoreIdent;
faluco@274
   230
faluco@274
   231
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   232
		!= HandleError_None)
faluco@274
   233
	{
faluco@274
   234
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   235
	}
faluco@274
   236
faluco@274
   237
	fseek(pFile, params[2], params[3]);
faluco@274
   238
	
faluco@274
   239
	return 1;
faluco@274
   240
}
faluco@274
   241
faluco@274
   242
static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params)
faluco@274
   243
{
faluco@274
   244
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   245
	HandleError herr;
faluco@274
   246
	HandleSecurity sec;
faluco@274
   247
	FILE *pFile;
faluco@274
   248
faluco@274
   249
	sec.pOwner = NULL;
faluco@274
   250
	sec.pIdentity = g_pCoreIdent;
faluco@274
   251
faluco@274
   252
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   253
		!= HandleError_None)
faluco@274
   254
	{
faluco@274
   255
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   256
	}
faluco@274
   257
faluco@274
   258
	return ftell(pFile);
faluco@274
   259
}
faluco@274
   260
faluco@274
   261
static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
faluco@274
   262
{
faluco@274
   263
	char *name;
faluco@274
   264
	int err;
faluco@274
   265
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   266
	{
faluco@274
   267
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   268
		return 0;
faluco@274
   269
	}
faluco@274
   270
faluco@274
   271
	char realpath[PLATFORM_MAX_PATH+1];
dvander@317
   272
	g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
faluco@274
   273
#ifdef PLATFORM_WINDOWS
faluco@274
   274
	struct _stat s;
faluco@274
   275
	if (_stat(realpath, &s) != 0)
faluco@274
   276
	{
faluco@274
   277
		return 0;
faluco@274
   278
	}
faluco@274
   279
	if (s.st_mode & S_IFREG)
faluco@274
   280
	{
faluco@274
   281
		return 1;
faluco@274
   282
	}
faluco@274
   283
	return 0;
dvander@351
   284
#elif defined PLATFORM_POSIX
faluco@274
   285
	struct stat s;
faluco@274
   286
	if (stat(realpath, &s) != 0)
faluco@274
   287
	{
faluco@274
   288
		return 0;
faluco@274
   289
	}
faluco@274
   290
	if (S_ISREG(s.st_mode))
faluco@274
   291
	{
faluco@274
   292
		return 1;
faluco@274
   293
	}
faluco@274
   294
	return 0;
faluco@274
   295
#endif
faluco@274
   296
}
faluco@274
   297
faluco@274
   298
static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
faluco@274
   299
{
faluco@274
   300
	char *newpath, *oldpath;
faluco@274
   301
	int err;
faluco@274
   302
	if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE)
faluco@274
   303
	{
faluco@274
   304
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   305
		return 0;
faluco@274
   306
	}
faluco@274
   307
	if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE)
faluco@274
   308
	{
faluco@274
   309
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   310
		return 0;
faluco@274
   311
	}
faluco@274
   312
faluco@274
   313
	char new_realpath[PLATFORM_MAX_PATH+1];
dvander@317
   314
	g_SourceMod.BuildPath(Path_SM, new_realpath, sizeof(new_realpath), "%s", newpath);
faluco@274
   315
	char old_realpath[PLATFORM_MAX_PATH+1];
dvander@317
   316
	g_SourceMod.BuildPath(Path_SM, old_realpath, sizeof(old_realpath), "%s", oldpath);
faluco@274
   317
faluco@274
   318
#ifdef PLATFORM_WINDOWS
faluco@274
   319
	return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0;
dvander@351
   320
#elif defined PLATFORM_POSIX
faluco@274
   321
	return (rename(old_realpath, new_realpath)) ? 0 : 1;
faluco@274
   322
#endif
faluco@274
   323
}
faluco@274
   324
faluco@274
   325
static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
faluco@274
   326
{
faluco@274
   327
	char *name;
faluco@274
   328
	int err;
faluco@274
   329
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   330
	{
faluco@274
   331
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   332
		return 0;
faluco@274
   333
	}
faluco@274
   334
faluco@274
   335
	char realpath[PLATFORM_MAX_PATH+1];
dvander@317
   336
	g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
faluco@274
   337
#ifdef PLATFORM_WINDOWS
faluco@274
   338
	struct _stat s;
faluco@274
   339
	if (_stat(realpath, &s) != 0)
faluco@274
   340
	{
faluco@274
   341
		return 0;
faluco@274
   342
	}
faluco@274
   343
	if (s.st_mode & S_IFDIR)
faluco@274
   344
	{
faluco@274
   345
		return 1;
faluco@274
   346
	}
faluco@274
   347
	return 0;
dvander@351
   348
#elif defined PLATFORM_POSIX
faluco@274
   349
	struct stat s;
faluco@274
   350
	if (stat(realpath, &s) != 0)
faluco@274
   351
	{
faluco@274
   352
		return 0;
faluco@274
   353
	}
faluco@274
   354
	if (S_ISDIR(s.st_mode))
faluco@274
   355
	{
faluco@274
   356
		return 1;
faluco@274
   357
	}
faluco@274
   358
	return 0;
faluco@274
   359
#endif
faluco@274
   360
}
faluco@274
   361
faluco@274
   362
static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
faluco@274
   363
{
faluco@274
   364
	char *name;
faluco@274
   365
	int err;
faluco@274
   366
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   367
	{
faluco@274
   368
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   369
		return -1;
faluco@274
   370
	}
faluco@274
   371
faluco@274
   372
	char realpath[PLATFORM_MAX_PATH+1];
dvander@317
   373
	g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
faluco@274
   374
#ifdef PLATFORM_WINDOWS
faluco@274
   375
	struct _stat s;
faluco@274
   376
	if (_stat(realpath, &s) != 0)
faluco@274
   377
	{
faluco@274
   378
		return -1;
faluco@274
   379
	}
faluco@274
   380
	if (s.st_mode & S_IFREG)
faluco@274
   381
	{
faluco@274
   382
		return static_cast<cell_t>(s.st_size);
faluco@274
   383
	}
faluco@274
   384
	return -1;
dvander@351
   385
#elif defined PLATFORM_POSIX
faluco@274
   386
	struct stat s;
faluco@274
   387
	if (stat(realpath, &s) != 0)
faluco@274
   388
	{
faluco@274
   389
		return -1;
faluco@274
   390
	}
faluco@274
   391
	if (S_ISREG(s.st_mode))
faluco@274
   392
	{
faluco@274
   393
		return static_cast<cell_t>(s.st_size);
faluco@274
   394
	}
faluco@274
   395
	return -1;
faluco@274
   396
#endif
faluco@274
   397
}
faluco@274
   398
faluco@274
   399
static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params)
faluco@274
   400
{
faluco@274
   401
	char *name;
faluco@274
   402
	int err;
faluco@274
   403
	if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
faluco@274
   404
	{
faluco@274
   405
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   406
		return 0;
faluco@274
   407
	}
faluco@274
   408
faluco@274
   409
	char realpath[PLATFORM_MAX_PATH+1];
dvander@317
   410
	g_SourceMod.BuildPath(Path_SM, realpath, sizeof(realpath), "%s", name);
faluco@274
   411
faluco@274
   412
	return (rmdir(realpath)) ? 0 : 1;
faluco@274
   413
}
faluco@274
   414
faluco@274
   415
static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
faluco@274
   416
{
faluco@274
   417
	Handle_t hndl = static_cast<Handle_t>(params[1]);
faluco@274
   418
	HandleError herr;
faluco@274
   419
	HandleSecurity sec;
faluco@274
   420
	FILE *pFile;
faluco@274
   421
faluco@274
   422
	sec.pOwner = NULL;
faluco@274
   423
	sec.pIdentity = g_pCoreIdent;
faluco@274
   424
faluco@274
   425
	if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
faluco@274
   426
		!= HandleError_None)
faluco@274
   427
	{
faluco@274
   428
		return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
faluco@274
   429
	}
faluco@274
   430
faluco@274
   431
	char *fmt;
faluco@274
   432
	int err;
faluco@274
   433
	if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE)
faluco@274
   434
	{
faluco@274
   435
		pContext->ThrowNativeErrorEx(err, NULL);
faluco@274
   436
		return 0;
faluco@274
   437
	}
faluco@274
   438
faluco@274
   439
	char buffer[2048];
faluco@274
   440
	int arg = 3;
faluco@274
   441
	atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
faluco@274
   442
	fprintf(pFile, "%s\n", buffer);
faluco@274
   443
faluco@274
   444
	return 1;
faluco@274
   445
}
faluco@274
   446
dvander@345
   447
static cell_t sm_BuildPath(IPluginContext *pContext, const cell_t *params)
dvander@345
   448
{
dvander@345
   449
	char path[PLATFORM_MAX_PATH], *fmt, *buffer;
dvander@345
   450
	int arg = 4;
dvander@345
   451
	pContext->LocalToString(params[2], &buffer);
dvander@345
   452
	pContext->LocalToString(params[4], &fmt);
dvander@345
   453
dvander@345
   454
	atcprintf(path, sizeof(path), fmt, pContext, params, &arg);
dvander@345
   455
dvander@345
   456
	return g_SourceMod.BuildPath(Path_SM_Rel, buffer, params[3], "%s", path);
dvander@345
   457
}
dvander@345
   458
dvander@347
   459
static cell_t sm_LogToGame(IPluginContext *pContext, const cell_t *params)
dvander@347
   460
{
dvander@347
   461
	char buffer[1024];
dvander@347
   462
	size_t len = g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   463
dvander@347
   464
	if (len >= sizeof(buffer)-2)
dvander@347
   465
	{
dvander@347
   466
		buffer[1022] = '\n';
dvander@347
   467
		buffer[1023] = '\0';
dvander@347
   468
	} else {
dvander@347
   469
		buffer[len++] = '\n';
dvander@347
   470
		buffer[len] = '\0';
dvander@347
   471
	}
dvander@347
   472
dvander@347
   473
	engine->LogPrint(buffer);
dvander@347
   474
dvander@347
   475
	return 1;
dvander@347
   476
}
dvander@347
   477
dvander@347
   478
static cell_t sm_LogMessage(IPluginContext *pContext, const cell_t *params)
dvander@347
   479
{
dvander@347
   480
	char buffer[1024];
dvander@351
   481
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   482
dvander@347
   483
	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
dvander@347
   484
	g_Logger.LogMessage("[%s] %s", pPlugin->GetFilename(), buffer);
dvander@347
   485
dvander@347
   486
	return 1;
dvander@347
   487
}
dvander@347
   488
dvander@347
   489
static cell_t sm_LogError(IPluginContext *pContext, const cell_t *params)
dvander@347
   490
{
dvander@347
   491
	char buffer[1024];
dvander@351
   492
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@347
   493
dvander@347
   494
	IPlugin *pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
dvander@347
   495
	g_Logger.LogError("[%s] %s", pPlugin->GetFilename(), buffer);
dvander@347
   496
dvander@347
   497
	return 1;
dvander@347
   498
}
dvander@347
   499
dvander@255
   500
static FileNatives s_FileNatives;
dvander@252
   501
dvander@252
   502
REGISTER_NATIVES(filesystem)
dvander@252
   503
{
dvander@252
   504
	{"OpenDirectory",			sm_OpenDirectory},
dvander@252
   505
	{"ReadDirEntry",			sm_ReadDirEntry},
faluco@274
   506
	{"OpenFile",				sm_OpenFile},
faluco@274
   507
	{"DeleteFile",				sm_DeleteFile},
faluco@274
   508
	{"ReadFileLine",			sm_ReadFileLine},
faluco@274
   509
	{"IsEndOfFile",				sm_IsEndOfFile},
faluco@274
   510
	{"FileSeek",				sm_FileSeek},
faluco@274
   511
	{"FilePosition",			sm_FilePosition},
faluco@274
   512
	{"FileExists",				sm_FileExists},
faluco@274
   513
	{"RenameFile",				sm_RenameFile},
faluco@274
   514
	{"DirExists",				sm_DirExists},
faluco@274
   515
	{"FileSize",				sm_FileSize},
faluco@274
   516
	{"RemoveDir",				sm_RemoveDir},
faluco@274
   517
	{"WriteFileLine",			sm_WriteFileLine},
dvander@345
   518
	{"BuildPath",				sm_BuildPath},
dvander@347
   519
	{"LogToGame",			sm_LogToGame},
dvander@347
   520
	{"LogMessage",			sm_LogMessage},
dvander@347
   521
	{"LogError",				sm_LogError},
dvander@252
   522
	{NULL,						NULL},
dvander@252
   523
};