Skip to content

Commit 494f833

Browse files
committed
Fix cstrike extension natives/forwards due to update
Fix using Q_strcpy instead of Q_strncpy Fix CS:S build Fix trying to use signature function for WeaponIDToAlias on linux.
1 parent 79a8bdd commit 494f833

File tree

6 files changed

+523
-336
lines changed

6 files changed

+523
-336
lines changed

extensions/cstrike/forwards.cpp

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ bool g_pIgnoreTerminateDetour = false;
88
bool g_pIgnoreCSWeaponDropDetour = false;
99
bool g_PriceDetoured = false;
1010
bool g_HandleBuyDetoured = false;
11+
#if SOURCE_ENGINE != SE_CSGO
1112
int lastclient = -1;
13+
#endif
1214

1315
IForward *g_pHandleBuyForward = NULL;
1416
IForward *g_pPriceForward = NULL;
@@ -22,10 +24,77 @@ CDetour *DCSWeaponDrop = NULL;
2224
int weaponNameOffset = -1;
2325

2426
#if SOURCE_ENGINE == SE_CSGO
25-
DETOUR_DECL_MEMBER3(DetourHandleBuy, int, int, iUnknown, const char *, weapon, bool, bRebuy)
27+
DETOUR_DECL_MEMBER3(DetourHandleBuy, int, int, iLoadoutSlot, void *, pWpnDataRef, bool, bRebuy)
28+
{
29+
int client = gamehelpers->EntityToBCompatRef(reinterpret_cast<CBaseEntity *>(this));
30+
31+
CEconItemView *pView = GetEconItemView(this, iLoadoutSlot);
32+
33+
if (!pView)
34+
{
35+
return DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);
36+
}
37+
38+
void *pWpnData = GetCCSWeaponData(pView);
39+
40+
if (!pWpnData)
41+
{
42+
return DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);
43+
}
44+
45+
const char *szClassname = *(const char **)((intptr_t)pWpnData + weaponNameOffset);
46+
47+
char weaponName[128];
48+
49+
if (strstr(szClassname, "knife"))
50+
{
51+
Q_strncpy(weaponName, "knife", sizeof(weaponName));
52+
}
53+
else
54+
{
55+
const char *underscore = strstr(szClassname, "_");
56+
if (underscore)
57+
{
58+
Q_strncpy(weaponName, (const char *)((intptr_t)underscore + 1), sizeof(weaponName));
59+
}
60+
else
61+
{
62+
Q_strncpy(weaponName, szClassname, sizeof(weaponName));
63+
}
64+
}
65+
66+
cell_t result = Pl_Continue;
67+
68+
g_pHandleBuyForward->PushCell(client);
69+
g_pHandleBuyForward->PushString(weaponName);
70+
g_pHandleBuyForward->Execute(&result);
71+
72+
if (result != Pl_Continue)
73+
{
74+
return 0;
75+
}
76+
77+
int originalPrice = 0;
78+
79+
if (g_iPriceOffset != -1)
80+
{
81+
originalPrice = *(int *)((intptr_t)pWpnData + g_iPriceOffset);
82+
83+
int changedPrice = CallPriceForward(client, weaponName, originalPrice);
84+
85+
if (originalPrice != changedPrice)
86+
*(int *)((intptr_t)pWpnData + g_iPriceOffset) = changedPrice;
87+
}
88+
89+
int ret = DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);
90+
91+
if (g_iPriceOffset != -1)
92+
*(int *)((intptr_t)pWpnData + g_iPriceOffset) = originalPrice;
93+
94+
return ret;
95+
}
2696
#else
2797
DETOUR_DECL_MEMBER1(DetourHandleBuy, int, const char *, weapon)
28-
#endif
2998
{
3099
int client = gamehelpers->EntityToBCompatRef(reinterpret_cast<CBaseEntity *>(this));
31100

@@ -43,31 +112,17 @@ DETOUR_DECL_MEMBER1(DetourHandleBuy, int, const char *, weapon)
43112
return 0;
44113
}
45114

46-
#if SOURCE_ENGINE == SE_CSGO
47-
int val = DETOUR_MEMBER_CALL(DetourHandleBuy)(iUnknown, weapon, bRebuy);
48-
#else
49115
int val = DETOUR_MEMBER_CALL(DetourHandleBuy)(weapon);
50-
#endif
116+
51117
lastclient = -1;
52118
return val;
53119
}
120+
#endif
54121

