Skip to content

Commit

Permalink
Refactor PlantedC4 class
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Oct 1, 2024
1 parent d0d6292 commit c5f2f8d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Source/Features/Visuals/OutlineGlow/OutlineGlowContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OutlineGlowContext {

void applyGlowToPlantedBomb(auto& entity) const noexcept
{
hookContext.template make<TickingBombOutlineGlow>().applyGlowToPlantedBomb(PlantedC4{PlantedC4Base{static_cast<cs2::CPlantedC4*>(&entity)}, hookContext});
hookContext.template make<TickingBombOutlineGlow>().applyGlowToPlantedBomb(hookContext.template make<PlantedC4<HookContext>>(static_cast<cs2::CPlantedC4*>(&entity)));
}

void applyGlowToHostage(auto& entity) const noexcept
Expand Down
67 changes: 46 additions & 21 deletions Source/GameClasses/PlantedC4.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,109 @@
#include <GameDependencies/PlantedC4Deps.h>
#include <GameClasses/GlobalVars.h>

struct PlantedC4Base {
template <typename HookContext>
class PlantedC4Context {
public:
PlantedC4Context(HookContext& hookContext, cs2::CPlantedC4* plantedC4) noexcept
: hookContext{hookContext}
, plantedC4{plantedC4}
{
}

[[nodiscard]] auto blowTime() const noexcept
{
return PlantedC4Deps::instance().blowTime.of(thisptr);
return deps().blowTime.of(plantedC4);
}

[[nodiscard]] auto defuser() const noexcept
{
return PlantedC4Deps::instance().defuser.of(thisptr);
return deps().defuser.of(plantedC4);
}

[[nodiscard]] auto defuseEndTime() const noexcept
{
return PlantedC4Deps::instance().defuseEndTime.of(thisptr);
return deps().defuseEndTime.of(plantedC4);
}

[[nodiscard]] auto bombSite() const noexcept
{
return PlantedC4Deps::instance().bombSite.of(thisptr);
return deps().bombSite.of(plantedC4);
}

[[nodiscard]] auto ticking() const noexcept
{
return PlantedC4Deps::instance().ticking.of(thisptr);
return deps().ticking.of(plantedC4);
}

[[nodiscard]] decltype(auto) baseEntity() const noexcept
{
return hookContext.template make<BaseEntity>(plantedC4);
}

[[nodiscard]] decltype(auto) curtime() const noexcept
{
return hookContext.globalVars().curtime();
}

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

cs2::CPlantedC4* thisptr;
HookContext& hookContext;
cs2::CPlantedC4* plantedC4;
};

template <typename Dependencies>
struct PlantedC4 {
explicit PlantedC4(PlantedC4Base base, Dependencies& dependencies) noexcept
: base{base}
, dependencies{dependencies}
template <typename HookContext, typename Context = PlantedC4Context<HookContext>>
class PlantedC4 {
public:
template <typename... Args>
explicit PlantedC4(Args&&... args) noexcept
: context{std::forward<Args>(args)...}
{
}

[[nodiscard]] decltype(auto) baseEntity() const noexcept
{
return dependencies.template make<BaseEntity>(base.thisptr);
return context.baseEntity();
}

[[nodiscard]] auto getTimeToExplosion() const noexcept
{
return base.blowTime().toOptional() - dependencies.globalVars().curtime();
return context.blowTime().toOptional() - context.curtime();
}

[[nodiscard]] auto isTicking() const noexcept
{
return base.ticking().toOptional();
return context.ticking().toOptional();
}

[[nodiscard]] bool isBeingDefused() const noexcept
{
return base.defuser().valueOr(cs2::INVALID_EHANDLE_INDEX) != cs2::INVALID_EHANDLE_INDEX;
return context.defuser().valueOr(cs2::INVALID_EHANDLE_INDEX) != cs2::INVALID_EHANDLE_INDEX;
}

[[nodiscard]] auto canBeDefused() const noexcept
{
return base.defuseEndTime().toOptional().lessThan(base.blowTime().toOptional());
return context.defuseEndTime().toOptional().lessThan(context.blowTime().toOptional());
}

[[nodiscard]] auto getTimeToDefuseEnd() const noexcept
{
return base.defuseEndTime().toOptional() - dependencies.globalVars().curtime();
return context.defuseEndTime().toOptional() - context.curtime();
}

[[nodiscard]] const char* getBombSiteIconUrl() const noexcept
{
constexpr auto INVALID_BOMBSITE_INDEX = -1;
switch (base.bombSite().valueOr(INVALID_BOMBSITE_INDEX)) {
switch (context.bombSite().valueOr(INVALID_BOMBSITE_INDEX)) {
case cs2::BOMBSITE_A_INDEX: return cs2::kBombSiteAIconUrl;
case cs2::BOMBSITE_B_INDEX: return cs2::kBombSiteBIconUrl;
default: return nullptr;
}
}

private:
PlantedC4Base base;
Dependencies& dependencies;
Context context;
};
5 changes: 0 additions & 5 deletions Source/GameDependencies/DepsInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ inline HudDeps& HudDeps::instance() noexcept
return GlobalContext::instance().fullContext().gameDependencies.hudDeps;
}

inline const PlantedC4Deps& PlantedC4Deps::instance() noexcept
{
return GlobalContext::instance().fullContext().gameDependencies.plantedC4Deps;
}

inline const FileNameSymbolTableDeps& FileNameSymbolTableDeps::instance() noexcept
{
return GlobalContext::instance().fullContext().gameDependencies.fileNameSymbolTableDeps;
Expand Down
2 changes: 0 additions & 2 deletions Source/GameDependencies/PlantedC4Deps.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ struct PlantedC4Deps {
{
}

[[nodiscard]] static const PlantedC4Deps& instance() noexcept;

cs2::CUtlVector<cs2::CPlantedC4*>* plantedC4s;

BombSiteOffset bombSite;
Expand Down
5 changes: 1 addition & 4 deletions Source/HookDependencies/HookDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ struct HookDependencies {

[[nodiscard]] auto plantedC4() noexcept
{
const auto base = PlantedC4Base{getPlantedC4()};
if (base.thisptr)
return std::optional<PlantedC4<HookDependencies&>>{PlantedC4<HookDependencies&>{base, *this}};
return std::optional<PlantedC4<HookDependencies&>>{};
return std::optional{make<PlantedC4<HookDependencies>>(getPlantedC4())};
}

[[nodiscard]] ConVarAccessor getConVarAccessor() noexcept
Expand Down

0 comments on commit c5f2f8d

Please sign in to comment.