Skip to content

Commit

Permalink
Library: code refactoring for reducing cohesion
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed Nov 4, 2023
1 parent 6543f8f commit 5deba33
Show file tree
Hide file tree
Showing 30 changed files with 214 additions and 215 deletions.
39 changes: 24 additions & 15 deletions sources/library/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -72,12 +80,12 @@ void CApplication::Run()
void CApplication::InitializeDisplayModes()
{
m_displayModes.clear();
m_displayModes.push_back(std::make_shared<CModeFull>());
m_displayModes.push_back(std::make_shared<CModeSpeedometer>());
m_displayModes.push_back(std::make_shared<CModeEntityReport>());
m_displayModes.push_back(std::make_shared<CModeMeasurement>());
m_displayModes.push_back(std::make_shared<CModeFaceReport>());
m_displayModes.push_back(std::make_shared<CModeAngleTracking>());
m_displayModes.push_back(std::make_shared<CModeFull>(m_localPlayer));
m_displayModes.push_back(std::make_shared<CModeSpeedometer>(m_localPlayer));
m_displayModes.push_back(std::make_shared<CModeEntityReport>(m_localPlayer, m_engineModule));
m_displayModes.push_back(std::make_shared<CModeMeasurement>(m_localPlayer));
m_displayModes.push_back(std::make_shared<CModeFaceReport>(m_localPlayer));
m_displayModes.push_back(std::make_shared<CModeAngleTracking>(m_localPlayer));
}

void CApplication::InitializePrimitivesRenderer()
Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 14 additions & 6 deletions sources/library/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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();
Expand All @@ -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<IDisplayMode> m_currentDisplayMode = nullptr;
std::shared_ptr<IPrimitivesRenderer> m_primitivesRenderer = nullptr;
std::vector<std::shared_ptr<IDisplayMode>> m_displayModes;
};

extern CApplication &g_Application;
12 changes: 6 additions & 6 deletions sources/library/bvh_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ GNU General Public License for more details.
#include <cfloat>
#include <algorithm>

CBVHTree::CBVHTree(const std::vector<CEntityDescription> *descList)
CBVHTree::CBVHTree(const std::vector<CEntityDescription> &descList)
: m_descList(descList)
{
m_descList = descList;
}

void CBVHTree::Reset()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -380,7 +380,7 @@ std::vector<int> CBVHTree::GetGameObjects()
{
// skip worldspawn here
std::vector<int> 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;
Expand Down
4 changes: 2 additions & 2 deletions sources/library/bvh_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ GNU General Public License for more details.
class CBVHTree
{
public:
CBVHTree(const std::vector<CEntityDescription> *descList);
CBVHTree(const std::vector<CEntityDescription> &descList);
void Reset();
void Build();
double ComputeCost() const;
Expand All @@ -49,5 +49,5 @@ class CBVHTree
std::stack<int> m_nodesStack;
std::stack<ObjectList> m_objectListStack;
std::vector<CBVHTreeNode> m_nodes;
const std::vector<CEntityDescription> *m_descList;
const std::vector<CEntityDescription> &m_descList;
};
28 changes: 11 additions & 17 deletions sources/library/client_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<uint8_t*>(GetProcAddress(GetHandle(), funcName));
}

CClientModule::CClientModule()
{
m_moduleInfo.baseAddress = nullptr;
m_moduleInfo.entryPointAddress = nullptr;
m_moduleInfo.imageSize = 0;
}
16 changes: 4 additions & 12 deletions sources/library/client_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>

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;
}
7 changes: 4 additions & 3 deletions sources/library/displaymode_angletracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion sources/library/displaymode_angletracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,4 +34,5 @@ class CModeAngleTracking : public IDisplayMode
float m_trackStartTime;
float m_lastYawVelocity;
float m_lastPitchVelocity;
const CLocalPlayer &m_localPlayer;
};
Loading

0 comments on commit 5deba33

Please sign in to comment.