-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhl2dm.cpp
More file actions
278 lines (237 loc) · 9.03 KB
/
hl2dm.cpp
File metadata and controls
278 lines (237 loc) · 9.03 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "bitflags.h"
#include "generic.h"
#include <igameevents.h>
#include <smsdk_ext.h>
using namespace SourceMod;
void OnPluginMsgsHelpModeChanged(IConVar*, const char*, float);
SAFE_ENUM(EPluginMessagesHelpMode,
Disabled,
ConnectOnly,
Full
)
SAFE_ENUM(EPlayerPluginMessagesFlag,
PostChecking, // Checking plugin messages other than per connect
EnabledSetting // Whether client had the setting enabled at least once. Used to disable checking from that time.
)
struct SPlayerData
{
CBitFlags<> mFlags;
ITimer* mpConnectCheckTimer;
QueryCvarCookie_t mCurrentQueryCookie = InvalidQueryCvarCookie;
};
class CHL2MPExtension : public SDKExtension, IConCommandBaseAccessor, IClientListener, IGameEventListener2, ITimedEvent
{
bool RegisterConCommandBase(ConCommandBase*) override;
bool SDK_OnMetamodLoad(ISmmAPI*, char*, size_t, bool) override;
void SDK_OnAllLoaded();
void SDK_OnUnload() override;
void OnClientConnected(int) override;
void OnClientDisconnected(int) override;
void OnClientPostAdminCheck(int) override;
ResultType OnTimer(ITimer*, void*) override;
void OnTimerEnd(ITimer*, void*) override {};
void FireGameEvent(IGameEvent*) override;
};
static CHL2MPExtension sHL2MPExtension;
SMEXT_LINK(&sHL2MPExtension);
static ISmmAPI* spMetamod;
static IPhraseCollection* spPhrases;
static IServerPluginHelpers* spPluginHelpers;
static IPluginHelpersCheck* spPluginHelpersCheck;
static IGameEventManager2* spEventManager;
static int sMessageCreateHookId, sCVarQueryFinishedHookId;
static SPlayerData sPlayersData[SM_MAXPLAYERS + 1];
static ConVar sPluginMsgsHelpModeCVar("sm_pluginmessages_help_mode", "2", 0, "Activation mode of plugin messages"
" setting help. If 0, the feature will be completely disabled and clients won't get any help to enable the setting."
" If 1, the help will only happen on connect. If 2, the help will happen on connect and after sending dialogs.",
OnPluginMsgsHelpModeChanged);
static ConVar sPluginMsgsJoinDelayCVar("sm_pluginmessages_join_help_delay", "6.0", 0,
"When 'sm_pluginmessages_help_mode' is enabled (1 or 2), this defines the delay in seconds"
" after which the plugin messages setting will be checked to help clients upon connect", true, 0.0f, true, 10.0f);
static ConVar sPluginMsgsChatUseCVar("sm_pluginmessages_use_chat", "0", 0, "Whether to display plugin messages"
" activation hints to chat (1) or to console (0) under 'sm_pluginmessages_help_mode 2'. Leaving it disabled"
" to use console (0) is useful to prevent the hints from resulting in potential spam to clients, otherwise.");
SH_DECL_HOOK4(IPluginHelpersCheck, CreateMessage, SH_NOATTRIB, 0, bool, const char*, edict_t*, DIALOG_TYPE, KeyValues*);
SH_DECL_HOOK5_void(IServerPluginCallbacks, OnQueryCvarValueFinished, SH_NOATTRIB, 0,
QueryCvarCookie_t, edict_t*, EQueryCvarValueStatus, const char*, const char*);
static void PrintPluginMessagesHint(int client, const char* pCVarName, const char* pToken, bool chat)
{
smutils->SetGlobalTarget(client);
char message[MAX_USER_MSG_DATA];
const void* args[] = { pToken, "PluginMessages_ConsoleHint", pCVarName };
if (spPhrases->FormatString(message, sizeof(message), "\x01[SM] %t. %t: \x04%s 1",
(void**)args, ARRAYSIZE(args), NULL, NULL))
{
if (chat)
{
int userMsg = usermsgs->GetMessageIndex("SayText2"), clients[] = { client };
if (userMsg > -1)
{
bf_write* pData = usermsgs->StartBitBufMessage(userMsg, clients,
1, USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);
if (pData != NULL)
{
pData->WriteByte(0); // From world
pData->WriteByte(true); // Show on console as well
pData->WriteString(message);
usermsgs->EndMessage();
return;
}
}
}
gamehelpers->TextMsg(client, TEXTMSG_DEST_CONSOLE, message);
}
}
static void QueryPluginMessagesValue(int client)
{
sPlayersData[client].mCurrentQueryCookie = spPluginHelpers
->StartQueryCvarValue(gamehelpers->EdictOfIndex(client), "cl_showpluginmessages");
}
static bool OnMessageCreate(const char*, edict_t* pPlayer, DIALOG_TYPE, KeyValues*)
{
int client = gamehelpers->IndexOfEdict(pPlayer);
if (!sPlayersData[client].mFlags.IsAnyBitSet(EPlayerPluginMessagesFlag::PostChecking,
EPlayerPluginMessagesFlag::EnabledSetting))
{
sPlayersData[client].mFlags.SetBit(EPlayerPluginMessagesFlag::PostChecking);
QueryPluginMessagesValue(client);
}
return true;
}
static void OnCVarValueQueryFinished(QueryCvarCookie_t cookie, edict_t* pPlayer,
EQueryCvarValueStatus status, const char* pName, const char* pValue)
{
int client = gamehelpers->IndexOfEdict(pPlayer);
if (sPlayersData[client].mCurrentQueryCookie == cookie)
{
sPlayersData[client].mCurrentQueryCookie = InvalidQueryCvarCookie;
CBitFlags<>& playerFlags = sPlayersData[client].mFlags;
playerFlags.ClearBit(EPlayerPluginMessagesFlag::PostChecking);
if (status == eQueryCvarValueStatus_ValueIntact)
{
if (Q_atoi(pValue) != 0)
{
return playerFlags.SetBit(EPlayerPluginMessagesFlag::EnabledSetting);
}
else if (playerFlags.IsBitSet(EPlayerPluginMessagesFlag::EnabledSetting)) // Are we handling connect check?
{
playerFlags.ClearBit(EPlayerPluginMessagesFlag::EnabledSetting);
return PrintPluginMessagesHint(client, pName, "PluginMessagesOff_Connect", true);
}
PrintPluginMessagesHint(client, pName, "PluginMessageFailed", sPluginMsgsChatUseCVar.GetBool());
}
}
}
static void HookMessageCreate()
{
sMessageCreateHookId = SH_ADD_HOOK_STATICFUNC(IPluginHelpersCheck,
CreateMessage, spPluginHelpersCheck, OnMessageCreate, true);
}
static void HookCVarValueQueryFinished()
{
sCVarQueryFinishedHookId = SH_ADD_HOOK_STATICFUNC(IServerPluginCallbacks, OnQueryCvarValueFinished,
spMetamod->GetVSPInfo(NULL), OnCVarValueQueryFinished, true);
}
void OnPluginMsgsHelpModeChanged(IConVar*, const char*, float oldValue)
{
if (oldValue == EPluginMessagesHelpMode::Disabled)
{
HookCVarValueQueryFinished();
}
else if (sPluginMsgsHelpModeCVar.GetInt() == EPluginMessagesHelpMode::Disabled)
{
SH_REMOVE_HOOK_ID(sCVarQueryFinishedHookId);
}
if (sPluginMsgsHelpModeCVar.GetInt() == EPluginMessagesHelpMode::Full)
{
HookMessageCreate();
}
else if (oldValue == EPluginMessagesHelpMode::Full)
{
SH_REMOVE_HOOK_ID(sMessageCreateHookId);
}
}
bool CHL2MPExtension::RegisterConCommandBase(ConCommandBase* pCommandBase)
{
return META_REGCVAR(pCommandBase);
}
bool CHL2MPExtension::SDK_OnMetamodLoad(ISmmAPI* ismm, char* error, size_t maxlen, bool)
{
spMetamod = ismm;
GET_V_IFACE_CURRENT(GetEngineFactory, g_pCVar, ICvar, CVAR_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, spPluginHelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
GET_V_IFACE_CURRENT(GetServerFactory, spPluginHelpersCheck, IPluginHelpersCheck,
INTERFACEVERSION_PLUGINHELPERSCHECK);
GET_V_IFACE_CURRENT(GetEngineFactory, spEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
return true;
}
void CHL2MPExtension::SDK_OnAllLoaded()
{
ConVar_Register(0, this);
OnPluginMsgsHelpModeChanged(&sPluginMsgsJoinDelayCVar, "", EPluginMessagesHelpMode::Disabled);
spPhrases = translator->CreatePhraseCollection();
spPhrases->AddPhraseFile("hl2dm.ext.phrases");
playerhelpers->AddClientListener(this);
spEventManager->AddListener(this, "round_start", true);
// Late load clients
for (int i = 1; i <= playerhelpers->GetMaxClients(); ++i)
{
OnClientConnected(i);
IGamePlayer* pPlayer = playerhelpers->GetGamePlayer(i);
if (pPlayer->IsAuthorized() && pPlayer->IsInGame())
{
OnClientPostAdminCheck(i);
}
}
}
void CHL2MPExtension::SDK_OnUnload()
{
SH_REMOVE_HOOK_ID(sMessageCreateHookId);
SH_REMOVE_HOOK_ID(sCVarQueryFinishedHookId);
ConVar_Unregister();
spPhrases->Destroy();
playerhelpers->RemoveClientListener(this);
spEventManager->RemoveListener(this);
for (int i = 1; i <= playerhelpers->GetMaxClients(); ++i)
{
OnClientDisconnected(i);
}
}
void CHL2MPExtension::OnClientConnected(int client)
{
// Assume messages are on, which simplifies correct logic (ensures no queries happen until connect check)
sPlayersData[client].mFlags.SetBit(EPlayerPluginMessagesFlag::EnabledSetting);
}
void CHL2MPExtension::OnClientDisconnected(int client)
{
if (sPlayersData[client].mpConnectCheckTimer != NULL)
{
timersys->KillTimer(sPlayersData[client].mpConnectCheckTimer);
}
sPlayersData[client] = {};
sPlayersData[client].mCurrentQueryCookie = InvalidQueryCvarCookie;
}
void CHL2MPExtension::OnClientPostAdminCheck(int client)
{
if (sPluginMsgsHelpModeCVar.GetInt() > EPluginMessagesHelpMode::Disabled)
{
sPlayersData[client].mpConnectCheckTimer = timersys->CreateTimer(this, sPluginMsgsJoinDelayCVar.GetFloat(),
(void*)playerhelpers->GetGamePlayer(client)->GetUserId(), TIMER_FLAG_NO_MAPCHANGE);
}
}
ResultType CHL2MPExtension::OnTimer(ITimer*, void* userId)
{
int client = playerhelpers->GetClientOfUserId((size_t)userId);
if (client > 0)
{
sPlayersData[client].mpConnectCheckTimer = NULL;
QueryPluginMessagesValue(client);
}
return ResultType::Pl_Handled;
}
void CHL2MPExtension::FireGameEvent(IGameEvent*)
{
// Fix up unsynced SM cached timeleft upon 'round_start' events
timersys->NotifyOfGameStart();
timersys->MapTimeLeftChanged();
}