core/smn_handles.cpp
author Borja Ferrer <faluco@alliedmods.net>
Thu Jul 05 14:47:36 2007 +0000 (2007-07-05)
changeset 1041 f7b2073013df
parent 653 ef60870067ff
child 1231 17badaf66ce4
permissions -rw-r--r--
added a time stamp argument to FormatTime
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_globals.h"
    16 #include "HandleSys.h"
    17 #include "PluginSys.h"
    18 
    19 static cell_t sm_IsValidHandle(IPluginContext *pContext, const cell_t *params)
    20 {
    21 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    22 
    23 	HandleError err = g_HandleSys.ReadHandle(hndl, 0, NULL, NULL);
    24 
    25 	if (err != HandleError_Access
    26 		&& err != HandleError_None)
    27 	{
    28 		return 0;
    29 	}
    30 
    31 	return 1;
    32 }
    33 
    34 static cell_t sm_CloseHandle(IPluginContext *pContext, const cell_t *params)
    35 {
    36 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    37 	HandleSecurity sec;
    38 
    39 	sec.pIdentity = NULL;
    40 	sec.pOwner = pContext->GetIdentity();
    41 
    42 	HandleError err = g_HandleSys.FreeHandle(hndl, &sec);
    43   
    44 	if (err == HandleError_None)
    45 	{
    46 		return 1;
    47 	} else if (err == HandleError_Access) {
    48 		return 0;
    49 	} else {
    50 		return pContext->ThrowNativeError("Handle %x is invalid (error %d)", hndl, err);
    51 	}
    52 }
    53 
    54 static cell_t sm_CloneHandle(IPluginContext *pContext, const cell_t *params)
    55 {
    56 	Handle_t new_hndl;
    57 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    58 	HandleError err;
    59 	IdentityToken_t *pNewOwner;
    60 	
    61 	if (params[2] == 0)
    62 	{
    63 		pNewOwner = pContext->GetIdentity();
    64 	} else {
    65 		Handle_t hPlugin = static_cast<Handle_t>(params[2]);
    66 		IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hPlugin, &err);
    67 		if (!pPlugin)
    68 		{
    69 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
    70 		}
    71 		pNewOwner = pPlugin->GetIdentity();
    72 	}
    73 
    74 	err = g_HandleSys.CloneHandle(hndl, &new_hndl, pNewOwner, NULL);
    75 
    76 	if (err == HandleError_Access)
    77 	{
    78 		return 0;
    79 	} else if (err == HandleError_None) {
    80 		return new_hndl;
    81 	} else {
    82 		return pContext->ThrowNativeError("Handle %x cannot be cloned because it is invalid (error %d)", hndl, err);
    83 	}
    84 }
    85 
    86 static cell_t sm_GetMyHandle(IPluginContext *pContext, const cell_t *params)
    87 {
    88 	CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
    89 
    90 	return pPlugin->GetMyHandle();
    91 }
    92 
    93 REGISTER_NATIVES(handles)
    94 {
    95 	{"IsValidHandle",			sm_IsValidHandle},
    96 	{"CloseHandle",				sm_CloseHandle},
    97 	{"CloneHandle",				sm_CloneHandle},
    98 	{"GetMyHandle",				sm_GetMyHandle},
    99 	{NULL,						NULL},
   100 };