Skip to content

Commit

Permalink
Add WeaponServices class
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Sep 24, 2024
1 parent 8f6d02a commit 8e0ec44
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Source/GameClasses/PlayerPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "BaseEntity.h"
#include "GameSceneNode.h"
#include "PlayerWeapons.h"
#include "WeaponServices.h"

class EntityFromHandleFinder;

Expand All @@ -31,12 +32,14 @@ class PlayerPawn {
return hookContext.template make<BaseEntity>(playerPawn);
}

[[nodiscard]] decltype(auto) weaponServices() const noexcept
{
return hookContext.template make<WeaponServices>(hookContext.gameDependencies().playerPawnDeps.offsetToWeaponServices.of(playerPawn).valueOr(nullptr));
}

[[nodiscard]] decltype(auto) weapons() const noexcept
{
const auto weaponServices = hookContext.gameDependencies().playerPawnDeps.offsetToWeaponServices.of(playerPawn).valueOr(nullptr);
if (!weaponServices)
return hookContext.template make<PlayerWeapons>(nullptr);
return hookContext.template make<PlayerWeapons>(hookContext.gameDependencies().weaponServicesDeps.offsetToWeapons.of(weaponServices).get());
return weaponServices().weapons();
}

[[nodiscard]] TeamNumber teamNumber() const noexcept
Expand Down Expand Up @@ -128,11 +131,7 @@ class PlayerPawn {

[[nodiscard]] cs2::C_CSWeaponBase* getActiveWeapon() const noexcept
{
const auto weaponServices = hookContext.gameDependencies().playerPawnDeps.offsetToWeaponServices.of(playerPawn).valueOr(nullptr);
if (!weaponServices)
return nullptr;

return static_cast<cs2::C_CSWeaponBase*>(hookContext.template make<EntitySystem>().getEntityFromHandle(hookContext.gameDependencies().weaponServicesDeps.offsetToActiveWeapon.of(weaponServices).valueOr(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX})));
return weaponServices().getActiveWeapon();
}

private:
Expand Down
34 changes: 34 additions & 0 deletions Source/GameClasses/WeaponServices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <CS2/Classes/CCSPlayer_WeaponServices.h>
#include <CS2/Classes/Entities/C_CSWeaponBase.h>
#include "PlayerWeapons.h"

template <typename HookContext>
class WeaponServices {
public:
WeaponServices(HookContext& hookContext, cs2::CCSPlayer_WeaponServices* weaponServices) noexcept
: hookContext{hookContext}
, weaponServices{weaponServices}
{
}

[[nodiscard]] decltype(auto) weapons() const noexcept
{
return hookContext.template make<PlayerWeapons>(deps().offsetToWeapons.of(weaponServices).valueOr(nullptr));
}

[[nodiscard]] cs2::C_CSWeaponBase* getActiveWeapon() const noexcept
{
return static_cast<cs2::C_CSWeaponBase*>(hookContext.template make<EntitySystem>().getEntityFromHandle(deps().offsetToActiveWeapon.of(weaponServices).valueOr(cs2::CEntityHandle{cs2::INVALID_EHANDLE_INDEX})));
}

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

HookContext& hookContext;
cs2::CCSPlayer_WeaponServices* weaponServices;
};
1 change: 1 addition & 0 deletions Source/Osiris.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
<ClInclude Include="GameClasses\SceneSystem.h" />
<ClInclude Include="GameClasses\SmokeGrenadeProjectile.h" />
<ClInclude Include="GameClasses\TopLevelWindow.h" />
<ClInclude Include="GameClasses\WeaponServices.h" />
<ClInclude Include="GameDependencies\ConVarDeps.h" />
<ClInclude Include="GameDependencies\ConVars.h" />
<ClInclude Include="GameDependencies\ConVarsBase.h" />
Expand Down
3 changes: 3 additions & 0 deletions Source/Osiris.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,9 @@
<ClInclude Include="CS2\Constants\StylePropertyTypeNames.h">
<Filter>CS2\Constants</Filter>
</ClInclude>
<ClInclude Include="GameClasses\WeaponServices.h">
<Filter>GameClasses</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="UI\Panorama\CreateGUI.js">
Expand Down

0 comments on commit 8e0ec44

Please sign in to comment.