Skip to content
Permalink
Browse files

Add templated helper class to promote type-safety (#965)

  • Loading branch information...
Headline committed Apr 12, 2019
1 parent dd81d7c commit e2eac382c056bc1d8ee435421afb34c28e4c0988
@@ -34,6 +34,7 @@
#include "forwards.h"
#include "util_cstrike.h"
#include <server_class.h>
#include <sm_argbuffer.h>

#if SOURCE_ENGINE == SE_CSGO
#include "itemdef-hash.h"
@@ -178,12 +179,8 @@ static cell_t CS_SwitchTeam(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Client index %d is not valid", params[1]);
}

unsigned char vstk[sizeof(CBaseEntity *) + sizeof(int)];
unsigned char *vptr = vstk;
ArgBuffer<CBaseEntity*, int> vstk(pEntity, params[2]);

*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(int *)vptr = params[2];
pWrapper->Execute(vstk, NULL);
#else
if (g_pSDKTools == NULL)
@@ -273,20 +270,10 @@ static cell_t CS_DropWeapon(IPluginContext *pContext, const cell_t *params)
if (params[4] == 1 && g_pCSWeaponDropDetoured)
g_pIgnoreCSWeaponDropDetour = true;

unsigned char vstk[sizeof(CBaseEntity *) * 2 + sizeof(bool) * 2];
unsigned char *vptr = vstk;

// <psychonic> first one is always false. second is true to toss, false to just drop
*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(CBaseEntity **)vptr = pWeapon;
vptr += sizeof(CBaseEntity *);
*(bool *)vptr = false;
vptr += sizeof(bool);
*(bool *)vptr = (params[3]) ? true : false;

pWrapper->Execute(vstk, NULL);
ArgBuffer<CBaseEntity*, CBaseEntity*, bool, bool> vstk(pEntity, pWeapon, false, (params[3]) ? true : false);

pWrapper->Execute(vstk, NULL);
return 1;
}

@@ -333,14 +320,7 @@ static cell_t CS_TerminateRound(IPluginContext *pContext, const cell_t *params)
if (params[3] == 1 && g_pTerminateRoundDetoured)
g_pIgnoreTerminateDetour = true;

unsigned char vstk[sizeof(void *) + sizeof(float)+ sizeof(int)];
unsigned char *vptr = vstk;

*(void **)vptr = gamerules;
vptr += sizeof(void *);
*(float *)vptr = sp_ctof(params[1]);
vptr += sizeof(float);
*(int*)vptr = reason;
ArgBuffer<void*, float, int> vstk(gamerules, sp_ctof(params[1]), reason);

pWrapper->Execute(vstk, NULL);
#elif SOURCE_ENGINE == SE_CSGO && !defined(WIN32)
@@ -368,18 +348,7 @@ static cell_t CS_TerminateRound(IPluginContext *pContext, const cell_t *params)
if (params[3] == 1 && g_pTerminateRoundDetoured)
g_pIgnoreTerminateDetour = true;

unsigned char vstk[sizeof(void *) + sizeof(float) + (sizeof(int)*3)];
unsigned char *vptr = vstk;

*(void **)vptr = gamerules;
vptr += sizeof(void *);
*(float *)vptr = sp_ctof(params[1]);
vptr += sizeof(float);
*(int*)vptr = reason;
vptr += sizeof(int);
*(int*)vptr = 0;
vptr += sizeof(int);
*(int*)vptr = 0;
ArgBuffer<void*, float, int, int, int> vstk(gamerules, sp_ctof(params[1]), reason, 0, 0);

pWrapper->Execute(vstk, NULL);
#else // CSGO Win32
@@ -881,15 +850,9 @@ static cell_t CS_SetClientClanTag(IPluginContext *pContext, const cell_t *params
char *szNewTag;
pContext->LocalToString(params[2], &szNewTag);

unsigned char vstk[sizeof(CBaseEntity *) + sizeof(char *)];
unsigned char *vptr = vstk;

*(CBaseEntity **)vptr = pEntity;
vptr += sizeof(CBaseEntity *);
*(char **)vptr = szNewTag;
ArgBuffer<CBaseEntity*, char*> vstk(pEntity, szNewTag);

pWrapper->Execute(vstk, NULL);

return 1;
#endif
}
@@ -35,6 +35,7 @@
#include <iplayerinfo.h>
#if SOURCE_ENGINE == SE_CSGO
#include "itemdef-hash.h"
#include <sm_argbuffer.h>

