core/smn_handles.cpp
author David Anderson <dvander@alliedmods.net>
Wed Jan 17 06:49:59 2007 +0000 (2007-01-17)
changeset 308 f0e65f92cb34
parent 282 cd7c334f7ae0
child 370 d3a484f98f6d
permissions -rw-r--r--
Redesigned how server commands work. The "plugins" command will be back soon.
     1 #include "sm_globals.h"
     2 #include "HandleSys.h"
     3 #include "PluginSys.h"
     4 
     5 static cell_t sm_IsValidHandle(IPluginContext *pContext, const cell_t *params)
     6 {
     7 	Handle_t hndl = static_cast<Handle_t>(params[1]);
     8 
     9 	HandleError err = g_HandleSys.ReadHandle(hndl, 0, NULL, NULL);
    10 
    11 	if (err != HandleError_Access
    12 		&& err != HandleError_None)
    13 	{
    14 		return 0;
    15 	}
    16 
    17 	return 1;
    18 }
    19 
    20 static cell_t sm_CloseHandle(IPluginContext *pContext, const cell_t *params)
    21 {
    22 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    23 	HandleSecurity sec;
    24 
    25 	sec.pIdentity = NULL;
    26 	sec.pOwner = pContext->GetIdentity();
    27 
    28 	HandleError err = g_HandleSys.FreeHandle(hndl, &sec);
    29   
    30 	if (err == HandleError_None)
    31 	{
    32 		return 1;
    33 	} else if (err == HandleError_Access) {
    34 		return 0;
    35 	} else {
    36 		return pContext->ThrowNativeError("Handle %x is invalid (error %d)", hndl, err);
    37 	}
    38 }
    39 
    40 static cell_t sm_CloneHandle(IPluginContext *pContext, const cell_t *params)
    41 {
    42 	Handle_t new_hndl;
    43 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    44 	HandleError err;
    45 	IdentityToken_t *pNewOwner;
    46 	
    47 	if (params[2] == 0)
    48 	{
    49 		pNewOwner = pContext->GetIdentity();
    50 	} else {
    51 		Handle_t hPlugin = static_cast<Handle_t>(params[2]);
    52 		IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hPlugin, &err);
    53 		if (!pPlugin)
    54 		{
    55 			return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
    56 		}
    57 		pNewOwner = pPlugin->GetIdentity();
    58 	}
    59 
    60 	err = g_HandleSys.CloneHandle(hndl, &new_hndl, pNewOwner, NULL);
    61 
    62 	if (err == HandleError_Access)
    63 	{
    64 		return 0;
    65 	} else if (err == HandleError_None) {
    66 		return new_hndl;
    67 	} else {
    68 		return pContext->ThrowNativeError("Handle %x cannot be cloned because it is invalid (error %d)", hndl, err);
    69 	}
    70 }
    71 
    72 REGISTER_NATIVES(handles)
    73 {
    74 	{"IsValidHandle",			sm_IsValidHandle},
    75 	{"CloseHandle",				sm_CloseHandle},
    76 	{"CloneHandle",				sm_CloneHandle},
    77 	{NULL,						NULL},
    78 };