core/smn_console.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 17 09:12:21 2007 +0000 (2007-02-17)
changeset 517 da53a0fa5721
parent 514 4c02b38c26ed
child 518 52810a1a9620
permissions -rw-r--r--
added functions for getting arguments
     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 #include "PluginSys.h"
    20 
    21 static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
    22 {
    23 	char *name, *defaultVal, *helpText;
    24 
    25 	pContext->LocalToString(params[1], &name);
    26 
    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)
    29 	{
    30 		return pContext->ThrowNativeError("Null or blank convar name is not allowed");
    31 	}
    32 
    33 	pContext->LocalToString(params[2], &defaultVal);
    34 	pContext->LocalToString(params[3], &helpText);
    35 
    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]);
    40 
    41 	return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
    42 }
    43 
    44 static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
    45 {
    46 	char *name;
    47 
    48 	pContext->LocalToString(params[1], &name);
    49 
    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)
    52 	{
    53 		return BAD_HANDLE;
    54 	}
    55 
    56 	return g_ConVarManager.FindConVar(name);
    57 }
    58 
    59 static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
    60 {
    61 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    62 	HandleError err;
    63 	ConVar *cvar;
    64 
    65 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    66 		!= HandleError_None)
    67 	{
    68 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    69 	}
    70 
    71 	g_ConVarManager.HookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    72 
    73 	return 1;
    74 }
    75 
    76 static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
    77 {
    78 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    79 	HandleError err;
    80 	ConVar *cvar;
    81 
    82 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    83 		!= HandleError_None)
    84 	{
    85 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    86 	}
    87 
    88 	g_ConVarManager.UnhookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    89 
    90 	return 1;
    91 }
    92 
    93 static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
    94 {
    95 	Handle_t hndl = static_cast<Handle_t>(params[1]);
    96 	HandleError err;
    97 	ConVar *cvar;
    98 
    99 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   100 		!= HandleError_None)
   101 	{
   102 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   103 	}
   104 
   105 	return cvar->GetBool();
   106 }
   107 
   108 static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
   109 {
   110 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   111 	HandleError err;
   112 	ConVar *cvar;
   113 
   114 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   115 		!= HandleError_None)
   116 	{
   117 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   118 	}
   119 
   120 	return cvar->GetInt();
   121 }
   122 
   123 /* This handles both SetConVarBool() and SetConVarInt() */
   124 static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
   125 {
   126 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   127 	HandleError err;
   128 	ConVar *cvar;
   129 
   130 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   131 		!= HandleError_None)
   132 	{
   133 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   134 	}
   135 
   136 	cvar->SetValue(params[2]);
   137 
   138 	return 1;
   139 }
   140 
   141 static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
   142 {
   143 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   144 	HandleError err;
   145 	ConVar *cvar;
   146 
   147 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   148 		!= HandleError_None)
   149 	{
   150 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   151 	}
   152 	
   153 	float value = cvar->GetFloat();
   154 
   155 	return sp_ftoc(value);
   156 }
   157 
   158 static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
   159 {
   160 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   161 	HandleError err;
   162 	ConVar *cvar;
   163 
   164 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   165 		!= HandleError_None)
   166 	{
   167 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   168 	}
   169 
   170 	float value = sp_ctof(params[2]);
   171 	cvar->SetValue(value);
   172 
   173 	return 1;
   174 }
   175 
   176 static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   179 	HandleError err;
   180 	ConVar *cvar;
   181 
   182 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   183 		!= HandleError_None)
   184 	{
   185 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   186 	}
   187 
   188 	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
   189 
   190 	return 1;
   191 }
   192 
   193 static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
   194 {
   195 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   196 	HandleError err;
   197 	ConVar *cvar;
   198 
   199 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   200 		!= HandleError_None)
   201 	{
   202 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   203 	}
   204 
   205 	char *value;
   206 	pContext->LocalToString(params[2], &value);
   207 
   208 	cvar->SetValue(value);
   209 
   210 	return 1;
   211 }
   212 
   213 static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
   214 {
   215 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   216 	HandleError err;
   217 	ConVar *cvar;
   218 
   219 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   220 		!= HandleError_None)
   221 	{
   222 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   223 	}
   224 
   225 	return cvar->GetFlags();
   226 }
   227 
   228 static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
   229 {
   230 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   231 	HandleError err;
   232 	ConVar *cvar;
   233 
   234 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   235 		!= HandleError_None)
   236 	{
   237 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   238 	}
   239 
   240 	cvar->SetFlags(params[2]);
   241 
   242 	return 1;
   243 }
   244 
   245 static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
   246 {
   247 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   248 	HandleError err;
   249 	ConVar *cvar;
   250 
   251 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   252 		!= HandleError_None)
   253 	{
   254 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   255 	}
   256 
   257 	cell_t *addr;
   258 	bool hasMin;
   259 	float min;
   260 
   261 	pContext->LocalToPhysAddr(params[2], &addr);
   262 
   263 	hasMin = cvar->GetMin(min);
   264 	*addr = sp_ftoc(min);
   265 
   266 	return hasMin;
   267 }
   268 
   269 static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
   270 {
   271 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   272 	HandleError err;
   273 	ConVar *cvar;
   274 
   275 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   276 		!= HandleError_None)
   277 	{
   278 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   279 	}
   280 
   281 	cell_t *addr;
   282 	bool hasMax;
   283 	float max;
   284 
   285 	pContext->LocalToPhysAddr(params[2], &addr);
   286 
   287 	hasMax = cvar->GetMax(max);
   288 	*addr = sp_ftoc(max);
   289 
   290 	return hasMax;
   291 }
   292 
   293 static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
   294 {
   295 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   296 	HandleError err;
   297 	ConVar *cvar;
   298 
   299 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   300 		!= HandleError_None)
   301 	{
   302 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   303 	}
   304 
   305 	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetName(), NULL);
   306 
   307 	return 1;
   308 }
   309 
   310 static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
   311 {
   312 	Handle_t hndl = static_cast<Handle_t>(params[1]);
   313 	HandleError err;
   314 	ConVar *cvar;
   315 
   316 	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   317 		!= HandleError_None)
   318 	{
   319 		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   320 	}
   321 
   322 	cvar->Revert();
   323 
   324 	return 1;
   325 }
   326 
   327 static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
   328 {
   329 	char *name,*help;
   330 	IPluginFunction *pFunction;
   331 
   332 	pContext->LocalToString(params[1], &name);
   333 	pContext->LocalToString(params[3], &help);
   334 	pFunction = pContext->GetFunctionById(params[2]);
   335 
   336 	if (!pFunction)
   337 	{
   338 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   339 	}
   340 
   341 	g_ConCmds.AddServerCommand(pFunction, name, help, params[4]);
   342 
   343 	return 1;
   344 }
   345 
   346 static cell_t sm_RegConsoleCmd(IPluginContext *pContext, const cell_t *params)
   347 {
   348 	char *name,*help;
   349 	IPluginFunction *pFunction;
   350 
   351 	pContext->LocalToString(params[1], &name);
   352 	pContext->LocalToString(params[3], &help);
   353 	pFunction = pContext->GetFunctionById(params[2]);
   354 
   355 	if (!pFunction)
   356 	{
   357 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   358 	}
   359 
   360 	g_ConCmds.AddConsoleCommand(pFunction, name, help, params[4]);
   361 
   362 	return 1;
   363 }
   364 
   365 static cell_t sm_RegAdminCmd(IPluginContext *pContext, const cell_t *params)
   366 {
   367 	char *name,*help;
   368 	const char *group;
   369 	IPluginFunction *pFunction;
   370 	FlagBits flags = params[3];
   371 	int cmdflags = params[6];
   372 
   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]);
   377 
   378 	if (group[0] == '\0')
   379 	{
   380 		CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
   381 		group = pPlugin->GetFilename();
   382 	}
   383 
   384 	if (!pFunction)
   385 	{
   386 		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   387 	}
   388 
   389 	g_ConCmds.AddAdminCommand(pFunction, name, group, flags, help, cmdflags);
   390 
   391 	return 1;
   392 }
   393 
   394 static cell_t sm_GetCmdArgs(IPluginContext *pContext, const cell_t *params)
   395 {
   396 	return engine->Cmd_Argc() - 1;
   397 }
   398 
   399 static cell_t sm_GetCmdArg(IPluginContext *pContext, const cell_t *params)
   400 {
   401 	const char *arg = engine->Cmd_Argv(params[1]);
   402 	
   403 	pContext->StringToLocalUTF8(params[2], params[3], arg, NULL);
   404 
   405 	return 1;
   406 }
   407 
   408 static cell_t sm_GetCmdArgString(IPluginContext *pContext, const cell_t *params)
   409 {
   410 	const char *args = engine->Cmd_Args();
   411 
   412 	pContext->StringToLocalUTF8(params[1], params[2], args, NULL);
   413 
   414 	return 1;
   415 }
   416 
   417 REGISTER_NATIVES(convarNatives)
   418 {
   419 	{"CreateConVar",		sm_CreateConVar},
   420 	{"FindConVar",			sm_FindConVar},
   421 	{"HookConVarChange",	sm_HookConVarChange},
   422 	{"UnhookConVarChange",	sm_UnhookConVarChange},
   423 	{"GetConVarBool",		sm_GetConVarBool},
   424 	{"SetConVarBool",		sm_SetConVarNum},
   425 	{"GetConVarInt",		sm_GetConVarInt},
   426 	{"SetConVarInt",		sm_SetConVarNum},
   427 	{"GetConVarFloat",		sm_GetConVarFloat},
   428 	{"SetConVarFloat",		sm_SetConVarFloat},
   429 	{"GetConVarString",		sm_GetConVarString},
   430 	{"SetConVarString",		sm_SetConVarString},
   431 	{"GetConVarFlags",		sm_GetConVarFlags},
   432 	{"SetConVarFlags",		sm_SetConVarFlags},
   433 	{"GetConVarName",		sm_GetConVarName},
   434 	{"GetConVarMin",		sm_GetConVarMin},
   435 	{"GetConVarMax",		sm_GetConVarMax},
   436 	{"ResetConVar",			sm_ResetConVar},
   437 	{"RegServerCmd",		sm_RegServerCmd},
   438 	{"RegConsoleCmd",		sm_RegConsoleCmd},
   439 	{"GetCmdArgString",		sm_GetCmdArgString},
   440 	{"GetCmdArgs",			sm_GetCmdArgs},
   441 	{"GetCmdArg",			sm_GetCmdArg},
   442 	{NULL,					NULL}
   443 };