From 5deba33062b823a65e8aee31048dd88f3edc5513 Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Sat, 4 Nov 2023 19:56:42 +0400 Subject: [PATCH] Library: code refactoring for reducing cohesion --- sources/library/application.cpp | 39 ++++++++------ sources/library/application.h | 20 ++++--- sources/library/bvh_tree.cpp | 12 ++--- sources/library/bvh_tree.h | 4 +- sources/library/client_module.cpp | 28 ++++------ sources/library/client_module.h | 16 ++---- sources/library/displaymode_angletracking.cpp | 7 +-- sources/library/displaymode_angletracking.h | 4 +- sources/library/displaymode_entityreport.cpp | 48 ++++++++--------- sources/library/displaymode_entityreport.h | 8 ++- sources/library/displaymode_facereport.cpp | 11 ++-- sources/library/displaymode_facereport.h | 4 +- sources/library/displaymode_full.cpp | 53 ++++++++++--------- sources/library/displaymode_full.h | 4 +- sources/library/displaymode_measurement.cpp | 7 +-- sources/library/displaymode_measurement.h | 4 +- sources/library/displaymode_speedometer.cpp | 11 ++-- sources/library/displaymode_speedometer.h | 4 +- sources/library/engine_module.cpp | 10 +--- sources/library/engine_module.h | 8 +-- sources/library/entity_description.h | 16 +++--- sources/library/entity_dictionary.cpp | 13 ++--- sources/library/entity_dictionary.h | 10 ++-- sources/library/hooks.cpp | 27 +++++----- sources/library/hooks.h | 4 +- sources/library/local_player.cpp | 7 --- sources/library/local_player.h | 8 ++- sources/library/memory_pattern.h | 10 ++-- sources/library/server_module.cpp | 13 ++--- sources/library/server_module.h | 19 +++---- 30 files changed, 214 insertions(+), 215 deletions(-) diff --git a/sources/library/application.cpp b/sources/library/application.cpp index 491ad2a..71ed37a 100644 --- a/sources/library/application.cpp +++ b/sources/library/application.cpp @@ -42,23 +42,31 @@ CApplication &CApplication::GetInstance() return instance; } +CApplication::CApplication() + : m_clientModule(m_engineModule), + m_serverModule(m_engineModule), + m_hooks(m_clientModule), + m_stringStack(128) +{ + InitializeDisplayModes(); + InitializePrimitivesRenderer(); +}; + void CApplication::Run() { - if (!g_EngineModule.FindHandle()) + if (!m_engineModule.FindHandle()) EXCEPT("failed to get engine module handle"); - if (!g_ClientModule.FindHandle()) + if (!m_clientModule.FindHandle()) EXCEPT("failed to get client module handle"); SysUtils::ModuleInfo engineDLL; - SysUtils::GetModuleInfo(SysUtils::GetCurrentProcessHandle(), g_EngineModule.GetHandle(), engineDLL); + SysUtils::GetModuleInfo(SysUtils::GetCurrentProcessHandle(), m_engineModule.GetHandle(), engineDLL); - - // if can't obtain directly, find engine functions pointer arrays m_buildInfo.Initialize(engineDLL); - g_ClientModule.FindEngfuncs(m_buildInfo); - g_ServerModule.FindEngfuncs(m_buildInfo); - g_ServerModule.FindHandle(); + m_clientModule.FindEngfuncs(m_buildInfo); + m_serverModule.FindEngfuncs(m_buildInfo); + m_serverModule.FindHandle(); InitializeConVars(engineDLL); SetCurrentDisplayMode(); @@ -72,12 +80,12 @@ void CApplication::Run() void CApplication::InitializeDisplayModes() { m_displayModes.clear(); - m_displayModes.push_back(std::make_shared()); - m_displayModes.push_back(std::make_shared()); - m_displayModes.push_back(std::make_shared()); - m_displayModes.push_back(std::make_shared()); - m_displayModes.push_back(std::make_shared()); - m_displayModes.push_back(std::make_shared()); + m_displayModes.push_back(std::make_shared(m_localPlayer)); + m_displayModes.push_back(std::make_shared(m_localPlayer)); + m_displayModes.push_back(std::make_shared(m_localPlayer, m_engineModule)); + m_displayModes.push_back(std::make_shared(m_localPlayer)); + m_displayModes.push_back(std::make_shared(m_localPlayer)); + m_displayModes.push_back(std::make_shared(m_localPlayer)); } void CApplication::InitializePrimitivesRenderer() @@ -163,13 +171,14 @@ void CApplication::PrintTitleText() static void CommandTimescale() { + auto &serverModule = g_Application.GetServerModule(); if (!ConVars::sys_timescale) { g_pClientEngfuncs->Con_Printf("sys_timescale address not found"); return; } - if (g_ServerModule.IsInitialized() || g_ServerModule.FindHandle()) + if (serverModule.IsInitialized() || serverModule.FindHandle()) { if (g_pClientEngfuncs->Cmd_Argc() > 1) { diff --git a/sources/library/application.h b/sources/library/application.h index d926c7f..de8a49b 100644 --- a/sources/library/application.h +++ b/sources/library/application.h @@ -20,6 +20,10 @@ GNU General Public License for more details. #include "app_info.h" #include "exception.h" #include "build_info.h" +#include "engine_module.h" +#include "client_module.h" +#include "server_module.h" +#include "local_player.h" #include "display_mode.h" #include "primitives_renderer.h" #include "string_stack.h" @@ -38,12 +42,11 @@ class CApplication bool KeyInput(int keyDown, int keyCode, const char *bindName); const SCREENINFO& GetScreenInfo() const { return m_screenInfo; }; auto GetRenderer() const { return m_primitivesRenderer; }; + auto &GetServerModule() { return m_serverModule; } + auto &GetLocalPlayer() { return m_localPlayer; } private: - CApplication() { - InitializeDisplayModes(); - InitializePrimitivesRenderer(); - }; + CApplication(); ~CApplication() {}; void InitializeDisplayModes(); @@ -58,12 +61,17 @@ class CApplication float m_frameTime; float m_lastFrameTime; float m_lastClientTime; - CHooks m_hooks; CBuildInfo m_buildInfo; + CEngineModule m_engineModule; + CClientModule m_clientModule; + CServerModule m_serverModule; + CHooks m_hooks; SCREENINFO m_screenInfo = { 0 }; - CStringStack m_stringStack = CStringStack(128); + CStringStack m_stringStack; + CLocalPlayer m_localPlayer; std::shared_ptr m_currentDisplayMode = nullptr; std::shared_ptr m_primitivesRenderer = nullptr; std::vector> m_displayModes; }; + extern CApplication &g_Application; diff --git a/sources/library/bvh_tree.cpp b/sources/library/bvh_tree.cpp index 24e9727..3369de5 100644 --- a/sources/library/bvh_tree.cpp +++ b/sources/library/bvh_tree.cpp @@ -21,9 +21,9 @@ GNU General Public License for more details. #include #include -CBVHTree::CBVHTree(const std::vector *descList) +CBVHTree::CBVHTree(const std::vector &descList) + : m_descList(descList) { - m_descList = descList; } void CBVHTree::Reset() @@ -174,7 +174,7 @@ void CBVHTree::BuildBottomUp() m_nodes.reserve(gameObjects.size()); for (int i : gameObjects) { - const CEntityDescription &desc = m_descList->at(i); + const CEntityDescription &desc = m_descList.at(i); int newNode = AppendNode(desc.GetBoundingBox()); NodeAt(newNode).SetDescriptionIndex(i); inputNodes.push_back(newNode); @@ -299,7 +299,7 @@ void CBVHTree::SplitNode(CBVHTreeNode &node, ObjectList nodeObjects) for (int i : nodeObjects) { - const CEntityDescription &desc = m_descList->at(i); + const CEntityDescription &desc = m_descList.at(i); vec3_t objectCenter = desc.GetBoundingBox().GetCenterPoint(); for (int j = 0; j < 3; ++j) { @@ -370,7 +370,7 @@ CBoundingBox CBVHTree::CalcNodeBoundingBox(ObjectList nodeObjects, float epsilon const vec3_t fudge = vec3_t(epsilon, epsilon, epsilon); for (int i : nodeObjects) { - const CEntityDescription &desc = m_descList->at(i); + const CEntityDescription &desc = m_descList.at(i); nodeBounds.CombineWith(desc.GetBoundingBox()); } return CBoundingBox(nodeBounds.GetMins() - fudge, nodeBounds.GetMaxs() + fudge); @@ -380,7 +380,7 @@ std::vector CBVHTree::GetGameObjects() { // skip worldspawn here std::vector objectList; - for (size_t i = 1; i < m_descList->size(); ++i) { + for (size_t i = 1; i < m_descList.size(); ++i) { objectList.push_back(i); } return objectList; diff --git a/sources/library/bvh_tree.h b/sources/library/bvh_tree.h index 8406ec6..507a537 100644 --- a/sources/library/bvh_tree.h +++ b/sources/library/bvh_tree.h @@ -23,7 +23,7 @@ GNU General Public License for more details. class CBVHTree { public: - CBVHTree(const std::vector *descList); + CBVHTree(const std::vector &descList); void Reset(); void Build(); double ComputeCost() const; @@ -49,5 +49,5 @@ class CBVHTree std::stack m_nodesStack; std::stack m_objectListStack; std::vector m_nodes; - const std::vector *m_descList; + const std::vector &m_descList; }; diff --git a/sources/library/client_module.cpp b/sources/library/client_module.cpp index f291ef2..1a406de 100644 --- a/sources/library/client_module.cpp +++ b/sources/library/client_module.cpp @@ -21,12 +21,13 @@ GNU General Public License for more details. #include "sys_utils.h" cl_enginefunc_t *g_pClientEngfuncs; -CClientModule& g_ClientModule = CClientModule::GetInstance(); -CClientModule& CClientModule::GetInstance() +CClientModule::CClientModule(const CEngineModule &moduleRef) + : m_engineModule(moduleRef) { - static CClientModule instance; - return instance; + m_moduleInfo.baseAddress = nullptr; + m_moduleInfo.entryPointAddress = nullptr; + m_moduleInfo.imageSize = 0; } bool CClientModule::FindHandle() @@ -41,11 +42,11 @@ bool CClientModule::FindEngfuncs(const CBuildInfo &buildInfo) { uint8_t *pfnSPR_Load; uint8_t *pfnSPR_Frames; - uint8_t *moduleAddr = g_EngineModule.GetAddress(); - uint8_t *moduleEndAddr = moduleAddr + g_EngineModule.GetSize(); + uint8_t *moduleAddr = m_engineModule.GetAddress(); + uint8_t *moduleEndAddr = moduleAddr + m_engineModule.GetSize(); const CBuildInfo::Entry *buildInfoEntry = buildInfo.GetInfoEntry(); - if (!g_EngineModule.GetFunctionsFromAPI(&pfnSPR_Load, &pfnSPR_Frames)) + if (!m_engineModule.GetFunctionsFromAPI(&pfnSPR_Load, &pfnSPR_Frames)) { if (!buildInfoEntry) { std::string errorMsg; @@ -87,9 +88,9 @@ cl_enginefunc_t *CClientModule::SearchEngfuncsTable(uint8_t *pfnSPR_Load, uint8_ { bool fallbackMethod = false; uint8_t *targetAddr = pfnSPR_Load; - uint8_t *moduleAddr = g_EngineModule.GetAddress(); + uint8_t *moduleAddr = m_engineModule.GetAddress(); uint8_t *scanStartAddr = moduleAddr; - uint8_t *moduleEndAddr = moduleAddr + g_EngineModule.GetSize(); + uint8_t *moduleEndAddr = moduleAddr + m_engineModule.GetSize(); constexpr size_t pointerSize = sizeof(void *); while (true) @@ -130,14 +131,7 @@ cl_enginefunc_t *CClientModule::SearchEngfuncsTable(uint8_t *pfnSPR_Load, uint8_ return nullptr; } -uint8_t* CClientModule::GetFuncAddress(const char *funcName) +uint8_t* CClientModule::GetFuncAddress(const char *funcName) const { return reinterpret_cast(GetProcAddress(GetHandle(), funcName)); } - -CClientModule::CClientModule() -{ - m_moduleInfo.baseAddress = nullptr; - m_moduleInfo.entryPointAddress = nullptr; - m_moduleInfo.imageSize = 0; -} diff --git a/sources/library/client_module.h b/sources/library/client_module.h index c0556ff..640b432 100644 --- a/sources/library/client_module.h +++ b/sources/library/client_module.h @@ -16,36 +16,28 @@ GNU General Public License for more details. #include "hlsdk.h" #include "sys_utils.h" #include "build_info.h" +#include "engine_module.h" #include class CClientModule { public: - static CClientModule& GetInstance(); + CClientModule(const CEngineModule &moduleRef); bool FindHandle(); bool FindEngfuncs(const CBuildInfo &buildInfo); - uint8_t *GetFuncAddress(const char *funcName); + uint8_t *GetFuncAddress(const char *funcName) const; ModuleHandle GetHandle() const { return m_moduleHandle; } uint8_t *GetBaseAddress() const { return m_moduleInfo.baseAddress; } uint8_t *GetEntryPoint() const { return m_moduleInfo.entryPointAddress; } size_t GetSize() const { return m_moduleInfo.imageSize; } private: - CClientModule(); - CClientModule(const CClientModule&) = delete; - CClientModule& operator=(const CClientModule&) = delete; - cl_enginefunc_t* SearchEngfuncsTable(uint8_t *pfnSPR_Load, uint8_t *pfnSPR_Frames); ModuleHandle m_moduleHandle = NULL; SysUtils::ModuleInfo m_moduleInfo; + const CEngineModule &m_engineModule; }; -extern CClientModule& g_ClientModule; extern cl_enginefunc_t *g_pClientEngfuncs; - -inline uint8_t *GetAddrCL(uint32_t offset) -{ - return g_ClientModule.GetBaseAddress() + offset; -} diff --git a/sources/library/displaymode_angletracking.cpp b/sources/library/displaymode_angletracking.cpp index 0840044..a985d02 100644 --- a/sources/library/displaymode_angletracking.cpp +++ b/sources/library/displaymode_angletracking.cpp @@ -17,7 +17,8 @@ GNU General Public License for more details. #include "client_module.h" #include "local_player.h" -CModeAngleTracking::CModeAngleTracking() +CModeAngleTracking::CModeAngleTracking(const CLocalPlayer &playerRef) + : m_localPlayer(playerRef) { m_lastAngles = Vector(0.0f, 0.0f, 0.0f); m_trackStartTime = 0.0f; @@ -27,11 +28,11 @@ CModeAngleTracking::CModeAngleTracking() void CModeAngleTracking::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) { - if (!g_LocalPlayer.PredictionDataValid()) + if (!m_localPlayer.PredictionDataValid()) return; const float threshold = 0.001f; - const vec3_t &currAngles = g_LocalPlayer.GetAngles(); + const vec3_t &currAngles = m_localPlayer.GetAngles(); float pitchVelocity = (currAngles.x - m_lastAngles.x) / frameTime; float yawVelocity = (currAngles.y - m_lastAngles.y) / frameTime; diff --git a/sources/library/displaymode_angletracking.h b/sources/library/displaymode_angletracking.h index dd5a250..810994e 100644 --- a/sources/library/displaymode_angletracking.h +++ b/sources/library/displaymode_angletracking.h @@ -14,12 +14,13 @@ GNU General Public License for more details. #pragma once #include "display_mode.h" +#include "local_player.h" #include "hlsdk.h" class CModeAngleTracking : public IDisplayMode { public: - CModeAngleTracking(); + CModeAngleTracking(const CLocalPlayer &playerRef); virtual ~CModeAngleTracking() {}; void Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) override; @@ -33,4 +34,5 @@ class CModeAngleTracking : public IDisplayMode float m_trackStartTime; float m_lastYawVelocity; float m_lastPitchVelocity; + const CLocalPlayer &m_localPlayer; }; diff --git a/sources/library/displaymode_entityreport.cpp b/sources/library/displaymode_entityreport.cpp index c5c37d1..31074c6 100644 --- a/sources/library/displaymode_entityreport.cpp +++ b/sources/library/displaymode_entityreport.cpp @@ -18,13 +18,13 @@ GNU General Public License for more details. #include "client_module.h" #include "engine_module.h" #include "studio.h" -#include "entity_dictionary.h" #include "local_player.h" #include "bounding_box.h" #include #include -CModeEntityReport::CModeEntityReport() +CModeEntityReport::CModeEntityReport(const CLocalPlayer &playerRef, const CEngineModule &moduleRef) + : m_localPlayer(playerRef), m_engineModule(moduleRef) { m_entityIndex = 0; m_lockedEntityIndex = 0; @@ -34,11 +34,11 @@ CModeEntityReport::CModeEntityReport() void CModeEntityReport::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) { - if (g_LocalPlayer.PredictionDataValid()) + if (m_localPlayer.PredictionDataValid()) { int debugMode = ConVars::gsm_debug->value; - if (!g_EntityDictionary.IsInitialized()) - g_EntityDictionary.Initialize(); + if (!m_entityDictionary.IsInitialized()) + m_entityDictionary.Initialize(); screenText.Clear(); m_entityIndex = TraceEntity(); @@ -49,7 +49,7 @@ void CModeEntityReport::Render2D(float frameTime, int scrWidth, int scrHeight, C } if (debugMode == 2) { - g_EntityDictionary.VisualizeTree(true); + m_entityDictionary.VisualizeTree(true); } } else @@ -73,13 +73,13 @@ void CModeEntityReport::Render3D() const Color colorGreen = Color(0.f, 1.f, 0.f, 1.f); if (debugMode == 1) { - g_EntityDictionary.VisualizeDescriptions(); + m_entityDictionary.VisualizeDescriptions(); } else if (debugMode == 2) { - g_EntityDictionary.VisualizeTree(false); + m_entityDictionary.VisualizeTree(false); } - if (currentEntity > 0 && !g_EngineModule.IsSoftwareRenderer()) + if (currentEntity > 0 && !m_engineModule.IsSoftwareRenderer()) { cl_entity_t *entity = g_pClientEngfuncs->GetEntityByIndex(currentEntity); Utils::GetEntityBoundingBox(currentEntity, entityBbox); @@ -115,7 +115,7 @@ bool CModeEntityReport::KeyInput(bool keyDown, int keyCode, const char *bindName void CModeEntityReport::HandleChangelevel() { - g_EntityDictionary.Reset(); + m_entityDictionary.Reset(); } int CModeEntityReport::TraceEntity() @@ -129,11 +129,11 @@ int CModeEntityReport::TraceEntity() m_entityIndexList.clear(); m_entityDistanceList.clear(); - viewOrigin = g_LocalPlayer.GetViewOrigin(); - viewDir = g_LocalPlayer.GetViewDirection(); + viewOrigin = m_localPlayer.GetViewOrigin(); + viewDir = m_localPlayer.GetViewDirection(); - if (g_LocalPlayer.IsSpectate()) - ignoredEnt = g_LocalPlayer.GetSpectateTargetIndex(); + if (m_localPlayer.IsSpectate()) + ignoredEnt = m_localPlayer.GetSpectateTargetIndex(); Utils::TraceLine(viewOrigin, viewDir, lineLen, &traceData, ignoredEnt); if (traceData.fraction < 1.f) @@ -146,14 +146,14 @@ int CModeEntityReport::TraceEntity() const int listCount = 3; const physent_t *physEntLists[listCount] = { - g_LocalPlayer.GetVisents(), - g_LocalPlayer.GetPhysents(), - g_LocalPlayer.GetMoveents() + m_localPlayer.GetVisents(), + m_localPlayer.GetPhysents(), + m_localPlayer.GetMoveents() }; int physEntListsLen[listCount] = { - g_LocalPlayer.GetVisentsCount(), - g_LocalPlayer.GetPhysentsCount(), - g_LocalPlayer.GetMoveentsCount() + m_localPlayer.GetVisentsCount(), + m_localPlayer.GetPhysentsCount(), + m_localPlayer.GetMoveentsCount() }; for (int i = 0; i < listCount; ++i) @@ -224,7 +224,7 @@ float CModeEntityReport::GetEntityDistance(int entityIndex) if (entity) { model_t *entityModel = entity->model; - vec3_t viewOrigin = g_LocalPlayer.GetViewOrigin(); + vec3_t viewOrigin = m_localPlayer.GetViewOrigin(); // get nearest bbox-to-player distance by point caged in bbox Utils::GetEntityBoundingBox(entityIndex, entityBbox); @@ -246,9 +246,9 @@ bool CModeEntityReport::PrintEntityInfo(int entityIndex, CStringStack &screenTex std::string mapName = Utils::GetCurrentMapName(); screenText.Push("Entity not found"); screenText.PushPrintf("Map: %s", mapName.c_str()); - screenText.PushPrintf("Entity descriptions: %d", g_EntityDictionary.GetDescriptionsCount()); + screenText.PushPrintf("Entity descriptions: %d", m_entityDictionary.GetDescriptionsCount()); } - else if (Utils::IsGameDirEquals("cstrike") && g_LocalPlayer.IsSpectate() && g_LocalPlayer.GetSpectatingMode() != SpectatingMode::Roaming) + else if (Utils::IsGameDirEquals("cstrike") && m_localPlayer.IsSpectate() && m_localPlayer.GetSpectatingMode() != SpectatingMode::Roaming) { // disable print in non free-look spectating modes screenText.Push("Print enabled only in free look mode"); @@ -259,7 +259,7 @@ bool CModeEntityReport::PrintEntityInfo(int entityIndex, CStringStack &screenTex int iterCount; CEntityDescription entityDesc; cl_entity_t *entity = g_pClientEngfuncs->GetEntityByIndex(entityIndex); - bool descFound = g_EntityDictionary.FindDescription(entityIndex, entityDesc, iterCount); + bool descFound = m_entityDictionary.FindDescription(entityIndex, entityDesc, iterCount); PrintEntityCommonInfo(entityIndex, screenText); if (descFound) diff --git a/sources/library/displaymode_entityreport.h b/sources/library/displaymode_entityreport.h index 5400aee..b410762 100644 --- a/sources/library/displaymode_entityreport.h +++ b/sources/library/displaymode_entityreport.h @@ -15,12 +15,15 @@ GNU General Public License for more details. #pragma once #include "stdafx.h" #include "display_mode.h" +#include "engine_module.h" +#include "local_player.h" +#include "entity_dictionary.h" #include class CModeEntityReport : public IDisplayMode { public: - CModeEntityReport(); + CModeEntityReport(const CLocalPlayer &playerRef, const CEngineModule &moduleRef); virtual ~CModeEntityReport() {}; void Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) override; @@ -42,4 +45,7 @@ class CModeEntityReport : public IDisplayMode int m_lockedEntityIndex; std::vector m_entityIndexList; std::vector m_entityDistanceList; + CEntityDictionary m_entityDictionary; + const CLocalPlayer &m_localPlayer; + const CEngineModule &m_engineModule; }; diff --git a/sources/library/displaymode_facereport.cpp b/sources/library/displaymode_facereport.cpp index e86e247..244cf64 100644 --- a/sources/library/displaymode_facereport.cpp +++ b/sources/library/displaymode_facereport.cpp @@ -26,7 +26,8 @@ GNU General Public License for more details. #define SURF_DRAWTILED 0x20 #define SURF_DRAWBACKGROUND 0x40 -CModeFaceReport::CModeFaceReport() +CModeFaceReport::CModeFaceReport(const CLocalPlayer &playerRef) + : m_localPlayer(playerRef) { m_colorProbe = { 0 }; m_currentModel = nullptr; @@ -38,8 +39,8 @@ void CModeFaceReport::Render2D(float frameTime, int scrWidth, int scrHeight, CSt { const float lineLen = 11590.0f; vec3_t intersectPoint; - vec3_t viewOrigin = g_LocalPlayer.GetViewOrigin(); - vec3_t viewDir = g_LocalPlayer.GetViewDirection(); + vec3_t viewOrigin = m_localPlayer.GetViewOrigin(); + vec3_t viewDir = m_localPlayer.GetViewDirection(); m_currentFace = TraceSurface(viewOrigin, viewDir, lineLen, intersectPoint); screenText.Clear(); @@ -127,8 +128,8 @@ int CModeFaceReport::TraceEntity(vec3_t origin, vec3_t dir, float distance, vec3 pmtrace_t traceData; int ignoredEnt = -1; - if (g_LocalPlayer.IsSpectate()) { - ignoredEnt = g_LocalPlayer.GetSpectateTargetIndex(); + if (m_localPlayer.IsSpectate()) { + ignoredEnt = m_localPlayer.GetSpectateTargetIndex(); } Utils::TraceLine(origin, dir, distance, &traceData, ignoredEnt); diff --git a/sources/library/displaymode_facereport.h b/sources/library/displaymode_facereport.h index 2c4d96c..f85f916 100644 --- a/sources/library/displaymode_facereport.h +++ b/sources/library/displaymode_facereport.h @@ -17,12 +17,13 @@ GNU General Public License for more details. #include "hlsdk.h" #include "engine_types.h" #include "bounding_box.h" +#include "local_player.h" #include class CModeFaceReport : public IDisplayMode { public: - CModeFaceReport(); + CModeFaceReport(const CLocalPlayer &playerRef); virtual ~CModeFaceReport() {}; void Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) override; @@ -46,4 +47,5 @@ class CModeFaceReport : public IDisplayMode Engine::msurface_t *m_currentFace; CBoundingBox m_currentFaceBounds; std::vector m_boundPoints; + const CLocalPlayer &m_localPlayer; }; diff --git a/sources/library/displaymode_full.cpp b/sources/library/displaymode_full.cpp index 4ffa53c..c853d47 100644 --- a/sources/library/displaymode_full.cpp +++ b/sources/library/displaymode_full.cpp @@ -18,7 +18,8 @@ GNU General Public License for more details. #include "utils.h" #include "local_player.h" -CModeFull::CModeFull() +CModeFull::CModeFull(const CLocalPlayer &playerRef) + : m_localPlayer(playerRef) { m_frameTime = 0.0f; m_lastFrameTime = 0.0f; @@ -27,16 +28,16 @@ CModeFull::CModeFull() void CModeFull::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) { - if (g_LocalPlayer.PredictionDataValid()) + if (m_localPlayer.PredictionDataValid()) { float timeDelta = GetSmoothSystemFrametime(); - float velocityNum = g_LocalPlayer.GetVelocity().Length2D(); - const vec3_t &origin = g_LocalPlayer.GetOrigin(); - const vec3_t &velocity = g_LocalPlayer.GetVelocity(); - const vec3_t &angles = g_LocalPlayer.GetAngles(); - const vec3_t &baseVelocity = g_LocalPlayer.GetBaseVelocity(); - const vec3_t &punchAngle = g_LocalPlayer.GetPunchAngles(); - const vec3_t &viewOffset = g_LocalPlayer.GetViewOffset(); + float velocityNum = m_localPlayer.GetVelocity().Length2D(); + const vec3_t &origin = m_localPlayer.GetOrigin(); + const vec3_t &velocity = m_localPlayer.GetVelocity(); + const vec3_t &angles = m_localPlayer.GetAngles(); + const vec3_t &baseVelocity = m_localPlayer.GetBaseVelocity(); + const vec3_t &punchAngle = m_localPlayer.GetPunchAngles(); + const vec3_t &viewOffset = m_localPlayer.GetViewOffset(); screenText.Clear(); screenText.PushPrintf("FPS: %.1f", 1.f / timeDelta); @@ -47,29 +48,29 @@ void CModeFull::Render2D(float frameTime, int scrWidth, int scrHeight, CStringSt screenText.PushPrintf("Origin: (%.2f, %.2f, %.2f)", origin.x, origin.y, origin.z); screenText.PushPrintf("Angles: (%.2f, %.2f, %.2f)", angles.x, angles.y, angles.z); screenText.PushPrintf("Base Velocity: (%.2f, %.2f, %.2f)", baseVelocity.x, baseVelocity.y, baseVelocity.z); - screenText.PushPrintf("Max Velocity: %.2f (client %.2f)", g_LocalPlayer.GetMaxSpeed(), g_LocalPlayer.GetClientMaxSpeed()); - screenText.PushPrintf("Movetype: %s\n", Utils::GetMovetypeName(g_LocalPlayer.GetMovetype())); + screenText.PushPrintf("Max Velocity: %.2f (client %.2f)", m_localPlayer.GetMaxSpeed(), m_localPlayer.GetClientMaxSpeed()); + screenText.PushPrintf("Movetype: %s\n", Utils::GetMovetypeName(m_localPlayer.GetMovetype())); screenText.PushPrintf("View Offset: (%.2f, %.2f, %.2f)", viewOffset.x, viewOffset.y, viewOffset.z); screenText.PushPrintf("Punch Angle: (%.2f, %.2f, %.2f)", punchAngle.x, punchAngle.y, punchAngle.z); - screenText.PushPrintf("Duck Time: %.2f", g_LocalPlayer.GetDuckTime()); - screenText.PushPrintf("In Duck Process: %s", g_LocalPlayer.IsDucking() ? "yes" : "no"); - screenText.PushPrintf("Player Flags: %d", g_LocalPlayer.GetFlags()); - screenText.PushPrintf("Hull Type: %d", g_LocalPlayer.GetHullType()); - screenText.PushPrintf("Gravity: %.2f", g_LocalPlayer.GetGravity()); - screenText.PushPrintf("Friction: %.2f", g_LocalPlayer.GetFriction()); - screenText.PushPrintf("On Ground: %s", g_LocalPlayer.OnGround() ? "yes" : "no"); + screenText.PushPrintf("Duck Time: %.2f", m_localPlayer.GetDuckTime()); + screenText.PushPrintf("In Duck Process: %s", m_localPlayer.IsDucking() ? "yes" : "no"); + screenText.PushPrintf("Player Flags: %d", m_localPlayer.GetFlags()); + screenText.PushPrintf("Hull Type: %d", m_localPlayer.GetHullType()); + screenText.PushPrintf("Gravity: %.2f", m_localPlayer.GetGravity()); + screenText.PushPrintf("Friction: %.2f", m_localPlayer.GetFriction()); + screenText.PushPrintf("On Ground: %s", m_localPlayer.OnGround() ? "yes" : "no"); screenText.PushPrintf("fuserX: %.2f / %.2f / %.2f / %.2f", - g_LocalPlayer.GetFloatUserVar(1), - g_LocalPlayer.GetFloatUserVar(2), - g_LocalPlayer.GetFloatUserVar(3), - g_LocalPlayer.GetFloatUserVar(4) + m_localPlayer.GetFloatUserVar(1), + m_localPlayer.GetFloatUserVar(2), + m_localPlayer.GetFloatUserVar(3), + m_localPlayer.GetFloatUserVar(4) ); screenText.PushPrintf("iuserX: %d / %d / %d / %d", - g_LocalPlayer.GetIntUserVar(1), - g_LocalPlayer.GetIntUserVar(2), - g_LocalPlayer.GetIntUserVar(3), - g_LocalPlayer.GetIntUserVar(4) + m_localPlayer.GetIntUserVar(1), + m_localPlayer.GetIntUserVar(2), + m_localPlayer.GetIntUserVar(3), + m_localPlayer.GetIntUserVar(4) ); } else diff --git a/sources/library/displaymode_full.h b/sources/library/displaymode_full.h index 186b9bc..39e2491 100644 --- a/sources/library/displaymode_full.h +++ b/sources/library/displaymode_full.h @@ -14,11 +14,12 @@ GNU General Public License for more details. #pragma once #include "display_mode.h" +#include "local_player.h" class CModeFull : public IDisplayMode { public: - CModeFull(); + CModeFull(const CLocalPlayer &playerRef); virtual ~CModeFull() {}; void Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) override; @@ -33,4 +34,5 @@ class CModeFull : public IDisplayMode float m_frameTime; float m_lastFrameTime; float m_lastSysTime; + const CLocalPlayer &m_localPlayer; }; diff --git a/sources/library/displaymode_measurement.cpp b/sources/library/displaymode_measurement.cpp index 0c362a5..9246820 100644 --- a/sources/library/displaymode_measurement.cpp +++ b/sources/library/displaymode_measurement.cpp @@ -21,7 +21,8 @@ GNU General Public License for more details. // HLSDK #include "keydefs.h" -CModeMeasurement::CModeMeasurement() +CModeMeasurement::CModeMeasurement(const CLocalPlayer &playerRef) + : m_localPlayer(playerRef) { const vec3_t zeroVector = { 0.0f, 0.0f, 0.0f }; m_pointA = zeroVector; @@ -121,7 +122,7 @@ bool CModeMeasurement::KeyInput(bool keyDown, int keyCode, const char *) g_pClientEngfuncs->pfnPlaySoundByName("buttons/lightswitch2.wav", 1.0f); g_pClientEngfuncs->GetViewAngles(viewAngles); g_pClientEngfuncs->pfnAngleVectors(viewAngles, viewDir, nullptr, nullptr); - viewOrigin = g_LocalPlayer.GetViewOrigin(); + viewOrigin = m_localPlayer.GetViewOrigin(); Utils::TraceLine(viewOrigin, viewDir, traceLen, &traceData); if (keyCode == K_MOUSE1) @@ -286,7 +287,7 @@ void CModeMeasurement::LoadLineSprite() void CModeMeasurement::Render2D(float frameTime, int screenWidth, int screenHeight, CStringStack &screenText) { - if (g_LocalPlayer.PredictionDataValid()) + if (m_localPlayer.PredictionDataValid()) { const vec3_t &originPointA = GetPointOriginA(); const vec3_t &originPointB = GetPointOriginB(); diff --git a/sources/library/displaymode_measurement.h b/sources/library/displaymode_measurement.h index 5c07bbf..8cde13f 100644 --- a/sources/library/displaymode_measurement.h +++ b/sources/library/displaymode_measurement.h @@ -15,6 +15,7 @@ GNU General Public License for more details. #pragma once #include "stdafx.h" #include "display_mode.h" +#include "local_player.h" class CModeMeasurement : public IDisplayMode { @@ -30,7 +31,7 @@ class CModeMeasurement : public IDisplayMode }; public: - CModeMeasurement(); + CModeMeasurement(const CLocalPlayer &playerRef); virtual ~CModeMeasurement() {}; void Render2D(float frameTime, int screenWidth, int screenHeight, CStringStack &screenText) override; @@ -60,4 +61,5 @@ class CModeMeasurement : public IDisplayMode vec3_t m_pointB; HLSPRITE m_lineSprite; SnapMode m_snapMode; + const CLocalPlayer &m_localPlayer; }; diff --git a/sources/library/displaymode_speedometer.cpp b/sources/library/displaymode_speedometer.cpp index 42ec9b4..3bfe356 100644 --- a/sources/library/displaymode_speedometer.cpp +++ b/sources/library/displaymode_speedometer.cpp @@ -17,6 +17,11 @@ GNU General Public License for more details. #include "utils.h" #include "local_player.h" +CModeSpeedometer::CModeSpeedometer(const CLocalPlayer &playerRef) + : m_localPlayer(playerRef) +{ +} + void CModeSpeedometer::Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) { const int centerX = scrWidth / 2; @@ -60,11 +65,11 @@ void CModeSpeedometer::DrawVelocityBar(int centerX, int centerY, float velocity) void CModeSpeedometer::CalculateVelocity(float frameTime) { - if (g_LocalPlayer.IsSpectate()) { - m_velocity = GetEntityVelocityApprox(g_LocalPlayer.GetSpectateTargetIndex()); + if (m_localPlayer.IsSpectate()) { + m_velocity = GetEntityVelocityApprox(m_localPlayer.GetSpectateTargetIndex()); } else { - m_velocity = (g_LocalPlayer.GetVelocity() + g_LocalPlayer.GetBaseVelocity()).Length2D(); + m_velocity = (m_localPlayer.GetVelocity() + m_localPlayer.GetBaseVelocity()).Length2D(); } } diff --git a/sources/library/displaymode_speedometer.h b/sources/library/displaymode_speedometer.h index 77e8e81..f5131cc 100644 --- a/sources/library/displaymode_speedometer.h +++ b/sources/library/displaymode_speedometer.h @@ -14,12 +14,13 @@ GNU General Public License for more details. #pragma once #include "display_mode.h" +#include "local_player.h" #include "hlsdk.h" class CModeSpeedometer : public IDisplayMode { public: - CModeSpeedometer() {}; + CModeSpeedometer(const CLocalPlayer &playerRef); virtual ~CModeSpeedometer() {}; void Render2D(float frameTime, int scrWidth, int scrHeight, CStringStack &screenText) override; @@ -35,4 +36,5 @@ class CModeSpeedometer : public IDisplayMode float m_velocity; float m_lastUpdateTime; + const CLocalPlayer &m_localPlayer; }; diff --git a/sources/library/engine_module.cpp b/sources/library/engine_module.cpp index 4a23e87..600bb1a 100644 --- a/sources/library/engine_module.cpp +++ b/sources/library/engine_module.cpp @@ -15,14 +15,6 @@ GNU General Public License for more details. #include "engine_module.h" #include "hlsdk.h" -CEngineModule& g_EngineModule = CEngineModule::GetInstance(); - -CEngineModule& CEngineModule::GetInstance() -{ - static CEngineModule instance; - return instance; -} - bool CEngineModule::FindHandle() { m_moduleHandle = GetModuleHandle("hw.dll"); @@ -42,7 +34,7 @@ bool CEngineModule::FindHandle() return (m_moduleHandle != NULL) && SetupModuleInfo(); } -bool CEngineModule::GetFunctionsFromAPI(uint8_t **pfnSPR_Load, uint8_t **pfnSPR_Frames) +bool CEngineModule::GetFunctionsFromAPI(uint8_t **pfnSPR_Load, uint8_t **pfnSPR_Frames) const { if (m_isXashEngine) { diff --git a/sources/library/engine_module.h b/sources/library/engine_module.h index feb78c9..099001a 100644 --- a/sources/library/engine_module.h +++ b/sources/library/engine_module.h @@ -19,10 +19,8 @@ GNU General Public License for more details. class CEngineModule { public: - static CEngineModule& GetInstance(); - bool FindHandle(); - bool GetFunctionsFromAPI(uint8_t **pfnSPR_Load, uint8_t **pfnSPR_Frames); + bool GetFunctionsFromAPI(uint8_t **pfnSPR_Load, uint8_t **pfnSPR_Frames) const; bool IsXashEngine() const { return m_isXashEngine; } bool IsSoftwareRenderer() const { return m_isSoftwareRenderer; }; ModuleHandle GetHandle() const { return m_moduleHandle; }; @@ -30,9 +28,6 @@ class CEngineModule size_t GetSize() const { return m_moduleInfo.imageSize; } private: - CEngineModule() {}; - CEngineModule(const CEngineModule&) = delete; - CEngineModule& operator=(const CEngineModule&) = delete; bool SetupModuleInfo(); ModuleHandle m_moduleHandle = NULL; @@ -40,4 +35,3 @@ class CEngineModule bool m_isXashEngine = false; bool m_isSoftwareRenderer = false; }; -extern CEngineModule& g_EngineModule; diff --git a/sources/library/entity_description.h b/sources/library/entity_description.h index af693e3..998879a 100644 --- a/sources/library/entity_description.h +++ b/sources/library/entity_description.h @@ -27,14 +27,14 @@ class CEntityDescription void GetPropertyString(int index, std::string &buffer) const; void AssociateEntity(int entityIndex); - inline int GetPropertiesCount() const { return m_EntityProps.size(); } - inline int GetAssocEntityIndex() const { return m_iAssociatedEntity; } - inline const std::string& GetClassname() const { return m_szClassname; } - inline const std::string& GetTargetname() const { return m_szTargetname; } - inline const std::string& GetModelName() const { return m_szModelName; } - inline const vec3_t &GetOrigin() const { return m_vecOrigin; } - inline const vec3_t &GetAngles() const { return m_vecAngles; } - inline const CBoundingBox &GetBoundingBox() const { return m_boundingBox; } + int GetPropertiesCount() const { return m_EntityProps.size(); } + int GetAssocEntityIndex() const { return m_iAssociatedEntity; } + const std::string& GetClassname() const { return m_szClassname; } + const std::string& GetTargetname() const { return m_szTargetname; } + const std::string& GetModelName() const { return m_szModelName; } + const vec3_t &GetOrigin() const { return m_vecOrigin; } + const vec3_t &GetAngles() const { return m_vecAngles; } + const CBoundingBox &GetBoundingBox() const { return m_boundingBox; } private: void Reset(); diff --git a/sources/library/entity_dictionary.cpp b/sources/library/entity_dictionary.cpp index 8e72777..8a80561 100644 --- a/sources/library/entity_dictionary.cpp +++ b/sources/library/entity_dictionary.cpp @@ -16,14 +16,6 @@ GNU General Public License for more details. #include "utils.h" #include "client_module.h" -CEntityDictionary &g_EntityDictionary = CEntityDictionary::GetInstance(); - -CEntityDictionary &CEntityDictionary::GetInstance() -{ - static CEntityDictionary instance; - return instance; -} - // Should be initialized after every map change void CEntityDictionary::Initialize() { @@ -80,6 +72,11 @@ bool CEntityDictionary::FindDescription(int entityIndex, CEntityDescription &des return false; } +CEntityDictionary::CEntityDictionary() + : m_entityDescTree(m_entityDescList) +{ +} + void CEntityDictionary::AssociateDescription(int entityIndex, int descIndex) { CEntityDescription &entityDesc = m_entityDescList[descIndex]; diff --git a/sources/library/entity_dictionary.h b/sources/library/entity_dictionary.h index b722f91..7ebb8bc 100644 --- a/sources/library/entity_dictionary.h +++ b/sources/library/entity_dictionary.h @@ -22,7 +22,8 @@ GNU General Public License for more details. class CEntityDictionary { public: - static CEntityDictionary &GetInstance(); + CEntityDictionary(); + void Reset(); void Initialize(); void VisualizeTree(bool textRendering); @@ -33,16 +34,11 @@ class CEntityDictionary bool IsInitialized() const { return m_entityDescList.size() > 0; } private: - CEntityDictionary() {}; - CEntityDictionary(const CEntityDictionary &) = delete; - CEntityDictionary &operator=(const CEntityDictionary &) = delete; - void AssociateDescription(int entityIndex, int descIndex); void BuildDescriptionsTree(); void ParseEntityData(); std::map m_associations; - CBVHTree m_entityDescTree = CBVHTree(&m_entityDescList); std::vector m_entityDescList; + CBVHTree m_entityDescTree; }; -extern CEntityDictionary &g_EntityDictionary; diff --git a/sources/library/hooks.cpp b/sources/library/hooks.cpp index 94da88c..0a55182 100644 --- a/sources/library/hooks.cpp +++ b/sources/library/hooks.cpp @@ -31,7 +31,7 @@ NOINLINE static int __cdecl HookRedraw(float time, int intermission) NOINLINE static void __cdecl HookPlayerMove(playermove_t *pmove, int server) { PLH::FnCast(CHooks::Impl::m_hookPlayerMove.GetTrampolineAddr(), CHooks::Impl::pfnPlayerMove_t())(pmove, server); - g_LocalPlayer.UpdatePlayerMove(pmove); + g_Application.GetLocalPlayer().UpdatePlayerMove(pmove); } NOINLINE static int __cdecl HookKeyEvent(int keyDown, int keyCode, const char *bindName) @@ -51,16 +51,16 @@ NOINLINE static void __cdecl HookDrawTriangles() NOINLINE static int __cdecl HookIsThirdPerson() { int returnCode = PLH::FnCast(CHooks::Impl::m_hookIsThirdPerson.GetTrampolineAddr(), CHooks::Impl::pfnIsThirdPerson_t())(); - return (returnCode || g_LocalPlayer.IsThirdPersonForced()) ? 1 : 0; + return (returnCode || g_Application.GetLocalPlayer().IsThirdPersonForced()) ? 1 : 0; } NOINLINE static void __cdecl HookCameraOffset(float *cameraOffset) { PLH::FnCast(CHooks::Impl::m_hookCameraOffset.GetTrampolineAddr(), CHooks::Impl::pfnCameraOffset_t())(cameraOffset); - if (g_LocalPlayer.IsThirdPersonForced()) + if (g_Application.GetLocalPlayer().IsThirdPersonForced()) { g_pClientEngfuncs->GetViewAngles(cameraOffset); - cameraOffset[2] = g_LocalPlayer.GetThirdPersonCameraDist(); + cameraOffset[2] = g_Application.GetLocalPlayer().GetThirdPersonCameraDist(); } } @@ -71,7 +71,8 @@ NOINLINE static int __cdecl HookVidInit() return returnCode; } -CHooks::CHooks() +CHooks::CHooks(const CClientModule &moduleRef) + : m_clientModule(moduleRef) { m_pImpl = std::make_unique(); } @@ -82,13 +83,13 @@ CHooks::~CHooks() void CHooks::Apply() { - Impl::pfnRedraw_t pfnRedraw = (Impl::pfnRedraw_t)g_ClientModule.GetFuncAddress("HUD_Redraw"); - Impl::pfnPlayerMove_t pfnPlayerMove = (Impl::pfnPlayerMove_t)g_ClientModule.GetFuncAddress("HUD_PlayerMove"); - Impl::pfnKeyEvent_t pfnKeyEvent = (Impl::pfnKeyEvent_t)g_ClientModule.GetFuncAddress("HUD_Key_Event"); - Impl::pfnDrawTriangles_t pfnDrawTriangles = (Impl::pfnDrawTriangles_t)g_ClientModule.GetFuncAddress("HUD_DrawTransparentTriangles"); - Impl::pfnIsThirdPerson_t pfnIsThirdPerson = (Impl::pfnIsThirdPerson_t)g_ClientModule.GetFuncAddress("CL_IsThirdPerson"); - Impl::pfnCameraOffset_t pfnCameraOffset = (Impl::pfnCameraOffset_t)g_ClientModule.GetFuncAddress("CL_CameraOffset"); - Impl::pfnVidInit_t pfnVidInit = (Impl::pfnVidInit_t)g_ClientModule.GetFuncAddress("HUD_VidInit"); + Impl::pfnRedraw_t pfnRedraw = (Impl::pfnRedraw_t)m_clientModule.GetFuncAddress("HUD_Redraw"); + Impl::pfnPlayerMove_t pfnPlayerMove = (Impl::pfnPlayerMove_t)m_clientModule.GetFuncAddress("HUD_PlayerMove"); + Impl::pfnKeyEvent_t pfnKeyEvent = (Impl::pfnKeyEvent_t)m_clientModule.GetFuncAddress("HUD_Key_Event"); + Impl::pfnDrawTriangles_t pfnDrawTriangles = (Impl::pfnDrawTriangles_t)m_clientModule.GetFuncAddress("HUD_DrawTransparentTriangles"); + Impl::pfnIsThirdPerson_t pfnIsThirdPerson = (Impl::pfnIsThirdPerson_t)m_clientModule.GetFuncAddress("CL_IsThirdPerson"); + Impl::pfnCameraOffset_t pfnCameraOffset = (Impl::pfnCameraOffset_t)m_clientModule.GetFuncAddress("CL_CameraOffset"); + Impl::pfnVidInit_t pfnVidInit = (Impl::pfnVidInit_t)m_clientModule.GetFuncAddress("HUD_VidInit"); m_pImpl->InitializeLogger(); Impl::m_hookRedraw.Hook(pfnRedraw, &HookRedraw); @@ -109,7 +110,7 @@ void CHooks::Apply() if (!Impl::m_hookDrawTriangles.IsHooked()) { - pfnDrawTriangles = (Impl::pfnDrawTriangles_t)g_ClientModule.GetFuncAddress("HUD_DrawNormalTriangles"); + pfnDrawTriangles = (Impl::pfnDrawTriangles_t)m_clientModule.GetFuncAddress("HUD_DrawNormalTriangles"); if (!Impl::m_hookDrawTriangles.Hook(pfnDrawTriangles, &HookDrawTriangles)) { g_pClientEngfuncs->Con_Printf( diff --git a/sources/library/hooks.h b/sources/library/hooks.h index d386f06..843b368 100644 --- a/sources/library/hooks.h +++ b/sources/library/hooks.h @@ -13,6 +13,7 @@ GNU General Public License for more details. */ #pragma once +#include "client_module.h" #include class CHooks @@ -21,11 +22,12 @@ class CHooks class Impl; class Logger; - CHooks(); + CHooks(const CClientModule &moduleRef); ~CHooks(); void Apply(); void Remove(); private: + const CClientModule &m_clientModule; std::unique_ptr m_pImpl; }; diff --git a/sources/library/local_player.cpp b/sources/library/local_player.cpp index f9764e2..92073a8 100644 --- a/sources/library/local_player.cpp +++ b/sources/library/local_player.cpp @@ -17,13 +17,6 @@ GNU General Public License for more details. #include "client_module.h" #include -CLocalPlayer &g_LocalPlayer = CLocalPlayer::GetInstance(); -CLocalPlayer &CLocalPlayer::GetInstance() -{ - static CLocalPlayer instance; - return instance; -} - void CLocalPlayer::UpdatePlayerMove(playermove_t *pmove) { m_pPlayerMove = pmove; diff --git a/sources/library/local_player.h b/sources/library/local_player.h index 7d633f1..c62f9cc 100644 --- a/sources/library/local_player.h +++ b/sources/library/local_player.h @@ -30,7 +30,8 @@ enum class SpectatingMode class CLocalPlayer { public: - static CLocalPlayer &GetInstance(); + CLocalPlayer() {}; + ~CLocalPlayer() = default; void UpdatePlayerMove(playermove_t *pmove); bool PredictionDataValid() const; @@ -70,9 +71,6 @@ class CLocalPlayer float GetThirdPersonCameraDist() const; private: - CLocalPlayer() {}; - ~CLocalPlayer() {}; - playermove_t *m_pPlayerMove = nullptr; }; -extern CLocalPlayer &g_LocalPlayer; + diff --git a/sources/library/memory_pattern.h b/sources/library/memory_pattern.h index 14f116c..f11010d 100644 --- a/sources/library/memory_pattern.h +++ b/sources/library/memory_pattern.h @@ -28,12 +28,12 @@ class CMemoryPattern void InitFromString(const std::string &pattern); bool IsInitialized() const; - inline int GetLength() const { return m_signature.size(); }; - inline uint8_t GetByteAt(int offset) const { return m_signature[offset]; }; - inline bool ShouldCheckByteAt(int offset) const { return m_mask[offset]; }; + int GetLength() const { return m_signature.size(); }; + uint8_t GetByteAt(int offset) const { return m_signature[offset]; }; + bool ShouldCheckByteAt(int offset) const { return m_mask[offset]; }; // To provide an optimal way to finding pattern address - inline const int *GetMaskAddress() const { return m_mask.data(); }; - inline const uint8_t *GetSignatureAddress() const { return m_signature.data(); }; + const int *GetMaskAddress() const { return m_mask.data(); }; + const uint8_t *GetSignatureAddress() const { return m_signature.data(); }; private: void ReserveElements(size_t elemCount); diff --git a/sources/library/server_module.cpp b/sources/library/server_module.cpp index ecae69e..c046044 100644 --- a/sources/library/server_module.cpp +++ b/sources/library/server_module.cpp @@ -21,12 +21,13 @@ GNU General Public License for more details. #include "utils.h" enginefuncs_t *g_pServerEngfuncs; -CServerModule &g_ServerModule = CServerModule::GetInstance(); -CServerModule &CServerModule::GetInstance() +CServerModule::CServerModule(const CEngineModule &moduleRef) + : m_engineModule(moduleRef) { - static CServerModule instance; - return instance; + m_moduleInfo.baseAddress = nullptr; + m_moduleInfo.entryPointAddress = nullptr; + m_moduleInfo.imageSize = 0; } bool CServerModule::FindHandle() @@ -46,8 +47,8 @@ bool CServerModule::FindEngfuncs(const CBuildInfo &buildInfo) uint8_t *scanStartAddr; const size_t pointerSize = sizeof(void *); - uint8_t *moduleAddr = g_EngineModule.GetAddress(); - uint8_t *moduleEndAddr = moduleAddr + g_EngineModule.GetSize(); + uint8_t *moduleAddr = m_engineModule.GetAddress(); + uint8_t *moduleEndAddr = moduleAddr + m_engineModule.GetSize(); const CBuildInfo::Entry *buildInfoEntry = buildInfo.GetInfoEntry(); if (!buildInfoEntry) { diff --git a/sources/library/server_module.h b/sources/library/server_module.h index 97acba5..e6348ae 100644 --- a/sources/library/server_module.h +++ b/sources/library/server_module.h @@ -16,30 +16,27 @@ GNU General Public License for more details. #include "hlsdk.h" #include "sys_utils.h" #include "build_info.h" +#include "engine_module.h" #include class CServerModule { public: - static CServerModule& GetInstance(); + CServerModule(const CEngineModule &moduleRef); bool FindHandle(); bool FindEngfuncs(const CBuildInfo &buildInfo); uint8_t *GetFuncAddress(const char *funcName); - inline ModuleHandle GetHandle() const { return m_module; } - inline uint8_t *GetBaseAddress() const { return m_moduleInfo.baseAddress; } - inline uint8_t *GetEntryPoint() const { return m_moduleInfo.entryPointAddress; } - inline size_t GetSize() const { return m_moduleInfo.imageSize; } - inline bool IsInitialized() const { return m_module != NULL; } + ModuleHandle GetHandle() const { return m_module; } + uint8_t *GetBaseAddress() const { return m_moduleInfo.baseAddress; } + uint8_t *GetEntryPoint() const { return m_moduleInfo.entryPointAddress; } + size_t GetSize() const { return m_moduleInfo.imageSize; } + bool IsInitialized() const { return m_module != NULL; } private: - CServerModule() {}; - CServerModule(const CServerModule&) = delete; - CServerModule& operator=(const CServerModule&) = delete; - ModuleHandle m_module = NULL; SysUtils::ModuleInfo m_moduleInfo; + const CEngineModule &m_engineModule; }; -extern CServerModule &g_ServerModule; extern enginefuncs_t *g_pServerEngfuncs;