core/smn_console.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Feb 17 05:33:31 2007 +0000 (2007-02-17)
changeset 511 c29f8796bcf4
parent 500 868e9b19a872
child 514 4c02b38c26ed
permissions -rw-r--r--
Initial fixes to ensure static strings passed to new convars
Also fixed minor warning in smn_bitbuffer.cpp
     1 /**
     2 * ===============================================================
     3 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4 * ===============================================================
     5 *
     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
    10 *
    11 * Version: $Id$
    12 */
    13 
    14 #include "sm_globals.h"
    15 #include "sourcemm_api.h"
    16 #include "HandleSys.h"
    17 #include "CConVarManager.h"
    18 #include "CConCmdManager.h"
    19 
    20 static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
    21 {
    22 	char *name, *defaultVal, *helpText;
    23 
    24 	pContext->LocalToString(params[1], &name);
    25 
    26 	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
    27 	if (name == NULL || strcmp(name, "") == 0)
    28 	{
    29 		return pContext->ThrowNativeError("Null or blank convar name is not allowed");
    30 	}
    31 
    32 	pContext->LocalToString(params[2], &defaultVal);
    33 	pContext->LocalToString(params[3], &helpText);
    34 
    35 	bool hasMin = params[5] ? true : false;
    36 	bool hasMax = params[7] ? true : false;
    37 	float min = sp_ctof(params[6]);
    38 	float max = sp_ctof(params[8]);
    39 
    40 	return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
    41 }
    42 
    43 static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
    44 {
    45 	char *name;
    46 
    47 	pContext->LocalToString(params[1], &name);
    48 
    49 	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
    50 	if (name == NULL || strcmp(name, "") == 0)
    51 	{
    52 		return BAD_HANDLE;
    53 	}
    54 
    55 	return g_ConVarManager.FindConVar(name);
    56 }
    57 
    58 static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
    59 {
    60 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    61 	HandleError err;
    62 	ConVar *cvar;
    63 
    64 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    65 		!= HandleError_None)
    66 	{
    67 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    68 	}
    69 
    70 	g_ConVarManager.HookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    71 
    72 	return 1;
    73 }
    74 
    75 static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
    76 {
    77 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    78 	HandleError err;
    79 	ConVar *cvar;
    80 
    81 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    82 		!= HandleError_None)
    83 	{
    84 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    85 	}
    86 
    87 	g_ConVarManager.UnhookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    88 
    89 	return 1;
    90 }
    91 
    92 static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
    93 {
    94 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    95 	HandleError err;
    96 	ConVar *cvar;
    97 
    98 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    99 		!= HandleError_None)
   100 	{
   101 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   102 	}
   103 
   104 	return cvar->GetBool();
   105 }
   106 
   107 static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
   108 {
   109 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   110 	HandleError err;
   111 	ConVar *cvar;
   112 
   113 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   114 		!= HandleError_None)
   115 	{
   116 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   117 	}
   118 
   119 	return cvar->GetInt();
   120 }
   121 
   122 /* This handles both SetConVarBool() and SetConVarInt() */
   123 static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
   124 {
   125 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   126 	HandleError err;
   127 	ConVar *cvar;
   128 
   129 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   130 		!= HandleError_None)
   131 	{
   132 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   133 	}
   134 
   135 	cvar->SetValue(params[2]);
   136 
   137 	return 1;
   138 }
   139 
   140 static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
   141 {
   142 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   143 	HandleError err;
   144 	ConVar *cvar;
   145 
   146 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   147 		!= HandleError_None)
   148 	{
   149 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   150 	}
   151 	
   152 	float value = cvar->GetFloat();
   153 
   154 	return sp_ftoc(value);
   155 }
   156 
   157 static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
   158 {
   159 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   160 	HandleError err;
   161 	ConVar *cvar;
   162 
   163 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   164 		!= HandleError_None)
   165 	{
   166 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   167 	}
   168 
   169 	float value = sp_ctof(params[2]);
   170 	cvar->SetValue(value);
   171 
   172 	return 1;
   173 }
   174 
   175 static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
   176 {
   177 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   178 	HandleError err;
   179 	ConVar *cvar;
   180 
   181 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   182 		!= HandleError_None)
   183 	{
   184 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   185 	}
   186 
   187 	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
   188 
   189 	return 1;
   190 }
   191 
   192 static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
   193 {
   194 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   195 	HandleError err;
   196 	ConVar *cvar;
   197 
   198 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   199 		!= HandleError_None)
   200 	{
   201 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   202 	}
   203 
   204 	char *value;
   205 	pContext->LocalToString(params[2], &value);
   206 
   207 	cvar->SetValue(value);
   208 
   209 	return 1;
   210 }
   211 
   212 static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
   213 {
   214 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   215 	HandleError err;
   216 	ConVar *cvar;
   217 
   218 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   219 		!= HandleError_None)
   220 	{
   221 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   222 	}
   223 
   224 	return cvar->GetFlags();
   225 }
   226 
   227 static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
   228 {
   229 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   230 	HandleError err;
   231 	ConVar *cvar;
   232 
   233 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   234 		!= HandleError_None)
   235 	{
   236 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   237 	}
   238 
   239 	cvar->SetFlags(params[2]);
   240 
   241 	return 1;
   242 }
   243 
   244 static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
   245 {
   246 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   247 	HandleError err;
   248 	ConVar *cvar;
   249 
   250 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   251 		!= HandleError_None)
   252 	{
   253 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   254 	}
   255 
   256 	cell_t *addr;
   257 	bool hasMin;
   258 	float min;
   259 
   260 	pContext->LocalToPhysAddr(params[2], &addr);
   261 
   262 	hasMin = cvar->GetMin(min);
   263 	*addr = sp_ftoc(min);
   264 
   265 	return hasMin;
   266 }
   267 
   268 static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
   269 {
   270 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   271 	HandleError err;
   272 	ConVar *cvar;
   273 
   274 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   275 		!= HandleError_None)
   276 	{
   277 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   278 	}
   279 
   280 	cell_t *addr;
   281 	bool hasMax;
   282 	float max;
   283 
   284 	pContext->LocalToPhysAddr(params[2], &addr);
   285 
   286 	hasMax = cvar->GetMax(max);
   287 	*addr = sp_ftoc(max);
   288 
   289 	return hasMax;
   290 }
   291 
   292 static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
   293 {
   294 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   295 	HandleError err;
   296 	ConVar *cvar;
   297 
   298 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   299 		!= HandleError_None)
   300 	{
   301 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   302 	}
   303 
   304 	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetName(), NULL);
   305 
   306 	return 1;
   307 }
   308 
   309 static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
   310 {
   311 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   312 	HandleError err;
   313 	ConVar *cvar;
   314 
   315 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   316 		!= HandleError_None)
   317 	{
   318 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   319 	}
   320 
   321 	cvar->Revert();
   322 
   323 	return 1;
   324 }
   325 
   326 static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
   327 {
   328 	char *name,*help;
   329 	IPluginFunction *pFunction;
   330 
   331 	pContext->LocalToString(params[1], &name);
   332 	pContext->LocalToString(params[3], &help);
   333 	pFunction = pContext->GetFunctionById(params[2]);
   334 
   335 	if (!pFunction)
   336 	{
   337 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   338 	}
   339 
   340 	g_ConCmds.AddServerCommand(pFunction, name, help, params[4]);
   341 
   342 	return 1;
   343 }
   344 
   345 static cell_t sm_RegConsoleCmd(IPluginContext *pContext, const cell_t *params)
   346 {
   347 	char *name,*help;
   348 	IPluginFunction *pFunction;
   349 
   350 	pContext->LocalToString(params[1], &name);
   351 	pContext->LocalToString(params[3], &help);
   352 	pFunction = pContext->GetFunctionById(params[2]);
   353 
   354 	if (!pFunction)
   355 	{
   356 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   357 	}
   358 
   359 	g_ConCmds.AddConsoleCommand(pFunction, name, help, params[4]);
   360 
   361 	return 1;
   362 }
   363 
   364 REGISTER_NATIVES(convarNatives)
   365 {
   366 	{"CreateConVar",		sm_CreateConVar},
   367 	{"FindConVar",			sm_FindConVar},
   368 	{"HookConVarChange",	sm_HookConVarChange},
   369 	{"UnhookConVarChange",	sm_UnhookConVarChange},
   370 	{"GetConVarBool",		sm_GetConVarBool},
   371 	{"SetConVarBool",		sm_SetConVarNum},
   372 	{"GetConVarInt",		sm_GetConVarInt},
   373 	{"SetConVarInt",		sm_SetConVarNum},
   374 	{"GetConVarFloat",		sm_GetConVarFloat},
   375 	{"SetConVarFloat",		sm_SetConVarFloat},
   376 	{"GetConVarString",		sm_GetConVarString},
   377 	{"SetConVarString",		sm_SetConVarString},
   378 	{"GetConVarFlags",		sm_GetConVarFlags},
   379 	{"SetConVarFlags",		sm_SetConVarFlags},
   380 	{"GetConVarName",		sm_GetConVarName},
   381 	{"GetConVarMin",		sm_GetConVarMin},
   382 	{"GetConVarMax",		sm_GetConVarMax},
   383 	{"ResetConVar",			sm_ResetConVar},
   384 	{"RegServerCmd",		sm_RegServerCmd},
   385 	{"RegConsoleCmd",		sm_RegConsoleCmd},
   386 	{NULL,					NULL}
   387 };