core/smn_core.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 24 04:35:48 2008 +0000 (2008-08-24)
changeset 2237 4403a982ec91
parent 2193 db7c6362f3f2
child 2289 44045ead61cd
permissions -rw-r--r--
Improved bot detection slightly. This was meant to go along with the fix for amb1801.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <time.h>
ds@1952
    33
#include <string.h>
ds@1952
    34
#include <stdlib.h>
ds@1952
    35
#include "sm_stringutil.h"
ds@1952
    36
#include "sm_globals.h"
ds@1952
    37
#include "sourcemod.h"
ds@1952
    38
#include "PluginSys.h"
ds@1952
    39
#include "HandleSys.h"
ds@1952
    40
#include "LibrarySys.h"
ds@1952
    41
#include "TimerSys.h"
ds@1952
    42
#include "ForwardSys.h"
ds@1952
    43
#include "Logger.h"
ds@1952
    44
#include "ExtensionSys.h"
ds@1952
    45
ds@1952
    46
#if defined PLATFORM_WINDOWS
ds@1952
    47
#include <windows.h>
ds@1952
    48
#elif defined PLATFORM_LINUX
ds@1952
    49
#include <limits.h>
ds@1952
    50
#include <unistd.h>
ds@1952
    51
#include <sys/times.h>
ds@1952
    52
#endif
ds@1952
    53
ds@1952
    54
HandleType_t g_PlIter;
ds@1952
    55
ConVar sm_datetime_format("sm_datetime_format", "%m/%d/%Y - %H:%M:%S", 0, "Default formatting time rules");
ds@1952
    56
IForward *g_OnLogAction = NULL;
ds@1952
    57
ds@1952
    58
class CoreNativeHelpers : 
ds@1952
    59
	public SMGlobalClass,
ds@1952
    60
	public IHandleTypeDispatch
ds@1952
    61
{
ds@1952
    62
public:
ds@1952
    63
	void OnSourceModAllInitialized()
ds@1952
    64
	{
ds@1952
    65
		HandleAccess hacc;
ds@1952
    66
		g_HandleSys.InitAccessDefaults(NULL,  &hacc);
ds@1952
    67
		hacc.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
ds@1952
    68
ds@1952
    69
		g_PlIter = g_HandleSys.CreateType("PluginIterator", this, 0, NULL, NULL, g_pCoreIdent, NULL);
ds@1952
    70
ds@1952
    71
		g_OnLogAction = g_Forwards.CreateForward("OnLogAction", 
ds@1952
    72
			ET_Hook, 
ds@1952
    73
			5, 
ds@1952
    74
			NULL,
ds@1952
    75
			Param_Cell,
ds@1952
    76
			Param_Cell,
ds@1952
    77
			Param_Cell,
ds@1952
    78
			Param_Cell,
ds@1952
    79
			Param_String);
ds@1952
    80
	}
ds@1952
    81
	void OnHandleDestroy(HandleType_t type, void *object)
ds@1952
    82
	{
ds@1952
    83
		IPluginIterator *iter = (IPluginIterator *)object;
ds@1952
    84
		iter->Release();
ds@1952
    85
	}
ds@1952
    86
	void OnSourceModShutdown()
ds@1952
    87
	{
ds@1952
    88
		g_Forwards.ReleaseForward(g_OnLogAction);
ds@1952
    89
		g_HandleSys.RemoveType(g_PlIter, g_pCoreIdent);
ds@1952
    90
	}
ds@1952
    91
} g_CoreNativeHelpers;
ds@1952
    92
ds@1952
    93
