Fixed requiring dependencies at the wrong time.
3 * =============================================================================
4 * SourceMod Client Preferences Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "extension.h"
36 cell_t RegClientPrefCookie(IPluginContext *pContext, const cell_t *params)
39 pContext->LocalToString(params[1], &name);
43 return pContext->ThrowNativeError("Cannot create preference cookie with no name");
47 pContext->LocalToString(params[2], &desc);
49 Cookie *pCookie = g_CookieManager.CreateCookie(name, desc, (CookieAccess)params[3]);
56 return handlesys->CreateHandle(g_CookieType,
58 pContext->GetIdentity(),
59 myself->GetIdentity(),
63 cell_t FindClientPrefCookie(IPluginContext *pContext, const cell_t *params)
66 pContext->LocalToString(params[1], &name);
68 Cookie *pCookie = g_CookieManager.FindCookie(name);
75 return handlesys->CreateHandle(g_CookieType,
77 pContext->GetIdentity(),
78 myself->GetIdentity(),
82 cell_t SetClientPrefCookie(IPluginContext *pContext, const cell_t *params)
84 int client = params[1];
86 if ((client < 1) || (client > playerhelpers->GetMaxClients()))
88 return pContext->ThrowNativeError("Client index %d is invalid", client);
91 Handle_t hndl = static_cast<Handle_t>(params[2]);
96 sec.pIdentity = myself->GetIdentity();
100 if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
103 return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
107 pContext->LocalToString(params[3], &value);
109 return g_CookieManager.SetCookieValue(pCookie, client, value);
112 cell_t GetClientPrefCookie(IPluginContext *pContext, const cell_t *params)
114 int client = params[1];
116 if ((client < 1) || (client > playerhelpers->GetMaxClients()))
118 return pContext->ThrowNativeError("Client index %d is invalid", client);
121 Handle_t hndl = static_cast<Handle_t>(params[2]);
126 sec.pIdentity = myself->GetIdentity();
130 if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
133 return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
138 g_CookieManager.GetCookieValue(pCookie, client, &value);
140 pContext->StringToLocal(params[3], params[4], value);
145 cell_t AreClientCookiesCached(IPluginContext *pContext, const cell_t *params)
147 int client = params[1];
149 if ((client < 1) || (client > playerhelpers->GetMaxClients()))
151 return pContext->ThrowNativeError("Client index %d is invalid", client);
154 return g_CookieManager.AreClientCookiesCached(client);
157 cell_t GetCookieAccess(IPluginContext *pContext, const cell_t *params)
159 Handle_t hndl = static_cast<Handle_t>(params[1]);
164 sec.pIdentity = myself->GetIdentity();
168 if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
171 return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
174 return pCookie->access;
177 static cell_t GetCookieIterator(IPluginContext *pContext, const cell_t *params)
179 SourceHook::List<Cookie *>::iterator *iter = new SourceHook::List<Cookie *>::iterator;
180 *iter = g_CookieManager.cookieList.begin();
182 Handle_t hndl = handlesys->CreateHandle(g_CookieIterator, iter, pContext->GetIdentity(), myself->GetIdentity(), NULL);
183 if (hndl == BAD_HANDLE)
191 static cell_t ReadCookieIterator(IPluginContext *pContext, const cell_t *params)
193 SourceHook::List<Cookie *>::iterator *iter;
195 Handle_t hndl = static_cast<Handle_t>(params[1]);
200 sec.pIdentity = myself->GetIdentity();
202 if ((err = handlesys->ReadHandle(hndl, g_CookieIterator, &sec, (void **)&iter))
205 return pContext->ThrowNativeError("Invalid Cookie iterator handle %x (error %d)", hndl, err);
208 if (*iter == g_CookieManager.cookieList.end())
213 Cookie *pCookie = (Cookie *)**iter;
217 pContext->StringToLocalUTF8(params[2], params[3], pCookie->name, NULL);
218 pContext->StringToLocalUTF8(params[5], params[6], pCookie->description, NULL);
221 pContext->LocalToPhysAddr(params[4], &addr);
222 *addr = pCookie->access;
227 cell_t ShowSettingsMenu(IPluginContext *pContext, const cell_t *params)
230 Translate(message, sizeof(message), "%T:", 2, NULL, "Client Settings", ¶ms[1]);
232 g_CookieManager.clientMenu->SetDefaultTitle(message);
233 g_CookieManager.clientMenu->Display(params[1], 0, NULL);
238 cell_t AddSettingsMenuItem(IPluginContext *pContext, const cell_t *params)
241 pContext->LocalToString(params[3], &display);
243 /* Register a callback */
244 ItemHandler *pItem = new ItemHandler;
245 pItem->isAutoMenu = false;
246 pItem->forward = forwards->CreateForwardEx(NULL, ET_Ignore, 5, NULL, Param_Cell, Param_Cell, Param_Cell, Param_String, Param_Cell);
248 pItem->forward->AddFunction(pContext, static_cast<funcid_t>(params[1]));
251 AutoMenuData *data = new AutoMenuData;
252 data->datavalue = params[2];
253 data->handler = pItem;
254 UTIL_Format(info, sizeof(info), "%x", data);
256 ItemDrawInfo draw(display, 0);
258 g_CookieManager.clientMenu->AppendItem(info, draw);
260 /* Track this in case the plugin unloads */
262 IPlugin *pPlugin = plsys->FindPluginByContext(pContext->GetContext());
263 SourceHook::List<char *> *pList = NULL;
265 if (!pPlugin->GetProperty("SettingsMenuItems", (void **)&pList, false) || !pList)
267 pList = new SourceHook::List<char *>;
268 pPlugin->SetProperty("SettingsMenuItems", pList);
271 char *copyarray = new char[strlen(display)+1];
272 UTIL_Format(copyarray, strlen(display)+1, "%s", display);
274 pList->push_back(copyarray);
279 cell_t AddSettingsPrefabMenuItem(IPluginContext *pContext, const cell_t *params)
281 Handle_t hndl = static_cast<Handle_t>(params[1]);
286 sec.pIdentity = myself->GetIdentity();
290 if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
293 return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
296 /* Register a callback */
297 ItemHandler *pItem = new ItemHandler;
298 pItem->isAutoMenu = true;
299 pItem->autoMenuType = (CookieMenu)params[2];
302 /* User passed a function id for a callback */
305 pItem->forward = forwards->CreateForwardEx(NULL, ET_Ignore, 5, NULL, Param_Cell, Param_Cell, Param_Cell, Param_String, Param_Cell);
306 pItem->forward->AddFunction(pContext, static_cast<funcid_t>(params[4]));
310 pItem->forward = NULL;
314 pContext->LocalToString(params[3], &display);
316 ItemDrawInfo draw(display, 0);
319 AutoMenuData *data = new AutoMenuData;
320 data->datavalue = params[5];
321 data->pCookie = pCookie;
322 data->type = (CookieMenu)params[2];
323 data->handler = pItem;
324 UTIL_Format(info, sizeof(info), "%x", data);
326 g_CookieManager.clientMenu->AppendItem(info, draw);
328 /* Track this in case the plugin unloads */
330 IPlugin *pPlugin = plsys->FindPluginByContext(pContext->GetContext());
331 SourceHook::List<char *> *pList = NULL;
333 if (!pPlugin->GetProperty("SettingsMenuItems", (void **)&pList, false) || !pList)
335 pList = new SourceHook::List<char *>;
336 pPlugin->SetProperty("SettingsMenuItems", pList);
339 char *copyarray = new char[strlen(display)+1];
340 UTIL_Format(copyarray, strlen(display)+1, "%s", display);
342 pList->push_back(copyarray);
347 sp_nativeinfo_t g_ClientPrefNatives[] =
349 {"RegClientCookie", RegClientPrefCookie},
350 {"FindClientCookie", FindClientPrefCookie},
351 {"SetClientCookie", SetClientPrefCookie},
352 {"GetClientCookie", GetClientPrefCookie},
353 {"AreClientCookiesCached", AreClientCookiesCached},
354 {"GetCookieAccess", GetCookieAccess},
355 {"ReadCookieIterator", ReadCookieIterator},
356 {"GetCookieIterator", GetCookieIterator},
357 {"ShowCookieMenu", ShowSettingsMenu},
358 {"SetCookieMenuItem", AddSettingsMenuItem},
359 {"SetCookiePrefabMenu", AddSettingsPrefabMenuItem},