Fixed Critical hits being permanently disabled (bug 2674, r=dvander)

This commit is contained in:
Matt Woodrow 2009-02-27 10:19:46 +13:00
parent 8541040bb9
commit 407e58604f
5 changed files with 120 additions and 47 deletions

View File

@ -39,6 +39,13 @@ CDetour *calcIsAttackCriticalKnifeDetour = NULL;
IForward *g_critForward = NULL;
enum DetourResult
{
Result_Ignore,
Result_NoCrit,
Result_Crit,
};
int CheckBaseHandle(CBaseHandle &hndl)
{
if (!hndl.IsValid())
@ -72,14 +79,14 @@ int CheckBaseHandle(CBaseHandle &hndl)
return index;
}
DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelper, bool)
DetourResult DetourCallback(CBaseEntity *pEnt)
{
edict_t *pEdict = gameents->BaseEntityToEdict((CBaseEntity *)this);
edict_t *pEdict = gameents->BaseEntityToEdict((CBaseEntity *)pEnt);
if (!pEdict)
{
g_pSM->LogMessage(myself, "Entity Error");
return false;
return Result_Ignore;
}
sm_sendprop_info_t info;
@ -87,18 +94,18 @@ DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelper, bool)
if (!gamehelpers->FindSendPropInfo(pEdict->GetNetworkable()->GetServerClass()->GetName(), "m_hOwnerEntity", &info))
{
g_pSM->LogMessage(myself, "Offset Error");
return false;
return Result_Ignore;
}
if (!g_critForward)
{
g_pSM->LogMessage(myself, "Invalid Forward");
return false;
return Result_Ignore;
}
int returnValue=0;
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)this + info.actual_offset);
CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pEnt + info.actual_offset);
int index = CheckBaseHandle(hndl);
g_critForward->PushCell(index); //Client index
@ -112,20 +119,80 @@ DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelper, bool)
if (result)
{
return !!returnValue;
if (returnValue)
{
return Result_Crit;
}
else
{
return Result_NoCrit;
}
}
else
{
return Result_Ignore;
}
}
DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelperMelee, bool)
{
DetourResult result = DetourCallback((CBaseEntity *)this);
if (result == Result_Ignore)
{
return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelperMelee)();
}
else if (result == Result_NoCrit)
{
return 0;
}
else
{
return 1;
}
}
DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelperKnife, bool)
{
DetourResult result = DetourCallback((CBaseEntity *)this);
if (result == Result_Ignore)
{
return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelperKnife)();
}
else if (result == Result_NoCrit)
{
return 0;
}
else
{
return 1;
}
}
DETOUR_DECL_MEMBER0(CalcIsAttackCriticalHelper, bool)
{
DetourResult result = DetourCallback((CBaseEntity *)this);
if (result == Result_Ignore)
{
return DETOUR_MEMBER_CALL(CalcIsAttackCriticalHelper)();
}
else if (result == Result_NoCrit)
{
return 0;
}
else
{
return 1;
}
}
void InitialiseDetours()
{
calcIsAttackCriticalDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelper, "CalcCritical");
calcIsAttackCriticalMeleeDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelper, "CalcCriticalMelee");
calcIsAttackCriticalKnifeDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelper, "CalcCriticalKnife");
calcIsAttackCriticalMeleeDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelperMelee, "CalcCriticalMelee");
calcIsAttackCriticalKnifeDetour = DETOUR_CREATE_MEMBER(CalcIsAttackCriticalHelperKnife, "CalcCriticalKnife");
bool HookCreated = false;

View File