void LogAction(Handle_t hndl, int type, int client, int target, const char *message)
ds@1952
    94
{
ds@1952
    95
	if (g_OnLogAction->GetFunctionCount())
ds@1952
    96
	{
ds@1952
    97
		cell_t result = 0;
ds@1952
    98
		g_OnLogAction->PushCell(hndl);
ds@1952
    99
		g_OnLogAction->PushCell(type);
ds@1952
   100
		g_OnLogAction->PushCell(client);
ds@1952
   101
		g_OnLogAction->PushCell(target);
ds@1952
   102
		g_OnLogAction->PushString(message);
ds@1952
   103
		g_OnLogAction->Execute(&result);
ds@1952
   104
ds@1952
   105
		if (result >= (ResultType)Pl_Handled)
ds@1952
   106
		{
ds@1952
   107
			return;
ds@1952
   108
		}
ds@1952
   109
	}
ds@1952
   110
ds@1952
   111
	const char *logtag = "SM";
ds@1952
   112
	if (type == 2)
ds@1952
   113
	{
ds@1952
   114
		HandleError err;
ds@1952
   115
		IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@1952
   116
		if (pPlugin)
ds@1952
   117
		{
ds@1952
   118
			logtag = pPlugin->GetFilename();
ds@1952
   119
		}
ds@1952
   120
	}
ds@1952
   121
ds@1952
   122
	g_Logger.LogMessage("[%s] %s", logtag, message);
ds@1952
   123
}
ds@1952
   124
ds@1952
   125
static cell_t ThrowError(IPluginContext *pContext, const cell_t *params)
ds@1952
   126
{
ds@1952
   127
	char buffer[512];
ds@1952
   128
dvander@2042
   129
	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
ds@1952
   130
ds@1952
   131
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
ds@1952
   132
dvander@2193
   133
	if (pContext->GetLastNativeError() == SP_ERROR_NONE)
ds@1952
   134
	{
ds@1952
   135
		pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
ds@1952
   136
	}
ds@1952
   137
ds@1952
   138
	return 0;
ds@1952
   139
}
ds@1952
   140
ds@1952
   141
static cell_t GetTime(IPluginContext *pContext, const cell_t *params)
ds@1952
   142
{
ds@1952
   143
	time_t t = GetAdjustedTime();
ds@1952
   144
	cell_t *addr;
ds@1952
   145
	pContext->LocalToPhysAddr(params[1], &addr);
ds@1952
   146
ds@1952
   147
	*(time_t *)addr = t;
ds@1952
   148
ds@1952
   149
	return static_cast<cell_t>(t);
ds@1952
   150
}
ds@1952
   151
ds@1952
   152
#if defined SUBPLATFORM_SECURECRT
ds@1952
   153
void _ignore_invalid_parameter(
ds@1952
   154
						const wchar_t * expression,
ds@1952
   155
						const wchar_t * function, 
ds@1952
   156
						const wchar_t * file, 
ds@1952
   157
						unsigned int line,
ds@1952
   158
						uintptr_t pReserved
ds@1952
   159
						)
ds@1952
   160
{
ds@1952
   161
	/* Wow we don't care, thanks Microsoft. */
ds@1952
   162
}
ds@1952
   163
#endif
ds@1952
   164
ds@1952
   165
static cell_t FormatTime(IPluginContext *pContext, const cell_t *params)
ds@1952
   166
{
ds@1952
   167
	char *format, *buffer;
ds@1952
   168
	pContext->LocalToString(params[1], &buffer);
ds@1952
   169
	pContext->LocalToStringNULL(params[3], &format);
ds@1952
   170
ds@1952
   171
	if (format == NULL)
ds@1952
   172
	{
ds@1952
   173
		format = const_cast<char *>(sm_datetime_format.GetString());
ds@1952
   174
	}
ds@1952
   175
ds@1952
   176
#if defined SUBPLATFORM_SECURECRT
ds@1952
   177
	_invalid_parameter_handler handler = _set_invalid_parameter_handler(_ignore_invalid_parameter);
ds@1952
   178
#endif
ds@1952
   179
ds@1952
   180
	time_t t = (params[4] == -1) ? GetAdjustedTime() : (time_t)params[4];
ds@1952
   181
	size_t written = strftime(buffer, params[2], format, localtime(&t));
ds@1952
   182
ds@1952
   183
#if defined SUBPLATFORM_SECURECRT
ds@1952
   184
	_set_invalid_parameter_handler(handler);
ds@1952
   185
#endif
ds@1952
   186
ds@1952
   187
	if (params[2] && format[0] != '\0' && !written)
ds@1952
   188
	{
ds@1952
   189
		pContext->ThrowNativeError("Invalid time format or buffer too small");
ds@1952
   190
		return 0;
ds@1952
   191
	}
ds@1952
   192
ds@1952
   193
	return 1;
ds@1952
   194
}
ds@1952
   195
