core/smn_convar.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Feb 11 11:09:38 2007 +0000 (2007-02-11)
changeset 465 8856d2e7018d
parent 457 9b8a85bf4175
child 469 e9a53be35abd
permissions -rw-r--r--
More convar insanity
1) Added natives: HookConVarChange(), UnhookConVarChange(), and GetConVarName()
2) Fixed bug(s) where ConVar handles were being created when they didn't need to be
3) Fixed bug where convars not created by SourceMod would be unregistered
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@465
    28
		return pContext->ThrowNativeError("Null or blank convar name is not allowed.");
ds@457
    29
	}
ds@457
    30
ds@457
    31
	pContext->LocalToString(params[2], &defaultVal);
ds@457
    32
	pContext->LocalToString(params[3], &helpText);
ds@457
    33
ds@457
    34
	bool hasMin = params[5] ? true : false;
ds@457
    35
	bool hasMax = params[7] ? true : false;
ds@457
    36
	float min = sp_ctof(params[6]);
ds@457
    37
	float max = sp_ctof(params[8]);
ds@457
    38
ds@457
    39
	return g_ConVarManager.CreateConVar(pContext, name, defaultVal, helpText, params[4], hasMin, min, hasMax, max);
ds@457
    40
}
ds@457
    41
ds@457
    42
static cell_t sm_FindConVar(IPluginContext *pContext, const cell_t *params)
ds@457
    43
{
ds@457
    44
	char *name;
ds@457
    45
ds@457
    46
	pContext->LocalToString(params[1], &name);
ds@457
    47
ds@457
    48
	// While the engine seems to accept a blank convar name, it causes a crash upon server quit
ds@457
    49
	if (name == NULL || strcmp(name, "") == 0)
ds@457
    50
	{
ds@457
    51
		return BAD_HANDLE;
ds@457
    52
	}
ds@457
    53
ds@457
    54
	return g_ConVarManager.FindConVar(name);
ds@457
    55
}
ds@457
    56
ds@465
    57
static cell_t sm_HookConVarChange(IPluginContext *pContext, const cell_t *params)
ds@465
    58
{
ds@465
    59
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@465
    60
	HandleError err;
ds@465
    61
	ConVar *cvar;
ds@465
    62
ds@465
    63
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@465
    64
		!= HandleError_None)
ds@465
    65
	{
ds@465
    66
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@465
    67
	}
ds@465
    68
ds@465
    69
	g_ConVarManager.HookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
ds@465
    70
ds@465
    71
	return 1;
ds@465
    72
}
ds@465
    73
ds@465
    74
static cell_t sm_UnhookConVarChange(IPluginContext *pContext, const cell_t *params)
ds@465
    75
{
ds@465
    76
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@465
    77
	HandleError err;
ds@465
    78
	ConVar *cvar;
ds@465
    79
ds@465
    80
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@465
    81
		!= HandleError_None)
ds@465
    82
	{
ds@465
    83
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@465
    84
	}
ds@465
    85
ds@465
    86
	g_ConVarManager.UnhookConVarChange(pContext, cvar, static_cast<funcid_t>(params[2]));
ds@465
    87
ds@465
    88
	return 1;
ds@465
    89
}
ds@465
    90
ds@457
    91
static cell_t sm_GetConVarBool(IPluginContext *pContext, const cell_t *params)
ds@457
    92
{
ds@457
    93
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
    94
	HandleError err;
ds@457
    95
	ConVar *cvar;
ds@457
    96
ds@457
    97
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
    98
		!= HandleError_None)
ds@457
    99
	{
ds@457
   100
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   101
	}
ds@457
   102
ds@457
   103
	return cvar->GetBool();
ds@457
   104
}
ds@457
   105
ds@457
   106
static cell_t sm_GetConVarInt(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
	return cvar->GetInt();
ds@457
   119
}
ds@457
   120
ds@457
   121
/* This handles both SetConVarBool() and SetConVarInt() */
ds@457
   122
static cell_t sm_SetConVarNum(IPluginContext *pContext, const cell_t *params)
ds@457
   123
{
ds@457
   124
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   125
	HandleError err;
ds@457
   126
	ConVar *cvar;
ds@457
   127
ds@457
   128
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   129
		!= HandleError_None)
ds@457
   130
	{
ds@457
   131
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   132
	}
