From 9137471b7567d1e338d4a490ec88e007b8cc108a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20P=C5=99ikryl?= Date: Thu, 11 Jun 2026 16:07:46 +0200 Subject: [PATCH] Remove unused Tasks class --- CMakeLists.txt | 2 +- src/listeners/listeners.cpp | 26 ++---- src/listeners/listeners.h | 1 - src/plugin.cpp | 3 - src/schema/CTimer.cpp | 84 ------------------- src/schema/CTimer.h | 76 ----------------- src/tasks.cpp | 160 ------------------------------------ src/tasks.h | 43 ---------- 8 files changed, 6 insertions(+), 389 deletions(-) delete mode 100644 src/schema/CTimer.cpp delete mode 100644 src/schema/CTimer.h delete mode 100644 src/tasks.cpp delete mode 100644 src/tasks.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d447a71..a156007 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ include(makefiles/shared.cmake) include(makefiles/protobuf.cmake) add_subdirectory(vendor/spdlog) -add_subdirectory(vendor/khook) +# add_subdirectory(vendor/khook) file(GLOB_RECURSE SOURCE_FILES src/*.cpp diff --git a/src/listeners/listeners.cpp b/src/listeners/listeners.cpp index 0a67215..95bfadb 100644 --- a/src/listeners/listeners.cpp +++ b/src/listeners/listeners.cpp @@ -5,7 +5,6 @@ #include "listeners.h" #include #include -#include #include class GameSessionConfiguration_t @@ -15,37 +14,22 @@ class GameSessionConfiguration_t namespace RayTracePlugin::Listeners { SourceHooks sourceHooks; - SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool); - SH_DECL_HOOK3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const GameSessionConfiguration_t&, - ISource2WorldSession*, const char*); + SH_DECL_HOOK3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const GameSessionConfiguration_t&, ISource2WorldSession*, const char*); SH_DECL_HOOK2(IGameEventManager2, LoadEventsFromFile, SH_NOATTRIB, 0, int, const char*, bool); int g_iLoadEventsFromFileId = -1; void InitListeners() { - SH_ADD_HOOK(IServerGameDLL, GameFrame, shared::g_pServer, - SH_MEMBER(&sourceHooks,&SourceHooks::Hook_GameFrame), false); - SH_ADD_HOOK(INetworkServerService, StartupServer, shared::g_pNetworkServerService, - SH_MEMBER(&sourceHooks, &SourceHooks::Hook_StartupServer), true); - auto pCGameEventManagerVTable = DynLibUtils::CModule(shared::g_pServer). - GetVirtualTableByName("CGameEventManager").RCast(); - g_iLoadEventsFromFileId = SH_ADD_DVPHOOK(IGameEventManager2, LoadEventsFromFile, pCGameEventManagerVTable, - SH_MEMBER(&sourceHooks, &SourceHooks::Hook_LoadEventsFromFile), false); + SH_ADD_HOOK(INetworkServerService, StartupServer, shared::g_pNetworkServerService, SH_MEMBER(&sourceHooks, &SourceHooks::Hook_StartupServer), true); + auto pCGameEventManagerVTable = DynLibUtils::CModule(shared::g_pServer).GetVirtualTableByName("CGameEventManager").RCast(); + g_iLoadEventsFromFileId = SH_ADD_DVPHOOK(IGameEventManager2, LoadEventsFromFile, pCGameEventManagerVTable, SH_MEMBER(&sourceHooks, &SourceHooks::Hook_LoadEventsFromFile), false); } void DestructListeners() { - SH_REMOVE_HOOK(IServerGameDLL, GameFrame, shared::g_pServer, - SH_MEMBER(&sourceHooks,&SourceHooks::Hook_GameFrame), false); - SH_REMOVE_HOOK(INetworkServerService, StartupServer, shared::g_pNetworkServerService, - SH_MEMBER(&sourceHooks, &SourceHooks::Hook_StartupServer), true); + SH_REMOVE_HOOK(INetworkServerService, StartupServer, shared::g_pNetworkServerService, SH_MEMBER(&sourceHooks, &SourceHooks::Hook_StartupServer), true); SH_REMOVE_HOOK_ID(g_iLoadEventsFromFileId); } - void SourceHooks::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick) - { - Tasks::Tick(simulating); - } - void SourceHooks::Hook_StartupServer(const GameSessionConfiguration_t& config, ISource2WorldSession*, const char*) { diff --git a/src/listeners/listeners.h b/src/listeners/listeners.h index 06f70eb..2e78278 100644 --- a/src/listeners/listeners.h +++ b/src/listeners/listeners.h @@ -14,7 +14,6 @@ namespace RayTracePlugin::Listeners { class SourceHooks { public: - void Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick); void Hook_StartupServer(const GameSessionConfiguration_t& config, ISource2WorldSession*, const char*); int Hook_LoadEventsFromFile(const char* filename, bool bSearchAll); }; diff --git a/src/plugin.cpp b/src/plugin.cpp index 2017782..a8446d1 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -17,7 +17,6 @@ #include "craytraceinterface.h" #include "log.h" #include "raytrace.h" -#include "tasks.h" #define VERSION_STRING SEMVER " @ " GITHUB_SHA #define BUILD_TIMESTAMP __DATE__ " " __TIME__ @@ -67,7 +66,6 @@ namespace RayTracePlugin return false; Log::Init(); - Tasks::Init(); auto gamedata_path = std::string(Paths::GetRootDirectory() + "/gamedata.json"); shared::g_pGameConfig = new CGameConfig(gamedata_path); @@ -99,7 +97,6 @@ namespace RayTracePlugin bool IPlugin::Unload(char* error, size_t maxlen) { Listeners::DestructListeners(); - Tasks::Shutdown(); FP_INFO("<<< Unload() success! >>>"); diff --git a/src/schema/CTimer.cpp b/src/schema/CTimer.cpp deleted file mode 100644 index b49c911..0000000 --- a/src/schema/CTimer.cpp +++ /dev/null @@ -1,84 +0,0 @@ -// -// Created by Michal Přikryl on 31.10.2025. -// Copyright (c) 2025 slynxcz. All rights reserved. -// -#include "CTimer.h" - -#include - -namespace -RayTracePlugin -{ - std::list> g_timers; - - void RunTimers() - { - auto iterator = g_timers.begin(); - - while (iterator != g_timers.end()) - { - auto pTimer = *iterator; - pTimer->Initialize(); - - // Timer execute - if (pTimer->GetLastExecute() + pTimer->GetInterval() <= universal_time && !pTimer->Execute(true)) - iterator = g_timers.erase(iterator); - else - iterator++; - } - } - - void RemoveAllTimers() - { - g_timers.clear(); - } - - void RemoveTimers(uint64 iTimerFlag) - { - auto iterator = g_timers.begin(); - - while (iterator != g_timers.end()) - if ((*iterator)->IsTimerFlagSet(iTimerFlag)) - iterator = g_timers.erase(iterator); - else - iterator++; - } - - std::weak_ptr CTimer::Create(float flInitialInterval, uint64 nTimerFlags, std::function func) - { - auto pTimer = std::make_shared(flInitialInterval, nTimerFlags, func, _timer_constructor_tag{}); - - g_timers.push_back(pTimer); - return pTimer; - } - - bool CTimer::Execute(bool bAutomaticExecute) - { - SetInterval(m_func()); - SetLastExecute(universal_time); - - bool bContinue = GetInterval() >= 0; - - // Only scan the timer list if this isn't an automatic execute (RunTimers() already has the iterator to erase) - if (!bAutomaticExecute && !bContinue) - Cancel(); - - return bContinue; - } - - void CTimer::Cancel() - { - auto iterator = g_timers.begin(); - - while (iterator != g_timers.end()) - { - if (*iterator == shared_from_this()) - { - g_timers.erase(iterator); - break; - } - - iterator++; - } - } -} diff --git a/src/schema/CTimer.h b/src/schema/CTimer.h deleted file mode 100644 index 00b31f3..0000000 --- a/src/schema/CTimer.h +++ /dev/null @@ -1,76 +0,0 @@ -// -// Created by Michal Přikryl on 31.10.2025. -// Copyright (c) 2025 slynxcz. All rights reserved. -// -#pragma once -#include -#include -#include -#include -#include - -#include "tasks.h" - -// clang-format off -#define TIMERFLAG_NONE (0) -#define TIMERFLAG_MAP (1 << 0) // Only valid for this map, cancels on map change -#define TIMERFLAG_ROUND (1 << 1) // Only valid for this round, cancels on new round - -namespace RayTracePlugin { - class CTimerBase - { - protected: - CTimerBase(float flInitialInterval, uint64 nTimerFlags) : - m_flInterval(flInitialInterval), m_nTimerFlags(nTimerFlags) - {} - - void SetInterval(float flInterval) { m_flInterval = flInterval; } - void SetLastExecute(float flLastExecute) { m_flLastExecute = flLastExecute; } - - public: - virtual bool Execute(bool bAutomaticExecute = false) = 0; - virtual void Cancel() = 0; - - float GetInterval() { return m_flInterval; } - float GetLastExecute() { return m_flLastExecute; } - bool IsTimerFlagSet(uint64 iTimerFlag) { return !iTimerFlag || (m_nTimerFlags & iTimerFlag); } - void Initialize() - { - if (m_flLastExecute == -1) - m_flLastExecute = universal_time; - } - - private: - float m_flInterval; - float m_flLastExecute = -1; - uint64 m_nTimerFlags; - }; - - // Timer functions should return the time until next execution, or a negative value like -1.0f to stop - // Having an interval of 0 is fine, in this case it will run on every game frame - class CTimer : public CTimerBase, public std::enable_shared_from_this - { - private: - // Silly workaround to achieve a "private constructor" only Create() can call - struct _timer_constructor_tag - { - explicit _timer_constructor_tag() = default; - }; - - public: - CTimer(float flInitialInterval, uint64 nTimerFlags, std::function func, _timer_constructor_tag) : - CTimerBase(flInitialInterval, nTimerFlags), m_func(func) - {} - - static std::weak_ptr Create(float flInitialInterval, uint64 nTimerFlags, std::function func); - bool Execute(bool bAutomaticExecute) override; - void Cancel() override; - - private: - std::function m_func; - }; - - void RunTimers(); - void RemoveAllTimers(); - void RemoveTimers(uint64 iTimerFlag); -} diff --git a/src/tasks.cpp b/src/tasks.cpp deleted file mode 100644 index 46498c4..0000000 --- a/src/tasks.cpp +++ /dev/null @@ -1,160 +0,0 @@ -// -// Created by Michal Přikryl on 10.07.2025. -// Copyright (c) 2025 slynxcz. All rights reserved. -// -#include "tasks.h" -#include -#include -#include -#include - -namespace RayTracePlugin { - double universal_time = 0.0; - double last_tick_time = 0.0; - double timer_next_think = 0.0; - - namespace { - std::vector once_off_timers; - std::vector repeat_timers; - std::mutex nextFrameMutex; - std::queue > nextFrameQueue; - } - void Tasks::NextFrame(std::function &&task) { - std::lock_guard lock(nextFrameMutex); - nextFrameQueue.emplace(std::move(task)); - } - - Timer::Timer(float interval, double execTime, TimerCallback callback, int flags) - : Interval(interval), ExecTime(execTime), Callback(std::move(callback)), Flags(flags) { - } - - void Tasks::Init() { - universal_time = 0.0; - last_tick_time = 0.0; - timer_next_think = 0.0; - } - - void Tasks::Shutdown() { - for (auto *timer: once_off_timers) - delete timer; - for (auto *timer: repeat_timers) - delete timer; - once_off_timers.clear(); - repeat_timers.clear(); - std::lock_guard lock(nextFrameMutex); - std::queue > empty; - std::swap(nextFrameQueue, empty); - } - - void Tasks::Tick(bool simulating) { - std::queue> localQueue; - { - std::lock_guard lock(nextFrameMutex); - std::swap(localQueue, nextFrameQueue); - } - - while (!localQueue.empty()) { - try { - localQueue.front()(); - } catch (...) { - } - localQueue.pop(); - } - - double now = std::chrono::duration_cast >( - std::chrono::steady_clock::now().time_since_epoch()).count(); - - if (simulating) - universal_time += now - last_tick_time; - else - universal_time += 0.015; - - last_tick_time = now; - - if (universal_time < timer_next_think) - return; - - for (int i = static_cast(once_off_timers.size()) - 1; i >= 0; --i) { - Timer *timer = once_off_timers[i]; - if (universal_time >= timer->ExecTime) { - timer->InExec = true; - try { - timer->Callback(); - } catch (...) { - } - delete timer; - once_off_timers.erase(once_off_timers.begin() + i); - } - } - - for (int i = static_cast(repeat_timers.size()) - 1; i >= 0; --i) { - Timer *timer = repeat_timers[i]; - if (universal_time >= timer->ExecTime) { - timer->InExec = true; - try { - timer->Callback(); - } catch (...) { - } - - if (timer->KillMe) { - delete timer; - repeat_timers.erase(repeat_timers.begin() + i); - continue; - } - - timer->InExec = false; - timer->ExecTime = universal_time + timer->Interval; - } - } - - timer_next_think = universal_time + 0.1; - } - - Timer *Tasks::AddTimer(float interval, TimerCallback callback, int flags) { - Timer *timer = new Timer(interval, universal_time + interval, std::move(callback), flags); - - if (flags & TIMER_FLAG_REPEAT) - repeat_timers.push_back(timer); - else - once_off_timers.push_back(timer); - - return timer; - } - - void Tasks::KillTimer(Timer *timer) { - if (!timer) return; - - auto killFrom = [](std::vector &list, Timer *target) { - auto it = std::remove_if(list.begin(), list.end(), [=](Timer *t) { return t == target; }); - if (it != list.end()) { - delete target; - list.erase(it, list.end()); - } - }; - - if (timer->InExec) { - timer->KillMe = true; - return; - } - - if (timer->Flags & TIMER_FLAG_REPEAT) - killFrom(repeat_timers, timer); - else - killFrom(once_off_timers, timer); - } - - void Tasks::RemoveMapChangeTimers() { - auto removeFrom = [](std::vector& list) { - for (int i = static_cast(list.size()) - 1; i >= 0; --i) { - Timer* t = list[i]; - if (t->Flags & TIMER_FLAG_NO_MAPCHANGE) { - delete t; - list.erase(list.begin() + i); - } - } - }; - - removeFrom(once_off_timers); - removeFrom(repeat_timers); - } -} diff --git a/src/tasks.h b/src/tasks.h deleted file mode 100644 index 0393a97..0000000 --- a/src/tasks.h +++ /dev/null @@ -1,43 +0,0 @@ -// -// Created by Michal Přikryl on 10.07.2025. -// Copyright (c) 2025 slynxcz. All rights reserved. -// -#pragma once - -#include - -namespace RayTracePlugin { - using TimerCallback = std::function; - - enum TimerFlags { - TIMER_FLAG_REPEAT = 1 << 0, - TIMER_FLAG_NO_MAPCHANGE = 1 << 1, - }; - - extern double universal_time; - extern double last_tick_time; - extern double timer_next_think; - - class Timer { - public: - Timer(float interval, double execTime, TimerCallback callback, int flags); - ~Timer() = default; - - float Interval; - double ExecTime; - TimerCallback Callback; - int Flags; - bool InExec = false; - bool KillMe = false; - }; - - namespace Tasks { - void Init(); - void Shutdown(); - void Tick(bool simulating = true); - void NextFrame(std::function &&task); - Timer* AddTimer(float interval, TimerCallback callback, int flags = 0); - void KillTimer(Timer* timer); - void RemoveMapChangeTimers(); - } -}