ds@1952
   196
static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
ds@1952
   197
{
ds@1952
   198
	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
ds@1952
   199
ds@1952
   200
	Handle_t hndl = g_HandleSys.CreateHandle(g_PlIter, iter, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@1952
   201
ds@1952
   202
	if (hndl == BAD_HANDLE)
ds@1952
   203
	{
ds@1952
   204
		iter->Release();
ds@1952
   205
	}
ds@1952
   206
ds@1952
   207
	return hndl;
ds@1952
   208
}
ds@1952
   209
ds@1952
   210
static cell_t MorePlugins(IPluginContext *pContext, const cell_t *params)
ds@1952
   211
{
ds@1952
   212
	Handle_t hndl = (Handle_t)params[1];
ds@1952
   213
	HandleError err;
ds@1952
   214
	IPluginIterator *pIter;
ds@1952
   215
ds@1952
   216
	HandleSecurity sec;
ds@1952
   217
	sec.pIdentity = g_pCoreIdent;
ds@1952
   218
	sec.pOwner = pContext->GetIdentity();
ds@1952
   219
ds@1952
   220
	if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
ds@1952
   221
	{
ds@1952
   222
		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
ds@1952
   223
	}
ds@1952
   224
ds@1952
   225
	return pIter->MorePlugins() ? 1 : 0;
ds@1952
   226
}
ds@1952
   227
ds@1952
   228
static cell_t ReadPlugin(IPluginContext *pContext, const cell_t *params)
ds@1952
   229
{
ds@1952
   230
	Handle_t hndl = (Handle_t)params[1];
ds@1952
   231
	HandleError err;
ds@1952
   232
	IPluginIterator *pIter;
ds@1952
   233
ds@1952
   234
	HandleSecurity sec;
ds@1952
   235
	sec.pIdentity = g_pCoreIdent;
ds@1952
   236
	sec.pOwner = pContext->GetIdentity();
ds@1952
   237
ds@1952
   238
	if ((err=g_HandleSys.ReadHandle(hndl, g_PlIter, &sec, (void **)&pIter)) != HandleError_None)
ds@1952
   239
	{
ds@1952
   240
		return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
ds@1952
   241
	}
ds@1952
   242
ds@1952
   243
	CPlugin *pPlugin = (CPlugin *)pIter->GetPlugin();
ds@1952
   244
	if (!pPlugin)
ds@1952
   245
	{
ds@1952
   246
		return BAD_HANDLE;
ds@1952
   247
	}
ds@1952
   248
ds@1952
   249
	pIter->NextPlugin();
ds@1952
   250
ds@1952
   251
	return pPlugin->GetMyHandle();
ds@1952
   252
}
ds@1952
   253
ds@1952
   254
CPlugin *GetPluginFromHandle(IPluginContext *pContext, Handle_t hndl)
ds@1952
   255
{
ds@1952
   256
	if (hndl == BAD_HANDLE)
ds@1952
   257
	{
ds@1952
   258
		return g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   259
	} else {
ds@1952
   260
		HandleError err;
ds@1952
   261
		CPlugin *pPlugin = (CPlugin *)g_PluginSys.PluginFromHandle(hndl, &err);
ds@1952
   262
		if (!pPlugin)
ds@1952
   263
		{
ds@1952
   264
			pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
ds@1952
   265
		}
ds@1952
   266
		return pPlugin;
ds@1952
   267
	}
ds@1952
   268
}
ds@1952
   269
ds@1952
   270
static cell_t GetPluginStatus(IPluginContext *pContext, const cell_t *params)
ds@1952
   271
{
ds@1952
   272
	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
ds@1952
   273
	if (!pPlugin)
ds@1952
   274
	{
ds@1952
   275
		return 0;
ds@1952
   276
	}
ds@1952
   277
ds@1952
   278
	return pPlugin->GetStatus();
ds@1952
   279
}
ds@1952
   280
ds@1952
   281
static cell_t GetPluginFilename(IPluginContext *pContext, const cell_t *params)
ds@1952
   282
{
ds@1952
   283
	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
ds@1952
   284
	if (!pPlugin)
ds@1952
   285
	{
ds@1952
   286
		return 0;
ds@1952
   287
	}
ds@1952
   288
ds@1952
   289
	pContext->StringToLocalUTF8(params[2], params[3], pPlugin->GetFilename(), NULL);
ds@1952
   290
ds@1952
   291
	return 1;
ds@1952
   292
}
ds@1952
   293
ds@1952
   294
static cell_t IsPluginDebugging(IPluginContext *pContext, const cell_t *params)
ds@1952
   295
{
ds@1952
   296
	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
ds@1952
   297
	if (!pPlugin)
ds@1952
   298
	{
ds@1952
   299
		return 0;
ds@1952
   300
	}
ds@1952
   301
ds@1952
   302
	return pPlugin->IsDebugging() ? 1 : 0;
ds@1952
   303
}
ds@1952
   304
ds@1952
   305
/* Local to plugins only */
ds@1952
   306
enum PluginInfo
ds@1952
   307
{
ds@1952
   308
	PlInfo_Name,			/**< Plugin name */
ds@1952
   309
	PlInfo_Author,			/**< Plugin author */
ds@1952
   310
	PlInfo_Description,		/**< Plugin description */
ds@1952
   311
	PlInfo_Version,			/**< Plugin verison */
ds@1952
   312
	PlInfo_URL,				/**< Plugin URL */
ds@1952
   313
};
ds@1952
   314
ds@1952
   315
static cell_t GetPluginInfo(IPluginContext *pContext, const cell_t *params)
ds@1952
   316
{
ds@1952
   317
	CPlugin *pPlugin = GetPluginFromHandle(pContext, params[1]);
ds@1952
   318
	if (!pPlugin)
ds@1952
   319
	{
ds@1952
   320
		return 0;
ds@1952
   321
	}
ds@1952
   322
ds@1952
   323
	const sm_plugininfo_t *info = pPlugin->GetPublicInfo();
ds@1952
   324
ds@1952
   325
	if (!info)
ds@1952
   326
	{
ds@1952
   327
		return 0;
ds@1952
   328
	}
ds@1952
   329
ds@1952
   330
	const char *str = NULL;
ds@1952
   331
ds@1952
   332
	switch ((PluginInfo)params[2])
ds@1952
   333
	{
ds@1952
   334
	case PlInfo_Name:
ds@1952
   335
		{
ds@1952
   336
			str = info->name;
ds@1952
   337
			break;
ds@1952
   338
		}
ds@1952
   339
	case PlInfo_Author:
ds@1952
   340
		{
ds@1952
   341
			str = info->author;
ds@1952
   342
			break;
ds@1952
   343
		}
ds@1952
   344
	case PlInfo_Description:
ds@1952
   345
		{
ds@1952
   346
			str = info->description;
ds@1952
   347
			break;
ds@1952
   348
		}
ds@1952
   349
	case PlInfo_Version:
ds@1952
   350
		{
ds@1952
   351
			str = info->version;
ds@1952
   352
			break;
ds@1952
   353
		}
ds@1952
   354
	case PlInfo_URL:
ds@1952
   355
		{
ds@1952
   356
			str = info->url;
ds@1952
   357
			break;
ds@1952
   358
		}
ds@1952
   359
	}
ds@1952
   360
ds@1952
   361
	if (!str || str[0] == '\0')
ds@1952
   362
	{
ds@1952
   363
		return 0;
ds@1952
   364
	}
ds@1952
   365
ds@1952
   366
	pContext->StringToLocalUTF8(params[3], params[4], str, NULL);
ds@1952
   367
ds@1952
   368
	return 1;
ds@1952
   369
}
ds@1952
   370
ds@1952
   371
static cell_t SetFailState(IPluginContext *pContext, const cell_t *params)
ds@1952
   372
{
ds@1952
   373
	char *str;
ds@1952
   374
	CPlugin *pPlugin;
ds@1952
   375
ds@1952
   376
	pContext->LocalToString(params[1], &str);
ds@1952
   377
	pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   378
ds@1952
   379
	if (params[0] == 1)
ds@1952
   380
	{
ds@1952
   381
		pPlugin->SetErrorState(Plugin_Error, "%s", str);
ds@1952
   382
ds@1952
   383
		return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", str);
ds@1952
   384
	}
ds@1952
   385
	else
ds@1952
   386
	{
ds@1952
   387
		char buffer[2048];
ds@1952
   388
ds@1952
   389
		g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 1);
dvander@2193
   390
		if (pContext->GetLastNativeError() != SP_ERROR_NONE)
ds@1952
   391
		{
ds@1952
   392
			pPlugin->SetErrorState(Plugin_Error, "%s", str);
ds@1952
   393
			return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "Formatting error (%s)", str);
ds@1952
   394
		}
ds@1952
   395
		else
ds@1952
   396
		{
ds@1952
   397
			pPlugin->SetErrorState(Plugin_Error, "%s", buffer);
ds@1952
   398
			return pContext->ThrowNativeErrorEx(SP_ERROR_ABORTED, "%s", buffer);
ds@1952
   399
		}
ds@1952
   400
	}
