core/smn_console.cpp
changeset 498 b404e8154de4
child 500 868e9b19a872
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_console.cpp	Thu Feb 15 22:17:01 2007 +0000
     1.3 @@ -0,0 +1,367 @@
     1.4 +/**
     1.5 +* ===============================================================
     1.6 +* SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.7 +* ===============================================================
     1.8 +*
     1.9 +* This file is not open source and may not be copied without explicit
    1.10 +* written permission of AlliedModders LLC.  This file may not be redistributed 
    1.11 +* in whole or significant part.
    1.12 +* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.13 +*
    1.14 +* Version: $Id$
    1.15 +*/
    1.16 +
    1.17 +#include "sm_globals.h"
    1.18 +#include "sourcemm_api.h"
    1.19 +#include "HandleSys.h"
    1.20 +#include "CConVarManager.h"
    1.21 +#include "CConCmdManager.h"
    1.22 +
    1.23 +static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
    1.24 +{
    1.25 +	char *name, *defaultVal, *helpText;
    1.26 +
    1.27 +	pContext->LocalToString(params[1], &name);
    1.28 +
    1.29 +	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
    1.30 +	if (name == NULL || strcmp(name, "") == 0)
    1.31 +	{
    1.32 +		return pContext->ThrowNativeError("Null or blank convar name is not allowed");
    1.33 +	}
    1.34 +
    1.35 +	pContext->LocalToString(params[2], &defaultVal);
    1.36 +	pContext->LocalToString(params[3], &helpText);
    1.37 +
    1.38 +	bool hasMin = params[5] ? true : false;
    1.39 +	bool hasMax = params[7] ? true : false;
    1.40 +	float min = sp_ctof(params[6]);
    1.41 +	float max = sp_ctof(params[8]);
    1.42 +
    1.43 +	return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
    1.44 +}
    1.45 +
    1.46 +static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
    1.47 +{
    1.48 +	char *name;
    1.49 +
    1.50 +	pContext->LocalToString(params[1], &name);
    1.51 +
    1.52 +	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
    1.53 +	if (name == NULL || strcmp(name, "") == 0)
    1.54 +	{
    1.55 +		return BAD_HANDLE;
    1.56 +	}
    1.57 +
    1.58 +	return g_ConVarManager.FindConVar(name);
    1.59 +}
    1.60 +
    1.61 +static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
    1.62 +{
    1.63 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.64 +	HandleError err;
    1.65 +	ConVar *cvar;
    1.66 +
    1.67 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    1.68 +		!= HandleError_None)
    1.69 +	{
    1.70 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    1.71 +	}
    1.72 +
    1.73 +	g_ConVarManager.HookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    1.74 +
    1.75 +	return 1;
    1.76 +}
    1.77 +
    1.78 +static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
    1.79 +{
    1.80 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.81 +	HandleError err;
    1.82 +	ConVar *cvar;
    1.83 +
    1.84 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
    1.85 +		!= HandleError_None)
    1.86 +	{
    1.87 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
    1.88 +	}
    1.89 +
    1.90 +	g_ConVarManager.UnhookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
    1.91 +
    1.92 +	return 1;
    1.93 +}
    1.94 +
    1.95 +static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
    1.96 +{
    1.97 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.98 +	HandleError err;
    1.99 +	ConVar *cvar;
   1.100 +
   1.101 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.102 +		!= HandleError_None)
   1.103 +	{
   1.104 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.105 +	}
   1.106 +
   1.107 +	return cvar->GetBool();
   1.108 +}
   1.109 +
   1.110 +static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
   1.111 +{
   1.112 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.113 +	HandleError err;
   1.114 +	ConVar *cvar;
   1.115 +
   1.116 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.117 +		!= HandleError_None)
   1.118 +	{
   1.119 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.120 +	}
   1.121 +
   1.122 +	return cvar->GetInt();
   1.123 +}
   1.124 +
   1.125 +/* This handles both SetConVarBool() and SetConVarInt() */
   1.126 +static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
   1.127 +{
   1.128 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.129 +	HandleError err;
   1.130 +	ConVar *cvar;
   1.131 +
   1.132 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.133 +		!= HandleError_None)
   1.134 +	{
   1.135 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.136 +	}
   1.137 +
   1.138 +	cvar->SetValue(params[2]);
   1.139 +
   1.140 +	return 1;
   1.141 +}
   1.142 +
   1.143 +static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
   1.144 +{
   1.145 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.146 +	HandleError err;
   1.147 +	ConVar *cvar;
   1.148 +
   1.149 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.150 +		!= HandleError_None)
   1.151 +	{
   1.152 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.153 +	}
   1.154 +	
   1.155 +	float value = cvar->GetFloat();
   1.156 +
   1.157 +	return sp_ftoc(value);
   1.158 +}
   1.159 +
   1.160 +static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
   1.161 +{
   1.162 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.163 +	HandleError err;
   1.164 +	ConVar *cvar;
   1.165 +
   1.166 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.167 +		!= HandleError_None)
   1.168 +	{
   1.169 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.170 +	}
   1.171 +
   1.172 +	float value = sp_ctof(params[2]);
   1.173 +	cvar->SetValue(value);
   1.174 +
   1.175 +	return 1;
   1.176 +}
   1.177 +
   1.178 +static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
   1.179 +{
   1.180 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.181 +	HandleError err;
   1.182 +	ConVar *cvar;
   1.183 +
   1.184 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.185 +		!= HandleError_None)
   1.186 +	{
   1.187 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.188 +	}
   1.189 +
   1.190 +	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
   1.191 +
   1.192 +	return 1;
   1.193 +}
   1.194 +
   1.195 +static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
   1.196 +{
   1.197 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.198 +	HandleError err;
   1.199 +	ConVar *cvar;
   1.200 +
   1.201 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.202 +		!= HandleError_None)
   1.203 +	{
   1.204 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.205 +	}
   1.206 +
   1.207 +	char *value;
   1.208 +	pContext->LocalToString(params[2], &value);
   1.209 +
   1.210 +	cvar->SetValue(value);
   1.211 +
   1.212 +	return 1;
   1.213 +}
   1.214 +
   1.215 +static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
   1.216 +{
   1.217 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.218 +	HandleError err;
   1.219 +	ConVar *cvar;
   1.220 +
   1.221 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.222 +		!= HandleError_None)
   1.223 +	{
   1.224 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.225 +	}
   1.226 +
   1.227 +	return cvar->GetFlags();
   1.228 +}
   1.229 +
   1.230 +static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
   1.231 +{
   1.232 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.233 +	HandleError err;
   1.234 +	ConVar *cvar;
   1.235 +
   1.236 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.237 +		!= HandleError_None)
   1.238 +	{
   1.239 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.240 +	}
   1.241 +
   1.242 +	cvar->SetFlags(params[2]);
   1.243 +
   1.244 +	return 1;
   1.245 +}
   1.246 +
   1.247 +static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
   1.248 +{
   1.249 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.250 +	HandleError err;
   1.251 +	ConVar *cvar;
   1.252 +
   1.253 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.254 +		!= HandleError_None)
   1.255 +	{
   1.256 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.257 +	}
   1.258 +
   1.259 +	cell_t *addr;
   1.260 +	bool hasMin;
   1.261 +	float min;
   1.262 +
   1.263 +	pContext->LocalToPhysAddr(params[2], &addr);
   1.264 +
   1.265 +	hasMin = cvar->GetMin(min);
   1.266 +	*addr = sp_ftoc(min);
   1.267 +
   1.268 +	return hasMin;
   1.269 +}
   1.270 +
   1.271 +static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
   1.272 +{
   1.273 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.274 +	HandleError err;
   1.275 +	ConVar *cvar;
   1.276 +
   1.277 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.278 +		!= HandleError_None)
   1.279 +	{
   1.280 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.281 +	}
   1.282 +
   1.283 +	cell_t *addr;
   1.284 +	bool hasMax;
   1.285 +	float max;
   1.286 +
   1.287 +	pContext->LocalToPhysAddr(params[2], &addr);
   1.288 +
   1.289 +	hasMax = cvar->GetMax(max);
   1.290 +	*addr = sp_ftoc(max);
   1.291 +
   1.292 +	return hasMax;
   1.293 +}
   1.294 +
   1.295 +static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
   1.296 +{
   1.297 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.298 +	HandleError err;
   1.299 +	ConVar *cvar;
   1.300 +
   1.301 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.302 +		!= HandleError_None)
   1.303 +	{
   1.304 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.305 +	}
   1.306 +
   1.307 +	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetName(), NULL);
   1.308 +
   1.309 +	return 1;
   1.310 +}
   1.311 +
   1.312 +static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
   1.313 +{
   1.314 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.315 +	HandleError err;
   1.316 +	ConVar *cvar;
   1.317 +
   1.318 +	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
   1.319 +		!= HandleError_None)
   1.320 +	{
   1.321 +		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
   1.322 +	}
   1.323 +
   1.324 +	cvar->Revert();
   1.325 +
   1.326 +	return 1;
   1.327 +}
   1.328 +
   1.329 +static cell_t sm_RegServerCmd(IPluginContext *pContext, const cell_t *params)
   1.330 +{
   1.331 +	char *name,*help;
   1.332 +	IPluginFunction *pFunction;
   1.333 +
   1.334 +	pContext->LocalToString(params[1], &name);
   1.335 +	pContext->LocalToString(params[3], &help);
   1.336 +	pFunction = pContext->GetFunctionById(params[2]);
   1.337 +
   1.338 +	if (!pFunction)
   1.339 +	{
   1.340 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
   1.341 +	}
   1.342 +
   1.343 +	g_ConCmds.AddServerCommand(pFunction, name, help, params[4]);
   1.344 +
   1.345 +	return 1;
   1.346 +}
   1.347 +
   1.348 +REGISTER_NATIVES(convarNatives)
   1.349 +{
   1.350 +	{"CreateConVar",		sm_CreateConVar},
   1.351 +	{"FindConVar",			sm_FindConVar},
   1.352 +	{"HookConVarChange",	sm_HookConVarChange},
   1.353 +	{"UnhookConVarChange",	sm_UnhookConVarChange},
   1.354 +	{"GetConVarBool",		sm_GetConVarBool},
   1.355 +	{"SetConVarBool",		sm_SetConVarNum},
   1.356 +	{"GetConVarInt",		sm_GetConVarInt},
   1.357 +	{"SetConVarInt",		sm_SetConVarNum},
   1.358 +	{"GetConVarFloat",		sm_GetConVarFloat},
   1.359 +	{"SetConVarFloat",		sm_SetConVarFloat},
   1.360 +	{"GetConVarString",		sm_GetConVarString},
   1.361 +	{"SetConVarString",		sm_SetConVarString},
   1.362 +	{"GetConVarFlags",		sm_GetConVarFlags},
   1.363 +	{"SetConVarFlags",		sm_SetConVarFlags},
   1.364 +	{"GetConVarName",		sm_GetConVarName},
   1.365 +	{"GetConVarMin",		sm_GetConVarMin},
   1.366 +	{"GetConVarMax",		sm_GetConVarMax},
   1.367 +	{"ResetConVar",			sm_ResetConVar},
   1.368 +	{"RegServerCmd",		sm_RegServerCmd},
   1.369 +	{NULL,					NULL}
   1.370 +};