Skip to content

Commit

Permalink
Hide accessing curtime in functions directly needing it
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Jun 14, 2024
1 parent efdc020 commit 01a3baa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
11 changes: 2 additions & 9 deletions Source/Features/Hud/BombTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,14 @@ class BombTimer {
updatePanelHandles();
hideBombStatusPanel();

const auto globalVars = hookDependencies.globalVars();
if (!globalVars || !hookDependencies.requestDependency<PlantedC4>()) {
state.hideBombTimerPanel();
return;
}

const auto curtime = globalVars->curtime();
if (!curtime) {
if (!hookDependencies.requestDependency<PlantedC4>()) {
state.hideBombTimerPanel();
return;
}

const PlantedC4 bomb{hookDependencies.getDependency<PlantedC4>()};

if (const auto timeToExplosion = bomb.getTimeToExplosion(*curtime); timeToExplosion > 0.0f) {
if (const auto timeToExplosion = bomb.getTimeToExplosion(); timeToExplosion > 0.0f) {
showBombTimerPanel(bomb.getBombSiteIconUrl(), timeToExplosion);
} else {
state.restorePanels();
Expand Down
18 changes: 3 additions & 15 deletions Source/Features/Hud/DefusingAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,13 @@ class DefusingAlert {
return;
}

const auto globalVars = hookDependencies.globalVars();
if (!globalVars) {
state.hideDefusingAlert();
return;
}

const auto curtime = globalVars->curtime();
if (!curtime) {
state.hideDefusingAlert();
return;
}

const auto timeToEnd = bomb.getTimeToDefuseEnd(*curtime);
if (timeToEnd > 0.0f) {
const auto timeToEnd = bomb.getTimeToDefuseEnd();
if (timeToEnd && *timeToEnd > 0.0f) {
if (const auto defusingAlertContainer = state.defusingAlertContainerPanel.get())
defusingAlertContainer.setVisible(true);

if (const auto defusingTimer = state.defusingTimerPanel.get()) {
PanoramaLabel{ static_cast<cs2::CLabel*>(defusingTimer.getClientPanel()) }.setTextInternal(DefusingCountdownStringBuilder{}(timeToEnd), 0, true);
PanoramaLabel{ static_cast<cs2::CLabel*>(defusingTimer.getClientPanel()) }.setTextInternal(DefusingCountdownStringBuilder{}(*timeToEnd), 0, true);
if (const auto style = defusingTimer.getStyle())
hookDependencies.getDependency<PanelConfigurator>().panelStyle(*style).setSimpleForegroundColor(getTimerColor(bomb));
}
Expand Down
18 changes: 9 additions & 9 deletions Source/Features/Hud/KillfeedPreserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ class KillfeedPreserver {
if (!gameRules || !*gameRules)
return;

const auto globalVars = hookDependencies.globalVars();
if (!globalVars)
return;

const auto curtime = globalVars->curtime();
if (!curtime)
return;

const auto roundStartTime = GameRules{(*gameRules)}.getRoundStartTime();

initSymbols();
Expand All @@ -70,12 +62,20 @@ class KillfeedPreserver {
StringParser{ spawnTimeString }.parseFloat(spawnTime);

if (spawnTime > roundStartTime) {
panel.setAttributeString(state.spawnTimeSymbol, StringBuilderStorage<20>{}.builder().put(static_cast<std::uint64_t>(*curtime), '.', '0').cstring());
markAsJustSpawned(panel);
}
}
}

private:
void markAsJustSpawned(PanoramaUiPanel deathNotice) noexcept
{
if (const auto globalVars = hookDependencies.globalVars()) {
if (const auto curtime = globalVars->curtime())
deathNotice.setAttributeString(state.spawnTimeSymbol, StringBuilderStorage<20>{}.builder().put(static_cast<std::uint64_t>(*curtime), '.', '0').cstring());
}
}

[[nodiscard]] PanoramaUiPanel getDeathNoticesPanel() noexcept
{
if (const auto deathNoticesPanel = state.deathNoticesPointer.get())
Expand Down
2 changes: 1 addition & 1 deletion Source/GameClasses/GlobalVars/GlobalVarsCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include <CS2/Classes/GlobalVars.h>

struct GlobalVarsCache {
cs2::GlobalVars* globalVars;
cs2::GlobalVars* globalVars{nullptr};
std::optional<float> curtime;
};
21 changes: 15 additions & 6 deletions Source/GameClasses/PlantedC4.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
#include <CS2/Constants/BombSiteIndex.h>
#include <CS2/Constants/EntityHandle.h>
#include <GameDependencies/PlantedC4Deps.h>
#include "GlobalVars/GlobalVarsOptional.h"

struct PlantedC4 {
explicit PlantedC4(cs2::CPlantedC4& thisptr, const PlantedC4Deps& deps) noexcept
explicit PlantedC4(cs2::CPlantedC4& thisptr, const PlantedC4Deps& deps, GlobalVarsOptional globalVars) noexcept
: thisptr{thisptr}
, deps{deps}
, globalVars{globalVars}
{
}

[[nodiscard]] float getTimeToExplosion(float curtime) const noexcept
[[nodiscard]] float getTimeToExplosion() const noexcept
{
if (ticking())
return deps.blowTime.of(&thisptr).valueOr(0.0f) - curtime;
if (ticking() && globalVars) {
if (const auto curtime = globalVars->curtime())
return deps.blowTime.of(&thisptr).valueOr(0.0f) - *curtime;
}
return -1.0f;
}

Expand All @@ -34,9 +38,13 @@ struct PlantedC4 {
return {};
}

[[nodiscard]] float getTimeToDefuseEnd(float curtime) const noexcept
[[nodiscard]] std::optional<float> getTimeToDefuseEnd() const noexcept
{
return deps.defuseEndTime.of(&thisptr).valueOr(0.0f) - curtime;
if (globalVars) {
if (const auto curtime = globalVars->curtime())
return deps.defuseEndTime.of(&thisptr).valueOr(0.0f) - *curtime;
}
return {};
}

[[nodiscard]] const char* getBombSiteIconUrl() const noexcept
Expand All @@ -57,4 +65,5 @@ struct PlantedC4 {

cs2::CPlantedC4& thisptr;
const PlantedC4Deps& deps;
GlobalVarsOptional globalVars;
};
2 changes: 1 addition & 1 deletion Source/HookDependencies/HookDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct HookDependencies {
} else if constexpr (std::is_same_v<Dependency, FovScale>) {
return fovScale;
} else if constexpr (std::is_same_v<Dependency, PlantedC4>) {
return PlantedC4{*plantedC4, _gameDependencies.plantedC4Deps};
return PlantedC4{*plantedC4, _gameDependencies.plantedC4Deps, globalVars()};
} else if constexpr (std::is_same_v<Dependency, SoundChannels>) {
return (*soundChannels);
} else if constexpr (std::is_same_v<Dependency, FileSystem>) {
Expand Down

0 comments on commit 01a3baa

Please sign in to comment.