diff --git a/Source/GameClasses/GameRules.h b/Source/GameClasses/GameRules.h index c6dad6bcc7a..1891d00fd04 100644 --- a/Source/GameClasses/GameRules.h +++ b/Source/GameClasses/GameRules.h @@ -14,16 +14,12 @@ struct GameRules { [[nodiscard]] std::optional 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 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 diff --git a/Source/GameClasses/PlayerPawn.h b/Source/GameClasses/PlayerPawn.h index 005d94dd96e..872dba11391 100644 --- a/Source/GameClasses/PlayerPawn.h +++ b/Source/GameClasses/PlayerPawn.h @@ -64,10 +64,7 @@ class PlayerPawn { [[nodiscard]] std::optional 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 healthColor() const noexcept @@ -79,10 +76,7 @@ class PlayerPawn { [[nodiscard]] std::optional 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 absOrigin() const noexcept @@ -90,11 +84,7 @@ class PlayerPawn { 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 @@ -115,18 +105,12 @@ class PlayerPawn { [[nodiscard]] std::optional 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 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 diff --git a/Source/Utils/FieldOffset.h b/Source/Utils/FieldOffset.h index 9e4b9ccdaa8..fa8ddc1e5e9 100644 --- a/Source/Utils/FieldOffset.h +++ b/Source/Utils/FieldOffset.h @@ -1,6 +1,7 @@ #pragma once #include +#include template struct FieldValueProxy { @@ -14,6 +15,13 @@ struct FieldValueProxy { return field; } + [[nodiscard]] std::optional toOptional() const noexcept + { + if (field) + return *field; + return {}; + } + [[nodiscard]] FieldType valueOr(const FieldType& defaultValue) const noexcept { if (field)