3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
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
15 #include "sm_globals.h"
16 #include "HandleSys.h"
17 #include "PluginSys.h"
19 static cell_t sm_IsValidHandle(IPluginContext *pContext, const cell_t *params)
21 Handle_t hndl = static_cast<Handle_t>(params[1]);
23 HandleError err = g_HandleSys.ReadHandle(hndl, 0, NULL, NULL);
25 if (err != HandleError_Access
26 && err != HandleError_None)
34 static cell_t sm_CloseHandle(IPluginContext *pContext, const cell_t *params)
36 Handle_t hndl = static_cast<Handle_t>(params[1]);
40 sec.pOwner = pContext->GetIdentity();
42 HandleError err = g_HandleSys.FreeHandle(hndl, &sec);
44 if (err == HandleError_None)
47 } else if (err == HandleError_Access) {
50 return pContext->ThrowNativeError("Handle %x is invalid (error %d)", hndl, err);
54 static cell_t sm_CloneHandle(IPluginContext *pContext, const cell_t *params)
57 Handle_t hndl = static_cast<Handle_t>(params[1]);
59 IdentityToken_t *pNewOwner;
63 pNewOwner = pContext->GetIdentity();
65 Handle_t hPlugin = static_cast<Handle_t>(params[2]);
66 IPlugin *pPlugin = g_PluginSys.PluginFromHandle(hPlugin, &err);
69 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
71 pNewOwner = pPlugin->GetIdentity();
74 err = g_HandleSys.CloneHandle(hndl, &new_hndl, pNewOwner, NULL);
76 if (err == HandleError_Access)
79 } else if (err == HandleError_None) {
82 return pContext->ThrowNativeError("Handle %x cannot be cloned because it is invalid (error %d)", hndl, err);
86 static cell_t sm_GetMyHandle(IPluginContext *pContext, const cell_t *params)
88 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
90 return pPlugin->GetMyHandle();
93 REGISTER_NATIVES(handles)
95 {"IsValidHandle", sm_IsValidHandle},
96 {"CloseHandle", sm_CloseHandle},
97 {"CloneHandle", sm_CloneHandle},
98 {"GetMyHandle", sm_GetMyHandle},