From 60cdbeb2198475e7fbbfbab3763165beb05487e6 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Sat, 25 Jul 2015 13:33:30 +0200 Subject: [PATCH] Cstrike: Add CS_OnGetItemPrice forward to get/alter an item price on purchase --- modules/cstrike/cstrike/CstrikeHacks.cpp | 13 +++++++++++++ modules/cstrike/cstrike/CstrikeHacks.h | 1 + modules/cstrike/cstrike/CstrikeMain.cpp | 6 ++++-- plugins/include/cstrike.inc | 18 +++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/modules/cstrike/cstrike/CstrikeHacks.cpp b/modules/cstrike/cstrike/CstrikeHacks.cpp index 7d1c883b..17911a16 100644 --- a/modules/cstrike/cstrike/CstrikeHacks.cpp +++ b/modules/cstrike/cstrike/CstrikeHacks.cpp @@ -23,6 +23,7 @@ void CtrlDetours_Natives(bool set); int ForwardInternalCommand = -1; int ForwardOnBuy = -1; int ForwardOnBuyAttempt = -1; +int ForwardOnGetItemPrice = -1; int *UseBotArgs; const char **BotArgs; @@ -258,6 +259,18 @@ DETOUR_DECL_MEMBER2(AddAccount, void, int, amount, bool, bTrackChange) // void C { return; } + else if (ForwardOnGetItemPrice != -1) + { + int client = G_HL_TypeConversion.cbase_to_id(this); + + cell price[1]; *price = -amount; + cell preparedCell = MF_PrepareCellArrayA(price, sizeof(price), true); + + if (MF_IsPlayerAlive(client) && MF_ExecuteForward(ForwardOnGetItemPrice, client, CurrentItemId, preparedCell) > 0) + { + amount = -*price; + } + } } DETOUR_MEMBER_CALL(AddAccount)(amount, bTrackChange); diff --git a/modules/cstrike/cstrike/CstrikeHacks.h b/modules/cstrike/cstrike/CstrikeHacks.h index 13404784..64328016 100644 --- a/modules/cstrike/cstrike/CstrikeHacks.h +++ b/modules/cstrike/cstrike/CstrikeHacks.h @@ -34,6 +34,7 @@ extern IGameConfig *CommonConfig; extern int ForwardInternalCommand; extern int ForwardOnBuy; extern int ForwardOnBuyAttempt; +extern int ForwardOnGetItemPrice; typedef edict_t* (*CreateNamedEntityFunc)(string_t iszClassname); typedef void* (*UTIL_FindEntityByStringFunc)(void* pStartEntity, const char *szKeyword, const char *szValue); diff --git a/modules/cstrike/cstrike/CstrikeMain.cpp b/modules/cstrike/cstrike/CstrikeMain.cpp index 8339f60b..e08da9e0 100644 --- a/modules/cstrike/cstrike/CstrikeMain.cpp +++ b/modules/cstrike/cstrike/CstrikeMain.cpp @@ -65,16 +65,18 @@ void OnPluginsLoaded() ForwardInternalCommand = MF_RegisterForward("CS_InternalCommand", ET_STOP, FP_CELL, FP_STRING, FP_DONE); ForwardOnBuy = MF_RegisterForward("CS_OnBuy" , ET_STOP, FP_CELL, FP_CELL, FP_DONE); ForwardOnBuyAttempt = MF_RegisterForward("CS_OnBuyAttempt" , ET_STOP, FP_CELL, FP_CELL, FP_DONE); + ForwardOnGetItemPrice = MF_RegisterForward("CS_OnGetItemPrice" , ET_STOP, FP_CELL, FP_CELL, FP_ARRAY, FP_DONE); // Checking whether such public forwards are used in plugins. // Resetting variable to -1 to avoid running unnecessary code in ClientCommand. if (!UTIL_CheckForPublic("CS_InternalCommand")) { ForwardInternalCommand = -1; } if (!UTIL_CheckForPublic("CS_OnBuy")) { ForwardOnBuy = -1; } if (!UTIL_CheckForPublic("CS_OnBuyAttempt")) { ForwardOnBuyAttempt = -1; } + if (!UTIL_CheckForPublic("CS_OnGetItemPrice")) { ForwardOnGetItemPrice = -1; } // And enable/disable detours when necessary. - ToggleDetour_ClientCommands(ForwardInternalCommand != -1 || ForwardOnBuy != -1 || ForwardOnBuyAttempt != -1); - ToggleDetour_BuyCommands(ForwardOnBuy != -1); + ToggleDetour_ClientCommands(ForwardInternalCommand != -1 || ForwardOnBuy != -1 || ForwardOnBuyAttempt != -1 || ForwardOnGetItemPrice != -1); + ToggleDetour_BuyCommands(ForwardOnBuy != -1 || ForwardOnGetItemPrice != -1); // Search pev/vtable offset automatically. TypeConversion.init(); diff --git a/plugins/include/cstrike.inc b/plugins/include/cstrike.inc index 8c769f6f..fcbeb47b 100755 --- a/plugins/include/cstrike.inc +++ b/plugins/include/cstrike.inc @@ -1104,4 +1104,20 @@ forward CS_OnBuyAttempt(index, item); * @return PLUGIN_CONTINUE to let the buy continue * PLUGIN_HANDLED to block the buy */ -forward CS_OnBuy(index, item); +forward CS_OnBuy(index, item, &price); + +/** + * Called when a client purchased an item and game is about to apply item's price. + * + * @note This is called right after the user received the item and before the + * money is deducted from their cash reserves. + * @note For a list of possible item ids see the CSI_* constants. + * + * @param index Client index + * @param item Item id + * @param price Item price or buffer to store a newly price to + * + * @return PLUGIN_CONTINUE to use the default price + * PLUGIN_HANDLED or higher to use a newly-set price + */ +forward CS_OnGetItemPrice(index, item, &price);