Skip to content
This repository was archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
2.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Pisex committed Jan 22, 2024
1 parent 8d6ca4c commit b114249
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 111 deletions.
1 change: 1 addition & 0 deletions AMBuilder
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ for sdk_name in MMSPlugin.sdks:
binary.sources += [
'admin_system.cpp',
os.path.join('sdk', 'schemasystem.cpp'),
os.path.join('sdk', 'ctimer.cpp'),
os.path.join('sdk', 'module.cpp'),
os.path.join('sdk', 'memaddr.cpp')
]
Expand Down
123 changes: 70 additions & 53 deletions admin_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ IMenusApi* g_pMenus;
char g_szCategory[64][64];
char g_szItem[64][64];

Timer g_Timer;
float g_flUniversalTime;
float g_flLastTickedTime;
bool g_bHasTicked;

int g_iTarget[64];
int g_iReason[64];
Expand Down Expand Up @@ -610,11 +612,9 @@ void ShowPlayerList(int iSlot)

if(!pController->m_hPlayerPawn().Get())
continue;

// if(i == iSlot) continue;

// if(g_pAdminCore->GetClientImmunity(iSlot) < g_pAdminCore->GetClientImmunity(i))
// continue;
if(g_pAdminCore->GetClientImmunity(iSlot) < g_pAdminCore->GetClientImmunity(i))
continue;

char sBuff[16], sBuff2[100];
g_SMAPI->Format(sBuff, sizeof(sBuff), "%i", i);
Expand Down Expand Up @@ -649,21 +649,23 @@ void MapMenuHandle(const char* szBack, const char* szFront, int iItem, int iSlot
g_SMAPI->Format(szBuffer, sizeof(szBuffer), g_AdminSystem.Translate("Changing map"), szFront);
ClientPrintAll( "%s", szBuffer);

g_Timer.AddTimer([sCommand, szBack]()
{
new CTimer(5.0, [sCommand]()
{
engine->ServerCommand(sCommand);
}, 5000);
return -1.0f;
});
return;
}

char szBuffer[256];
g_SMAPI->Format(szBuffer, sizeof(szBuffer), g_AdminSystem.Translate("Changing map"), szBack);
ClientPrintAll( "%s", szBuffer);

g_Timer.AddTimer([szBack]()
{
new CTimer(5.0, [szBack]()
{
engine->ChangeLevel(szBack, nullptr);
}, 5000);
return -1.0f;
});
}
else if(iItem == 7) AdminMenu(iSlot);
}
Expand Down Expand Up @@ -701,23 +703,22 @@ void OnServerCommands(const char* szName, int iSlot)