ds@457
   133
ds@457
   134
	cvar->SetValue(params[2]);
ds@457
   135
ds@457
   136
	return 1;
ds@457
   137
}
ds@457
   138
ds@457
   139
static cell_t sm_GetConVarFloat(IPluginContext *pContext, const cell_t *params)
ds@457
   140
{
ds@457
   141
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   142
	HandleError err;
ds@457
   143
	ConVar *cvar;
ds@457
   144
ds@457
   145
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   146
		!= HandleError_None)
ds@457
   147
	{
ds@457
   148
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   149
	}
ds@457
   150
	
ds@457
   151
	float value = cvar->GetFloat();
ds@457
   152
ds@457
   153
	return sp_ftoc(value);
ds@457
   154
}
ds@457
   155
ds@457
   156
static cell_t sm_SetConVarFloat(IPluginContext *pContext, const cell_t *params)
ds@457
   157
{
ds@457
   158
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   159
	HandleError err;
ds@457
   160
	ConVar *cvar;
ds@457
   161
ds@457
   162
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   163
		!= HandleError_None)
ds@457
   164
	{
ds@457
   165
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   166
	}
ds@457
   167
ds@457
   168
	float value = sp_ctof(params[2]);
ds@457
   169
	cvar->SetValue(value);
ds@457
   170
ds@457
   171
	return 1;
ds@457
   172
}
ds@457
   173
ds@457
   174
static cell_t sm_GetConVarString(IPluginContext *pContext, const cell_t *params)
ds@457
   175
{
ds@457
   176
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   177
	HandleError err;
ds@457
   178
	ConVar *cvar;
ds@457
   179
ds@457
   180
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   181
		!= HandleError_None)
ds@457
   182
	{
ds@457
   183
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   184
	}
ds@457
   185
ds@457
   186
	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetString(), NULL);
ds@457
   187
ds@457
   188
	return 1;
ds@457
   189
}
ds@457
   190
ds@457
   191
static cell_t sm_SetConVarString(IPluginContext *pContext, const cell_t *params)
ds@457
   192
{
ds@457
   193
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   194
	HandleError err;
ds@457
   195
	ConVar *cvar;
ds@457
   196
ds@457
   197
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   198
		!= HandleError_None)
ds@457
   199
	{
ds@457
   200
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   201
	}
ds@457
   202
ds@457
   203
	char *value;
ds@457
   204
	pContext->LocalToString(params[2], &value);
ds@457
   205
ds@457
   206
	cvar->SetValue(value);
ds@457
   207
ds@457
   208
	return 1;
ds@457
   209
}
ds@457
   210
ds@457
   211
static cell_t sm_GetConVarFlags(IPluginContext *pContext, const cell_t *params)
ds@457
   212
{
ds@457
   213
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   214
	HandleError err;
ds@457
   215
	ConVar *cvar;
ds@457
   216
ds@457
   217
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   218
		!= HandleError_None)
ds@457
   219
	{
ds@457
   220
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   221
	}
ds@457
   222
ds@457
   223
	return cvar->GetFlags();
ds@457
   224
}
ds@457
   225
ds@457
   226
static cell_t sm_SetConVarFlags(IPluginContext *pContext, const cell_t *params)
ds@457
   227
{
ds@457
   228
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   229
	HandleError err;
ds@457
   230
	ConVar *cvar;
ds@457
   231
ds@457
   232
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   233
		!= HandleError_None)
ds@457
   234
	{
ds@457
   235
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   236
	}
ds@457
   237
ds@457
   238
	cvar->SetFlags(params[2]);
ds@457
   239
ds@457
   240
	return 1;
ds@457
   241
}
ds@457
   242
ds@457
   243
static cell_t sm_GetConVarMin(IPluginContext *pContext, const cell_t *params)
ds@457
   244
{
ds@457
   245
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   246
	HandleError err;
ds@457
   247
	ConVar *cvar;
ds@457
   248
ds@457
   249
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   250
		!= HandleError_None)
ds@457
   251
	{
ds@457
   252
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   253
	}
ds@457
   254
ds@457
   255
	cell_t *addr;
ds@457
   256
	bool hasMin;
ds@457
   257
	float min;
ds@457
   258
ds@457
   259
	pContext->LocalToPhysAddr(params[2], &addr);
