core/smn_convar.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Feb 08 21:41:28 2007 +0000 (2007-02-08)
changeset 457 9b8a85bf4175
child 465 8856d2e7018d
permissions -rw-r--r--
Initial import of convar stuff
1) Natives to create, find, set, and get convars
2) "cvars" option added to sm menu
3) Some internal additions to CPlugin to store a convar list
Still some things left to do...
ds@457
     1
/**
ds@457
     2
* ===============================================================
ds@457
     3
* SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@457
     4
* ===============================================================
ds@457
     5
*
ds@457
     6
* This file is not open source and may not be copied without explicit
ds@457
     7
* written permission of AlliedModders LLC.  This file may not be redistributed 
ds@457
     8
* in whole or significant part.
ds@457
     9
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@457
    10
*
ds@457
    11
* Version: $Id$
ds@457
    12
*/
ds@457
    13
ds@457
    14
#include "sm_globals.h"
ds@457
    15
#include "sourcemm_api.h"
ds@457
    16
#include "HandleSys.h"
ds@457
    17
#include "CConVarManager.h"
ds@457
    18
ds@457
    19
static cell_t sm_CreateConVar(IPluginContext *pContext, const cell_t *params)
ds@457
    20
{
ds@457
    21
	char *name, *defaultVal, *helpText;
ds@457
    22
ds@457
    23
	pContext->LocalToString(params[1], &name);
ds@457
    24
ds@457
    25
	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
ds@457
    26
	if (name == NULL || strcmp(name, "") == 0)
ds@457
    27
	{
ds@457
    28
		pContext->ThrowNativeError("Null or blank convar name is not allowed.");
ds@457
    29
		return BAD_HANDLE;
ds@457
    30
	}
ds@457
    31
ds@457
    32
	pContext->LocalToString(params[2], &defaultVal);
ds@457
    33
	pContext->LocalToString(params[3], &helpText);
ds@457
    34
ds@457
    35
	bool hasMin = params[5] ? true : false;
ds@457
    36
	bool hasMax = params[7] ? true : false;
ds@457
    37
	float min = sp_ctof(params[6]);
ds@457
    38
	float max = sp_ctof(params[8]);
ds@457
    39
ds@457
    40
	return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
ds@457
    41
}
ds@457
    42
ds@457
    43
static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
ds@457
    44
{
ds@457
    45
	char *name;
ds@457
    46
ds@457
    47
	pContext->LocalToString(params[1], &name);
ds@457
    48
ds@457
    49
	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
ds@457
    50
	if (name == NULL || strcmp(name, "") == 0)
ds@457
    51
	{
ds@457
    52
		return BAD_HANDLE;
ds@457
    53
	}
ds@457
    54
ds@457
    55
	return g_ConVarManager.FindConVar(name);
ds@457
    56
}
ds@457
    57
ds@457
    58
static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
ds@457
    59
{
ds@457
    60
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
    61
	HandleError err;
ds@457
    62
	ConVar *cvar;
ds@457
    63
ds@457
    64
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
    65
		!= HandleError_None)
ds@457
    66
	{
ds@457
    67
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
    68
	}
ds@457
    69
ds@457
    70
	return cvar->GetBool();
ds@457
    71
}
ds@457
    72
ds@457
    73
static cell_t sm_GetConVarInt(IPluginContext *pContext, const cell_t *params)
ds@457
    74
{
ds@457
    75
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
    76
	HandleError err;
ds@457
    77
	ConVar *cvar;
ds@457
    78
ds@457
    79
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
    80
		!= HandleError_None)
ds@457
    81
	{
ds@457
    82
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
    83
	}
ds@457
    84
ds@457
    85
	return cvar->GetInt();
ds@457
    86
}
ds@457
    87
ds@457
    88
/* This handles both SetConVarBool() and SetConVarInt() */
ds@457
    89
static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
ds@457
    90
{
ds@457
    91
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
    92
	HandleError err;
ds@457
    93
	ConVar *cvar;
ds@457
    94
ds@457
    95
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
    96
		!= HandleError_None)
ds@457
    97
	{
ds@457
    98
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
    99
	}
ds@457
   100
ds@457
   101
	cvar->SetValue(params[2]);
ds@457
   102
ds@457
   103
	return 1;
ds@457
   104
}
ds@457
   105
ds@457
   106
static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
ds@457
   107
{
ds@457
   108
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   109
	HandleError err;
ds@457
   110
	ConVar *cvar;
ds@457
   111
ds@457
   112
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   113
		!= HandleError_None)
ds@457
   114
	{
ds@457
   115
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   116
	}
ds@457
   117
	
ds@457
   118
	float value = cvar->GetFloat();
ds@457
   119
ds@457
   120
	return sp_ftoc(value);
ds@457
   121
}
ds@457
   122
ds@457
   123
static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
ds@457
   124
{
ds@457
   125
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   126
	HandleError err;
ds@457
   127
	ConVar *cvar;
ds@457
   128
ds@457
   129
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   130
		!= HandleError_None)
ds@457
   131
	{
ds@457
   132
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   133
	}
ds@457
   134
ds@457
   135
	float value = sp_ctof(params[2]);
ds@457
   136
	cvar->SetValue(value);
ds@457
   137
ds@457
   138
	return 1;
ds@457
   139
}
ds@457
   140
