Skip to content

Commit da3e5dc

Browse files
committed
Replaced CConVar.get() with a better workaround
1 parent e8b7f7b commit da3e5dc

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

addons/source-python/packages/source-python/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@
4848
from _core.settings import _CoreSettingsInstance
4949

5050
# Set the logging level
51-
CConVar.get('sp_logging_level').set_int(
51+
CConVar('sp_logging_level').set_int(
5252
int(_CoreSettingsInstance['LOG_SETTINGS']['level']))
5353

5454
# Set the logging areas
55-
CConVar.get('sp_logging_areas').set_int(
55+
CConVar('sp_logging_areas').set_int(
5656
int(_CoreSettingsInstance['LOG_SETTINGS']['areas']))
5757

5858
# Was an exception raised?
5959
except (ValueError, ConfigObjError):
6060

6161
# Set the logging level to max (5)
62-
CConVar.get('sp_logging_level').set_int(5)
62+
CConVar('sp_logging_level').set_int(5)
6363

6464
# Set the logging area to include console, SP logs, and main log
65-
CConVar.get('sp_logging_areas').set_int(7)
65+
CConVar('sp_logging_areas').set_int(7)
6666

6767
# Import the _SPLogger
6868
from loggers import _SPLogger

addons/source-python/packages/source-python/loggers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ def areas(self):
243243
return self._areas.get_int()
244244

245245
# Set the core ConVars
246-
_level = CConVar.get(
246+
_level = CConVar(
247247
'sp_logging_level', '0', 0, 'The Source.Python base logging level')
248-
_areas = CConVar.get(
248+
_areas = CConVar(
249249
'sp_logging_areas', '1', 0, 'The Source.Python base logging areas')
250250

251251
# Get the Source.Python main LogManager instance

src/core/modules/cvar/cvar_wrap_python.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,40 @@ void export_concommandbase()
189189
class ConVarExt
190190
{
191191
public:
192-
static ConVar* Get1(const char* szName)
192+
static ConVar* New1(PyObject* cls, const char* szName)
193193
{
194-
return Get2(szName, NULL, 0);
194+
return New2(cls, szName, NULL, 0);
195195
}
196196

197-
static ConVar* Get2(const char* szName, const char* szDefaultValue, int flags)
197+
static ConVar* New2(PyObject* cls, const char* szName, const char* szDefaultValue, int flags)
198198
{
199-
return Get3(szName, szDefaultValue, flags, NULL);
199+
return New3(cls, szName, szDefaultValue, flags, NULL);
200200
}
201201

202-
static ConVar* Get3(const char* szName, const char* szDefaultValue, int flags,
202+
static ConVar* New3(PyObject* cls, const char* szName, const char* szDefaultValue, int flags,
203203
const char* szHelpString)
204204
{
205-
return Get4(szName, szDefaultValue, flags, szHelpString, false, 0, false, 0);
205+
return New4(cls, szName, szDefaultValue, flags, szHelpString, false, 0, false, 0);
206206
}
207207

208-
static ConVar* Get4(const char* szName, const char* szDefaultValue, int flags,
208+
static ConVar* New4(PyObject* cls, const char* szName, const char* szDefaultValue, int flags,
209209
const char* szHelpString, bool bMin, float fMin, bool bMax, float fMax)
210210
{
211211
ConVar* pConVar = g_pCVar->FindVar(szName);
212212
return pConVar ? pConVar : new ConVar(szName, szDefaultValue, flags,
213213
strdup(szHelpString), bMin, fMin, bMax, fMax);
214214
}
215215

216+
static object __init__(tuple args, dict kw)
217+
{
218+
// This method accepts unlimited arguments and keywords, because
219+
// I don't want to write a method for each constructor.
220+
221+
// We cannot use a "void" function here, because raw_function()
222+
// requires the method to return an object.
223+
return object();
224+
}
225+
216226
static bool HasMin(ConVar* pConVar)
217227
{
218228
float fMin;
@@ -249,30 +259,33 @@ void export_convar()
249259
{
250260
// TODO: Rename it?
251261
class_<ConVar, bases<ConCommandBase, IConVar>, boost::noncopyable >("CConVar", no_init)
252-
.def("get",
253-
&ConVarExt::Get1,
262+
// We have to overload __init__. Otherwise we would get an error because of "no_init"
263+
.def("__init__", raw_function(&ConVarExt::__init__))
264+
265+
// Add constructors
266+
.def("__new__",
267+
&ConVarExt::New1,
254268
"Creates a new server variable. If it already exists, the existing one will be returned.",
255269
reference_existing_object_policy()
256270
)
257271

258-
.def("get",
259-
&ConVarExt::Get2,
272+
.def("__new__",
273+
&ConVarExt::New2,
260274
"Creates a new server variable. If it already exists, the existing one will be returned.",
261275
reference_existing_object_policy()
262276
)
263277

264-
.def("get",
265-
&ConVarExt::Get3,
278+
.def("__new__",
279+
&ConVarExt::New3,
266280
"Creates a new server variable. If it already exists, the existing one will be returned.",
267281
reference_existing_object_policy()
268282
)
269283

270-
.def("get",
271-
&ConVarExt::Get4,
284+
.def("__new__",
285+
&ConVarExt::New4,
272286
"Creates a new server variable. If it already exists, the existing one will be returned.",
273287
reference_existing_object_policy()
274288
)
275-
.staticmethod("get")
276289

277290
.def("get_float",
278291
&ConVar::GetFloat,

0 commit comments

Comments
 (0)