Update CS:GO team score gamedata.
3 * =============================================================================
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 "common_logic.h"
33 #include <IPluginSys.h>
34 #include <IHandleSys.h>
35 #include <IForwardSys.h>
36 #include <ISourceMod.h>
37 #include <am-utility.h>
39 HandleType_t g_GlobalFwdType = 0;
40 HandleType_t g_PrivateFwdType = 0;
42 static bool s_CallStarted = false;
43 static ICallable *s_pCallable = NULL;
44 static IPluginFunction *s_pFunction = NULL;
45 static IForward *s_pForward = NULL;
47 class ForwardNativeHelpers :
49 public IHandleTypeDispatch
52 void OnSourceModAllInitialized()
56 /* Set GlobalFwd handle access security */
57 handlesys->InitAccessDefaults(NULL, &sec);
58 sec.access[HandleAccess_Read] = 0;
59 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
61 /* Create 'GlobalFwd' handle type */
62 g_GlobalFwdType = handlesys->CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
64 /* Private forwards are cloneable */
65 sec.access[HandleAccess_Clone] = 0;
67 /* Create 'PrivateFwd' handle type */
68 g_PrivateFwdType = handlesys->CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
71 void OnSourceModShutdown()
73 handlesys->RemoveType(g_PrivateFwdType, g_pCoreIdent);
74 handlesys->RemoveType(g_GlobalFwdType, g_pCoreIdent);
77 void OnHandleDestroy(HandleType_t type, void *object)
79 IForward *pForward = static_cast<IForward *>(object);
81 forwardsys->ReleaseForward(pForward);
84 bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
86 *pSize = sizeof(IForward*) + (((IForward *)object)->GetFunctionCount() * 12);
89 } g_ForwardNativeHelpers;
92 /* Turn a public index into a function ID */
93 inline funcid_t PublicIndexToFuncId(uint32_t idx)
95 return (idx << 1) | (1 << 0);
98 /* Reset global function/forward call variables */
99 inline void ResetCall()
101 s_CallStarted = false;
107 static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
109 Handle_t hndl = static_cast<Handle_t>(params[1]);
117 pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
119 pPlugin = pluginsys->PluginFromHandle(hndl, &err);
123 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
127 pContext->LocalToString(params[2], &name);
129 /* Get public function index */
130 if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
132 /* Return INVALID_FUNCTION if not found */
136 /* Return function ID */
137 return PublicIndexToFuncId(idx);
140 static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
142 cell_t count = params[0];
144 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
146 if (count - 2 > SP_MAX_EXEC_PARAMS)
148 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
151 pContext->LocalToString(params[1], &name);
154 for (int i = 3; i <= count; i++)
156 pContext->LocalToPhysAddr(params[i], &addr);
157 forwardParams[i - 3] = static_cast<ParamType>(*addr);
160 IForward *pForward = forwardsys->CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
162 return handlesys->CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
165 static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
167 cell_t count = params[0];
168 ParamType forwardParams[SP_MAX_EXEC_PARAMS];
170 if (count - 1 > SP_MAX_EXEC_PARAMS)
172 return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
176 for (int i = 2; i <= count; i++)
178 pContext->LocalToPhysAddr(params[i], &addr);
179 forwardParams[i - 2] = static_cast<ParamType>(*addr);
182 IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
184 return handlesys->CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
187 static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
189 Handle_t hndl = static_cast<Handle_t>(params[1]);
192 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
194 if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
197 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
200 return pForward->GetFunctionCount();
203 static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
205 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
206 Handle_t plHandle = static_cast<Handle_t>(params[2]);
208 IChangeableForward *pForward;
210 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
212 if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
215 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
220 pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
222 pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
226 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
230 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
234 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
237 return pForward->AddFunction(pFunction);
240 static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
242 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
243 Handle_t plHandle = static_cast<Handle_t>(params[2]);
245 IChangeableForward *pForward;
247 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
249 if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
252 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
257 pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
259 pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
263 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
267 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
271 return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
274 return pForward->RemoveFunction(pFunction);
277 static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
279 Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
280 Handle_t plHandle = static_cast<Handle_t>(params[2]);
282 IChangeableForward *pForward;
284 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
286 if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
289 return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
294 pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
296 pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
300 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
304 return pForward->RemoveFunctionsOfPlugin(pPlugin);
307 static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
315 hndl = static_cast<Handle_t>(params[1]);
319 pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
321 pPlugin = pluginsys->PluginFromHandle(hndl, &err);
325 return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
329 s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
333 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
336 s_pCallable = static_cast<ICallable *>(s_pFunction);
338 s_CallStarted = true;
343 static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
348 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
352 hndl = static_cast<Handle_t>(params[1]);
354 if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
357 return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
360 s_pForward = pForward;
362 s_pCallable = static_cast<ICallable *>(pForward);
364 s_CallStarted = true;
369 static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
375 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
378 err = s_pCallable->PushCell(params[1]);
382 s_pCallable->Cancel();
384 return pContext->ThrowNativeErrorEx(err, NULL);
390 static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
397 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
400 pContext->LocalToPhysAddr(params[1], &addr);
402 err = s_pCallable->PushCellByRef(addr);
406 s_pCallable->Cancel();
408 return pContext->ThrowNativeErrorEx(err, NULL);
414 static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
420 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
423 err = s_pCallable->PushFloat(sp_ctof(params[1]));
427 s_pCallable->Cancel();
429 return pContext->ThrowNativeErrorEx(err, NULL);
435 static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
442 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
445 pContext->LocalToPhysAddr(params[1], &addr);
447 err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
451 s_pCallable->Cancel();
453 return pContext->ThrowNativeErrorEx(err, NULL);
459 static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
466 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
469 pContext->LocalToPhysAddr(params[1], &addr);
471 err = s_pCallable->PushArray(addr, params[2]);
475 s_pCallable->Cancel();
477 return pContext->ThrowNativeErrorEx(err, NULL);
483 static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
490 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
493 pContext->LocalToPhysAddr(params[1], &addr);
495 err = s_pCallable->PushArray(addr, params[2], params[3]);
499 s_pCallable->Cancel();
501 return pContext->ThrowNativeErrorEx(err, NULL);
507 static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
514 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
517 pContext->LocalToString(params[1], &value);
519 err = s_pCallable->PushString(value);
523 s_pCallable->Cancel();
525 return pContext->ThrowNativeErrorEx(err, NULL);
531 static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
538 return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
541 pContext->LocalToString(params[1], &value);
543 err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
547 s_pCallable->Cancel();
549 return pContext->ThrowNativeErrorEx(err, NULL);
555 static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
557 int err = SP_ERROR_NOT_RUNNABLE;
562 return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
565 pContext->LocalToPhysAddr(params[1], &result);
569 IPluginFunction *pFunction = s_pFunction;
571 err = pFunction->Execute(result);
572 } else if (s_pForward) {
573 IForward *pForward = s_pForward;
575 err = pForward->Execute(result, NULL);
581 static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
585 return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
588 s_pCallable->Cancel();
594 struct SMFrameActionData
596 SMFrameActionData(Handle_t handle, Handle_t ownerhandle, cell_t data) : handle(handle), ownerhandle(ownerhandle), data(data)
600 Handle_t ownerhandle;
604 static void PawnFrameAction(void *pData)
606 ke::AutoPtr<SMFrameActionData> frame(reinterpret_cast<SMFrameActionData *>(pData));
607 IPlugin *pPlugin = pluginsys->PluginFromHandle(frame->ownerhandle, NULL);
613 IChangeableForward *pForward;
614 HandleSecurity sec(pPlugin->GetIdentity(), g_pCoreIdent);
615 if (handlesys->ReadHandle(frame->handle, g_PrivateFwdType, &sec, (void **)&pForward) != HandleError_None)
620 pForward->PushCell(frame->data);
621 pForward->Execute(NULL);
623 handlesys->FreeHandle(frame->handle, &sec);
626 static cell_t sm_AddFrameAction(IPluginContext *pContext, const cell_t *params)
628 IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext->GetContext());
629 IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[1]);
632 return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
635 IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, ET_Ignore, 1, NULL, Param_Cell);
636 IdentityToken_t *pIdentity = pContext->GetIdentity();
637 Handle_t Handle = handlesys->CreateHandle(g_PrivateFwdType, pForward, pIdentity, g_pCoreIdent, NULL);
638 if (Handle == BAD_HANDLE)
640 forwardsys->ReleaseForward(pForward);
644 pForward->AddFunction(pFunction);
646 SMFrameActionData *pData = new SMFrameActionData(Handle, pPlugin->GetMyHandle(), params[2]);
647 g_pSM->AddFrameAction(PawnFrameAction, pData);
651 REGISTER_NATIVES(functionNatives)
653 {"GetFunctionByName", sm_GetFunctionByName},
654 {"CreateGlobalForward", sm_CreateGlobalForward},
655 {"CreateForward", sm_CreateForward},
656 {"GetForwardFunctionCount", sm_GetForwardFunctionCount},
657 {"AddToForward", sm_AddToForward},
658 {"RemoveFromForward", sm_RemoveFromForward},
659 {"RemoveAllFromForward", sm_RemoveAllFromForward},
660 {"Call_StartFunction", sm_CallStartFunction},
661 {"Call_StartForward", sm_CallStartForward},
662 {"Call_PushCell", sm_CallPushCell},
663 {"Call_PushCellRef", sm_CallPushCellRef},
664 {"Call_PushFloat", sm_CallPushFloat},
665 {"Call_PushFloatRef", sm_CallPushFloatRef},
666 {"Call_PushArray", sm_CallPushArray},
667 {"Call_PushArrayEx", sm_CallPushArrayEx},
668 {"Call_PushString", sm_CallPushString},
669 {"Call_PushStringEx", sm_CallPushStringEx},
670 {"Call_Finish", sm_CallFinish},
671 {"Call_Cancel", sm_CallCancel},
672 {"RequestFrame", sm_AddFrameAction},