1.1 --- a/extensions/clientprefs/natives.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,361 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod Client Preferences Extension
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "extension.h"
1.36 -#include "cookie.h"
1.37 -#include "menus.h"
1.38 -
1.39 -cell_t RegClientPrefCookie(IPluginContext *pContext, const cell_t *params)
1.40 -{
1.41 - char *name;
1.42 - pContext->LocalToString(params[1], &name);
1.43 -
1.44 - if (name[0] == '\0')
1.45 - {
1.46 - return pContext->ThrowNativeError("Cannot create preference cookie with no name");
1.47 - }
1.48 -
1.49 - char *desc;
1.50 - pContext->LocalToString(params[2], &desc);
1.51 -
1.52 - Cookie *pCookie = g_CookieManager.CreateCookie(name, desc, (CookieAccess)params[3]);
1.53 -
1.54 - if (!pCookie)
1.55 - {
1.56 - return BAD_HANDLE;
1.57 - }
1.58 -
1.59 - return handlesys->CreateHandle(g_CookieType,
1.60 - pCookie,
1.61 - pContext->GetIdentity(),
1.62 - myself->GetIdentity(),
1.63 - NULL);
1.64 -}
1.65 -
1.66 -cell_t FindClientPrefCookie(IPluginContext *pContext, const cell_t *params)
1.67 -{
1.68 - char *name;
1.69 - pContext->LocalToString(params[1], &name);
1.70 -
1.71 - Cookie *pCookie = g_CookieManager.FindCookie(name);
1.72 -
1.73 - if (!pCookie)
1.74 - {
1.75 - return BAD_HANDLE;
1.76 - }
1.77 -
1.78 - return handlesys->CreateHandle(g_CookieType,
1.79 - pCookie,
1.80 - pContext->GetIdentity(),
1.81 - myself->GetIdentity(),
1.82 - NULL);
1.83 -}
1.84 -
1.85 -cell_t SetClientPrefCookie(IPluginContext *pContext, const cell_t *params)
1.86 -{
1.87 - int client = params[1];
1.88 -
1.89 - if ((client < 1) || (client > playerhelpers->GetMaxClients()))
1.90 - {
1.91 - return pContext->ThrowNativeError("Client index %d is invalid", client);
1.92 - }
1.93 -
1.94 - Handle_t hndl = static_cast<Handle_t>(params[2]);
1.95 - HandleError err;
1.96 - HandleSecurity sec;
1.97 -
1.98 - sec.pOwner = NULL;
1.99 - sec.pIdentity = myself->GetIdentity();
1.100 -
1.101 - Cookie *pCookie;
1.102 -
1.103 - if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
1.104 - != HandleError_None)
1.105 - {
1.106 - return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
1.107 - }
1.108 -
1.109 - char *value;
1.110 - pContext->LocalToString(params[3], &value);
1.111 -
1.112 - return g_CookieManager.SetCookieValue(pCookie, client, value);
1.113 -}
1.114 -
1.115 -cell_t GetClientPrefCookie(IPluginContext *pContext, const cell_t *params)
1.116 -{
1.117 - int client = params[1];
1.118 -
1.119 - if ((client < 1) || (client > playerhelpers->GetMaxClients()))
1.120 - {
1.121 - return pContext->ThrowNativeError("Client index %d is invalid", client);
1.122 - }
1.123 -
1.124 - Handle_t hndl = static_cast<Handle_t>(params[2]);
1.125 - HandleError err;
1.126 - HandleSecurity sec;
1.127 -
1.128 - sec.pOwner = NULL;
1.129 - sec.pIdentity = myself->GetIdentity();
1.130 -
1.131 - Cookie *pCookie;
1.132 -
1.133 - if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
1.134 - != HandleError_None)
1.135 - {
1.136 - return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
1.137 - }
1.138 -
1.139 - char *value = NULL;
1.140 -
1.141 - g_CookieManager.GetCookieValue(pCookie, client, &value);
1.142 -
1.143 - pContext->StringToLocal(params[3], params[4], value);
1.144 -
1.145 - return 1;
1.146 -}
1.147 -
1.148 -cell_t AreClientCookiesCached(IPluginContext *pContext, const cell_t *params)
1.149 -{
1.150 - int client = params[1];
1.151 -
1.152 - if ((client < 1) || (client > playerhelpers->GetMaxClients()))
1.153 - {
1.154 - return pContext->ThrowNativeError("Client index %d is invalid", client);
1.155 - }
1.156 -
1.157 - return g_CookieManager.AreClientCookiesCached(client);
1.158 -}
1.159 -
1.160 -cell_t GetCookieAccess(IPluginContext *pContext, const cell_t *params)
1.161 -{
1.162 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.163 - HandleError err;
1.164 - HandleSecurity sec;
1.165 -
1.166 - sec.pOwner = NULL;
1.167 - sec.pIdentity = myself->GetIdentity();
1.168 -
1.169 - Cookie *pCookie;
1.170 -
1.171 - if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
1.172 - != HandleError_None)
1.173 - {
1.174 - return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
1.175 - }
1.176 -
1.177 - return pCookie->access;
1.178 -}
1.179 -
1.180 -static cell_t GetCookieIterator(IPluginContext *pContext, const cell_t *params)
1.181 -{
1.182 - SourceHook::List<Cookie *>::iterator *iter = new SourceHook::List<Cookie *>::iterator;
1.183 - *iter = g_CookieManager.cookieList.begin();
1.184 -
1.185 - Handle_t hndl = handlesys->CreateHandle(g_CookieIterator, iter, pContext->GetIdentity(), myself->GetIdentity(), NULL);
1.186 - if (hndl == BAD_HANDLE)
1.187 - {
1.188 - delete iter;
1.189 - }
1.190 -
1.191 - return hndl;
1.192 -}
1.193 -
1.194 -static cell_t ReadCookieIterator(IPluginContext *pContext, const cell_t *params)
1.195 -{
1.196 - SourceHook::List<Cookie *>::iterator *iter;
1.197 -
1.198 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.199 - HandleError err;
1.200 - HandleSecurity sec;
1.201 -
1.202 - sec.pOwner = NULL;
1.203 - sec.pIdentity = myself->GetIdentity();
1.204 -
1.205 - if ((err = handlesys->ReadHandle(hndl, g_CookieIterator, &sec, (void **)&iter))
1.206 - != HandleError_None)
1.207 - {
1.208 - return pContext->ThrowNativeError("Invalid Cookie iterator handle %x (error %d)", hndl, err);
1.209 - }
1.210 -
1.211 - if (*iter == g_CookieManager.cookieList.end())
1.212 - {
1.213 - return 0;
1.214 - }
1.215 -
1.216 - Cookie *pCookie = (Cookie *)**iter;
1.217 -
1.218 - (*iter)++;
1.219 -
1.220 - pContext->StringToLocalUTF8(params[2], params[3], pCookie->name, NULL);
1.221 - pContext->StringToLocalUTF8(params[5], params[6], pCookie->description, NULL);
1.222 -
1.223 - cell_t *addr;
1.224 - pContext->LocalToPhysAddr(params[4], &addr);
1.225 - *addr = pCookie->access;
1.226 -
1.227 - return 1;
1.228 -}
1.229 -
1.230 -cell_t ShowSettingsMenu(IPluginContext *pContext, const cell_t *params)
1.231 -{
1.232 - char message[256];
1.233 - Translate(message, sizeof(message), "%T:", 2, NULL, "Client Settings", ¶ms[1]);
1.234 -
1.235 - g_CookieManager.clientMenu->SetDefaultTitle(message);
1.236 - g_CookieManager.clientMenu->Display(params[1], 0, NULL);
1.237 -
1.238 - return 0;
1.239 -}
1.240 -
1.241 -cell_t AddSettingsMenuItem(IPluginContext *pContext, const cell_t *params)
1.242 -{
1.243 - char *display;
1.244 - pContext->LocalToString(params[3], &display);
1.245 -
1.246 - /* Register a callback */
1.247 - ItemHandler *pItem = new ItemHandler;
1.248 - pItem->isAutoMenu = false;
1.249 - pItem->forward = forwards->CreateForwardEx(NULL, ET_Ignore, 5, NULL, Param_Cell, Param_Cell, Param_Cell, Param_String, Param_Cell);
1.250 -
1.251 - pItem->forward->AddFunction(pContext, static_cast<funcid_t>(params[1]));
1.252 -
1.253 - char info[20];
1.254 - AutoMenuData *data = new AutoMenuData;
1.255 - data->datavalue = params[2];
1.256 - data->handler = pItem;
1.257 - UTIL_Format(info, sizeof(info), "%x", data);
1.258 -
1.259 - ItemDrawInfo draw(display, 0);
1.260 -
1.261 - g_CookieManager.clientMenu->AppendItem(info, draw);
1.262 -
1.263 - /* Track this in case the plugin unloads */
1.264 -
1.265 - IPlugin *pPlugin = plsys->FindPluginByContext(pContext->GetContext());
1.266 - SourceHook::List<char *> *pList = NULL;
1.267 -
1.268 - if (!pPlugin->GetProperty("SettingsMenuItems", (void **)&pList, false) || !pList)
1.269 - {
1.270 - pList = new SourceHook::List<char *>;
1.271 - pPlugin->SetProperty("SettingsMenuItems", pList);
1.272 - }
1.273 -
1.274 - char *copyarray = new char[strlen(display)+1];
1.275 - UTIL_Format(copyarray, strlen(display)+1, "%s", display);
1.276 -
1.277 - pList->push_back(copyarray);
1.278 -
1.279 - return 0;
1.280 -}
1.281 -
1.282 -cell_t AddSettingsPrefabMenuItem(IPluginContext *pContext, const cell_t *params)
1.283 -{
1.284 - Handle_t hndl = static_cast<Handle_t>(params[1]);
1.285 - HandleError err;
1.286 - HandleSecurity sec;
1.287 -
1.288 - sec.pOwner = NULL;
1.289 - sec.pIdentity = myself->GetIdentity();
1.290 -
1.291 - Cookie *pCookie;
1.292 -
1.293 - if ((err = handlesys->ReadHandle(hndl, g_CookieType, &sec, (void **)&pCookie))
1.294 - != HandleError_None)
1.295 - {
1.296 - return pContext->ThrowNativeError("Invalid Cookie handle %x (error %d)", hndl, err);
1.297 - }
1.298 -
1.299 - /* Register a callback */
1.300 - ItemHandler *pItem = new ItemHandler;
1.301 - pItem->isAutoMenu = true;
1.302 - pItem->autoMenuType = (CookieMenu)params[2];
1.303 -
1.304 -
1.305 - /* User passed a function id for a callback */
1.306 - if (params[4] != -1)
1.307 - {
1.308 - pItem->forward = forwards->CreateForwardEx(NULL, ET_Ignore, 5, NULL, Param_Cell, Param_Cell, Param_Cell, Param_String, Param_Cell);
1.309 - pItem->forward->AddFunction(pContext, static_cast<funcid_t>(params[4]));
1.310 - }
1.311 - else
1.312 - {
1.313 - pItem->forward = NULL;
1.314 - }
1.315 -
1.316 - char *display;
1.317 - pContext->LocalToString(params[3], &display);
1.318 -
1.319 - ItemDrawInfo draw(display, 0);
1.320 -
1.321 - char info[20];
1.322 - AutoMenuData *data = new AutoMenuData;
1.323 - data->datavalue = params[5];
1.324 - data->pCookie = pCookie;
1.325 - data->type = (CookieMenu)params[2];
1.326 - data->handler = pItem;
1.327 - UTIL_Format(info, sizeof(info), "%x", data);
1.328 -
1.329 - g_CookieManager.clientMenu->AppendItem(info, draw);
1.330 -
1.331 - /* Track this in case the plugin unloads */
1.332 -
1.333 - IPlugin *pPlugin = plsys->FindPluginByContext(pContext->GetContext());
1.334 - SourceHook::List<char *> *pList = NULL;
1.335 -
1.336 - if (!pPlugin->GetProperty("SettingsMenuItems", (void **)&pList, false) || !pList)
1.337 - {
1.338 - pList = new SourceHook::List<char *>;
1.339 - pPlugin->SetProperty("SettingsMenuItems", pList);
1.340 - }
1.341 -
1.342 - char *copyarray = new char[strlen(display)+1];
1.343 - UTIL_Format(copyarray, strlen(display)+1, "%s", display);
1.344 -
1.345 - pList->push_back(copyarray);
1.346 -
1.347 - return 0;
1.348 -}
1.349 -
1.350 -sp_nativeinfo_t g_ClientPrefNatives[] =
1.351 -{
1.352 - {"RegClientCookie", RegClientPrefCookie},
1.353 - {"FindClientCookie", FindClientPrefCookie},
1.354 - {"SetClientCookie", SetClientPrefCookie},
1.355 - {"GetClientCookie", GetClientPrefCookie},
1.356 - {"AreClientCookiesCached", AreClientCookiesCached},
1.357 - {"GetCookieAccess", GetCookieAccess},
1.358 - {"ReadCookieIterator", ReadCookieIterator},
1.359 - {"GetCookieIterator", GetCookieIterator},
1.360 - {"ShowCookieMenu", ShowSettingsMenu},
1.361 - {"SetCookieMenuItem", AddSettingsMenuItem},
1.362 - {"SetCookiePrefabMenu", AddSettingsPrefabMenuItem},
1.363 - {NULL, NULL}
1.364 -};