ds@1952
   401
ds@1952
   402
	return 0;
ds@1952
   403
}
ds@1952
   404
ds@1952
   405
static cell_t GetSysTickCount(IPluginContext *pContext, const cell_t *params)
ds@1952
   406
{
ds@1952
   407
#if defined PLATFORM_WINDOWS
ds@1952
   408
	return (cell_t)GetTickCount();
ds@1952
   409
#elif defined PLATFORM_LINUX
ds@1952
   410
	tms tm;
ds@1952
   411
	clock_t ticks = times(&tm);
ds@1952
   412
	long ticks_per_sec = sysconf(_SC_CLK_TCK);
ds@1952
   413
	double fticks = (double)ticks / (double)ticks_per_sec;
ds@1952
   414
	fticks *= 1000.0f;
ds@1952
   415
	if (fticks > INT_MAX)
ds@1952
   416
	{
ds@1952
   417
		double r = (int)(fticks / INT_MAX) * (double)INT_MAX;
ds@1952
   418
		fticks -= r;
ds@1952
   419
	}
ds@1952
   420
	return (cell_t)fticks;
ds@1952
   421
#endif
ds@1952
   422
}
ds@1952
   423
ds@1952
   424
static cell_t AutoExecConfig(IPluginContext *pContext, const cell_t *params)
ds@1952
   425
{
ds@1952
   426
	CPlugin *plugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   427
ds@1952
   428
	char *cfg, *folder;
ds@1952
   429
	pContext->LocalToString(params[2], &cfg);
ds@1952
   430
	pContext->LocalToString(params[3], &folder);
ds@1952
   431
ds@1952
   432
	if (cfg[0] == '\0')
ds@1952
   433
	{
ds@1952
   434
		static char temp_str[255];
ds@1952
   435
		static char temp_file[PLATFORM_MAX_PATH];
ds@1952
   436
		char *ptr;
ds@1952
   437
ds@1952
   438
		g_LibSys.GetFileFromPath(temp_str, sizeof(temp_str), plugin->GetFilename());
ds@1952
   439
		if ((ptr = strstr(temp_str, ".smx")) != NULL)
ds@1952
   440
		{
ds@1952
   441
			*ptr = '\0';
ds@1952
   442
		}
ds@1952
   443
ds@1952
   444
		/* We have the raw filename! */
ds@1952
   445
		UTIL_Format(temp_file, sizeof(temp_file), "plugin.%s", temp_str);
ds@1952
   446
		cfg = temp_file;
ds@1952
   447
	}
ds@1952
   448
ds@1952
   449
	plugin->AddConfig(params[1] ? true : false, cfg, folder);
ds@1952
   450
ds@1952
   451
	return 1;
ds@1952
   452
}
ds@1952
   453
