Skip to content

Commit

Permalink
Fix cstrike extension natives/forwards due to update
Browse files Browse the repository at this point in the history
Fix using Q_strcpy instead of Q_strncpy

Fix CS:S build

Fix trying to use signature function for WeaponIDToAlias on linux.
  • Loading branch information
Drifter321 committed Aug 24, 2017
1 parent 79a8bdd commit 494f833
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 336 deletions.
135 changes: 106 additions & 29 deletions extensions/cstrike/forwards.cpp
Expand Up @@ -8,7 +8,9 @@ bool g_pIgnoreTerminateDetour = false;
bool g_pIgnoreCSWeaponDropDetour = false;
bool g_PriceDetoured = false;
bool g_HandleBuyDetoured = false;
#if SOURCE_ENGINE != SE_CSGO
int lastclient = -1;
#endif

IForward *g_pHandleBuyForward = NULL;
IForward *g_pPriceForward = NULL;
Expand All @@ -22,10 +24,77 @@ CDetour *DCSWeaponDrop = NULL;
int weaponNameOffset = -1;

#if SOURCE_ENGINE == SE_CSGO
DETOUR_DECL_MEMBER3(DetourHandleBuy, int, int, iUnknown, const char *, weapon, bool, bRebuy)
DETOUR_DECL_MEMBER3(DetourHandleBuy, int, int, iLoadoutSlot, void *, pWpnDataRef, bool, bRebuy)
{
int client = gamehelpers->EntityToBCompatRef(reinterpret_cast<CBaseEntity *>(this));

CEconItemView *pView = GetEconItemView(this, iLoadoutSlot);

if (!pView)
{
return DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);
}

void *pWpnData = GetCCSWeaponData(pView);

if (!pWpnData)
{
return DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);
}

const char *szClassname = *(const char **)((intptr_t)pWpnData + weaponNameOffset);

char weaponName[128];

if (strstr(szClassname, "knife"))
{
Q_strncpy(weaponName, "knife", sizeof(weaponName));
}
else
{
const char *underscore = strstr(szClassname, "_");
if (underscore)
{
Q_strncpy(weaponName, (const char *)((intptr_t)underscore + 1), sizeof(weaponName));
}
else
{
Q_strncpy(weaponName, szClassname, sizeof(weaponName));
}
}

cell_t result = Pl_Continue;

g_pHandleBuyForward->PushCell(client);
g_pHandleBuyForward->PushString(weaponName);
g_pHandleBuyForward->Execute(&result);

if (result != Pl_Continue)
{
return 0;
}

int originalPrice = 0;

if (g_iPriceOffset != -1)
{
originalPrice = *(int *)((intptr_t)pWpnData + g_iPriceOffset);

int changedPrice = CallPriceForward(client, weaponName, originalPrice);

if (originalPrice != changedPrice)
*(int *)((intptr_t)pWpnData + g_iPriceOffset) = changedPrice;
}

int ret = DETOUR_MEMBER_CALL(DetourHandleBuy)(iLoadoutSlot, pWpnDataRef, bRebuy);

if (g_iPriceOffset != -1)
*(int *)((intptr_t)pWpnData + g_iPriceOffset) = originalPrice;

return ret;
}
#else
DETOUR_DECL_MEMBER1(DetourHandleBuy, int, const char *, weapon)
#endif
{
int client = gamehelpers->EntityToBCompatRef(reinterpret_cast<CBaseEntity *>(this));

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

#if SOURCE_ENGINE == SE_CSGO
int val = DETOUR_MEMBER_CALL(DetourHandleBuy)(iUnknown, weapon, bRebuy);
#else
int val = DETOUR_MEMBER_CALL(DetourHandleBuy)(weapon);
#endif

lastclient = -1;
return val;
}
#endif

