-
Notifications
You must be signed in to change notification settings - Fork 24
/
extension.cpp
232 lines (203 loc) · 7.16 KB
/
extension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "extension.h"
#include "CDetour/detours.h"
#include <iplayerinfo.h>
#define GAMEDATA_FILE "defibfix"
CDetour *Detour_GetPlayerByCharacter = NULL;
CDetour *Detour_DefibrillatorOnStartAction = NULL;
CDetour *Detour_DefibrillatorOnActionComplete = NULL;
CDetour *Detour_CSurvivorDeathModel__Create = NULL;
tCBaseEntity__SetAbsOrigin CBaseEntity__SetAbsOrigin;
Vector g_SurvivorDeathPos[L4D_MAX_PLAYERS+1];
IGameConfig *g_pGameConf = NULL;
IServerGameEnts *gameents = NULL;
DefibFix g_DefibFix; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_DefibFix);
#define TCF_DefibrillatorIDLE 0
#define TCF_DefibrillatorOnStartAction 1
#define TCF_DefibrillatorOnActionComplete 2
BYTE TCF = TCF_DefibrillatorIDLE;
Vector g_lastedict_pos;
DETOUR_DECL_STATIC1(GetPlayerByCharacter, void *, int, charaster)
{
if (TCF == TCF_DefibrillatorOnStartAction || TCF == TCF_DefibrillatorOnActionComplete)
{
TCF = TCF_DefibrillatorIDLE;
float fMinDistance = 50.0;
int iAnyDeadSurvIndex = -1;
int iMinDistPlayerIndex = -1;
const char* pMinDistPlayerName = NULL;
const char* pAnyDeadSurvPlayerName = NULL;
for (int i = 1; i <= L4D_MAX_PLAYERS; i++)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(i);
if (player && player->IsInGame())
{
IPlayerInfo* info = player->GetPlayerInfo();
if(info)
{
if(info->GetTeamIndex() == L4D_TEAM_SURVIVOR && (info->IsDead() || info->IsObserver()))
{
const char* pName = info->GetName();
iAnyDeadSurvIndex = i;
pAnyDeadSurvPlayerName = pName;
float tdist = g_lastedict_pos.DistTo(g_SurvivorDeathPos[i]);
if (tdist < fMinDistance)
{
fMinDistance = tdist;
iMinDistPlayerIndex = i;
pMinDistPlayerName = pName;
}
}
}
}
}
CBaseEntity* result = NULL;
if (iMinDistPlayerIndex != -1)
{
g_pSM->LogMessage(myself,"Found player (%s)%d, distance %f .", pMinDistPlayerName, iMinDistPlayerIndex, fMinDistance);
result = gamehelpers->ReferenceToEntity(iMinDistPlayerIndex);
}
if (!result && iAnyDeadSurvIndex != -1)
{
g_pSM->LogMessage(myself,"Found player (%s)%d, distance unknown.", pAnyDeadSurvPlayerName, iAnyDeadSurvIndex);
result = gamehelpers->ReferenceToEntity(iAnyDeadSurvIndex);
}
if (result)
{
return result;
}
g_pSM->LogMessage(myself,"Player NOT FOUND. Return null.");
return 0;
}
return DETOUR_STATIC_CALL(GetPlayerByCharacter)(charaster);
}
DETOUR_DECL_STATIC1(CSurvivorDeathModel__Create, CBaseEntity *, CBasePlayer*, bplayer)
{
edict_t *pEdict=gameents->BaseEntityToEdict((CBaseEntity*)bplayer);
int client=gamehelpers->IndexOfEdict(pEdict);
CBaseEntity * result = DETOUR_STATIC_CALL(CSurvivorDeathModel__Create)(bplayer);
if (client > 0)
{
Vector PlayerOrigin=pEdict->GetCollideable()->GetCollisionOrigin();
CBaseEntity__SetAbsOrigin(result,&PlayerOrigin);
g_SurvivorDeathPos[client] = PlayerOrigin;
g_pSM->LogMessage(myself,"Save player(%d) pos: %f %f %f",client,PlayerOrigin[0],PlayerOrigin[1],PlayerOrigin[2]);
}
return result;
}
DETOUR_DECL_MEMBER4(DefibrillatorOnStartAction, void *, int,reserved, void*,player, void*,entity, int,reserved2)
{
edict_t *edict=gameents->BaseEntityToEdict((CBaseEntity*)entity);
if (edict)
{
int iAlive = 1;
for (int i=1; i<=L4D_MAX_PLAYERS; i++)
{
IGamePlayer *player = playerhelpers->GetGamePlayer(i);
if (player && player->IsInGame())
{
IPlayerInfo* info = player->GetPlayerInfo();
if(info)
{
if(info->GetTeamIndex() == L4D_TEAM_SURVIVOR && (info->IsDead() || info->IsObserver()))
{
iAlive = 0;
break;
}
}
}
}
if (iAlive == 1)
{
engine->RemoveEdict(edict);
g_pSM->LogMessage(myself,"DefibrillatorOnStart: All players is alive! Dead body removed.");
}
else
{
g_lastedict_pos=edict->GetCollideable()->GetCollisionOrigin();
g_pSM->LogMessage(myself,"DefibrillatorOnStart pos: %f %f %f",g_lastedict_pos[0],g_lastedict_pos[1],g_lastedict_pos[2]);
TCF = TCF_DefibrillatorOnStartAction;
}
}
return DETOUR_MEMBER_CALL(DefibrillatorOnStartAction)(reserved,player,entity,reserved2);
}
DETOUR_DECL_MEMBER2(DefibrillatorOnActionComplete, void *, void*, player, void*, entity)
{
edict_t *edict=gameents->BaseEntityToEdict((CBaseEntity*)entity);
if (edict)
{
g_lastedict_pos=edict->GetCollideable()->GetCollisionOrigin();
g_pSM->LogMessage(myself,"DefibrillatorComplete pos: %f %f %f",g_lastedict_pos[0],g_lastedict_pos[1],g_lastedict_pos[2]);
TCF = TCF_DefibrillatorOnActionComplete;
}
return DETOUR_MEMBER_CALL(DefibrillatorOnActionComplete)(player,entity);
}
bool DefibFix::SDK_OnMetamodLoad( ISmmAPI *ismm, char *error, size_t maxlength, bool late )
{
size_t maxlen=maxlength;
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
return true;
}
bool DefibFix::SDK_OnLoad( char *error, size_t maxlength, bool late )
{
char conf_error[255];
if (!gameconfs->LoadGameConfigFile(GAMEDATA_FILE, &g_pGameConf, conf_error, sizeof(conf_error))){
snprintf(error, maxlength, "Could not read defibfix.txt: %s", conf_error);
return false;
}
if (!SetupHooks()) {
snprintf(error, maxlength, "Cannot SetupHooks or GetOffset.");
return false;
}
return true;
}
void DefibFix::SDK_OnUnload()
{
DefibFix::RemoveHooks();
gameconfs->CloseGameConfigFile(g_pGameConf);
}
bool DefibFix::SetupHooks()
{
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
g_pGameConf->GetMemSig("CBaseEntity::SetAbsOrigin",(void **)&CBaseEntity__SetAbsOrigin);
Detour_GetPlayerByCharacter = DETOUR_CREATE_STATIC(GetPlayerByCharacter, "GetPlayerByCharacter");
Detour_DefibrillatorOnStartAction = DETOUR_CREATE_MEMBER(DefibrillatorOnStartAction, "DefibrillatorOnStartAction");
Detour_DefibrillatorOnActionComplete = DETOUR_CREATE_MEMBER(DefibrillatorOnActionComplete, "DefibrillatorOnActionComplete");
Detour_CSurvivorDeathModel__Create = DETOUR_CREATE_STATIC(CSurvivorDeathModel__Create, "CSurvivorDeathModel::Create");
if (Detour_GetPlayerByCharacter!=NULL && Detour_DefibrillatorOnStartAction!=NULL
&& Detour_DefibrillatorOnActionComplete!=NULL && Detour_CSurvivorDeathModel__Create!=NULL && CBaseEntity__SetAbsOrigin!=NULL){
Detour_GetPlayerByCharacter->EnableDetour();
Detour_DefibrillatorOnStartAction->EnableDetour();
Detour_DefibrillatorOnActionComplete->EnableDetour();
Detour_CSurvivorDeathModel__Create->EnableDetour();
}else{
RemoveHooks();
g_pSM->LogMessage(myself,"CBaseEntity__SetAbsOrigin = 0x%x", CBaseEntity__SetAbsOrigin);
return false;
}
return true;
}
void DefibFix::RemoveHooks()
{
if (Detour_GetPlayerByCharacter != NULL)
{
Detour_GetPlayerByCharacter->Destroy();
Detour_GetPlayerByCharacter = NULL;
}
if (Detour_DefibrillatorOnStartAction != NULL)
{
Detour_DefibrillatorOnStartAction->Destroy();
Detour_DefibrillatorOnStartAction = NULL;
}
if (Detour_DefibrillatorOnActionComplete != NULL)
{
Detour_DefibrillatorOnActionComplete->Destroy();
Detour_DefibrillatorOnActionComplete = NULL;
}
if (Detour_CSurvivorDeathModel__Create != NULL)
{
Detour_CSurvivorDeathModel__Create->Destroy();
Detour_CSurvivorDeathModel__Create = NULL;
}
}