Skip to content

Commit

Permalink
SDK: Various utilities for the localplayer and pawn
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 10, 2023
1 parent 06b71e4 commit 8846582
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ set(CMKR_TARGET sdk)
set(sdk_SOURCES "")

list(APPEND sdk_SOURCES
"shared/sdk/APlayerController.cpp"
"shared/sdk/CVar.cpp"
"shared/sdk/ConsoleManager.cpp"
"shared/sdk/DynamicRHI.cpp"
Expand All @@ -338,6 +339,7 @@ list(APPEND sdk_SOURCES
"shared/sdk/UObjectBase.cpp"
"shared/sdk/UProperty.cpp"
"shared/sdk/Utility.cpp"
"shared/sdk/APlayerController.hpp"
"shared/sdk/CVar.hpp"
"shared/sdk/ConsoleManager.hpp"
"shared/sdk/DynamicRHI.hpp"
Expand Down Expand Up @@ -417,6 +419,7 @@ set(CMKR_TARGET sdk-nolog)
set(sdk-nolog_SOURCES "")

list(APPEND sdk-nolog_SOURCES
"shared/sdk/APlayerController.cpp"
"shared/sdk/CVar.cpp"
"shared/sdk/ConsoleManager.cpp"
"shared/sdk/DynamicRHI.cpp"
Expand All @@ -441,6 +444,7 @@ list(APPEND sdk-nolog_SOURCES
"shared/sdk/UObjectBase.cpp"
"shared/sdk/UProperty.cpp"
"shared/sdk/Utility.cpp"
"shared/sdk/APlayerController.hpp"
"shared/sdk/CVar.hpp"
"shared/sdk/ConsoleManager.hpp"
"shared/sdk/DynamicRHI.hpp"
Expand Down
7 changes: 7 additions & 0 deletions shared/sdk/APlayerController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "APlayerController.hpp"

namespace sdk {
APawn* APlayerController::get_acknowledged_pawn() const {
return get_property<APawn*>(L"AcknowledgedPawn");
}
}
14 changes: 14 additions & 0 deletions shared/sdk/APlayerController.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "UObject.hpp"

namespace sdk {
class APawn;

class APlayerController : public UObject {
public:
APawn* get_acknowledged_pawn() const;

protected:
};
}
32 changes: 28 additions & 4 deletions shared/sdk/UEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,44 @@ UEngine* UEngine::get() {
return *engine;
}

UWorld* UEngine::get_world() {
ULocalPlayer* UEngine::get_localplayer(int32_t index) {
if (index < 0) {
return nullptr;
}

const auto& game_instance = get_property<sdk::UObject*>(L"GameInstance");

if (game_instance == nullptr) {
return nullptr;
}

const auto& localplayers = game_instance->get_property<sdk::TArray<sdk::UObject*>>(L"LocalPlayers");
const auto& localplayers = game_instance->get_property<sdk::TArray<sdk::ULocalPlayer*>>(L"LocalPlayers");

if (localplayers.count == 0) {
if (localplayers.count <= index) {
return nullptr;
}

const auto localplayer = localplayers.data[0];
return localplayers.data[index];
}

APawn* UEngine::get_localpawn(int32_t index) {
const auto player = (UObject*)get_localplayer(index);

if (player == nullptr) {
return nullptr;
}

const auto player_controller = (UObject*)player->get_property<sdk::APlayerController*>(L"PlayerController");

if (player_controller == nullptr) {
return nullptr;
}

return player_controller->get_property<sdk::APawn*>(L"AcknowledgedPawn");
}

UWorld* UEngine::get_world() {
const auto localplayer = (sdk::UObject*)get_localplayer(0);

if (localplayer == nullptr) {
return nullptr;
Expand Down
6 changes: 5 additions & 1 deletion shared/sdk/UEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

namespace sdk {
class UWorld;
class ULocalPlayer;
class APawn;
class APlayerController;

class UEngine : public sdk::UObject {
public:
static UEngine** get_lvalue();
static UEngine* get();

ULocalPlayer* get_localplayer(int32_t index = 0);
APawn* get_localpawn(int32_t index = 0);
UWorld* get_world();

public:
void initialize_hmd_device();
static std::optional<uintptr_t> get_initialize_hmd_device_address();
Expand Down

0 comments on commit 8846582

Please sign in to comment.