This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mini_vip.cpp
363 lines (298 loc) · 10.7 KB
/
mini_vip.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* vim: set ts=4 sw=4 tw=99 noet :
* ======================================================
* Mini VIP
* Written by Phoenix (˙·٠●Феникс●٠·˙) 2023.
* ======================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*/
#include <stdio.h>
#include "mini_vip.h"
#include "metamod_oslink.h"
#include "utils.hpp"
#include <utlstring.h>
#include <KeyValues.h>
#include "sdk/schemasystem.h"
#include "sdk/CBaseEntity.h"
#include "sdk/CGameRulesProxy.h"
#include "sdk/CBasePlayerPawn.h"
#include "sdk/CCSPlayerController.h"
#include "sdk/CCSPlayer_ItemServices.h"
#include "sdk/CSmokeGrenadeProjectile.h"
#include <map>
MiniVIP g_MiniVIP;
PLUGIN_EXPOSE(MiniVIP, g_MiniVIP);
IVEngineServer2* engine = nullptr;
IGameEventManager2* gameeventmanager = nullptr;
IGameResourceServiceServer* g_pGameResourceService = nullptr;
CGameEntitySystem* g_pGameEntitySystem = nullptr;
CEntitySystem* g_pEntitySystem = nullptr;
CSchemaSystem* g_pCSchemaSystem = nullptr;
CCSGameRules* g_pGameRules = nullptr;
CPlayerSpawnEvent g_PlayerSpawnEvent;
CRoundPreStartEvent g_RoundPreStartEvent;
CEntityListener g_EntityListener;
bool g_bPistolRound;
std::map<uint32, VipPlayer> g_VipPlayers;
class GameSessionConfiguration_t { };
SH_DECL_HOOK3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const GameSessionConfiguration_t&, ISource2WorldSession*, const char*);
SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool);
bool MiniVIP::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer2, SOURCE2ENGINETOSERVER_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, g_pCVar, ICvar, CVAR_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetServerFactory, g_pSource2Server, ISource2Server, SOURCE2SERVER_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, g_pGameResourceService, IGameResourceServiceServer, GAMERESOURCESERVICESERVER_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetFileSystemFactory, g_pFullFileSystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION);
// Get CSchemaSystem
{
HINSTANCE m_hModule = dlmount(WIN_LINUX("schemasystem.dll", "libschemasystem.so"));
g_pCSchemaSystem = reinterpret_cast<CSchemaSystem*>(reinterpret_cast<CreateInterfaceFn>(dlsym(m_hModule, "CreateInterface"))(SCHEMASYSTEM_INTERFACE_VERSION, nullptr));
dlclose(m_hModule);
}
if (!g_MiniVIP.LoadVips(error, maxlen))
{
ConColorMsg(Color(255, 0, 0, 255), "[%s] %s\n", GetLogTag(), error);
return false;
}
SH_ADD_HOOK(INetworkServerService, StartupServer, g_pNetworkServerService, SH_MEMBER(this, &MiniVIP::StartupServer), true);
SH_ADD_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &MiniVIP::GameFrame), true);
gameeventmanager = static_cast<IGameEventManager2*>(CallVFunc<IToolGameEventAPI*, 91>(g_pSource2Server));
ConVar_Register(FCVAR_GAMEDLL);
return true;
}
bool MiniVIP::Unload(char *error, size_t maxlen)
{
SH_REMOVE_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &MiniVIP::GameFrame), true);
SH_REMOVE_HOOK(INetworkServerService, StartupServer, g_pNetworkServerService, SH_MEMBER(this, &MiniVIP::StartupServer), true);
gameeventmanager->RemoveListener(&g_PlayerSpawnEvent);
gameeventmanager->RemoveListener(&g_RoundPreStartEvent);
g_pGameEntitySystem->RemoveListenerEntity(&g_EntityListener);
ConVar_Unregister();
return true;
}
bool MiniVIP::LoadVips(char* error, size_t maxlen)
{
KeyValues* pKVConfig = new KeyValues("MiniVIP");
KeyValues::AutoDelete autoDelete(pKVConfig);
if (!pKVConfig->LoadFromFile(g_pFullFileSystem, "addons/mini_vip/mini_vip.ini"))
{
V_strncpy(error, "Failed to load vip config 'addons/mini_vip/mini_vip.ini'", maxlen);
return false;
}
for (KeyValues* pKey = pKVConfig->GetFirstSubKey(); pKey; pKey = pKey->GetNextKey())
{
uint32 accontId = V_StringToUint32(pKey->GetName(), 0);
if (accontId == 0)
{
Warning("[%s] accontid is 0\n", GetLogTag());
continue;
}
VipPlayer& player = g_VipPlayers[accontId];
player.m_iHealth = pKey->GetInt("health", -1);
player.m_iArmor = pKey->GetInt("armor", -1);
player.m_fGravity = pKey->GetFloat("gravity", 1.f);
player.m_iMoneyMin = pKey->GetInt("money_min", -1);
player.m_iMoneyAdd = pKey->GetInt("money_add", -1);
player.m_bDefuser = pKey->GetBool("defuser", false);
if (const char* pszItems = pKey->GetString("items"))
{
V_SplitString(pszItems, " ", player.m_items);
}
if (!pKey->IsEmpty("smoke_color"))
{
player.m_vSmokeColor = new Vector();
const char* pszSmokeColor = pKey->GetString("smoke_color");
if (strcmp(pszSmokeColor, "random") == 0)
{
player.m_vSmokeColor->Invalidate();
}
else
{
Vector& vSmokeColor = *player.m_vSmokeColor;
if (sscanf(pszSmokeColor, "%f %f %f", &vSmokeColor.x, &vSmokeColor.y, &vSmokeColor.z) != 3)
{
Warning("[%s] %u incorrect smoke_color value is specified (%s), must be r g b or random\n", GetLogTag(), accontId, pszSmokeColor);
delete player.m_vSmokeColor;
}
}
}
}
return true;
}
void MiniVIP::NextFrame(std::function<void()> fn)
{
m_nextFrame.push_back(fn);
}
void MiniVIP::StartupServer(const GameSessionConfiguration_t& config, ISource2WorldSession*, const char*)
{
g_pGameRules = nullptr;
static bool bDone = false;
if (!bDone)
{
g_pGameEntitySystem = *reinterpret_cast<CGameEntitySystem**>(reinterpret_cast<uintptr_t>(g_pGameResourceService) + WIN_LINUX(0x58, 0x50));
g_pEntitySystem = g_pGameEntitySystem;
g_pGameEntitySystem->AddListenerEntity(&g_EntityListener);
gameeventmanager->AddListener(&g_PlayerSpawnEvent, "player_spawn", true);
gameeventmanager->AddListener(&g_RoundPreStartEvent, "round_prestart", true);
bDone = true;
}
}
void MiniVIP::GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
{
if (!g_pGameRules)
{
CCSGameRulesProxy* pGameRulesProxy = static_cast<CCSGameRulesProxy*>(UTIL_FindEntityByClassname(nullptr, "cs_gamerules"));
if (pGameRulesProxy)
{
g_pGameRules = pGameRulesProxy->m_pGameRules();
}
}
while (!m_nextFrame.empty())
{
m_nextFrame.front()();
m_nextFrame.pop_front();
}
}
void CPlayerSpawnEvent::FireGameEvent(IGameEvent* event)
{
if (!g_pGameRules || g_pGameRules->m_bWarmupPeriod())
return;
CBasePlayerController* pPlayerController = static_cast<CBasePlayerController*>(event->GetPlayerController("userid"));
if (!pPlayerController || pPlayerController->m_steamID() == 0) // Ignore bots
return;
g_MiniVIP.NextFrame([hPlayerController = CHandle<CBasePlayerController>(pPlayerController)]()
{
CCSPlayerController* pPlayerController = static_cast<CCSPlayerController*>(hPlayerController.Get());
if (!pPlayerController)
return;
CCSPlayerPawnBase* pPlayerPawn = pPlayerController->m_hPlayerPawn();
if (!pPlayerPawn || pPlayerPawn->m_lifeState() != LIFE_ALIVE)
return;
auto vipPlayer = g_VipPlayers.find(static_cast<uint32>(pPlayerController->m_steamID()));
if (vipPlayer == g_VipPlayers.end() || !engine->IsClientFullyAuthenticated(pPlayerController->m_pEntity->m_EHandle.GetEntryIndex() - 1))
return;
CCSPlayer_ItemServices* pItemServices = static_cast<CCSPlayer_ItemServices*>(pPlayerPawn->m_pItemServices());
VipPlayer& data = vipPlayer->second;
if (!g_bPistolRound)
{
CCSPlayerController_InGameMoneyServices* pMoneyServices = pPlayerController->m_pInGameMoneyServices();
if (data.m_iMoneyAdd != -1)
pMoneyServices->m_iAccount() += data.m_iMoneyAdd;
if (data.m_iMoneyMin != -1)
{
if (pMoneyServices->m_iAccount() < data.m_iMoneyMin)
pMoneyServices->m_iAccount() = data.m_iMoneyMin;
}
for (int i = 0; i < data.m_items.Count(); i++)
{
pItemServices->GiveNamedItem(data.m_items[i]);
}
}
if (data.m_iHealth != -1)
{
pPlayerPawn->m_iMaxHealth() = data.m_iHealth;
pPlayerPawn->m_iHealth() = data.m_iHealth;
}
if (data.m_iArmor != -1)
{
pPlayerPawn->m_ArmorValue() = data.m_iArmor;
pItemServices->m_bHasHelmet() = true;
}
if (data.m_bDefuser && pPlayerPawn->m_iTeamNum() == 3)
{
pItemServices->m_bHasDefuser() = true;
}
if (data.m_fGravity != 1.f)
{
pPlayerPawn->m_flGravityScale() = data.m_fGravity;
}
});
}
void CRoundPreStartEvent::FireGameEvent(IGameEvent* event)
{
if (g_pGameRules)
{
g_bPistolRound = g_pGameRules->m_totalRoundsPlayed() == 0 || (g_pGameRules->m_bSwitchingTeamsAtRoundReset() && g_pGameRules->m_nOvertimePlaying() == 0) || g_pGameRules->m_bGameRestart();
}
}
void CEntityListener::OnEntitySpawned(CEntityInstance* pEntity)
{
CSmokeGrenadeProjectile* pGrenadeProjectile = dynamic_cast<CSmokeGrenadeProjectile*>(pEntity);
if (!pGrenadeProjectile)
return;
g_MiniVIP.NextFrame([hGrenadeProjectile = CHandle<CSmokeGrenadeProjectile>(pGrenadeProjectile)]()
{
CSmokeGrenadeProjectile* pGrenadeProjectile = hGrenadeProjectile;
if (!pGrenadeProjectile)
return;
CCSPlayerPawn* pPlayerPawn = pGrenadeProjectile->m_hThrower();
if (!pPlayerPawn)
return;
CBasePlayerController* pPlayerController = pPlayerPawn->m_hController();
if (!pPlayerController || pPlayerController->m_steamID() == 0)
return;
auto vipPlayer = g_VipPlayers.find(static_cast<uint32>(pPlayerController->m_steamID()));
if (vipPlayer == g_VipPlayers.end())
return;
Vector* pvSmokeColor = vipPlayer->second.m_vSmokeColor;
if (!pvSmokeColor)
return;
pGrenadeProjectile->m_vSmokeColor() = pvSmokeColor->IsValid() ? *pvSmokeColor : Vector(rand() % 255, rand() % 255, rand() % 255);
});
}
CON_COMMAND_F(mini_vip_reload, "reloads list of vip players", FCVAR_NONE)
{
g_VipPlayers.clear();
char szError[256];
if (g_MiniVIP.LoadVips(szError, sizeof(szError)))
{
ConColorMsg({ 0, 255, 0, 255 }, "VIP players has been successfully updated\n");
}
else
{
ConColorMsg({ 255, 0, 0, 255 }, "Reload error: %s\n", szError);
}
}
///////////////////////////////////////
const char* MiniVIP::GetLicense()
{
return "GPL";
}
const char* MiniVIP::GetVersion()
{
return "1.0.0";
}
const char* MiniVIP::GetDate()
{
return __DATE__;
}
const char *MiniVIP::GetLogTag()
{
return "MiniVIP";
}
const char* MiniVIP::GetAuthor()
{
return "Phoenix (˙·٠●Феникс●٠·˙)";
}
const char* MiniVIP::GetDescription()
{
return "Mini VIP system";
}
const char* MiniVIP::GetName()
{
return "Mini VIP";
}
const char* MiniVIP::GetURL()
{
return "https://github.com/komashchenko/MiniVIP";
}