ds@1952
   454
static cell_t MarkNativeAsOptional(IPluginContext *pContext, const cell_t *params)
ds@1952
   455
{
ds@1952
   456
	char *name;
ds@1952
   457
	uint32_t idx;
dvander@2193
   458
	sp_native_t *native;
ds@1952
   459
ds@1952
   460
	pContext->LocalToString(params[1], &name);
ds@1952
   461
	if (pContext->FindNativeByName(name, &idx) != SP_ERROR_NONE)
ds@1952
   462
	{
ds@1952
   463
		/* Oops! This HAS to silently fail! */
ds@1952
   464
		return 0;
ds@1952
   465
	}
ds@1952
   466
dvander@2193
   467
	pContext->GetNativeByIndex(idx, &native);
dvander@2193
   468
dvander@2193
   469
	native->flags |= SP_NTVFLAG_OPTIONAL;
ds@1952
   470
ds@1952
   471
	return 1;
ds@1952
   472
}
ds@1952
   473
ds@1952
   474
static cell_t RegPluginLibrary(IPluginContext *pContext, const cell_t *params)
ds@1952
   475
{
ds@1952
   476
	char *name;
ds@1952
   477
	CPlugin *pl = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   478
ds@1952
   479
	pContext->LocalToString(params[1], &name);
ds@1952
   480
ds@1952
   481
	pl->AddLibrary(name);
ds@1952
   482
ds@1952
   483
	return 1;
ds@1952
   484
}
ds@1952
   485