void StartupServer()
{
g_pNetworkGameServer = g_pNetworkServerService->GetIGameServer();
gpGlobals = g_pNetworkGameServer->GetGlobals();
g_bHasTicked = false;
g_pGameEntitySystem = g_pUtils->GetCGameEntitySystem();
g_pEntitySystem = g_pUtils->GetCEntitySystem();

static bool bDone = false;
if (!bDone)
{
g_pUtils->AddChatListenerPre(g_PLID, ChatListener);
g_pUtils->RegCommand(g_PLID, {"mm_ban", "mm_unban", "mm_mute", "mm_unmute", "mm_gag", "mm_ungag", "mm_silence", "mm_unsilence", "mm_status", "mm_kick", "mm_who", "mm_rcon", "mm_noclip", "mm_setteam", "mm_changeteam", "mm_slap", "mm_slay", "mm_map", "mm_reload_admins", "mm_add_admin", "mm_remove_admin"}, {"!admin","!ban","!unban","!mute","!unmute","!gag","!ungag","!silence","!unsilence","!status","!kick","!who","!csay","!hsay","!rcon","!freeze","!unfreeze","!noclip","!setteam","!changeteam","!slap","!slay","!map","!reload_admins","!add_admin","!remove_admin"}, [](int iSlot, const char* szContent){
g_pUtils->RegCommand(g_PLID, {}, {"!admin","!ban","!unban","!mute","!unmute","!gag","!ungag","!silence","!unsilence","!status","!kick","!who","!csay","!hsay","!rcon","!freeze","!unfreeze","!noclip","!setteam","!changeteam","!slap","!slay","!map","!reload_admins","!add_admin","!remove_admin"}, [](int iSlot, const char* szContent){
CCSPlayerController* pPlayerController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(iSlot + 1));
g_AdminSystem.ParseChatCommand(iSlot, szContent+1, pPlayerController);
g_AdminSystem.ParseChatCommand(iSlot, szContent+3, pPlayerController);
return false;
});

g_pUtils->RegCommand(g_PLID, {"mm_reload_infractions"}, {}, [](int iSlot, const char* szContent){
CCSPlayerController* pPlayerController = (CCSPlayerController *)g_pEntitySystem->GetBaseEntity((CEntityIndex)(iSlot + 1));
g_AdminSystem.ParseChatCommand(iSlot, szContent+3, pPlayerController);
char szCommand[256];
g_SMAPI->Format(szCommand, sizeof(szCommand), szContent);
szCommand[V_strlen(szCommand) - 1] = 0;
g_AdminSystem.ParseChatCommand(iSlot, szCommand+1, pPlayerController);
return false;
});

Expand Down Expand Up @@ -953,8 +954,6 @@ bool AdminSystem::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, b
g_pAdminApi = new AdminApi();
g_pAdminCore = g_pAdminApi;

g_Timer.Start();

return true;
}

Expand All @@ -964,8 +963,7 @@ bool AdminSystem::Unload(char *error, size_t maxlen)
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, g_pSource2GameClients, this, &AdminSystem::Hook_ClientConnect, false);

ConVar_Unregister();

g_Timer.Stop();
RemoveTimers();

if (g_pConnection)
g_pConnection->Destroy();
Expand All @@ -989,27 +987,44 @@ bool AdminSystem::Hook_ClientConnect( CPlayerSlot slot, const char *pszName, uin
RETURN_META_VALUE(MRES_IGNORED, true);
}

// void AdminSystem::CreateTimer(std::function<void()> fn, uint64_t time)
// {
// m_Timer.push_back(fn);
// auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// m_TimerTime.push_back(time*1000+millis);
// }

void AdminSystem::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
{
// auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
// while (!m_Timer.empty())
// {
// uint64_t time = m_TimerTime.front();
// if(millis >= time)
// {
// m_Timer.front()();
// m_Timer.pop_front();
// m_TimerTime.pop_front();
// }
// else break;
// }
if (simulating && g_bHasTicked)
{
g_flUniversalTime += gpGlobals->curtime - g_flLastTickedTime;
}
else
{
g_flUniversalTime += gpGlobals->interval_per_tick;
}

g_flLastTickedTime = gpGlobals->curtime;
g_bHasTicked = true;

for (int i = g_timers.Tail(); i != g_timers.InvalidIndex();)
{
auto timer = g_timers[i];

int prevIndex = i;
i = g_timers.Previous(i);

if (timer->m_flLastExecute == -1)
timer->m_flLastExecute = g_flUniversalTime;

// Timer execute
if (timer->m_flLastExecute + timer->m_flInterval <= g_flUniversalTime)
{
if (!timer->Execute())
{
delete timer;
g_timers.Remove(prevIndex);
}
else
{
timer->m_flLastExecute = g_flUniversalTime;
}
}
}

if(g_iLastTime == 0) g_iLastTime = std::time(0);
else if(std::time(0) - g_iLastTime >= 1 && g_pEntitySystem)
Expand Down Expand Up @@ -1824,21 +1839,23 @@ CON_COMMAND_CHAT_FLAGS(map, "change map", ADMFLAG_CHANGEMAP)
g_SMAPI->Format(szBuffer, sizeof(szBuffer), g_AdminSystem.Translate("Changing map"), args[1]);
ClientPrintAll( "%s", szBuffer);

g_Timer.AddTimer([sCommand, szMapName]()
{
new CTimer(5.0, [sCommand]()
{
engine->ServerCommand(sCommand);
}, 5000);
return -1.0f;
});
return;
}

char szBuffer[256];
g_SMAPI->Format(szBuffer, sizeof(szBuffer), g_AdminSystem.Translate("Changing map"), szMapName);
ClientPrintAll( "%s", szBuffer);

g_Timer.AddTimer([szMapName]()
{
new CTimer(5.0, [szMapName]()
{
engine->ChangeLevel(szMapName, nullptr);
}, 5000);
return -1.0f;
});
}

