Skip to content

Commit

Permalink
Make more classes constructible from hook context
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Dec 27, 2024
1 parent 3cbced1 commit a5175ad
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 57 deletions.
6 changes: 2 additions & 4 deletions Source/FeatureHelpers/RenderingHookEntityLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
template <typename HookContext>
class RenderingHookEntityLoop {
public:
explicit RenderingHookEntityLoop(HookContext& hookContext, PlayerInfoInWorld<HookContext>& playerInformationThroughWalls) noexcept
explicit RenderingHookEntityLoop(HookContext& hookContext) noexcept
: hookContext{hookContext}
, playerInformationThroughWalls{playerInformationThroughWalls}
{
}

Expand All @@ -29,7 +28,7 @@ class RenderingHookEntityLoop {

if (entityTypeInfo.template is<cs2::C_CSPlayerPawn>()) {
auto&& playerPawn = hookContext.template make<PlayerPawn>(static_cast<cs2::C_CSPlayerPawn*>(&entity));
playerInformationThroughWalls.drawPlayerInformation(playerPawn);
hookContext.template make<PlayerInfoInWorld>().drawPlayerInformation(playerPawn);
}

if (entityTypeInfo.isModelEntity())
Expand All @@ -38,5 +37,4 @@ class RenderingHookEntityLoop {
}

HookContext& hookContext;
PlayerInfoInWorld<HookContext>& playerInformationThroughWalls;
};
11 changes: 8 additions & 3 deletions Source/Features/Hud/DefusingAlert/DefusingAlert.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#pragma once

template <typename Context>
#include <utility>

#include "DefusingAlertContext.h"

template <typename HookContext, typename Context = DefusingAlertContext<HookContext>>
class DefusingAlert {
public:
explicit DefusingAlert(Context context) noexcept
: context{context}
template <typename... Args>
DefusingAlert(Args&&... args) noexcept
: context{std::forward<Args>(args)...}
{
}

Expand Down
18 changes: 8 additions & 10 deletions Source/Features/Hud/DefusingAlert/DefusingAlertContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
template <typename HookContext>
class DefusingAlertContext {
public:
DefusingAlertContext(HookContext& context, DefusingAlertState& state) noexcept
DefusingAlertContext(HookContext& context) noexcept
: context{context}
, _state{state}
{
}

[[nodiscard]] decltype(auto) defusingAlertContainerPanel() const noexcept
{
if (auto&& panel = uiEngine().getPanelFromHandle(_state.defusingAlertContainerPanelHandle))
if (auto&& panel = uiEngine().getPanelFromHandle(state().defusingAlertContainerPanelHandle))
return utils::lvalue<decltype(panel)>(panel);

updatePanelHandles();
return uiEngine().getPanelFromHandle(_state.defusingAlertContainerPanelHandle);
return uiEngine().getPanelFromHandle(state().defusingAlertContainerPanelHandle);
}

[[nodiscard]] auto defusingAlertCondition() const noexcept
Expand All @@ -48,12 +47,12 @@ class DefusingAlertContext {

[[nodiscard]] DefusingAlertState& state() const noexcept
{
return _state;
return context.featuresStates().hudFeaturesStates.defusingAlertState;
}

[[nodiscard]] auto defusingCountdownTextPanel() const noexcept
{
return DefusingCountdownTextPanel{uiEngine().getPanelFromHandle(_state.defusingTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
return DefusingCountdownTextPanel{uiEngine().getPanelFromHandle(state().defusingTimerPanelHandle).clientPanel().template as<PanoramaLabel>()};
}

private:
Expand All @@ -64,7 +63,7 @@ class DefusingAlertContext {

void updatePanelHandles() const noexcept
{
if (uiEngine().getPanelFromHandle(_state.defusingTimerPanelHandle))
if (uiEngine().getPanelFromHandle(state().defusingTimerPanelHandle))
return;

auto&& hudTeamCounter = context.hud().hudTeamCounter();
Expand Down Expand Up @@ -100,10 +99,9 @@ class DefusingAlertContext {
return;

defusingAlertContainer.setVisible(false);
_state.defusingAlertContainerPanelHandle = defusingAlertContainer.getHandle();
_state.defusingTimerPanelHandle = defusingTimer.getHandle();
state().defusingAlertContainerPanelHandle = defusingAlertContainer.getHandle();
state().defusingTimerPanelHandle = defusingTimer.getHandle();
}

HookContext& context;
DefusingAlertState& _state;
};
14 changes: 2 additions & 12 deletions Source/Features/Hud/HudFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,14 @@ struct HudFeatures {
return BombTimerToggle{BombTimerContext{hookContext}};
}

[[nodiscard]] auto defusingAlert() const noexcept
{
return DefusingAlert{DefusingAlertContext{hookContext, states.defusingAlertState}};
}

[[nodiscard]] auto defusingAlertToggle() const noexcept
{
return DefusingAlertToggle{DefusingAlertContext{hookContext, states.defusingAlertState}};
}

[[nodiscard]] auto killfeedPreserver() const noexcept
{
return KillfeedPreserver{KillfeedPreserverContext{states.killfeedPreserverState, hookContext}};
return DefusingAlertToggle{DefusingAlertContext{hookContext}};
}

[[nodiscard]] auto killfeedPreserveToggle() const noexcept
{
return KillfeedPreserveToggle{KillfeedPreserverContext{states.killfeedPreserverState, hookContext}};
return KillfeedPreserveToggle{KillfeedPreserverContext{hookContext}};
}

[[nodiscard]] decltype(auto) postRoundTimerToggle() const noexcept
Expand Down
9 changes: 6 additions & 3 deletions Source/Features/Hud/KillfeedPreserver/KillfeedPreserver.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

template <typename Context>
#include "KillfeedPreserverContext.h"

template <typename HookContext, typename Context = KillfeedPreserverContext<HookContext>>
class KillfeedPreserver {
public:
explicit KillfeedPreserver(Context context) noexcept
: context{context}
template <typename... Args>
KillfeedPreserver(Args&&... args) noexcept
: context{std::forward<Args>(args)...}
{
}

Expand Down
13 changes: 6 additions & 7 deletions Source/Features/Hud/KillfeedPreserver/KillfeedPreserverContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

#include "KillfeedPreserverState.h"

template <typename Context>
template <typename HookContext>
struct KillfeedPreserverContext {
KillfeedPreserverContext(KillfeedPreserverState& state, Context& context) noexcept
: _state{state}, _context{context}
KillfeedPreserverContext(HookContext& hookContext) noexcept
: hookContext{hookContext}
{
}

[[nodiscard]] decltype(auto) deathNotices() const noexcept
{
return _context.hud().deathNotices();
return hookContext.hud().deathNotices();
}

[[nodiscard]] auto preserveDeathNotice() const noexcept
Expand All @@ -25,10 +25,9 @@ struct KillfeedPreserverContext {

[[nodiscard]] auto& state() const noexcept
{
return _state;
return hookContext.featuresStates().hudFeaturesStates.killfeedPreserverState;
}

private:
KillfeedPreserverState& _state;
Context& _context;
HookContext& hookContext;
};
11 changes: 4 additions & 7 deletions Source/GlobalContext/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,11 @@ class GlobalContext {
soundWatcher.update();
fullContext().features(dependencies).soundFeatures().runOnViewMatrixUpdate();

auto&& playerInfoInWorld = dependencies.make<PlayerInfoInWorld>();
RenderingHookEntityLoop{dependencies, playerInfoInWorld}.run();
dependencies.make<RenderingHookEntityLoop>().run();
dependencies.make<GlowSceneObjects>().removeUnreferencedObjects();

fullContext().features(dependencies).hudFeatures().defusingAlert().run();
fullContext().features(dependencies).hudFeatures().killfeedPreserver().run();
BombStatusPanelManager{BombStatusPanelManagerContext{dependencies}}.run();

dependencies.make<DefusingAlert>().run();
dependencies.make<KillfeedPreserver>().run();
dependencies.make<BombStatusPanelManager>().run();
dependencies.make<InWorldPanels>().hideUnusedPanels();

UnloadFlag unloadFlag;
Expand Down
10 changes: 7 additions & 3 deletions Source/Hud/BombStatus/BombStatusPanelManager.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once

#include <utility>

#include "BombStatusPanelManagerContext.h"
#include <Common/Visibility.h>

template <typename Context>
template <typename HookContext, typename Context = BombStatusPanelManagerContext<HookContext>>
class BombStatusPanelManager {
public:
explicit BombStatusPanelManager(Context context) noexcept
: context{context}
template <typename... Args>
BombStatusPanelManager(Args&&... args) noexcept
: context{std::forward<Args>(args)...}
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DefusingAlertTest : public testing::Test {
testing::StrictMock<MockDefusingAlertCondition> mockDefusingAlertCondition;
testing::StrictMock<MockDefusingAlertPanel> mockDefusingAlertPanel;

DefusingAlert<MockDefusingAlertContext&> defusingAlert{mockDefusingAlertContext};
DefusingAlert<MockHookContext, MockDefusingAlertContext&> defusingAlert{mockDefusingAlertContext};
};

TEST_F(DefusingAlertTest, DoesNotRunIfShouldNotRun) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ class KillfeedPreserverContextTest : public testing::Test {
testing::StrictMock<MockDeathNotices> mockDeathNotices;

KillfeedPreserverState state;
KillfeedPreserverContext<MockHookContext&> killfeedPreserverContext{state, mockHookContext};
KillfeedPreserverContext<MockHookContext&> killfeedPreserverContext{mockHookContext};
};

TEST_F(KillfeedPreserverContextTest, StateCanBeAccessed) {
EXPECT_THAT(killfeedPreserverContext.state(), testing::Ref(state));
}

TEST_F(KillfeedPreserverContextTest, DeathNoticesCanBeAccessed) {
EXPECT_CALL(mockHookContext, hud()).WillOnce(testing::ReturnRef(mockHud));
EXPECT_CALL(mockHud, deathNotices()).WillOnce(testing::ReturnRef(mockDeathNotices));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class KillfeedPreserverTest : public testing::Test {
testing::StrictMock<MockKillfeedPreserverContext> mockContext;
testing::StrictMock<MockDeathNotices> mockDeathNotices;

KillfeedPreserver<MockKillfeedPreserverContext&> killfeedPreserver{mockContext};
KillfeedPreserver<MockHookContext, MockKillfeedPreserverContext&> killfeedPreserver{mockContext};
KillfeedPreserverState state;
std::function<void(MockDeathNotice&)> preserveDeathNoticeFunctor;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BombStatusPanelManagerTest : public testing::Test {
testing::StrictMock<MockBombTimer> mockBombTimer;
testing::StrictMock<MockBombStatusPanel> mockBombStatusPanel;

BombStatusPanelManager<MockBombStatusPanelManagerContext&> bombStatusPanelManager{mockContext};
BombStatusPanelManager<MockHookContext, MockBombStatusPanelManagerContext&> bombStatusPanelManager{mockContext};
};

TEST_F(BombStatusPanelManagerTest, BombStatusPanelAndBombTimerAreHiddenWhenPostRoundTimerIsVisible) {
Expand Down

0 comments on commit a5175ad

Please sign in to comment.