55122
#if SOURCE_ENGINE != SE_CSGO
56123
DETOUR_DECL_MEMBER0(DetourWeaponPrice, int)
57-
#elif defined(WIN32)
58-
DETOUR_DECL_MEMBER2(DetourWeaponPrice, int, CEconItemView *, pEconItem, int, iUnknown)
59-
#else
60-
DETOUR_DECL_MEMBER3(DetourWeaponPrice, int, CEconItemView *, pEconItem, int, iUnknown, float, fUnknown)
61-
#endif
62124
{
63-
64-
#if SOURCE_ENGINE != SE_CSGO
65125
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)();
66-
#elif defined(WIN32)
67-
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)(pEconItem, iUnknown);
68-
#else
69-
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)(pEconItem, iUnknown, fUnknown);
70-
#endif
71126

72127
if (lastclient == -1)
73128
return price;
@@ -76,6 +131,7 @@ DETOUR_DECL_MEMBER3(DetourWeaponPrice, int, CEconItemView *, pEconItem, int, iUn
76131

77132
return CallPriceForward(lastclient, weapon_name, price);
78133
}
134+
#endif
79135

80136
#if SOURCE_ENGINE != SE_CSGO || !defined(WIN32)
81137
DETOUR_DECL_MEMBER2(DetourTerminateRound, void, float, delay, int, reason)
@@ -179,6 +235,7 @@ DETOUR_DECL_MEMBER3(DetourCSWeaponDrop, void, CBaseEntity *, weapon, bool, bDrop
179235

180236
bool CreateWeaponPriceDetour()
181237
{
238+
#if SOURCE_ENGINE != SE_CSGO
182239
if (weaponNameOffset == -1)
183240
{
184241
if (!g_pGameConf->GetOffset("WeaponName", &weaponNameOffset))
@@ -188,18 +245,7 @@ bool CreateWeaponPriceDetour()
188245
}
189246
}
190247

191-
#if SOURCE_ENGINE == SE_CSGO
192-
void *pGetWeaponPriceAddress = GetWeaponPriceFunction();
193-
194-
if(!pGetWeaponPriceAddress)
195-
{
196-
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - Disabled OnGetWeaponPrice forward.");
197-
}
198-
199-
DWeaponPrice = DETOUR_CREATE_MEMBER(DetourWeaponPrice, pGetWeaponPriceAddress);
200-
#else
201248
DWeaponPrice = DETOUR_CREATE_MEMBER(DetourWeaponPrice, "GetWeaponPrice");
202-
#endif
203249
if (DWeaponPrice != NULL)
204250
{
205251
if (!CreateHandleBuyDetour())
@@ -211,7 +257,27 @@ bool CreateWeaponPriceDetour()
211257
g_PriceDetoured = true;
212258
return true;
213259
}
260+
#else
261+
if (g_iPriceOffset == -1)
262+
{
263+
if (!g_pGameConf->GetOffset("WeaponPrice", &g_iPriceOffset))
264+
{
265+
smutils->LogError(myself, "Could not find WeaponPrice offset - Disabled OnGetWeaponPrice forward");
266+
return false;
267+
}
268+
}
214269

270+
if (!CreateHandleBuyDetour())
271+
{
272+
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - HandleCommand_Buy_Internal failed to detour, disabled OnGetWeaponPrice forward.");
273+
return false;
274+
}
275+
else
276+
{
277+
g_PriceDetoured = true;
278+
return true;
279+
}
280+
#endif
215281
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - Disabled OnGetWeaponPrice forward.");
216282

217283
return false;
@@ -236,6 +302,16 @@ bool CreateHandleBuyDetour()
236302
if (g_HandleBuyDetoured)
237303
return true;
238304

305+
#if SOURCE_ENGINE == SE_CSGO
306+
if (weaponNameOffset == -1)
307+
{
308+
if (!g_pGameConf->GetOffset("WeaponName", &weaponNameOffset))
309+
{
310+
smutils->LogError(myself, "Could not find WeaponName offset - Disabled OnBuyCommand forward");
311+
return false;
312+
}
313+
}
314+
#endif
239315
DHandleBuy = DETOUR_CREATE_MEMBER(DetourHandleBuy, "HandleCommand_Buy_Internal");
240316

241317
if (DHandleBuy != NULL)
@@ -305,6 +381,7 @@ void RemoveCSWeaponDropDetour()
305381
}
306382
g_pCSWeaponDropDetoured = false;
307383
}
384+
308385
int CallPriceForward(int client, const char *weapon_name, int price)
309386
{
310387
int changedprice = price;

extensions/cstrike/forwards.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ extern IForward *g_pHandleBuyForward;
1414
extern IForward *g_pPriceForward;
1515
extern IForward *g_pTerminateRoundForward;
1616
extern IForward *g_pCSWeaponDropForward;
17+
#if SOURCE_ENGINE == SE_CSGO
18+
extern int g_iPriceOffset;
19+
#endif
1720
#endif //_INCLUDE_CSTRIKE_FORWARDS_H_

0 commit comments

Comments
 (0)