Skip to content

Commit

Permalink
Add FieldValueProxy::toOptional() method
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Sep 5, 2024
1 parent aab3267 commit b70c7ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
8 changes: 2 additions & 6 deletions Source/GameClasses/GameRules.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ struct GameRules {

[[nodiscard]] std::optional<float> roundStartTime() const noexcept
{
if (gameRules && GameRulesDeps::instance().roundStartTimeOffset)
return *GameRulesDeps::instance().roundStartTimeOffset.of(gameRules).get();
return {};
return GameRulesDeps::instance().roundStartTimeOffset.of(gameRules).toOptional();
}

[[nodiscard]] std::optional<float> roundRestartTime() const noexcept
{
if (gameRules && GameRulesDeps::instance().offsetToRoundRestartTime)
return *GameRulesDeps::instance().offsetToRoundRestartTime.of(gameRules).get();
return {};
return GameRulesDeps::instance().offsetToRoundRestartTime.of(gameRules).toOptional();
}

[[nodiscard]] bool hasScheduledRoundRestart() const noexcept
Expand Down
26 changes: 5 additions & 21 deletions Source/GameClasses/PlayerPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ class PlayerPawn {

[[nodiscard]] std::optional<int> health() const noexcept
{
const auto health = hookContext.gameDependencies().entityDeps.offsetToHealth.of(playerPawn).get();
if (health)
return *health;
return {};
return hookContext.gameDependencies().entityDeps.offsetToHealth.of(playerPawn).toOptional();
}

[[nodiscard]] std::optional<cs2::Color> healthColor() const noexcept
Expand All @@ -79,22 +76,15 @@ class PlayerPawn {

[[nodiscard]] std::optional<bool> hasImmunity() const noexcept
{
const auto immunity = hookContext.gameDependencies().playerPawnDeps.offsetToPlayerPawnImmunity.of(playerPawn).get();
if (immunity)
return *immunity;
return {};
return hookContext.gameDependencies().playerPawnDeps.offsetToPlayerPawnImmunity.of(playerPawn).toOptional();
}

[[nodiscard]] std::optional<cs2::Vector> absOrigin() const noexcept
{
const auto gameSceneNode = hookContext.gameDependencies().entityDeps.offsetToGameSceneNode.of(playerPawn).get();
if (!gameSceneNode || !*gameSceneNode)
return {};

const auto absOrigin = hookContext.gameDependencies().gameSceneNodeDeps.offsetToAbsOrigin.of(*gameSceneNode).get();
if (absOrigin)
return *absOrigin;
return {};
return hookContext.gameDependencies().gameSceneNodeDeps.offsetToAbsOrigin.of(*gameSceneNode).toOptional();
}

[[nodiscard]] bool isControlledByLocalPlayer() const noexcept
Expand All @@ -115,18 +105,12 @@ class PlayerPawn {

[[nodiscard]] std::optional<bool> isPickingUpHostage() const noexcept
{
const auto pickingUpHostage = hookContext.gameDependencies().playerPawnDeps.offsetToIsPickingUpHostage.of(playerPawn).get();
if (pickingUpHostage)
return *pickingUpHostage;
return {};
return hookContext.gameDependencies().playerPawnDeps.offsetToIsPickingUpHostage.of(playerPawn).toOptional();
}

[[nodiscard]] std::optional<bool> isDefusing() const noexcept
{
const auto defusing = hookContext.gameDependencies().playerPawnDeps.offsetToIsDefusing.of(playerPawn).get();
if (defusing)
return *defusing;
return {};
return hookContext.gameDependencies().playerPawnDeps.offsetToIsDefusing.of(playerPawn).toOptional();
}

[[nodiscard]] bool isRescuingHostage() const noexcept
Expand Down
8 changes: 8 additions & 0 deletions Source/Utils/FieldOffset.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <concepts>
#include <optional>

template <typename FieldType>
struct FieldValueProxy {
Expand All @@ -14,6 +15,13 @@ struct FieldValueProxy {
return field;
}

[[nodiscard]] std::optional<FieldType> toOptional() const noexcept
{
if (field)
return *field;
return {};
}

[[nodiscard]] FieldType valueOr(const FieldType& defaultValue) const noexcept
{
if (field)
Expand Down

0 comments on commit b70c7ac

Please sign in to comment.