2
0
mirror of https://github.com/rehlds/reapi.git synced 2024-12-29 08:05:36 +03:00

GetBonePosition/GetAttachment: Added index bounds check

This commit is contained in:
s1lentq 2023-07-04 01:48:13 +07:00
parent 5c5478c879
commit 8c670fa40f
3 changed files with 32 additions and 14 deletions

View File

@ -236,7 +236,7 @@ cell AMX_NATIVE_CALL amx_GetBonePosition(AMX *amx, cell *params)
*/ */
cell AMX_NATIVE_CALL amx_GetAttachment(AMX *amx, cell *params) cell AMX_NATIVE_CALL amx_GetAttachment(AMX *amx, cell *params)
{ {
enum args_e { arg_count, arg_index, arg_bone, arg_attachment, arg_angles }; enum args_e { arg_count, arg_index, arg_attachment, arg_origin, arg_angles };
CHECK_ISENTITY(arg_index); CHECK_ISENTITY(arg_index);
@ -251,9 +251,9 @@ cell AMX_NATIVE_CALL amx_GetAttachment(AMX *amx, cell *params)
return FALSE; return FALSE;
} }
Vector *pVecOrigin = (Vector *)getAmxAddr(amx, params[arg_attachment]); Vector *pVecOrigin = (Vector *)getAmxAddr(amx, params[arg_origin]);
Vector *pVecAngles = (PARAMS_COUNT == 4) ? (Vector *)getAmxAddr(amx, params[arg_angles]) : nullptr; Vector *pVecAngles = (PARAMS_COUNT == 4) ? (Vector *)getAmxAddr(amx, params[arg_angles]) : nullptr;
GetAttachment(pEntity, params[arg_bone], pVecOrigin, pVecAngles); GetAttachment(pEntity, params[arg_attachment], pVecOrigin, pVecAngles);
return TRUE; return TRUE;
} }

View File

@ -97,7 +97,7 @@ CBaseEntity *GiveNamedItemInternal(AMX *amx, CBasePlayer *pPlayer, const char *p
return pEntity; return pEntity;
} }
void StudioFrameAdvanceEnt(edict_t *pEdict) void StudioFrameAdvanceEnt(studiohdr_t *pstudiohdr, edict_t *pEdict)
{ {
float flInterval = gpGlobals->time - pEdict->v.animtime; float flInterval = gpGlobals->time - pEdict->v.animtime;
if (flInterval <= 0.001f) { if (flInterval <= 0.001f) {
@ -109,10 +109,8 @@ void StudioFrameAdvanceEnt(edict_t *pEdict)
flInterval = 0.0f; flInterval = 0.0f;
} }
studiohdr_t *pstudiohdr = static_cast<studiohdr_t *>(GET_MODEL_PTR(pEdict)); if (!pstudiohdr)
if (!pstudiohdr) {
return; return;
}
if (pEdict->v.sequence >= pstudiohdr->numseq || pEdict->v.sequence < 0) { if (pEdict->v.sequence >= pstudiohdr->numseq || pEdict->v.sequence < 0) {
return; return;
@ -160,8 +158,18 @@ void GetBonePosition(CBaseEntity *pEntity, int iBone, Vector *pVecOrigin, Vector
Vector vecOrigin, vecAngles; Vector vecOrigin, vecAngles;
edict_t *pEdict = pEntity->edict(); edict_t *pEdict = pEntity->edict();
if (pVecOrigin) *pVecOrigin = Vector(0, 0, 0);
if (pVecAngles) *pVecAngles = Vector(0, 0, 0);
studiohdr_t *pstudiohdr = static_cast<studiohdr_t *>(GET_MODEL_PTR(pEdict));
if (!pstudiohdr)
return;
if (iBone < 0 || iBone >= pstudiohdr->numbones)
return; // invalid bone
// force to update frame // force to update frame
StudioFrameAdvanceEnt(pEdict); StudioFrameAdvanceEnt(pstudiohdr, pEdict);
pEntity->pev->angles.x = -pEntity->pev->angles.x; pEntity->pev->angles.x = -pEntity->pev->angles.x;
GET_BONE_POSITION(pEdict, iBone, vecOrigin, vecAngles); GET_BONE_POSITION(pEdict, iBone, vecOrigin, vecAngles);
@ -180,15 +188,25 @@ void GetBonePosition(CBaseEntity *pEntity, int iBone, Vector *pVecOrigin, Vector
} }
} }
void GetAttachment(CBaseEntity *pEntity, int iBone, Vector *pVecOrigin, Vector *pVecAngles) void GetAttachment(CBaseEntity *pEntity, int iAttachment, Vector *pVecOrigin, Vector *pVecAngles)
{ {
Vector vecOrigin, vecAngles; Vector vecOrigin, vecAngles;
edict_t *pEdict = pEntity->edict(); edict_t *pEdict = pEntity->edict();
// force to update frame if (pVecOrigin) *pVecOrigin = Vector(0, 0, 0);
StudioFrameAdvanceEnt(pEdict); if (pVecAngles) *pVecAngles = Vector(0, 0, 0);
GET_ATTACHMENT(pEdict, iBone, vecOrigin, vecAngles); studiohdr_t *pstudiohdr = static_cast<studiohdr_t *>(GET_MODEL_PTR(pEdict));
if (!pstudiohdr)
return;
if (iAttachment < 0 || iAttachment >= pstudiohdr->numattachments)
return; // invalid attachment
// force to update frame
StudioFrameAdvanceEnt(pstudiohdr, pEdict);
GET_ATTACHMENT(pEdict, iAttachment, vecOrigin, vecAngles);
if (!pEntity->IsPlayer()) { if (!pEntity->IsPlayer()) {
FixupAngles(pEdict, vecOrigin); FixupAngles(pEdict, vecOrigin);

View File

@ -53,8 +53,8 @@ ModelName GetModelAuto(TeamName team);
void UTIL_ServerPrint(const char *fmt, ...); void UTIL_ServerPrint(const char *fmt, ...);
CBaseEntity *GiveNamedItemInternal(AMX *amx, CBasePlayer *pPlayer, const char *pszItemName, const size_t uid = 0); CBaseEntity *GiveNamedItemInternal(AMX *amx, CBasePlayer *pPlayer, const char *pszItemName, const size_t uid = 0);
void GetBonePosition(CBaseEntity *pEntity, int iBone, Vector *vecOrigin, Vector *vecAngles); void GetBonePosition(CBaseEntity *pEntity, int iBone, Vector *pVecOrigin, Vector *pVecAngles);
void GetAttachment(CBaseEntity *pEntity, int iBone, Vector *pVecOrigin, Vector *pVecAngles); void GetAttachment(CBaseEntity *pEntity, int iAttachment, Vector *pVecOrigin, Vector *pVecAngles);
void RemoveOrDropItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, GiveType type); void RemoveOrDropItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, GiveType type);
const char *getATypeStr(AType type); const char *getATypeStr(AType type);