ds@1952
   486
static cell_t LibraryExists(IPluginContext *pContext, const cell_t *params)
ds@1952
   487
{
ds@1952
   488
	char *str;
ds@1952
   489
	pContext->LocalToString(params[1], &str);
ds@1952
   490
ds@1952
   491
	if (g_PluginSys.LibraryExists(str))
ds@1952
   492
	{
ds@1952
   493
		return 1;
ds@1952
   494
	}
ds@1952
   495
ds@1952
   496
	if (g_Extensions.LibraryExists(str))
ds@1952
   497
	{
ds@1952
   498
		return 1;
ds@1952
   499
	}
ds@1952
   500
ds@1952
   501
	return 0;
ds@1952
   502
}
ds@1952
   503
ds@1952
   504
static cell_t sm_LogAction(IPluginContext *pContext, const cell_t *params)
ds@1952
   505
{
ds@1952
   506
	char buffer[2048];
dvander@2042
   507
	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
ds@1952
   508
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 3);
ds@1952
   509
dvander@2193
   510
	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
ds@1952
   511
	{
ds@1952
   512
		return 0;
ds@1952
   513
	}
ds@1952
   514
ds@1952
   515
	CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   516
ds@1952
   517
	LogAction(pPlugin->GetMyHandle(), 2, params[1], params[2], buffer);
ds@1952
   518
ds@1952
   519
	return 1;
ds@1952
   520
}
ds@1952
   521
ds@1952
   522
static cell_t LogToFile(IPluginContext *pContext, const cell_t *params)
ds@1952
   523
{
ds@1952
   524
	char *file;
ds@1952
   525
	pContext->LocalToString(params[1], &file);
ds@1952
   526
ds@1952
   527
	char path[PLATFORM_MAX_PATH];
ds@1952
   528
	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
ds@1952
   529
ds@1952
   530
	FILE *fp = fopen(path, "at");
ds@1952
   531
	if (!fp)
ds@1952
   532
	{
ds@1952
   533
		return pContext->ThrowNativeError("Could not open file \"%s\"", path);
ds@1952
   534
	}
ds@1952
   535
ds@1952
   536
	char buffer[2048];
dvander@2042
   537
	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
ds@1952
   538
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
ds@1952
   539
dvander@2193
   540
	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
ds@1952
   541
	{
ds@1952
   542
		fclose(fp);
ds@1952
   543
		return 0;
ds@1952
   544
	}
ds@1952
   545
ds@1952
   546
	CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
ds@1952
   547
ds@1952
   548
	g_Logger.LogToOpenFile(fp, "[%s] %s", pPlugin->GetFilename(), buffer);
ds@1952
   549
ds@1952
   550
	fclose(fp);
ds@1952
   551
ds@1952
   552
	return 1;
ds@1952
   553
}
ds@1952
   554
ds@1952
   555
static cell_t LogToFileEx(IPluginContext *pContext, const cell_t *params)
ds@1952
   556
{
ds@1952
   557
	char *file;
ds@1952
   558
	pContext->LocalToString(params[1], &file);
ds@1952
   559
ds@1952
   560
	char path[PLATFORM_MAX_PATH];
ds@1952
   561
	g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "%s", file);
ds@1952
   562
ds@1952
   563
	FILE *fp = fopen(path, "at");
ds@1952
   564
	if (!fp)
