Files
sm-plugins/FixVPhysicsCrush/scripting/FixVPhysicsCrush.sp
T

145 lines
4.5 KiB
SourcePawn

#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <dhooks>
#define FVPHYSICS_NO_IMPACT_DMG 0x0400 // don't do impact damage to anything
public Plugin myinfo =
{
name = "FixVPhysicsCrush",
author = "BotoX",
description = "",
version = "0.1",
url = ""
};
Handle g_hAcceptInput;
Handle g_hSetGameFlags;
Handle g_hGetGameFlags;
int g_iPhysicsObjectOffset;
public void OnPluginStart()
{
Handle hGameData = LoadGameConfigFile("FixVPhysicsCrush.games");
if(!hGameData)
SetFailState("Couldn't load FixVPhysicsCrush gamedata.");
int iPointerSize = GameConfGetOffset(hGameData, "PointerSize");
int iVariantSize = GameConfGetOffset(hGameData, "variant_t_size");
if(iPointerSize == -1 || iVariantSize == -1)
{
delete hGameData;
SetFailState("Missing PointerSize / variant_t_size in FixVPhysicsCrush gamedata.");
}
// CPhysicsObject::SetGameFlags(unsigned short)
StartPrepSDKCall(SDKCall_Raw);
if(!PrepSDKCall_SetFromConf(hGameData, SDKConf_Virtual, "SetGameFlags"))
{
delete hGameData;
SetFailState("PrepSDKCall_SetFromConf(hGameData, SDKConf_Virtual, \"SetGameFlags\") failed!");
}
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
g_hSetGameFlags = EndPrepSDKCall();
// CPhysicsObject::GetGameFlags()
StartPrepSDKCall(SDKCall_Raw);
if(!PrepSDKCall_SetFromConf(hGameData, SDKConf_Virtual, "GetGameFlags"))
{
delete hGameData;
SetFailState("PrepSDKCall_SetFromConf(hGameData, SDKConf_Virtual, \"GetGameFlags\") failed!");
}
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
g_hGetGameFlags = EndPrepSDKCall();
delete hGameData;
hGameData = LoadGameConfigFile("sdktools.games");
if(!hGameData)
SetFailState("Couldn't load sdktools gamedata.");
// CBaseEntity::AcceptInput( const char *szInputName, CBaseEntity *pActivator, CBaseEntity *pCaller, variant_t Value, int outputID )
int Offset = GameConfGetOffset(hGameData, "AcceptInput");
delete hGameData;
if(Offset == -1)
SetFailState("Failed to find AcceptInput offset.");
g_hAcceptInput = DHookCreate(Offset, HookType_Entity, ReturnType_Bool, ThisPointer_CBaseEntity, OnAcceptInput);
if(g_hAcceptInput == INVALID_HANDLE)
SetFailState("Failed to DHook AcceptInput.");
DHookAddParam(g_hAcceptInput, HookParamType_CharPtr);
DHookAddParam(g_hAcceptInput, HookParamType_CBaseEntity);
DHookAddParam(g_hAcceptInput, HookParamType_CBaseEntity);
DHookAddParam(g_hAcceptInput, HookParamType_Object, iVariantSize, DHookPass_ByVal|DHookPass_ODTOR|DHookPass_OCTOR|DHookPass_OASSIGNOP); // variant_t: 20 bytes on x86, 24 bytes on x64
DHookAddParam(g_hAcceptInput, HookParamType_Int);
int hCollisionGroup = FindSendPropInfo("CBaseEntity", "m_CollisionGroup");
if (hCollisionGroup == -1)
SetFailState("Couldn't find CBaseEntity::m_CollisionGroup");
// CNetworkVar( int, m_CollisionGroup );
// IPhysicsObject *m_pPhysicsObject; <- pointer, aligned to pointer size (4 on x86, 8 on x64)
g_iPhysicsObjectOffset = hCollisionGroup + 4;
g_iPhysicsObjectOffset = (g_iPhysicsObjectOffset + iPointerSize - 1) & ~(iPointerSize - 1);
// Late load.
int entity = INVALID_ENT_REFERENCE;
while((entity = FindEntityByClassname(entity, "phys_thruster")) != INVALID_ENT_REFERENCE)
{
OnEntityCreated(entity, "phys_thruster");
}
}
public void OnEntityCreated(int entity, const char[] classname)
{
if(StrEqual(classname, "phys_thruster"))
{
DHookEntity(g_hAcceptInput, true, entity);
}
}
public MRESReturn OnAcceptInput(int entity, Handle hReturn, Handle hParams)
{
if(!IsValidEntity(entity))
return MRES_Ignored;
char sCommand[128];
DHookGetParamString(hParams, 1, sCommand, sizeof(sCommand));
if(StrEqual(sCommand, "Activate", false))
{
int attachedObject = GetEntPropEnt(entity, Prop_Data, "m_attachedObject");
if(attachedObject != INVALID_ENT_REFERENCE)
{
Address vphysObj = GetVPhysicsObject(attachedObject);
if(vphysObj != Address_Null)
{
int flags = VPhysicsGetGameFlags(vphysObj);
flags |= FVPHYSICS_NO_IMPACT_DMG;
VPhysicsSetGameFlags(vphysObj, flags);
}
}
}
return MRES_Ignored;
}
int VPhysicsGetGameFlags(Address vphysicsObj)
{
return SDKCall(g_hGetGameFlags, vphysicsObj) & 0xFFFF; // returns unsigned short
}
void VPhysicsSetGameFlags(Address vphysicsObj, int flags)
{
SDKCall(g_hSetGameFlags, vphysicsObj, flags);
}
Address GetVPhysicsObject(int entity)
{
// Read the full IPhysicsObject* (4 bytes on x86, 8 bytes on x64)
return LoadAddressFromAddress(GetEntityAddress(entity) + view_as<Address>(g_iPhysicsObjectOffset));
}