ClassnameMap g_mapClassToDefIdx;
ItemIndexMap g_mapDefIdxToClass;
@@ -134,16 +135,9 @@ CEconItemView *GetEconItemView(CBaseEntity *pEntity, int iSlot)
if (team != 2 && team != 3)
return NULL;

CEconItemView *ret;
unsigned char vstk[sizeof(void *) + sizeof(int) * 2];
unsigned char *vptr = vstk;

*(void **)vptr = (void *)((intptr_t)pEntity + thisPtrOffset);
vptr += sizeof(void *);
*(int *)vptr = team;
vptr += sizeof(int);
*(int *)vptr = iSlot;
ArgBuffer<void*, int> vstk(reinterpret_cast<void*>(((intptr_t)pEntity + thisPtrOffset)), iSlot);

CEconItemView *ret = nullptr;
pWrapper->Execute(vstk, &ret);

return ret;
@@ -163,13 +157,9 @@ CCSWeaponData *GetCCSWeaponData(CEconItemView *view)
pWrapper = g_pBinTools->CreateCall(addr, CallConv_ThisCall, &retpass, NULL, 0))
}

unsigned char vstk[sizeof(CEconItemView *)];
unsigned char *vptr = vstk;

*(CEconItemView **)vptr = view;

CCSWeaponData *pWpnData = NULL;
ArgBuffer<CEconItemView*> vstk(view);

CCSWeaponData *pWpnData = nullptr;
pWrapper->Execute(vstk, &pWpnData);

return pWpnData;
@@ -234,14 +224,9 @@ CEconItemDefinition *GetItemDefintionByName(const char *classname)
g_RegNatives.Register(pWrapper);
}

unsigned char vstk[sizeof(void *) + sizeof(const char *)];
unsigned char *vptr = vstk;

*(void **)vptr = pSchema;
vptr += sizeof(void *);
*(const char **)vptr = classname;
ArgBuffer<void*, const char *> vstk(pSchema, classname);

CEconItemDefinition *pItemDef = NULL;
CEconItemDefinition *pItemDef = nullptr;
pWrapper->Execute(vstk, &pItemDef);