ds@1952
   565
	{
ds@1952
   566
		return pContext->ThrowNativeError("Could not open file \"%s\"", path);
ds@1952
   567
	}
ds@1952
   568
ds@1952
   569
	char buffer[2048];
dvander@2042
   570
	g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
ds@1952
   571
	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
ds@1952
   572
dvander@2193
   573
	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
ds@1952
   574
	{
ds@1952
   575
		fclose(fp);
ds@1952
   576
		return 0;
ds@1952
   577
	}
ds@1952
   578
ds@1952
   579
	g_Logger.LogToOpenFile(fp, "%s", buffer);
ds@1952
   580
ds@1952
   581
	fclose(fp);
ds@1952
   582
ds@1952
   583
	return 1;
ds@1952
   584
}
ds@1952
   585
ds@1952
   586
static cell_t GetExtensionFileStatus(IPluginContext *pContext, const cell_t *params)
ds@1952
   587
{
ds@1952
   588
	char *str;
ds@1952
   589
	pContext->LocalToString(params[1], &str);
ds@1952
   590
ds@1952
   591
	IExtension *pExtension = g_Extensions.FindExtensionByFile(str);
ds@1952
   592
	
ds@1952
   593
	if (!pExtension)
ds@1952
   594
	{
ds@1952
   595
		return -2;
ds@1952
   596
	}
ds@1952
   597
	
ds@1952
   598
	if (!pExtension->IsLoaded())
ds@1952
   599
	{
ds@1952
   600
		return -1;
ds@1952
   601
	}
ds@1952
   602
ds@1952
   603
	char *error;
ds@1952
   604
	pContext->LocalToString(params[2], &error);
ds@1952
   605
	if (!pExtension->IsRunning(error, params[3]))
ds@1952
   606
	{
ds@1952
   607
		return 0;
ds@1952
   608
	}
ds@1952
   609
ds@1952
   610
	return 1;
ds@1952
   611
}
ds@1952
   612
ds@1952
   613
static cell_t FindPluginByNumber(IPluginContext *pContext, const cell_t *params)
ds@1952
   614
{
ds@1952
   615
	CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(params[1]);
ds@1952
   616
	
ds@1952
   617
	if (pPlugin == NULL)
ds@1952
   618
	{
ds@1952
   619
		return BAD_HANDLE;
ds@1952
   620
	}
ds@1952
   621
ds@1952
   622
	return pPlugin->GetMyHandle();
ds@1952
   623
}
ds@1952
   624
ds@1952
   625
REGISTER_NATIVES(coreNatives)
ds@1952
   626
{
ds@1952
   627
	{"AutoExecConfig",			AutoExecConfig},
ds@1952
   628
	{"GetPluginFilename",		GetPluginFilename},
ds@1952
   629
	{"GetPluginInfo",			GetPluginInfo},
ds@1952
   630
	{"GetPluginIterator",		GetPluginIterator},
ds@1952
   631
	{"GetPluginStatus",			GetPluginStatus},
ds@1952
   632
	{"GetSysTickCount",			GetSysTickCount},
ds@1952
   633
	{"GetTime",					GetTime},
ds@1952
   634
	{"IsPluginDebugging",		IsPluginDebugging},
ds@1952
   635
	{"MorePlugins",				MorePlugins},
ds@1952
   636
	{"ReadPlugin",				ReadPlugin},
ds@1952
   637
	{"ThrowError",				ThrowError},
ds@1952
   638
	{"SetFailState",			SetFailState},
ds@1952
   639
	{"FormatTime",				FormatTime},
ds@1952
   640
	{"MarkNativeAsOptional",	MarkNativeAsOptional},
ds@1952
   641
	{"RegPluginLibrary",		RegPluginLibrary},
ds@1952
   642
	{"LibraryExists",			LibraryExists},
ds@1952
   643
	{"LogAction",				sm_LogAction},
ds@1952
   644
	{"LogToFile",				LogToFile},
ds@1952
   645
	{"LogToFileEx",				LogToFileEx},
ds@1952
   646
	{"GetExtensionFileStatus",	GetExtensionFileStatus},
ds@1952
   647
	{"FindPluginByNumber",		FindPluginByNumber},
ds@1952
   648
	{NULL,						NULL},
ds@1952
   649
};
ds@1952
   650