@ -102,12 +102,16 @@ bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
sharesys->AddNatives(myself, g_TFNatives);
sharesys->RegisterLibrary(myself, "tf2");
plsys->AddPluginsListener(this);
playerhelpers->RegisterCommandTargetProcessor(this);
g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
g_pCVar = icvar;
m_DetoursEnabled = false;
return true;
}
@ -137,16 +141,14 @@ void TF2Tools::SDK_OnUnload()
gameconfs->CloseGameConfigFile(g_pGameConf);
playerhelpers->UnregisterCommandTargetProcessor(this);
forwards->ReleaseForward(g_critForward);
plsys->RemovePluginsListener(this);
RemoveDetours();
forwards->ReleaseForward(g_critForward);
}
void TF2Tools::SDK_OnAllLoaded()
{
SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
InitialiseDetours();
}
bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
@ -187,7 +189,6 @@ void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
g_resourceEntity = FindResourceEntity();
}
bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
{
int max_clients;
@ -283,6 +284,23 @@ bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
return true;
}
void TF2Tools::OnPluginLoaded(IPlugin *plugin)
{
if (!m_DetoursEnabled && g_critForward->GetFunctionCount())
{
InitialiseDetours();
m_DetoursEnabled = true;
}
}
void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
{
if (m_DetoursEnabled && !g_critForward->GetFunctionCount())
{
RemoveDetours();
m_DetoursEnabled = false;
}
}
int FindResourceEntity()
{
return FindEntityByNetClass(-1, "CTFPlayerResource");

View File

@ -50,9 +50,10 @@ class TF2Tools :
public SDKExtension,
public ICommandTargetProcessor,
public IConCommandBaseAccessor,
public IGameEventListener2
public IGameEventListener2,
public IPluginsListener
{
public:
public: //SDKExtension
/**
* @brief This is called after the initial loading sequence has been processed.
*
@ -74,11 +75,6 @@ public:
*/
virtual void SDK_OnAllLoaded();
/**
* @brief Called when the pause state is changed.
*/
//virtual void SDK_OnPauseChange(bool paused);
/**
* @brief this is called when Core wants to know if your extension is working.
*
@ -90,12 +86,16 @@ public:
void NotifyInterfaceDrop(SMInterface *pInterface);
bool QueryInterfaceDrop(SMInterface *pInterface);
public:
public: //ICommandTargetProcessor
bool ProcessCommandTarget(cmd_target_info_t *info);
public: //IConCommandBaseAccessor
bool RegisterConCommandBase(ConCommandBase *pVar);
public: //IGameEventManager
IGameEventManager2 *m_GameEventManager;
void FireGameEvent( IGameEvent *event );
public: //IPluginsListener
void OnPluginLoaded(IPlugin *plugin);
void OnPluginUnloaded(IPlugin *plugin);
public:
#if defined SMEXT_CONF_METAMOD
/**
@ -107,28 +107,9 @@ public:
* @return True to succeed, false to fail.
*/
virtual bool SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlength, bool late);
/**
* @brief Called when Metamod is detaching, after the extension version is called.
* NOTE: By default this is blocked unless sent from SourceMod.
*
* @param error Error buffer.
* @param maxlength Maximum size of error buffer.
* @return True to succeed, false to fail.
*/
//virtual bool SDK_OnMetamodUnload(char *error, size_t maxlength);
/**
* @brief Called when Metamod's pause state is changing.
* NOTE: By default this is blocked unless sent from SourceMod.
*
* @param paused Pause state being set.
* @param error Error buffer.
* @param maxlength Maximum size of error buffer.
* @return True to succeed, false to fail.
*/
//virtual bool SDK_OnMetamodPauseChange(bool paused, char *error, size_t maxlength);
#endif
private:
bool m_DetoursEnabled;
};
enum TFClassType

View File

@ -72,5 +72,6 @@
//#define SMEXT_ENABLE_THREADER
//#define SMEXT_ENABLE_LIBSYS
//#define SMEXT_ENABLE_USERMSGS
#define SMEXT_ENABLE_PLUGINSYS
#endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_

View File

@ -76,6 +76,9 @@
#if defined SMEXT_ENABLE_USERMSGS
#include <IUserMessages.h>
#endif
#if defined SMEXT_ENABLE_PLUGINSYS
#include <IPluginSys.h>
#endif
#if defined SMEXT_CONF_METAMOD
#include <ISmmPlugin.h>
@ -266,6 +269,9 @@ extern ILibrarySys *libsys;
#if defined SMEXT_ENABLE_USERMSGS
extern IUserMessages *usermsgs;
#endif
#if defined SMEXT_ENABLE_PLUGINSYS
extern IPluginManager *plsys;
#endif
#if defined SMEXT_CONF_METAMOD
PLUGIN_GLOBALVARS();