CON_COMMAND_CHAT_FLAGS(add_admin, "add admin", ADMFLAG_ROOT)
Expand Down Expand Up @@ -1963,7 +1980,7 @@ void AdminApi::BanPlayer(int iSlot, int iAdmin, int iTime, const char* szReason)
{
char szQuery[512];
if(iTime == 0) ClientPrintAll(g_AdminSystem.Translate("BanPermanent"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"));
else ClientPrintAll(g_AdminSystem.Translate("Ban"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime);
else ClientPrintAll(g_AdminSystem.Translate("Ban"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime/60);
g_SMAPI->Format(szQuery, sizeof(szQuery), "INSERT INTO `as_bans` (`admin_steamid`, `steamid`, `admin_name`, `name`, `created`, `duration`, `end`, `reason`) VALUES ('%lld', '%lld', '%s', '%s', '%lld', '%i', '%lld', '%s');", iAdmin == -1?0:m_vecPlayers[iAdmin]->GetSteamID(), m_vecPlayers[iSlot]->GetSteamID(), iAdmin == -1?"Console":g_pConnection->Escape(engine->GetClientConVarValue(iAdmin, "name")).c_str(), g_pConnection->Escape(engine->GetClientConVarValue(iSlot, "name")).c_str(), std::time(0), iTime, std::time(0)+iTime, szReason);
g_pConnection->Query(szQuery, [](IMySQLQuery* test){});
engine->DisconnectClient(CPlayerSlot(iSlot), 41);
Expand All @@ -1973,7 +1990,7 @@ void AdminApi::MutePlayer(int iSlot, int iAdmin, int iTime, const char* szReason
{
char szQuery[512];
if(iTime == 0) ClientPrintAll(g_AdminSystem.Translate("MutePermanent"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"));
else ClientPrintAll(g_AdminSystem.Translate("Mute"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime);
else ClientPrintAll(g_AdminSystem.Translate("Mute"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime/60);
g_SMAPI->Format(szQuery, sizeof(szQuery), "INSERT INTO `as_mutes` (`admin_steamid`, `steamid`, `admin_name`, `name`, `created`, `duration`, `end`, `reason`) VALUES ('%lld', '%lld', '%s', '%s', '%lld', '%i', '%lld', '%s');", iAdmin == -1?0:m_vecPlayers[iAdmin]->GetSteamID(), m_vecPlayers[iSlot]->GetSteamID(), iAdmin == -1?"Console":g_pConnection->Escape(engine->GetClientConVarValue(iAdmin, "name")).c_str(), g_pConnection->Escape(engine->GetClientConVarValue(iSlot, "name")).c_str(), std::time(0), iTime, std::time(0)+iTime, szReason);
g_pConnection->Query(szQuery, [](IMySQLQuery* test){});
m_vecPlayers[iSlot]->SetMuted(iTime, std::time(0)+iTime);
Expand All @@ -1983,7 +2000,7 @@ void AdminApi::GagPlayer(int iSlot, int iAdmin, int iTime, const char* szReason)
{
char szQuery[512];
if(iTime == 0) ClientPrintAll(g_AdminSystem.Translate("GagPermanent"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"));
else ClientPrintAll(g_AdminSystem.Translate("Gag"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime);
else ClientPrintAll(g_AdminSystem.Translate("Gag"), iAdmin == -1?"Console":engine->GetClientConVarValue(iAdmin, "name"), engine->GetClientConVarValue(iSlot, "name"), iTime/60);
g_SMAPI->Format(szQuery, sizeof(szQuery), "INSERT INTO `as_gags` (`admin_steamid`, `steamid`, `admin_name`, `name`, `created`, `duration`, `end`, `reason`) VALUES ('%lld', '%lld', '%s', '%s', '%lld', '%i', '%lld', '%s');", iAdmin == -1?0:m_vecPlayers[iAdmin]->GetSteamID(), m_vecPlayers[iSlot]->GetSteamID(), iAdmin == -1?"Console":g_pConnection->Escape(engine->GetClientConVarValue(iAdmin, "name")).c_str(), g_pConnection->Escape(engine->GetClientConVarValue(iSlot, "name")).c_str(), std::time(0), iTime, std::time(0)+iTime, szReason);
g_pConnection->Query(szQuery, [](IMySQLQuery* test){});
m_vecPlayers[iSlot]->SetGagged(iTime*60, std::time(0)+iTime*60);
Expand Down Expand Up @@ -2014,7 +2031,7 @@ const char* AdminSystem::GetLicense()

const char* AdminSystem::GetVersion()
{
return "2.5.2";
return "2.5.3";
}

const char* AdminSystem::GetDate()
Expand Down
59 changes: 1 addition & 58 deletions admin_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "include/mysql_mm.h"
#include "include/menus.h"
#include "include/admin.h"
#include "sdk/ctimer.h"
#include "funchook.h"
#include <map>
#include <ctime>
Expand All @@ -40,64 +41,6 @@ typedef void (*FnChatCommandCallback_t)(int iSlot, const CCommand &args, CCSPlay

extern CUtlMap<uint32, CChatCommand*> g_CommandList;

class Timer {
public:
void AddTimer(std::function<void()> fn, uint64_t timeMilliseconds) {
std::lock_guard<std::mutex> lock(timerMutex);
auto currentTime = std::chrono::steady_clock::now();
timerQueue.push({ currentTime + std::chrono::milliseconds(timeMilliseconds), fn });
if (timerQueue.top().timeToFire == currentTime + std::chrono::milliseconds(timeMilliseconds)) {
timerCV.notify_one(); // Notify the thread if the new timer is the earliest one
}
}

void Start() {
timerThread = std::thread([this]() {
while (true) {
std::unique_lock<std::mutex> lock(timerMutex);
if (timerQueue.empty()) {
timerCV.wait(lock); // Wait if the queue is empty
} else {
auto currentTime = std::chrono::steady_clock::now();
if (timerQueue.top().timeToFire <= currentTime) {
auto fn = timerQueue.top().functionToCall;
timerQueue.pop();
lock.unlock();
fn(); // Execute the function
} else {
timerCV.wait_until(lock, timerQueue.top().timeToFire); // Wait for the nearest timer
}
}
}
});
}

void Stop() {
if (timerThread.joinable()) {
timerThread.join();
}
}

~Timer() {
Stop();
}

private:
struct TimerEvent {
std::chrono::steady_clock::time_point timeToFire;
std::function<void()> functionToCall;

bool operator>(const TimerEvent& other) const {
return timeToFire > other.timeToFire;
}
};

std::priority_queue<TimerEvent, std::vector<TimerEvent>, std::greater<>> timerQueue;
std::mutex timerMutex;
std::condition_variable timerCV;
std::thread timerThread;
};

class CChatCommand
{
public:
Expand Down
27 changes: 27 additions & 0 deletions sdk/ctimer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* =============================================================================
* CS2Fixes
* Copyright (C) 2023 Source2ZE
* =============================================================================
*
* 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 program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "ctimer.h"

CUtlLinkedList<CTimerBase*> g_timers;

void RemoveTimers()
{
g_timers.PurgeAndDeleteElements();
}
Loading

0 comments on commit b114249

Please sign in to comment.