2007-01-25 23:36:38 +01:00
|
|
|
/**
|
2007-03-22 22:50:20 +01:00
|
|
|
* vim: set ts=4 :
|
2007-08-15 08:19:30 +02:00
|
|
|
* =============================================================================
|
2007-08-01 04:12:47 +02:00
|
|
|
* SourceMod
|
|
|
|
* Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
|
2007-08-15 08:19:30 +02:00
|
|
|
* =============================================================================
|
2007-01-25 23:36:38 +01:00
|
|
|
*
|
2007-08-15 08:19:30 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
2007-08-01 04:12:47 +02:00
|
|
|
*
|
2007-08-15 08:19:30 +02:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
2007-08-01 04:12:47 +02:00
|
|
|
*
|
2007-08-15 08:19:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-01 04:12:47 +02:00
|
|
|
*
|
2007-08-15 08:19:30 +02:00
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
2007-01-25 23:36:38 +01:00
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2007-01-01 11:33:51 +01:00
|
|
|
#include "sm_globals.h"
|
|
|
|
#include "HandleSys.h"
|
|
|
|
#include "PluginSys.h"
|
|
|
|
|
|
|
|
static cell_t sm_IsValidHandle(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
|
|
|
|
|
|
|
HandleError err = g_HandleSys.ReadHandle(hndl, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (err != HandleError_Access
|
|
|
|
&& err != HandleError_None)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_CloseHandle(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
2007-01-04 03:08:27 +01:00
|
|
|
HandleSecurity sec;
|
|
|
|
|
|
|
|
sec.pIdentity = NULL;
|
|
|
|
sec.pOwner = pContext->GetIdentity();
|
|
|
|
|
|
|
|
HandleError err = g_HandleSys.FreeHandle(hndl, &sec);
|
2007-01-02 02:44:46 +01:00
|
|
|
|
2007-01-01 11:33:51 +01:00
|
|
|
if (err == HandleError_None)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
} else if (err == HandleError_Access) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return pContext->ThrowNativeError("Handle %x is invalid (error %d)", hndl, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static cell_t sm_CloneHandle(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
Handle_t new_hndl;
|
|
|
|
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
|
|
|
HandleError err;
|
2007-01-05 00:32:46 +01:00
|
|
|
IdentityToken_t *pNewOwner;
|
2007-01-01 11:33:51 +01:00
|
|
|
|
|
|
|
if (params[2] == 0)
|
|
|
|
{
|
2007-01-05 00:32:46 +01:00
|
|
|
pNewOwner = pContext->GetIdentity();
|
2007-01-01 11:33:51 +01:00
|
|
|
} else {
|
|
|
|
Handle_t hPlugin = static_cast<Handle_t>(params[2]);
|
2007-01-05 00:32:46 +01:00
|
|
|
IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hPlugin, &err);
|
2007-01-01 11:33:51 +01:00
|
|
|
if (!pPlugin)
|
|
|
|
{
|
|
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
|
|
|
|
}
|
2007-01-05 00:32:46 +01:00
|
|
|
pNewOwner = pPlugin->GetIdentity();
|
2007-01-01 11:33:51 +01:00
|
|
|
}
|
|
|
|
|
2007-01-05 00:32:46 +01:00
|
|
|
err = g_HandleSys.CloneHandle(hndl, &new_hndl, pNewOwner, NULL);
|
2007-01-01 11:33:51 +01:00
|
|
|
|
|
|
|
if (err == HandleError_Access)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
} else if (err == HandleError_None) {
|
|
|
|
return new_hndl;
|
|
|
|
} else {
|
2007-01-11 02:13:34 +01:00
|
|
|
return pContext->ThrowNativeError("Handle %x cannot be cloned because it is invalid (error %d)", hndl, err);
|
2007-01-01 11:33:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-26 09:32:20 +01:00
|
|
|
static cell_t sm_GetMyHandle(IPluginContext *pContext, const cell_t *params)
|
|
|
|
{
|
|
|
|
CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
|
|
|
|
|
|
|
|
return pPlugin->GetMyHandle();
|
|
|
|
}
|
|
|
|
|
2007-01-01 11:33:51 +01:00
|
|
|
REGISTER_NATIVES(handles)
|
|
|
|
{
|
|
|
|
{"IsValidHandle", sm_IsValidHandle},
|
|
|
|
{"CloseHandle", sm_CloseHandle},
|
|
|
|
{"CloneHandle", sm_CloneHandle},
|
2007-02-26 10:21:08 +01:00
|
|
|
{"GetMyHandle", sm_GetMyHandle},
|
2007-01-01 20:50:16 +01:00
|
|
|
{NULL, NULL},
|
2007-01-01 11:33:51 +01:00
|
|
|
};
|