#if SOURCE_ENGINE != SE_CSGO
DETOUR_DECL_MEMBER0(DetourWeaponPrice, int)
#elif defined(WIN32)
DETOUR_DECL_MEMBER2(DetourWeaponPrice, int, CEconItemView *, pEconItem, int, iUnknown)
#else
DETOUR_DECL_MEMBER3(DetourWeaponPrice, int, CEconItemView *, pEconItem, int, iUnknown, float, fUnknown)
#endif
{

#if SOURCE_ENGINE != SE_CSGO
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)();
#elif defined(WIN32)
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)(pEconItem, iUnknown);
#else
int price = DETOUR_MEMBER_CALL(DetourWeaponPrice)(pEconItem, iUnknown, fUnknown);
#endif

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

return CallPriceForward(lastclient, weapon_name, price);
}
#endif

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

bool CreateWeaponPriceDetour()
{
#if SOURCE_ENGINE != SE_CSGO
if (weaponNameOffset == -1)
{
if (!g_pGameConf->GetOffset("WeaponName", &weaponNameOffset))
Expand All @@ -188,18 +245,7 @@ bool CreateWeaponPriceDetour()
}
}

#if SOURCE_ENGINE == SE_CSGO
void *pGetWeaponPriceAddress = GetWeaponPriceFunction();

if(!pGetWeaponPriceAddress)
{
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - Disabled OnGetWeaponPrice forward.");
}

DWeaponPrice = DETOUR_CREATE_MEMBER(DetourWeaponPrice, pGetWeaponPriceAddress);
#else
DWeaponPrice = DETOUR_CREATE_MEMBER(DetourWeaponPrice, "GetWeaponPrice");
#endif
if (DWeaponPrice != NULL)
{
if (!CreateHandleBuyDetour())
Expand All @@ -211,7 +257,27 @@ bool CreateWeaponPriceDetour()
g_PriceDetoured = true;
return true;
}
#else
if (g_iPriceOffset == -1)
{
if (!g_pGameConf->GetOffset("WeaponPrice", &g_iPriceOffset))
{
smutils->LogError(myself, "Could not find WeaponPrice offset - Disabled OnGetWeaponPrice forward");
return false;
}
}

if (!CreateHandleBuyDetour())
{
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - HandleCommand_Buy_Internal failed to detour, disabled OnGetWeaponPrice forward.");
return false;
}
else
{
g_PriceDetoured = true;
return true;
}
#endif
g_pSM->LogError(myself, "GetWeaponPrice detour could not be initialized - Disabled OnGetWeaponPrice forward.");

return false;
Expand All @@ -236,6 +302,16 @@ bool CreateHandleBuyDetour()
if (g_HandleBuyDetoured)
return true;

#if SOURCE_ENGINE == SE_CSGO
if (weaponNameOffset == -1)
{
if (!g_pGameConf->GetOffset("WeaponName", &weaponNameOffset))
{
smutils->LogError(myself, "Could not find WeaponName offset - Disabled OnBuyCommand forward");
return false;
}
}
#endif
DHandleBuy = DETOUR_CREATE_MEMBER(DetourHandleBuy, "HandleCommand_Buy_Internal");

if (DHandleBuy != NULL)
Expand Down Expand Up @@ -305,6 +381,7 @@ void RemoveCSWeaponDropDetour()
}
g_pCSWeaponDropDetoured = false;
}

int CallPriceForward(int client, const char *weapon_name, int price)
{
int changedprice = price;
Expand Down
3 changes: 3 additions & 0 deletions extensions/cstrike/forwards.h
Expand Up @@ -14,4 +14,7 @@ extern IForward *g_pHandleBuyForward;
extern IForward *g_pPriceForward;
extern IForward *g_pTerminateRoundForward;
extern IForward *g_pCSWeaponDropForward;
#if SOURCE_ENGINE == SE_CSGO
extern int g_iPriceOffset;
#endif
#endif //_INCLUDE_CSTRIKE_FORWARDS_H_

0 comments on commit 494f833

Please sign in to comment.