return pItemDef;
@@ -390,8 +375,6 @@ ItemDefHashValue *GetHashValueFromWeapon(const char *szWeapon)
#if SOURCE_ENGINE != SE_CSGO
void *GetWeaponInfo(int weaponID)
{
void *info;

static ICallWrapper *pWrapper = NULL;
if (!pWrapper)
{
@@ -407,11 +390,9 @@ void *GetWeaponInfo(int weaponID)
pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
}

unsigned char vstk[sizeof(int)];
unsigned char *vptr = vstk;

*(int *)vptr = weaponID;
ArgBuffer<int> vstk(weaponID);

void *info = nullptr;
pWrapper->Execute(vstk, &info);

return info;
@@ -435,7 +416,6 @@ const char *GetWeaponNameFromClassname(const char *weapon)
const char *GetTranslatedWeaponAlias(const char *weapon)
{
#if SOURCE_ENGINE != SE_CSGO
const char *alias = NULL;

static ICallWrapper *pWrapper = NULL;

@@ -453,12 +433,11 @@ const char *GetTranslatedWeaponAlias(const char *weapon)
pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
}

unsigned char vstk[sizeof(const char *)];
unsigned char *vptr = vstk;

*(const char **)vptr = GetWeaponNameFromClassname(weapon);
ArgBuffer<const char *> vstk(GetWeaponNameFromClassname(weapon));

const char *alias = nullptr;
pWrapper->Execute(vstk, &alias);

return alias;
#else //this should work for both games maybe replace both?
static const char *szAliases[] =
@@ -492,8 +471,6 @@ const char *GetTranslatedWeaponAlias(const char *weapon)
int AliasToWeaponID(const char *weapon)
{
#if SOURCE_ENGINE != SE_CSGO
int weaponID = 0;

static ICallWrapper *pWrapper = NULL;

if (!pWrapper)
@@ -510,11 +487,9 @@ int AliasToWeaponID(const char *weapon)
pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
}

unsigned char vstk[sizeof(const char *)];
unsigned char *vptr = vstk;

*(const char **)vptr = GetWeaponNameFromClassname(weapon);
ArgBuffer<const char *> vstk(GetWeaponNameFromClassname(weapon));

int weaponID = 0;
pWrapper->Execute(vstk, &weaponID);

return weaponID;
@@ -531,10 +506,8 @@ int AliasToWeaponID(const char *weapon)
const char *WeaponIDToAlias(int weaponID)
{
#if SOURCE_ENGINE != SE_CSGO
const char *alias = NULL;

static ICallWrapper *pWrapper = NULL;

if (!pWrapper)
{
REGISTER_ADDR("WeaponIDToAlias", 0,
@@ -549,11 +522,9 @@ const char *WeaponIDToAlias(int weaponID)
pWrapper = g_pBinTools->CreateCall(addr, CallConv_Cdecl, &retpass, pass, 1))
}

unsigned char vstk[sizeof(int)];
unsigned char *vptr = vstk;

*(int *)vptr = weaponID;
ArgBuffer<int> vstk(weaponID);

const char *alias = nullptr;
pWrapper->Execute(vstk, &alias);

return alias;
@@ -32,6 +32,7 @@
#include "extension.h"
#include "variant-t.h"
#include <datamap.h>
#include <sm_argbuffer.h>

ICallWrapper *g_pAcceptInput = NULL;

@@ -79,9 +80,6 @@ static cell_t AcceptEntityInput(IPluginContext *pContext, const cell_t *params)
CBaseEntity *pActivator, *pCaller, *pDest;

char *inputname;
unsigned char vstk[sizeof(void *) + sizeof(const char *) + sizeof(CBaseEntity *)*2 + SIZEOF_VARIANT_T + sizeof(int)];
unsigned char *vptr = vstk;

ENTINDEX_TO_CBASEENTITY(params[1], pDest);
pContext->LocalToString(params[2], &inputname);
if (params[3] == -1)
@@ -97,19 +95,9 @@ static cell_t AcceptEntityInput(IPluginContext *pContext, const cell_t *params)
ENTINDEX_TO_CBASEENTITY(params[4], pCaller);
}

*(void **)vptr = pDest;
vptr += sizeof(void *);
*(const char **)vptr = inputname;
vptr += sizeof(const char *);
*(CBaseEntity **)vptr = pActivator;
vptr += sizeof(CBaseEntity *);
*(CBaseEntity **)vptr = pCaller;
vptr += sizeof(CBaseEntity *);
memcpy(vptr, g_Variant_t, SIZEOF_VARIANT_T);
vptr += SIZEOF_VARIANT_T;
*(int *)vptr = params[5];
ArgBuffer<void*, const char*, CBaseEntity*, CBaseEntity*, decltype(g_Variant_t), int> vstk(pDest, inputname, pActivator, pCaller, g_Variant_t, params[5]);

bool ret;
bool ret = false;
g_pAcceptInput->Execute(vstk, &ret);

_init_variant_t();
@@ -32,6 +32,7 @@
#include "extension.h"
#include "variant-t.h"
#include "output.h"
#include <sm_argbuffer.h>

ICallWrapper *g_pFireOutput = NULL;

@@ -365,9 +366,6 @@ static cell_t FireEntityOutput(IPluginContext *pContext, const cell_t *params)
void *pOutput = NULL;

char *outputname;
unsigned char vstk[sizeof(void *) + sizeof(CBaseEntity *)*2 + SIZEOF_VARIANT_T + sizeof(float)];
unsigned char *vptr = vstk;

ENTINDEX_TO_CBASEENTITY(params[1], pCaller);
pContext->LocalToString(params[2], &outputname);

@@ -382,17 +380,9 @@ static cell_t FireEntityOutput(IPluginContext *pContext, const cell_t *params)
ENTINDEX_TO_CBASEENTITY(params[3], pActivator);
}

*(void **)vptr = pOutput;
vptr += sizeof(void *);
memcpy(vptr, g_Variant_t, SIZEOF_VARIANT_T);
vptr += SIZEOF_VARIANT_T;
*(CBaseEntity **)vptr = pActivator;
vptr += sizeof(CBaseEntity *);
*(CBaseEntity **)vptr = pCaller;
vptr += sizeof(CBaseEntity *);
*(float *)vptr = sp_ctof(params[4]);

g_pFireOutput->Execute(vstk, NULL);
ArgBuffer<void*, decltype(g_Variant_t), CBaseEntity*, CBaseEntity*, float> vstk(pOutput, g_Variant_t, pActivator, pCaller, sp_ctof(params[4]));

g_pFireOutput->Execute(vstk, nullptr);

_init_variant_t();
return 1;
Oops, something went wrong.

0 comments on commit e2eac38

Please sign in to comment.
You can’t perform that action at this time.