|
ds@466
|
1 |
/** |
|
ds@466
|
2 |
* =============================================================== |
|
ds@466
|
3 |
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. |
|
ds@466
|
4 |
* =============================================================== |
|
ds@466
|
5 |
* |
|
ds@466
|
6 |
* This file is not open source and may not be copied without explicit |
|
ds@466
|
7 |
* written permission of AlliedModders LLC. This file may not be redistributed |
|
ds@466
|
8 |
* in whole or significant part. |
|
ds@466
|
9 |
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php |
|
ds@466
|
10 |
* |
|
ds@466
|
11 |
* Version: $Id$ |
|
ds@466
|
12 |
*/ |
|
ds@466
|
13 |
|
|
ds@466
|
14 |
#include "CConVarManager.h" |
|
ds@466
|
15 |
#include "CLogger.h" |
|
ds@466
|
16 |
#include "PluginSys.h" |
|
ds@466
|
17 |
#include "sm_srvcmds.h" |
|
ds@466
|
18 |
#include "sm_stringutil.h" |
|
ds@466
|
19 |
|
|
ds@466
|
20 |
CConVarManager g_ConVarManager; |
|
ds@466
|
21 |
|
|
ds@466
|
22 |
CConVarManager::CConVarManager() : m_ConVarType(0) |
|
ds@466
|
23 |
{ |
|
ds@466
|
24 |
// Create a convar lookup trie |
|
ds@466
|
25 |
m_ConVarCache = sm_trie_create(); |
|
ds@466
|
26 |
} |
|
ds@466
|
27 |
|
|
ds@466
|
28 |
CConVarManager::~CConVarManager() |
|
ds@466
|
29 |
{ |
|
ds@466
|
30 |
List<ConVarInfo *>::iterator i; |
|
ds@466
|
31 |
|
|
ds@466
|
32 |
// Destroy our convar lookup trie |
|
ds@466
|
33 |
sm_trie_destroy(m_ConVarCache); |
|
ds@466
|
34 |
|
|
ds@466
|
35 |
// Destroy all the ConVarInfo structures |
|
ds@466
|
36 |
for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) |
|
ds@466
|
37 |
{ |
|
ds@466
|
38 |
delete (*i); |
|
ds@466
|
39 |
} |
|
ds@466
|
40 |
|
|
ds@466
|
41 |
m_ConVars.clear(); |
|
ds@466
|
42 |
} |
|
ds@466
|
43 |
|
|
ds@466
|
44 |
void CConVarManager::OnSourceModAllInitialized() |
|
ds@466
|
45 |
{ |
|
ds@466
|
46 |
HandleAccess sec; |
|
ds@466
|
47 |
|
|
ds@466
|
48 |
// Set up access rights for the 'ConVar' handle type |
|
ds@466
|
49 |
sec.access[HandleAccess_Read] = 0; |
|
ds@466
|
50 |
sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; |
|
ds@466
|
51 |
sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER; |
|
ds@466
|
52 |
|
|
ds@466
|
53 |
// Create the 'ConVar' handle type |
|
ds@466
|
54 |
m_ConVarType = g_HandleSys.CreateType("ConVar", this, 0, NULL, &sec, g_pCoreIdent, NULL); |
|
ds@466
|
55 |
|
|
ds@466
|
56 |
// Add the 'cvars' option to the 'sm' console command |
|
ds@466
|
57 |
g_RootMenu.AddRootConsoleCommand("cvars", "View convars created by a plugin", this); |
|
ds@466
|
58 |
} |
|
ds@466
|
59 |
|
|
ds@466
|
60 |
void CConVarManager::OnSourceModShutdown() |
|
ds@466
|
61 |
{ |
|
ds@466
|
62 |
IChangeableForward *fwd; |
|
ds@466
|
63 |
List<ConVarInfo *>::iterator i; |
|
ds@466
|
64 |
|
|
ds@466
|
65 |
// Iterate list of ConVarInfo structures |
|
ds@466
|
66 |
for (i = m_ConVars.begin(); i != m_ConVars.end(); i++) |
|
ds@466
|
67 |
{ |
|
ds@466
|
68 |
fwd = (*i)->changeForward; |
|
ds@466
|
69 |
|
|
ds@466
|
70 |
// Free any convar-change forwards that still exist |
|
ds@466
|
71 |
if (fwd) |
|
ds@466
|
72 |
{ |
|
ds@466
|
73 |
g_Forwards.ReleaseForward(fwd); |
|
ds@466
|
74 |
} |
|
ds@466
|
75 |
} |
|
ds@466
|
76 |
|
|
ds@466
|
77 |
// Remove the 'cvars' option from the 'sm' console command |
|
ds@466
|
78 |
g_RootMenu.RemoveRootConsoleCommand("cvars", this); |
|
ds@466
|
79 |
|
|
ds@466
|
80 |
// Remove the 'ConVar' handle type |
|
ds@466
|
81 |
g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent); |
|
ds@466
|
82 |
} |
|
ds@466
|
83 |
|
|
ds@466
|
84 |
void CConVarManager::OnHandleDestroy(HandleType_t type, void *object) |
|
ds@466
|
85 |
{ |
|
ds@466
|
86 |
ConVarInfo *info; |
|
ds@466
|
87 |
ConCommandBase *cvar = static_cast<ConCommandBase *>(object); |
|
ds@466
|
88 |
|
|
ds@466
|
89 |
// Find convar in lookup trie |
|
ds@466
|
90 |
sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info)); |
|
ds@466
|
91 |
|
|
ds@466
|
92 |
// If convar was created by SourceMod plugin... |
|
ds@466
|
93 |
if (info->sourceMod) |
|
ds@466
|
94 |
{ |
|
ds@466
|
95 |
// Then unregister it |
|
ds@466
|
96 |
g_SMAPI->UnregisterConCmdBase(g_PLAPI, cvar); |
|
ds@466
|
97 |
} |
|
ds@466
|
98 |
} |
|
ds@466
|
99 |
|
|
ds@466
|
100 |
void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argcount) |
|
ds@466
|
101 |
{ |
|
ds@466
|
102 |
if (argcount >= 3) |
|
ds@466
|
103 |
{ |
|
ds@466
|
104 |
int id = 1; |
|
ds@466
|
105 |
|
|
ds@466
|
106 |
// Get plugin index that was passed |
|
ds@466
|
107 |
int num = atoi(g_RootMenu.GetArgument(2)); |
|
ds@466
|
108 |
|
|
ds@466
|
109 |
// If invalid plugin index... |
|
ds@466
|
110 |
if (num < 1 || num > (int)g_PluginSys.GetPluginCount()) |
|
ds@466
|
111 |
{ |
|
ds@466
|
112 |
g_RootMenu.ConsolePrint("[SM] Plugin index not found."); |
|
ds@466
|
113 |
return; |
|
ds@466
|
114 |
} |
|
ds@466
|
115 |
|
|
ds@466
|
116 |
// Get plugin object |
|
ds@466
|
117 |
CPlugin *pl = g_PluginSys.GetPluginByOrder(num); |
|
ds@466
|
118 |
|
|
ds@466
|
119 |
// Get number of convars created by plugin |
|
ds@466
|
120 |
int convarnum = pl->GetConVarCount(); |
|
ds@466
|
121 |
|
|
ds@466
|
122 |
if (convarnum == 0) |
|
ds@466
|
123 |
{ |
|
ds@466
|
124 |
g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name); |
|
ds@466
|
125 |
return; |
|
ds@466
|
126 |
} |
|
ds@466
|
127 |
|
|
ds@466
|
128 |
g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\":", pl->GetPublicInfo()->name); |
|
ds@466
|
129 |
|
|
ds@466
|
130 |
// Iterate convar list and display each one |
|
ds@466
|
131 |
for (int i = 0; i < convarnum; i++, id++) |
|
ds@466
|
132 |
{ |
|
ds@466
|
133 |
ConVar *cvar = pl->GetConVarByIndex(i); |
|
ds@466
|
134 |
g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); |
|
ds@466
|
135 |
} |
|
ds@466
|
136 |
|
|
ds@466
|
137 |
return; |
|
ds@466
|
138 |
} |
|
ds@466
|
139 |
|
|
ds@466
|
140 |
// Display usage of subcommand |
|
ds@466
|
141 |
g_RootMenu.ConsolePrint("[SM] Usage: sm cvars <#>"); |
|
ds@466
|
142 |
} |
|
ds@466
|
143 |
|
|
ds@466
|
144 |
Handle_t CConVarManager::CreateConVar(IPluginContext *pContext, const char *name, const char *defaultVal, const char *helpText, int flags, bool hasMin, float min, bool hasMax, float max) |
|
ds@466
|
145 |
{ |
|
ds@466
|
146 |
ConVar *cvar = NULL; |
|
ds@466
|
147 |
ConVarInfo *info = NULL; |
|
ds@466
|
148 |
Handle_t hndl = 0; |
|
ds@466
|
149 |
|
|
ds@466
|
150 |
// Find out if the convar exists already |
|
ds@466
|
151 |
cvar = icvar->FindVar(name); |
|
ds@466
|
152 |
|
|
ds@466
|
153 |
// If the convar already exists... |
|
ds@466
|
154 |
if (cvar != NULL) |
|
ds@466
|
155 |
{ |
|
ds@466
|
156 |
// First check if we already have a handle to it |
|
ds@466
|
157 |
if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info))) |
|
ds@466
|
158 |
{ |
|
ds@466
|
159 |
// If we do, then return that handle |
|
ds@466
|
160 |
return info->handle; |
|
ds@466
|
161 |
} else { |
|
ds@466
|
162 |
// If we don't, then create a new handle from the convar |
|
ds@466
|
163 |
hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); |
|
ds@466
|
164 |
|
|
ds@466
|
165 |
info = new ConVarInfo; |
|
ds@466
|
166 |
info->handle = hndl; |
|
ds@466
|
167 |
info->sourceMod = false; |
|
ds@466
|
168 |
info->changeForward = NULL; |
|
ds@466
|
169 |
info->origCallback = cvar->GetCallback(); |
|
ds@466
|
170 |
|
|
ds@466
|
171 |
m_ConVars.push_back(info); |
|
ds@466
|
172 |
|
|
ds@466
|
173 |
return hndl; |
|
ds@466
|
174 |
} |
|
ds@466
|
175 |
} |
|
ds@466
|
176 |
|
|
ds@466
|
177 |
// To prevent creating a convar that has the same name as a console command... ugh |
|
ds@466
|
178 |
ConCommandBase *pBase = icvar->GetCommands(); |
|
ds@466
|
179 |
|
|
ds@466
|
180 |
while (pBase) |
|
ds@466
|
181 |
{ |
|
ds@466
|
182 |
if (strcmp(pBase->GetName(), name) == 0) |
|
ds@466
|
183 |
{ |
|
ds@466
|
184 |
return pContext->ThrowNativeError("Convar \"%s\" was not created. A console command with the same name already exists.", name); |
|
ds@466
|
185 |
} |
|
ds@466
|
186 |
|
|
ds@466
|
187 |
pBase = const_cast<ConCommandBase *>(pBase->GetNext()); |
|
ds@466
|
188 |
} |
|
ds@466
|
189 |
|
|
ds@466
|
190 |
// Since we didn't find an existing convar (or concmd with the same name), now we can finally create it! |
|
ds@466
|
191 |
cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max); |
|
ds@466
|
192 |
|
|
ds@466
|
193 |
// Add new convar to plugin's list |
|
ds@466
|
194 |
g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar); |
|
ds@466
|
195 |
|
|
ds@466
|
196 |
// Create a handle from the new convar |
|
ds@466
|
197 |
hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); |
|
ds@466
|
198 |
|
|
ds@466
|
199 |
info = new ConVarInfo; |
|
ds@466
|
200 |
info->handle = hndl; |
|
ds@466
|
201 |
info->sourceMod = true; |
|
ds@466
|
202 |
info->changeForward = NULL; |
|
ds@466
|
203 |
info->origCallback = NULL; |
|
ds@466
|
204 |
|
|
ds@466
|
205 |
m_ConVars.push_back(info); |
|
ds@466
|
206 |
|
|
ds@466
|
207 |
// Insert the handle into our lookup trie |
|
ds@466
|
208 |
sm_trie_insert(m_ConVarCache, name, info); |
|
ds@466
|
209 |
|
|
ds@466
|
210 |
return hndl; |
|
ds@466
|
211 |
} |
|
ds@466
|
212 |
|
|
ds@466
|
213 |
Handle_t CConVarManager::FindConVar(const char *name) |
|
ds@466
|
214 |
{ |
|
ds@466
|
215 |
ConVar *cvar = NULL; |
|
ds@466
|
216 |
ConVarInfo *info = NULL; |
|
ds@466
|
217 |
Handle_t hndl = 0; |
|
ds@466
|
218 |
|
|
ds@466
|
219 |
// Search for convar |
|
ds@466
|
220 |
cvar = icvar->FindVar(name); |
|
ds@466
|
221 |
|
|
ds@466
|
222 |
// If it doesn't exist, then return an invalid handle |
|
ds@466
|
223 |
if (cvar == NULL) |
|
ds@466
|
224 |
{ |
|
ds@466
|
225 |
return BAD_HANDLE; |
|
ds@466
|
226 |
} |
|
ds@466
|
227 |
|
|
ds@466
|
228 |
// At this point, convar exists, so find out if we already have a handle for it |
|
ds@466
|
229 |
if (sm_trie_retrieve(m_ConVarCache, name, reinterpret_cast<void **>(&info))) |
|
ds@466
|
230 |
{ |
|
ds@466
|
231 |
// If we do, then return that handle |
|
ds@466
|
232 |
return info->handle; |
|
ds@466
|
233 |
} |
|
ds@466
|
234 |
|
|
ds@466
|
235 |
// If we don't, then create a new handle from the convar |
|
ds@466
|
236 |
hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); |
|
ds@466
|
237 |
|
|
ds@466
|
238 |
info = new ConVarInfo; |
|
ds@466
|
239 |
info->handle = hndl; |
|
ds@466
|
240 |
info->sourceMod = false; |
|
ds@466
|
241 |
info->changeForward = NULL; |
|
ds@466
|
242 |
info->origCallback = cvar->GetCallback(); |
|
ds@466
|
243 |
|
|
ds@466
|
244 |
m_ConVars.push_back(info); |
|
ds@466
|
245 |
|
|
ds@466
|
246 |
// Insert the handle into our cache |
|
ds@466
|
247 |
sm_trie_insert(m_ConVarCache, name, info); |
|
ds@466
|
248 |
|
|
ds@466
|
249 |
return hndl; |
|
ds@466
|
250 |
} |
|
ds@466
|
251 |
|
|
ds@466
|
252 |
void CConVarManager::HookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) |
|
ds@466
|
253 |
{ |
|
ds@466
|
254 |
IPluginFunction *func = pContext->GetFunctionById(funcid); |
|
ds@466
|
255 |
IChangeableForward *fwd = NULL; |
|
ds@466
|
256 |
char fwdName[64]; |
|
ds@466
|
257 |
ConVarInfo *info = NULL; |
|
ds@466
|
258 |
|
|
ds@466
|
259 |
// This shouldn't happen... |
|
ds@466
|
260 |
if (func == NULL) |
|
ds@466
|
261 |
{ |
|
ds@469
|
262 |
pContext->ThrowNativeError("Invalid function specified"); |
|
ds@466
|
263 |
return; |
|
ds@466
|
264 |
} |
|
ds@466
|
265 |
|
|
ds@466
|
266 |
// Create a forward name |
|
ds@466
|
267 |
UTIL_Format(fwdName, sizeof(fwdName), "ConVar.%s", cvar->GetName()); |
|
ds@466
|
268 |
|
|
ds@466
|
269 |
// First find out if the forward already exists |
|
ds@466
|
270 |
g_Forwards.FindForward(fwdName, &fwd); |
|
ds@466
|
271 |
|
|
ds@466
|
272 |
// If the forward doesn't exist... |
|
ds@466
|
273 |
if (fwd == NULL) |
|
ds@466
|
274 |
{ |
|
ds@466
|
275 |
// This is the forward's parameter type list |
|
ds@466
|
276 |
ParamType p[] = {Param_Cell, Param_String, Param_String}; |
|
ds@466
|
277 |
|
|
ds@466
|
278 |
// Create the forward |
|
ds@466
|
279 |
fwd = g_Forwards.CreateForwardEx(fwdName, ET_Ignore, 3, p); |
|
ds@466
|
280 |
|
|
ds@466
|
281 |
// Find the convar in the lookup trie |
|
ds@466
|
282 |
if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info))) |
|
ds@466
|
283 |
{ |
|
ds@466
|
284 |
// Set the convar's forward to the newly created one |
|
ds@466
|
285 |
info->changeForward = fwd; |
|
ds@466
|
286 |
|
|
ds@466
|
287 |
// Set the convar's callback to our static one |
|
ds@466
|
288 |
cvar->InstallChangeCallback(OnConVarChanged); |
|
ds@466
|
289 |
} |
|
ds@466
|
290 |
} |
|
ds@466
|
291 |
|
|
ds@466
|
292 |
// Add the function to the forward's list |
|
ds@466
|
293 |
fwd->AddFunction(func); |
|
ds@466
|
294 |
} |
|
ds@466
|
295 |
|
|
ds@466
|
296 |
void CConVarManager::UnhookConVarChange(IPluginContext *pContext, ConVar *cvar, funcid_t funcid) |
|
ds@466
|
297 |
{ |
|
ds@466
|
298 |
IPluginFunction *func = pContext->GetFunctionById(funcid); |
|
ds@466
|
299 |
IChangeableForward *fwd = NULL; |
|
ds@466
|
300 |
ConVarInfo *info = NULL; |
|
ds@466
|
301 |
|
|
ds@466
|
302 |
// This shouldn't happen... |
|
ds@466
|
303 |
if (func == NULL) |
|
ds@466
|
304 |
{ |
|
ds@469
|
305 |
pContext->ThrowNativeError("Invalid function specified"); |
|
ds@466
|
306 |
return; |
|
ds@466
|
307 |
} |
|
ds@466
|
308 |
|
|
ds@466
|
309 |
// Find the convar in the lookup trie |
|
ds@466
|
310 |
if (sm_trie_retrieve(m_ConVarCache, cvar->GetName(), reinterpret_cast<void **>(&info))) |
|
ds@466
|
311 |
{ |
|
ds@466
|
312 |
// Get the forward |
|
ds@466
|
313 |
fwd = info->changeForward; |
|
ds@466
|
314 |
|
|
ds@466
|
315 |
// If the forward doesn't exist, we can't unhook anything |
|
ds@466
|
316 |
if (fwd == NULL) |
|
ds@466
|
317 |
{ |
|
ds@469
|
318 |
pContext->ThrowNativeError("Convar \"%s\" has no active hook", cvar->GetName()); |
|
ds@466
|
319 |
return; |
|
ds@466
|
320 |
} |
|
ds@466
|
321 |
|
|
ds@466
|
322 |
// Remove the function from the forward's list |
|
ds@466
|
323 |
if (!fwd->RemoveFunction(func)) |
|
ds@466
|
324 |
{ |
|
ds@469
|
325 |
pContext->ThrowNativeError("Invalid hook callback specified for convar \"%s\"", cvar->GetName()); |
|
ds@466
|
326 |
return; |
|
ds@466
|
327 |
} |
|
ds@466
|
328 |
|
|
ds@466
|
329 |
// If the forward now has 0 functions in it... |
|
ds@466
|
330 |
if (fwd->GetFunctionCount() == 0) |
|
ds@466
|
331 |
{ |
|
ds@466
|
332 |
// Free this forward |
|
ds@466
|
333 |
g_Forwards.ReleaseForward(fwd); |
|
ds@466
|
334 |
info->changeForward = NULL; |
|
ds@466
|
335 |
|
|
ds@466
|
336 |
// Put the back the original convar callback |
|
ds@466
|
337 |
cvar->InstallChangeCallback(info->origCallback); |
|
ds@466
|
338 |
} |
|
ds@466
|
339 |
} |
|
ds@466
|
340 |
} |
|
ds@466
|
341 |
|
|
ds@466
|
342 |
void CConVarManager::OnConVarChanged(ConVar *cvar, const char *oldValue) |
|
ds@466
|
343 |
{ |
|
ds@466
|
344 |
Trie *cache = g_ConVarManager.GetConVarCache(); |
|
ds@466
|
345 |
ConVarInfo *info; |
|
ds@466
|
346 |
|
|
ds@466
|
347 |
// Find the convar in the lookup trie |
|
ds@466
|
348 |
sm_trie_retrieve(cache, cvar->GetName(), reinterpret_cast<void **>(&info)); |
|
ds@466
|
349 |
|
|
ds@466
|
350 |
FnChangeCallback origCallback = info->origCallback; |
|
ds@466
|
351 |
IChangeableForward *fwd = info->changeForward; |
|
ds@466
|
352 |
|
|
ds@466
|
353 |
// If there was a change callback installed previously, call it |
|
ds@466
|
354 |
if (origCallback) |
|
ds@466
|
355 |
{ |
|
ds@466
|
356 |
origCallback(cvar, oldValue); |
|
ds@466
|
357 |
} |
|
ds@466
|
358 |
|
|
ds@466
|
359 |
// Now call forwards in plugins that have hooked this |
|
ds@466
|
360 |
fwd->PushCell(info->handle); |
|
ds@466
|
361 |
fwd->PushString(cvar->GetString()); |
|
ds@466
|
362 |
fwd->PushString(oldValue); |
|
ds@466
|
363 |
fwd->Execute(NULL); |
|
ds@466
|
364 |
} |