Implemented
amb423 - Added IsClientObserver() native
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"
18 //Note: Do not add this to Linux yet, i haven't done the HPET timing research (if even available)
19 //nonetheless we need accurate counting
20 #if defined PLATFORM_LINUX
21 #error "Not supported"
31 #if defined PLATFORM_WINDOWS
40 HandleType_t g_ProfilerType = 0;
42 class ProfilerHelpers :
44 public IHandleTypeDispatch
47 void OnSourceModAllInitialized()
49 g_ProfilerType = g_HandleSys.CreateType("Profiler", this, 0, NULL, NULL, g_pCoreIdent, NULL);
51 void OnSourceModShutdown()
53 g_HandleSys.RemoveType(g_ProfilerType, g_pCoreIdent);
55 void OnHandleDestroy(HandleType_t type, void *object)
57 Profiler *prof = (Profiler *)object;
62 static cell_t CreateProfiler(IPluginContext *pContext, const cell_t *params)
64 Profiler *p = new Profiler();
66 #if defined PLATFORM_WINDOWS
68 QueryPerformanceFrequency(&qpf);
69 p->freq = 1.0 / (double)(qpf.QuadPart);
72 Handle_t hndl = g_HandleSys.CreateHandle(g_ProfilerType, p, pContext->GetIdentity(), g_pCoreIdent, NULL);
73 if (hndl == BAD_HANDLE)
81 static cell_t StartProfiling(IPluginContext *pContext, const cell_t *params)
83 Handle_t hndl = params[1];
84 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
88 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
91 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
94 #if defined PLATFORM_WINDOWS
95 QueryPerformanceCounter(&prof->start);
99 prof->stopped = false;
104 static cell_t StopProfiling(IPluginContext *pContext, const cell_t *params)
106 Handle_t hndl = params[1];
107 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
111 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
114 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
119 return pContext->ThrowNativeError("Profiler was never started");
122 #if defined PLATFORM_WINDOWS
123 QueryPerformanceCounter(&prof->end);
126 prof->started = false;
127 prof->stopped = true;
132 static cell_t GetProfilerTime(IPluginContext *pContext, const cell_t *params)
134 Handle_t hndl = params[1];
135 HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
139 if ((err = g_HandleSys.ReadHandle(hndl, g_ProfilerType, &sec, (void **)&prof))
142 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
147 return pContext->ThrowNativeError("Profiler was never stopped");
152 #if defined PLATFORM_WINDOWS
153 LONGLONG diff = prof->end.QuadPart - prof->start.QuadPart;
154 double seconds = diff * prof->freq;
155 fTime = (float)seconds;
158 return sp_ftoc(fTime);
161 REGISTER_NATIVES(profilerNatives)
163 {"CreateProfiler", CreateProfiler},
164 {"GetProfilerTime", GetProfilerTime},
165 {"StartProfiling", StartProfiling},
166 {"StopProfiling", StopProfiling},