Skip to content

Commit

Permalink
Add PatternPool class for storing compile-time code patterns. It allo…
Browse files Browse the repository at this point in the history
…ws iterating over the patterns and results in lower binary code size.
  • Loading branch information
danielkrupinski committed Oct 16, 2024
1 parent 3ff2168 commit 9d1afc8
Show file tree
Hide file tree
Showing 79 changed files with 800 additions and 1,003 deletions.
2 changes: 1 addition & 1 deletion Source/Features/Sound/Details/SoundVisualizationFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class SoundVisualizationFeature {

auto&& transformFactory = hookContext.panoramaTransformFactory();
PanoramaTransformations{
transformFactory.scale(SoundVisualization<SoundType>::getScale(soundInClipSpace.z, ViewToProjectionMatrix{hookContext.gameDependencies().viewToProjectionMatrix}.getFovScale())),
transformFactory.scale(SoundVisualization<SoundType>::getScale(soundInClipSpace.z, ViewToProjectionMatrix{hookContext.clientPatternSearchResults().template get<ViewToProjectionMatrixPointer>()}.getFovScale())),
transformFactory.translate(deviceCoordinates.getX(), deviceCoordinates.getY())
}.applyTo(panel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PlayerInfoPanel {

[[nodiscard]] float getFovScale() const noexcept
{
return ViewToProjectionMatrix{hookContext.gameDependencies().viewToProjectionMatrix}.getFovScale();
return ViewToProjectionMatrix{hookContext.clientPatternSearchResults().template get<ViewToProjectionMatrixPointer>()}.getFovScale();
}

template <typename... PanelTypes>
Expand Down
19 changes: 7 additions & 12 deletions Source/GameClasses/BaseEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class BaseEntity {

[[nodiscard]] decltype(auto) renderComponent() const noexcept
{
return hookContext.template make<RenderComponent>(deps().offsetToRenderComponent.of(entity).valueOr(nullptr));
return hookContext.template make<RenderComponent>(hookContext.clientPatternSearchResults().template get<OffsetToRenderComponent>().of(entity).valueOr(nullptr));
}

[[nodiscard]] decltype(auto) gameSceneNode() const noexcept
{
return hookContext.template make<GameSceneNode>(deps().offsetToGameSceneNode.of(entity).valueOr(nullptr));
return hookContext.template make<GameSceneNode>(hookContext.clientPatternSearchResults().template get<OffsetToGameSceneNode>().of(entity).valueOr(nullptr));
}

template <typename F>
Expand Down Expand Up @@ -70,27 +70,27 @@ class BaseEntity {

[[nodiscard]] auto hasOwner() const noexcept
{
return deps().offsetToOwnerEntity.of(entity).toOptional().notEqual(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX});
return hookContext.clientPatternSearchResults().template get<OffsetToOwnerEntity>().of(entity).toOptional().notEqual(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX});
}

[[nodiscard]] TeamNumber teamNumber() const noexcept
{
return TeamNumber{deps().offsetToTeamNumber.of(entity).valueOr({})};
return TeamNumber{hookContext.clientPatternSearchResults().template get<OffsetToTeamNumber>().of(entity).valueOr({})};
}

[[nodiscard]] auto vData() const noexcept
{
return deps().offsetToVData.of(entity).toOptional();
return hookContext.clientPatternSearchResults().template get<OffsetToVData>().of(entity).toOptional();
}

[[nodiscard]] auto health() const noexcept
{
return deps().offsetToHealth.of(entity).toOptional();
return hookContext.clientPatternSearchResults().template get<OffsetToHealth>().of(entity).toOptional();
}

[[nodiscard]] std::optional<bool> isAlive() const noexcept
{
const auto lifestate = deps().offsetToLifeState.of(entity).get();
const auto lifestate = hookContext.clientPatternSearchResults().template get<OffsetToLifeState>().of(entity).get();
if (lifestate)
return LifeState{*lifestate} == LifeState::Alive;
return {};
Expand All @@ -102,11 +102,6 @@ class BaseEntity {
}

private:
[[nodiscard]] const auto& deps() const noexcept
{
return hookContext.gameDependencies().entityDeps;
}

[[nodiscard]] auto invokeWithGameSceneNodeOwner(auto& f) const noexcept
{
return [&f](auto&& gameSceneNode) { f(gameSceneNode.owner()); };
Expand Down
8 changes: 4 additions & 4 deletions Source/GameClasses/EntitySystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ class EntitySystem {
private:
[[nodiscard]] cs2::CGameEntitySystem* entitySystem() const noexcept
{
if (hookContext.gameDependencies().entitySystemDeps.entitySystem)
return *hookContext.gameDependencies().entitySystemDeps.entitySystem;
if (hookContext.clientPatternSearchResults().template get<EntitySystemPointer>())
return *hookContext.clientPatternSearchResults().template get<EntitySystemPointer>();
return nullptr;
}

[[nodiscard]] auto getEntityList() const noexcept
{
return hookContext.gameDependencies().entitySystemDeps.entityListOffset.of(entitySystem()).get();
return hookContext.clientPatternSearchResults().template get<EntityListOffset>().of(entitySystem()).get();
}

[[nodiscard]] auto getHighestEntityIndex() const noexcept
{
const auto highestEntityIndex = hookContext.gameDependencies().entitySystemDeps.highestEntityIndexOffset.of(entitySystem()).get();
const auto highestEntityIndex = hookContext.clientPatternSearchResults().template get<HighestEntityIndexOffset>().of(entitySystem()).get();
if (highestEntityIndex && highestEntityIndex->isValid())
return *highestEntityIndex;
return cs2::kMaxValidEntityIndex;
Expand Down
11 changes: 2 additions & 9 deletions Source/GameClasses/GameRules.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <optional>

#include <GameDependencies/GameRulesDeps.h>

template <typename HookContext>
class GameRules {
public:
Expand All @@ -15,12 +13,12 @@ class GameRules {

[[nodiscard]] auto roundStartTime() const noexcept
{
return deps().roundStartTimeOffset.of(gameRules).toOptional();
return hookContext.clientPatternSearchResults().template get<RoundStartTimeOffset>().of(gameRules).toOptional();
}

[[nodiscard]] auto roundRestartTime() const noexcept
{
return deps().offsetToRoundRestartTime.of(gameRules).toOptional();
return hookContext.clientPatternSearchResults().template get<OffsetToRoundRestartTime>().of(gameRules).toOptional();
}

[[nodiscard]] bool hasScheduledRoundRestart() const noexcept
Expand All @@ -34,11 +32,6 @@ class GameRules {
}

private:
[[nodiscard]] const auto& deps() const noexcept
{
return hookContext.gameDependencies().gameRulesDeps;
}

HookContext& hookContext;
cs2::C_CSGameRules* gameRules;
};
13 changes: 4 additions & 9 deletions Source/GameClasses/GameSceneNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class GameSceneNode {

[[nodiscard]] auto absOrigin() const noexcept
{
return deps().offsetToAbsOrigin.of(gameSceneNode).toOptional();
return hookContext->clientPatternSearchResults().template get<OffsetToAbsOrigin>().of(gameSceneNode).toOptional();
}

[[nodiscard]] decltype(auto) owner() const noexcept
{
return hookContext->template make<BaseEntity>(static_cast<cs2::C_BaseEntity*>(deps().offsetToOwner.of(gameSceneNode).valueOr(nullptr)));
return hookContext->template make<BaseEntity>(static_cast<cs2::C_BaseEntity*>(hookContext->clientPatternSearchResults().template get<OffsetToGameSceneNodeOwner>().of(gameSceneNode).valueOr(nullptr)));
}

template <typename F>
Expand All @@ -39,17 +39,12 @@ class GameSceneNode {
private:
[[nodiscard]] decltype(auto) child() const noexcept
{
return hookContext->template make<GameSceneNode<HookContext>>(deps().offsetToChild.of(gameSceneNode).valueOr(nullptr));
return hookContext->template make<GameSceneNode<HookContext>>(hookContext->clientPatternSearchResults().template get<OffsetToChildGameSceneNode>().of(gameSceneNode).valueOr(nullptr));
}

[[nodiscard]] decltype(auto) nextSibling() const noexcept
{
return hookContext->template make<GameSceneNode<HookContext>>(deps().offsetToNextSibling.of(gameSceneNode).valueOr(nullptr));
}

[[nodiscard]] const auto& deps() const noexcept
{
return hookContext->gameDependencies().gameSceneNodeDeps;
return hookContext->template make<GameSceneNode<HookContext>>(hookContext->clientPatternSearchResults().template get<OffsetToNextSiblingGameSceneNode>().of(gameSceneNode).valueOr(nullptr));
}

HookContext* hookContext;
Expand Down
7 changes: 1 addition & 6 deletions Source/GameClasses/HostageServices.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ class HostageServices {

[[nodiscard]] bool hasCarriedHostage() const noexcept
{
return hookContext.template make<EntitySystem>().getEntityFromHandle(deps().offsetToCarriedHostage.of(hostageServices).valueOr(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX})) != nullptr;
return hookContext.template make<EntitySystem>().getEntityFromHandle(hookContext.clientPatternSearchResults().template get<OffsetToCarriedHostage>().of(hostageServices).valueOr(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX})) != nullptr;
}

private:
[[nodiscard]] const auto& deps() const noexcept
{
return hookContext.gameDependencies().hostageServicesDeps;
}

HookContext& hookContext;
cs2::CCSPlayer_HostageServices* hostageServices;
};
2 changes: 1 addition & 1 deletion Source/GameClasses/Hud/HudContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct HudContext {

[[nodiscard]] auto panel() noexcept
{
auto&& hud = hookContext.gameDependencies().hudDeps.hud;
auto&& hud = hookContext.clientPatternSearchResults().template get<HudPanelPointer>();
if (hud && *hud)
return hookContext.template make<PanoramaUiPanel>((*hud)->uiPanel);
return hookContext.template make<PanoramaUiPanel>(nullptr);
Expand Down
2 changes: 1 addition & 1 deletion Source/GameClasses/MemAlloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MemAlloc {
if (!deps().thisptr || !*deps().thisptr)
return nullptr;

if (const auto fn = deps().alloc.of((*deps().thisptr)->vmt).get())
if (const auto fn = hookContext.clientPatternSearchResults().template get<OffsetAllocVirtualMethod>().of((*deps().thisptr)->vmt).get())
return (*fn)(*deps().thisptr, size);

return nullptr;
Expand Down
17 changes: 0 additions & 17 deletions Source/GameClasses/OffsetTypes/EntityOffset.h

This file was deleted.

10 changes: 0 additions & 10 deletions Source/GameClasses/OffsetTypes/GameRulesOffset.h

This file was deleted.

14 changes: 0 additions & 14 deletions Source/GameClasses/OffsetTypes/GameSceneNodeOffset.h

This file was deleted.

13 changes: 0 additions & 13 deletions Source/GameClasses/OffsetTypes/GlowSceneObjectOffset.h

This file was deleted.

12 changes: 0 additions & 12 deletions Source/GameClasses/OffsetTypes/PanoramaImagePanelOffset.h

This file was deleted.

40 changes: 24 additions & 16 deletions Source/GameClasses/PanelFactory.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <GameClasses/PanoramaImagePanel.h>
#include <GameDependencies/PanoramaImagePanelDeps.h>
#include <GameDependencies/PanoramaLabelDeps.h>

template <typename HookContext>
struct PanelFactory {
Expand All @@ -13,47 +11,57 @@ struct PanelFactory {

[[nodiscard]] decltype(auto) createPanel(cs2::CUIPanel* parentPanel, const char* id = "") noexcept
{
if (parentPanel && panelDeps().create)
return hookContext.template make<ClientPanel>(panelDeps().create(id, parentPanel));
if (parentPanel && panelConstructor())
return hookContext.template make<ClientPanel>(panelConstructor()(id, parentPanel));
return hookContext.template make<ClientPanel>(nullptr);
}

[[nodiscard]] decltype(auto) createLabelPanel(cs2::CUIPanel* parentPanel, const char* id = "") const noexcept
{
if (!parentPanel || !labelPanelDeps().constructor || !labelPanelDeps().size)
if (!parentPanel || !labelPanelConstructor() || !labelPanelSize())
return hookContext.template make<ClientPanel>(nullptr);

const auto memory{static_cast<cs2::CLabel*>(hookContext.template make<MemAlloc>().allocate(*labelPanelDeps().size))};
const auto memory{static_cast<cs2::CLabel*>(hookContext.template make<MemAlloc>().allocate(labelPanelSize()))};
if (memory)
labelPanelDeps().constructor(memory, parentPanel->clientPanel, id);
labelPanelConstructor()(memory, parentPanel->clientPanel, id);
return hookContext.template make<ClientPanel>(memory);
}

[[nodiscard]] decltype(auto) createImagePanel(cs2::CUIPanel* parentPanel, const char* id = "") noexcept
{
if (!parentPanel || !imagePanelDeps().constructor || !imagePanelDeps().size)
if (!parentPanel || !imagePanelConstructor() || !imagePanelSize())
return hookContext.template make<ClientPanel>(nullptr).template as<PanoramaImagePanel>();

const auto memory{static_cast<cs2::CImagePanel*>(hookContext.template make<MemAlloc>().allocate(*imagePanelDeps().size))};
const auto memory{static_cast<cs2::CImagePanel*>(hookContext.template make<MemAlloc>().allocate(imagePanelSize()))};
if (memory)
imagePanelDeps().constructor(memory, parentPanel->clientPanel, id);
imagePanelConstructor()(memory, parentPanel->clientPanel, id);
return hookContext.template make<ClientPanel>(memory).template as<PanoramaImagePanel>();
}

private:
[[nodiscard]] const auto& panelDeps() const noexcept
[[nodiscard]] auto panelConstructor() const noexcept
{
return hookContext.gameDependencies().panelDeps;
return hookContext.clientPatternSearchResults().template get<PanelConstructorPointer>();
}

[[nodiscard]] const auto& imagePanelDeps() const noexcept
[[nodiscard]] auto imagePanelConstructor() const noexcept
{
return hookContext.gameDependencies().imagePanelDeps;
return hookContext.clientPatternSearchResults().template get<ImagePanelConstructorPointer>();
}

[[nodiscard]] const auto& labelPanelDeps() const noexcept
[[nodiscard]] auto imagePanelSize() const noexcept
{
return hookContext.gameDependencies().panoramaLabelDeps;
return hookContext.clientPatternSearchResults().template get<ImagePanelClassSize>();
}

[[nodiscard]] auto labelPanelConstructor() const noexcept
{
return hookContext.clientPatternSearchResults().template get<LabelPanelConstructorPointer>();
}

[[nodiscard]] auto labelPanelSize() const noexcept
{
return hookContext.clientPatternSearchResults().template get<LabelPanelObjectSize>();
}

HookContext& hookContext;
Expand Down
12 changes: 3 additions & 9 deletions Source/GameClasses/PanoramaImagePanel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <CS2/Panorama/CImagePanel.h>
#include <GameDependencies/PanoramaImagePanelDeps.h>

#include "PanoramaImagePanelContext.h"

Expand All @@ -25,7 +24,7 @@ struct PanoramaImagePanel {

[[nodiscard]] cs2::ImageProperties* getImageProperties() const noexcept
{
return deps().imagePropertiesOffset.of(context.panel).get();
return context.hookContext.clientPatternSearchResults().template get<ImagePropertiesOffset>().of(context.panel).get();
}

[[nodiscard]] std::string_view getImagePath() const noexcept
Expand All @@ -46,16 +45,11 @@ struct PanoramaImagePanel {

properties->scale = context.uiPanel().getUiScaleFactor().valueOr(1.0f);
properties->textureHeight = textureHeight;
if (deps().setImage)
deps().setImage(context.panel, imageUrl, nullptr, properties);
if (context.hookContext.clientPatternSearchResults().template get<SetImageFunctionPointer>())
context.hookContext.clientPatternSearchResults().template get<SetImageFunctionPointer>()(context.panel, imageUrl, nullptr, properties);
}

private:
[[nodiscard]] const auto& deps() const noexcept
{
return context.deps();
}

Context context;
};

Expand Down
Loading

0 comments on commit 9d1afc8

Please sign in to comment.