Skip to content

Commit

Permalink
BP: Fix GetOrientationAndPosition not working on >= 4.18
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Feb 4, 2024
1 parent 0e9f495 commit 98d675f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CommitHash.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#define UEVR_COMMIT_HASH "68c94c05de75d07823d215202c494ef591d8c9b9"
#define UEVR_COMMIT_HASH "0e9f4958afff13dcd9016d0311d1c517a1f25525"
#define UEVR_BUILD_DATE "04.02.2024"
#define UEVR_BUILD_TIME "00:00"
68 changes: 68 additions & 0 deletions src/mods/vr/IXRTrackingSystemHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <sdk/vtables/IXRTrackingSystemVTables.hpp>
#include <sdk/structures/FXRMotionControllerData.hpp>
#include <sdk/structures/FXRHMDData.hpp>
#include <sdk/UHeadMountedDisplayFunctionLibrary.hpp>
#include <sdk/UFunction.hpp>

#include "IXRTrackingSystemHook.hpp"

Expand Down Expand Up @@ -932,6 +934,20 @@ bool IXRTrackingSystemHook::analyze_head_tracking_allowed(uintptr_t return_addre
return true;
}

void* IXRTrackingSystemHook::get_orientation_and_position_native(void* rcx, void* rdx, void* r8, void* r9) {
SPDLOG_INFO_ONCE("GetOrientationAndPosition native function {:x}", (uintptr_t)_ReturnAddress());

auto og = g_hook->m_native_get_oap_hook->get_original<decltype(&get_orientation_and_position_native)>();

g_hook->m_within_get_oap_native = true;

const auto result = og(rcx, rdx, r8, r9);

g_hook->m_within_get_oap_native = false;

return result;
}

bool IXRTrackingSystemHook::is_head_tracking_allowed(sdk::IXRTrackingSystem*) {
SPDLOG_INFO_ONCE("is_head_tracking_allowed {:x}", (uintptr_t)_ReturnAddress());

Expand All @@ -945,7 +961,59 @@ bool IXRTrackingSystemHook::is_head_tracking_allowed(sdk::IXRTrackingSystem*) {
return true;
}

static bool is_allowed_return_true = false;
static bool attempted_check = false;

if (!vr->is_any_aim_method_active()) {
if (!is_allowed_return_true && !attempted_check) try {
attempted_check = true;
const auto uobjectarray = sdk::FUObjectArray::get();

if (uobjectarray == nullptr) {
SPDLOG_ERROR("Failed to find FUObjectArray");
return false;
}

const auto hmd_lib = sdk::UHeadMountedDisplayFunctionLibrary::static_class();

if (hmd_lib == nullptr) {
SPDLOG_ERROR("Failed to find UHeadMountedDisplayFunctionLibrary");
return false;
}

auto get_orientation_and_position_fn = hmd_lib->find_function(L"GetOrientationAndPosition");

if (get_orientation_and_position_fn == nullptr) {
SPDLOG_ERROR("Failed to find GetOrientationAndPosition");
return false;
}

if (sdk::UFunction::get_native_function_offset() == 0) {
SPDLOG_ERROR("UFunction::get_native_function_offset is 0");
return false;
}

auto& native_fn = get_orientation_and_position_fn->get_native_function();

if (native_fn == nullptr || IsBadReadPtr(native_fn, sizeof(void*))) {
SPDLOG_ERROR("Failed to find native function for GetOrientationAndPosition");
return false;
}

g_hook->m_native_get_oap_hook = std::make_unique<PointerHook>((void**)&native_fn, get_orientation_and_position_native);
is_allowed_return_true = true;

SPDLOG_INFO("Hooked GetOrientationAndPosition native function");
} catch(...) {

}

// Only allow this to return true if BP functions are the ones calling it
// Like GetOrientationAndPosition
if (is_allowed_return_true) {
return g_hook->m_within_get_oap_native;
}

return false;
}

Expand Down
7 changes: 7 additions & 0 deletions src/mods/vr/IXRTrackingSystemHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class IXRTrackingSystemHook : public ModComponent {
static void* process_view_rotation_analyzer(void*, size_t, size_t, size_t, size_t, size_t);
static void process_view_rotation(sdk::APlayerCameraManager* pcm, float delta_time, Rotator<float>* rot, Rotator<float>* delta_rot);

// UHeadMountedDisplayFunctionLibrary
static void* get_orientation_and_position_native(void*, void*, void*, void*);

void pre_update_view_rotation(sdk::UObject* reference_obj, Rotator<float>* rot);

FFakeStereoRenderingHook* m_stereo_hook{nullptr};
Expand Down Expand Up @@ -126,6 +129,10 @@ class IXRTrackingSystemHook : public ModComponent {
SharedPtr m_xr_camera_shared{};
SharedPtr m_view_extension_shared{};

// Hook for the UFunction GetOrientationAndPosition
std::unique_ptr<PointerHook> m_native_get_oap_hook{};
bool m_within_get_oap_native{false};

uintptr_t m_addr_of_process_view_rotation_ptr{};
//std::unique_ptr<PointerHook> m_process_view_rotation_hook{};
safetyhook::InlineHook m_process_view_rotation_hook{};
Expand Down

0 comments on commit 98d675f

Please sign in to comment.