-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcvars.cpp
executable file
·217 lines (185 loc) · 5.87 KB
/
cvars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
#include "cvars.h"
//-----------------------------------------------------------------------------
// Global ConVar changed callback mapping.
//-----------------------------------------------------------------------------
typedef std::vector<object> ChangedCallbacks;
typedef std::unordered_map<std::string, ChangedCallbacks> ConVarMap;
ConVarMap g_ConVarMap;
//-----------------------------------------------------------------------------
// ConVar extension class.
//-----------------------------------------------------------------------------
boost::shared_ptr<ConVar> ConVarExt::__init__(const char* name, const char* value,
const char* description, int flags, object min_value, object max_value)
{
if (!name || name[0] == '\0')
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "An empty string is not a valid ConVar name.")
float fMin = 0;
float fMax = 0;
try {
fMin = extract<float>(min_value);
}
catch (...) {
PyErr_Clear();
}
try {
fMax = extract<float>(max_value);
}
catch (...) {
PyErr_Clear();
}
ConVar *pConVar = g_pCVar->FindVar(name);
if (!pConVar)
{
// Find if the command already exists
ConCommand* pConCommand = g_pCVar->FindCommand(name);
if (pConCommand)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "ConCommand already exists.")
ConVar* pConVar = new ConVar(strdup(name), strdup(value), flags,
strdup(description), !min_value.is_none(), fMin, !max_value.is_none(), fMax);
return boost::shared_ptr<ConVar>(pConVar, &NeverDeleteDeleter<ConVar *>);
}
return boost::shared_ptr<ConVar>(pConVar, &NeverDeleteDeleter<ConVar *>);
}
bool ConVarExt::HasMin(ConVar* pConVar)
{
float fMin;
return pConVar->GetMin(fMin);
}
bool ConVarExt::HasMax(ConVar* pConVar)
{
float fMax;
return pConVar->GetMax(fMax);
}
float ConVarExt::GetMin(ConVar* pConVar)
{
float fMin;
pConVar->GetMin(fMin);
return fMin;
}
float ConVarExt::GetMax(ConVar* pConVar)
{
float fMax;
pConVar->GetMax(fMax);
return fMax;
}
void ConVarExt::SetValue(ConVar* pConVar, bool bValue)
{
pConVar->SetValue(bValue);
}
void ConVarExt::MakePublic(ConVar* pConVar)
{
pConVar->m_nFlags |= FCVAR_NOTIFY;
g_pCVar->CallGlobalChangeCallbacks(pConVar, pConVar->GetString(), pConVar->GetFloat());
}
void ConVarExt::RemovePublic(ConVar* pConVar)
{
pConVar->m_nFlags &= ~FCVAR_NOTIFY;
g_pCVar->CallGlobalChangeCallbacks(pConVar, pConVar->GetString(), pConVar->GetFloat());
}
void ConVarExt::ChangedCallback(IConVar* var, const char* pOldValue, float flOldValue)
{
ConVarMap::iterator map_it = g_ConVarMap.find(var->GetName());
if (map_it == g_ConVarMap.end())
return;
ConVar* pConVar = static_cast<ConVar*>(var);
ChangedCallbacks& callables = map_it->second;
for (ChangedCallbacks::iterator it = callables.begin(); it != callables.end(); ++it)
{
BEGIN_BOOST_PY()
(*it)(ptr(pConVar), pOldValue, pConVar->GetString());
END_BOOST_PY_NORET()
}
}
void ConVarExt::AddChangedCallback(ConVar* pConVar, PyObject* pCallable)
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));
ChangedCallbacks& callables = g_ConVarMap[pConVar->GetName()];
if (!callables.size())
{
if (!installed)
{
g_pCVar->InstallGlobalChangeCallback(ChangedCallback);
installed = true;
}
}
else
{
for (ChangedCallbacks::iterator it = callables.begin(); it != callables.end(); ++it)
{
if (is_same_func(oCallable, *it))
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback already registered.")
}
}
callables.push_back(oCallable);
}
void ConVarExt::RemoveChangedCallback(ConVar* pConVar, PyObject* pCallable)
{
ConVarMap::iterator map_it = g_ConVarMap.find(pConVar->GetName());
if (map_it == g_ConVarMap.end())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback not registered.")
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));
ChangedCallbacks& callables = map_it->second;
for (ChangedCallbacks::iterator it = callables.begin();;)
{
if(it == callables.end())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback not registered.")
if (is_same_func(oCallable, *it))
{
callables.erase(it);
break;
}
else
{
++it;
}
}
if (!callables.size())
{
g_ConVarMap.erase(map_it);
if (!g_ConVarMap.size())
{
g_pCVar->RemoveGlobalChangeCallback(ChangedCallback);
installed = false;
}
}
}
void ConVarExt::ClearCallback()
{
if (installed)
{
g_ConVarMap.clear();
g_pCVar->RemoveGlobalChangeCallback(ChangedCallback);
installed = false;
}
}
bool ConVarExt::installed = false;