ds@457
   141
static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
ds@457
   142
{
ds@457
   143
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   144
	HandleError err;
ds@457
   145
	ConVar *cvar;
ds@457
   146
ds@457
   147
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   148
		!= HandleError_None)
ds@457
   149
	{
ds@457
   150
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   151
	}
ds@457
   152
ds@457
   153
	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
ds@457
   154
ds@457
   155
	return 1;
ds@457
   156
}
ds@457
   157
ds@457
   158
static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
ds@457
   159
{
ds@457
   160
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   161
	HandleError err;
ds@457
   162
	ConVar *cvar;
ds@457
   163
ds@457
   164
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   165
		!= HandleError_None)
ds@457
   166
	{
ds@457
   167
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   168
	}
ds@457
   169
ds@457
   170
	char *value;
ds@457
   171
	pContext->LocalToString(params[2], &value);
ds@457
   172
ds@457
   173
	cvar->SetValue(value);
ds@457
   174
ds@457
   175
	return 1;
ds@457
   176
}
ds@457
   177
ds@457
   178
static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
ds@457
   179
{
ds@457
   180
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   181
	HandleError err;
ds@457
   182
	ConVar *cvar;
ds@457
   183
ds@457
   184
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   185
		!= HandleError_None)
ds@457
   186
	{
ds@457
   187
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   188
	}
ds@457
   189
ds@457
   190
	return cvar->GetFlags();
ds@457
   191
}
ds@457
   192
ds@457
   193
static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
ds@457
   194
{
ds@457
   195
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   196
	HandleError err;
ds@457
   197
	ConVar *cvar;
ds@457
   198
ds@457
   199
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   200
		!= HandleError_None)
ds@457
   201
	{
ds@457
   202
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   203
	}
ds@457
   204
ds@457
   205
	cvar->SetFlags(params[2]);
ds@457
   206
ds@457
   207
	return 1;
ds@457
   208
}
ds@457
   209
ds@457
   210
static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
ds@457
   211
{
ds@457
   212
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   213
	HandleError err;
ds@457
   214
	ConVar *cvar;
ds@457
   215
ds@457
   216
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   217
		!= HandleError_None)
ds@457
   218
	{
ds@457
   219
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   220
	}
ds@457
   221
ds@457
   222
	cell_t *addr;
ds@457
   223
	bool hasMin;
ds@457
   224
	float min;
ds@457
   225
ds@457
   226
	pContext->LocalToPhysAddr(params[2], &addr);
ds@457
   227
ds@457
   228
	hasMin = cvar->GetMin(min);
ds@457
   229
	*addr = sp_ftoc(min);
ds@457
   230
ds@457
   231
	return hasMin;
ds@457
   232
}
ds@457
   233
ds@457
   234
static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
ds@457
   235
{
ds@457
   236
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   237
	HandleError err;
ds@457
   238
	ConVar *cvar;
ds@457
   239
ds@457
   240
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   241
		!= HandleError_None)
ds@457
   242
	{
ds@457
   243
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   244
	}
ds@457
   245
ds@457
   246
	cell_t *addr;
ds@457
   247
	bool hasMax;
ds@457
   248
	float max;
ds@457
   249
ds@457
   250
	pContext->LocalToPhysAddr(params[2], &addr);
ds@457
   251
ds@457
   252
	hasMax = cvar->GetMax(max);
ds@457
   253
	*addr = sp_ftoc(max);
ds@457
   254
ds@457
   255
	return hasMax;
ds@457
   256
}
ds@457
   257
ds@457
   258
static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
ds@457
   259
{
ds@457
   260
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   261
	HandleError err;
ds@457
   262
	ConVar *cvar;
ds@457
   263
ds@457
   264
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   265
		!= HandleError_None)
ds@457
   266
	{
ds@457
   267
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   268
	}
ds@457
   269
ds@457
   270
	cvar->Revert();
ds@457
   271
ds@457
   272
	return 1;
ds@457
   273
}
ds@457
   274
ds@457
   275
REGISTER_NATIVES(convarNatives)
ds@457
   276
{
ds@457
   277
	{"CreateConVar",	sm_CreateConVar},
ds@457
   278
	{"FindConVar",		sm_FindConVar},
ds@457
   279
	{"GetConVarBool",	sm_GetConVarBool},
ds@457
   280
	{"SetConVarBool",	sm_SetConVarNum},
ds@457
   281
	{"GetConVarInt",	sm_GetConVarInt},
ds@457
   282
	{"SetConVarInt",	sm_SetConVarNum},
ds@457
   283
	{"GetConVarFloat",	sm_GetConVarFloat},
ds@457
   284
	{"SetConVarFloat",	sm_SetConVarFloat},
ds@457
   285
	{"GetConVarString",	sm_GetConVarString},
ds@457
   286
	{"SetConVarString",	sm_SetConVarString},
ds@457
   287
	{"GetConVarFlags",	sm_GetConVarFlags},
ds@457
   288
	{"SetConVarFlags",	sm_SetConVarFlags},
ds@457
   289
	{"GetConVarMin",	sm_GetConVarMin},
ds@457
   290
	{"GetConVarMax",	sm_GetConVarMax},
ds@457
   291
	{"ResetConVar",		sm_ResetConVar},
ds@457
   292
	{NULL,				NULL}
ds@457
   293
};