2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_globals.h"
15 #include "sourcemm_api.h"
16 #include "HandleSys.h"
17 #include "CConVarManager.h"
18 #include "CConCmdManager.h"
19 #include "PluginSys.h"
21 static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
23 char *name, *defaultVal, *helpText;
25 pContext->LocalToString(params[1], &name);
27 // While the engine seems to accept a blank convar name, it causes a crash upon server quit
28 if (name == NULL || strcmp(name, "") == 0)
30 return pContext->ThrowNativeError("Null or blank convar name is not allowed");
33 pContext->LocalToString(params[2], &defaultVal);
34 pContext->LocalToString(params[3], &helpText);
36 bool hasMin = params[5] ? true : false;
37 bool hasMax = params[7] ? true : false;
38 float min = sp_ctof(params[6]);
39 float max = sp_ctof(params[8]);
41 return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
44 static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
48 pContext->LocalToString(params[1], &name);
50 // While the engine seems to accept a blank convar name, it causes a crash upon server quit
51 if (name == NULL || strcmp(name, "") == 0)
56 return g_ConVarManager.FindConVar(name);
59 static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
61 Handle_t hndl = static_cast<Handle_t>(params[1]);
65 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
68 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
71 g_ConVarManager.HookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
76 static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
78 Handle_t hndl = static_cast<Handle_t>(params[1]);
82 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
85 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
88 g_ConVarManager.UnhookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
93 static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
95 Handle_t hndl = static_cast<Handle_t>(params[1]);
99 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
102 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
105 return cvar->GetBool();
108 static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
110 Handle_t hndl = static_cast<Handle_t>(params[1]);
114 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
117 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
120 return cvar->GetInt();
123 /* This handles both SetConVarBool() and SetConVarInt() */
124 static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
126 Handle_t hndl = static_cast<Handle_t>(params[1]);
130 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
133 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
136 cvar->SetValue(params[2]);
141 static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
143 Handle_t hndl = static_cast<Handle_t>(params[1]);
147 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
150 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
153 float value = cvar->GetFloat();
155 return sp_ftoc(value);
158 static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
160 Handle_t hndl = static_cast<Handle_t>(params[1]);
164 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
167 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
170 float value = sp_ctof(params[2]);
171 cvar->SetValue(value);
176 static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
178 Handle_t hndl = static_cast<Handle_t>(params[1]);
182 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
185 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
188 pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
193 static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
195 Handle_t hndl = static_cast<Handle_t>(params[1]);
199 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
202 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
206 pContext->LocalToString(params[2], &value);
208 cvar->SetValue(value);
213 static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
215 Handle_t hndl = static_cast<Handle_t>(params[1]);
219 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
222 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
225 return cvar->GetFlags();
228 static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
230 Handle_t hndl = static_cast<Handle_t>(params[1]);
234 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
237 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
240 cvar->SetFlags(params[2]);
245 static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
247 Handle_t hndl = static_cast<Handle_t>(params[1]);
251 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
254 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
261 pContext->LocalToPhysAddr(params[2], &addr);
263 hasMin = cvar->GetMin(min);
264 *addr = sp_ftoc(min);
269 static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
271 Handle_t hndl = static_cast<Handle_t>(params[1]);
275 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
278 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
285 pContext->LocalToPhysAddr(params[2], &addr);
287 hasMax = cvar->GetMax(max);
288 *addr = sp_ftoc(max);
293 static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
295 Handle_t hndl = static_cast<Handle_t>(params[1]);
299 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
302 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
305 pContext->StringToLocalUTF8(params[2], params[3], cvar->GetName(), NULL);
310 static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
312 Handle_t hndl = static_cast<Handle_t>(params[1]);
316 if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
319 return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
327 static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
330 IPluginFunction *pFunction;
332 pContext->LocalToString(params[1], &name);
333 pContext->LocalToString(params[3], &help);
334 pFunction = pContext->GetFunctionById(params[2]);
338 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
341 g_ConCmds.AddServerCommand(pFunction, name, help, params[4]);
346 static cell_t sm_RegConsoleCmd(IPluginContext *pContext, const cell_t *params)
349 IPluginFunction *pFunction;
351 pContext->LocalToString(params[1], &name);
352 pContext->LocalToString(params[3], &help);
353 pFunction = pContext->GetFunctionById(params[2]);
357 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
360 g_ConCmds.AddConsoleCommand(pFunction, name, help, params[4]);
365 static cell_t sm_RegAdminCmd(IPluginContext *pContext, const cell_t *params)
369 IPluginFunction *pFunction;
370 FlagBits flags = params[3];
371 int cmdflags = params[6];
373 pContext->LocalToString(params[1], &name);
374 pContext->LocalToString(params[4], &help);
375 pContext->LocalToString(params[5], (char **)&group);
376 pFunction = pContext->GetFunctionById(params[2]);
378 if (group[0] == '\0')
380 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
381 group = pPlugin->GetFilename();
386 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
389 g_ConCmds.AddAdminCommand(pFunction, name, group, flags, help, cmdflags);
394 REGISTER_NATIVES(convarNatives)
396 {"CreateConVar", sm_CreateConVar},
397 {"FindConVar", sm_FindConVar},
398 {"HookConVarChange", sm_HookConVarChange},
399 {"UnhookConVarChange", sm_UnhookConVarChange},
400 {"GetConVarBool", sm_GetConVarBool},
401 {"SetConVarBool", sm_SetConVarNum},
402 {"GetConVarInt", sm_GetConVarInt},
403 {"SetConVarInt", sm_SetConVarNum},
404 {"GetConVarFloat", sm_GetConVarFloat},
405 {"SetConVarFloat", sm_SetConVarFloat},
406 {"GetConVarString", sm_GetConVarString},
407 {"SetConVarString", sm_SetConVarString},
408 {"GetConVarFlags", sm_GetConVarFlags},
409 {"SetConVarFlags", sm_SetConVarFlags},
410 {"GetConVarName", sm_GetConVarName},
411 {"GetConVarMin", sm_GetConVarMin},
412 {"GetConVarMax", sm_GetConVarMax},
413 {"ResetConVar", sm_ResetConVar},
414 {"RegServerCmd", sm_RegServerCmd},
415 {"RegConsoleCmd", sm_RegConsoleCmd},