ds@457
   260
ds@457
   261
	hasMin = cvar->GetMin(min);
ds@457
   262
	*addr = sp_ftoc(min);
ds@457
   263
ds@457
   264
	return hasMin;
ds@457
   265
}
ds@457
   266
ds@457
   267
static cell_t sm_GetConVarMax(IPluginContext *pContext, const cell_t *params)
ds@457
   268
{
ds@457
   269
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   270
	HandleError err;
ds@457
   271
	ConVar *cvar;
ds@457
   272
ds@457
   273
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   274
		!= HandleError_None)
ds@457
   275
	{
ds@457
   276
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   277
	}
ds@457
   278
ds@457
   279
	cell_t *addr;
ds@457
   280
	bool hasMax;
ds@457
   281
	float max;
ds@457
   282
ds@457
   283
	pContext->LocalToPhysAddr(params[2], &addr);
ds@457
   284
ds@457
   285
	hasMax = cvar->GetMax(max);
ds@457
   286
	*addr = sp_ftoc(max);
ds@457
   287
ds@457
   288
	return hasMax;
ds@457
   289
}
ds@457
   290
ds@465
   291
static cell_t sm_GetConVarName(IPluginContext *pContext, const cell_t *params)
ds@465
   292
{
ds@465
   293
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@465
   294
	HandleError err;
ds@465
   295
	ConVar *cvar;
ds@465
   296
ds@465
   297
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@465
   298
		!= HandleError_None)
ds@465
   299
	{
ds@465
   300
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@465
   301
	}
ds@465
   302
ds@465
   303
	pContext->StringToLocalUTF8(params[2], params[3], cvar->GetName(), NULL);
ds@465
   304
ds@465
   305
	return 1;
ds@465
   306
}
ds@465
   307
ds@457
   308
static cell_t sm_ResetConVar(IPluginContext *pContext, const cell_t *params)
ds@457
   309
{
ds@457
   310
	Handle_t hndl = static_cast<Handle_t>(params[1]);
ds@457
   311
	HandleError err;
ds@457
   312
	ConVar *cvar;
ds@457
   313
ds@457
   314
	if ((err=g_HandleSys.ReadHandle(hndl, g_ConVarManager.GetHandleType(), NULL, (void **)&cvar))
ds@457
   315
		!= HandleError_None)
ds@457
   316
	{
ds@457
   317
		return pContext->ThrowNativeError("Invalid ConVar Handle %x (error %d)", hndl, err);
ds@457
   318
	}
ds@457
   319
ds@457
   320
	cvar->Revert();
ds@457
   321
ds@457
   322
	return 1;
ds@457
   323
}
ds@457
   324
ds@457
   325
REGISTER_NATIVES(convarNatives)
ds@457
   326
{
ds@465
   327
	{"CreateConVar",		sm_CreateConVar},
ds@465
   328
	{"FindConVar",			sm_FindConVar},
ds@465
   329
	{"HookConVarChange",	sm_HookConVarChange},
ds@465
   330
	{"UnhookConVarChange",	sm_UnhookConVarChange},
ds@465
   331
	{"GetConVarBool",		sm_GetConVarBool},
ds@465
   332
	{"SetConVarBool",		sm_SetConVarNum},
ds@465
   333
	{"GetConVarInt",		sm_GetConVarInt},
ds@465
   334
	{"SetConVarInt",		sm_SetConVarNum},
ds@465
   335
	{"GetConVarFloat",		sm_GetConVarFloat},
ds@465
   336
	{"SetConVarFloat",		sm_SetConVarFloat},
ds@465
   337
	{"GetConVarString",		sm_GetConVarString},
ds@465
   338
	{"SetConVarString",		sm_SetConVarString},
ds@465
   339
	{"GetConVarFlags",		sm_GetConVarFlags},
ds@465
   340
	{"SetConVarFlags",		sm_SetConVarFlags},
ds@465
   341
	{"GetConVarName",		sm_GetConVarName},
ds@465
   342
	{"GetConVarMin",		sm_GetConVarMin},
ds@465
   343
	{"GetConVarMax",		sm_GetConVarMax},
ds@465
   344
	{"ResetConVar",			sm_ResetConVar},
ds@465
   345
	{NULL,					NULL}
ds@457
   346
};