From af0a6b70255e30212a86e26ebb26fb0be996cbc1 Mon Sep 17 00:00:00 2001 From: mii443 Date: Fri, 1 Nov 2024 18:37:47 +0900 Subject: [PATCH] add hook injection, delete linux support --- CMakeLists.txt | 20 ++++------ src/Hooking.hpp | 5 +-- src/InterfaceHookInjector.cpp | 59 +++++++++++++++++++++++++++++ src/InterfaceHookInjector.hpp | 10 +++++ src/ServerTrackedDeviceProvider.cpp | 3 ++ 5 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 src/InterfaceHookInjector.cpp create mode 100644 src/InterfaceHookInjector.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1645d..73d85e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,17 +6,11 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) set(OPENVR_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib/openvr/headers") set(PROCESSOR_ARCH "64") -if (WIN) - set(PLATFORM_NAME "win") -elseif (UNIX AND NOT APPLE) - set(PLATFORM_NAME "linux") -elseif (APPLE) - set(PLATFORM_NAME "osx") -endif() +set(PLATFORM_NAME "win") + -if (WIN) add_subdirectory(lib/minhook) -endif() + find_library(OPENVR_LIB openvr_api HINTS "${CMAKE_CURRENT_SOURCE_DIR}/lib/openvr/lib/${PLATFORM_NAME}${PROCESSOR_ARCH}/" NO_DEFAULT_PATH) set(DRIVER_NAME "migawari") @@ -25,15 +19,15 @@ file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp") file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") add_library("${PROJECT}" SHARED "${HEADERS}" "${SOURCES}") -if (WIN) + target_include_directories("${PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/minhook/include") -endif() + target_include_directories("${PROJECT}" PUBLIC "${OPENVR_INCLUDE_DIR}") target_include_directories("${PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src/") target_link_libraries("${PROJECT}" PUBLIC "${OPENVR_LIB}") -if (WIN) + target_link_libraries("${PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/lib/minhook/Release/minhook.x64.lib") -endif() + source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Header Files" FILES ${HEADERS}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source Files" FILES ${SOURCES}) set_property(TARGET "${PROJECT}" PROPERTY CXX_STANDARD 17) diff --git a/src/Hooking.hpp b/src/Hooking.hpp index bda89f6..37aca5e 100644 --- a/src/Hooking.hpp +++ b/src/Hooking.hpp @@ -2,7 +2,7 @@ #pragma once -#include +#include "MinHook.h" #include #include @@ -37,18 +37,15 @@ public: auto err = MH_CreateHook(targetFunc, detourFunction, (LPVOID *)&originalFunc); if (err != MH_OK) { - vr::VRDriverLog()->Log("Failed to create hook.".c_str()); return false; } err = MH_EnableHook(targetFunc); if (err != MH_OK) { - vr::VRDriverLog()->Log("Failed to enable hook.".c_str()); MH_RemoveHook(targetFunc); return false; } - vr::VRDriverLog()->Log("Hook enabled.".c_str()); enabled = true; return true; } diff --git a/src/InterfaceHookInjector.cpp b/src/InterfaceHookInjector.cpp new file mode 100644 index 0000000..4f88901 --- /dev/null +++ b/src/InterfaceHookInjector.cpp @@ -0,0 +1,59 @@ +#include "Hooking.hpp" +#include "InterfaceHookInjector.hpp" +#include "ServerTrackedDeviceProvider.hpp" + +static ServerTrackedDeviceProvider *Driver = nullptr; + +static Hook + GetGenericInterfaceHook("IVRDriverContext::GetGenericInterface"); + +static Hook + TrackedDevicePoseUpdatedHook005("IVRServerDriverHost005::TrackedDevicePoseUpdated"); + +static Hook + TrackedDevicePoseUpdatedHook006("IVRServerDriverHost006::TrackedDevicePoseUpdated"); + +static void DetourTrackedDevicePoseUpdated005(vr::IVRServerDriverHost *_this, uint32_t unWhichDevice, const vr::DriverPose_t &newPose, uint32_t unPoseStructSize) { + auto pose = newPose; + TrackedDevicePoseUpdatedHook005.originalFunc(_this, unWhichDevice, pose, unPoseStructSize); +} + +static void DetourTrackedDevicePoseUpdated006(vr::IVRServerDriverHost *_this, uint32_t unWhichDevice, const vr::DriverPose_t &newPose, uint32_t unPoseStructSize) { + auto pose = newPose; + TrackedDevicePoseUpdatedHook006.originalFunc(_this, unWhichDevice, pose, unPoseStructSize); +} + +static void *DetourGetGenericInterface(vr::IVRDriverContext *_this, const char *pchInterfaceVersion, vr::EVRInitError *peError) { + auto originalInterface = GetGenericInterfaceHook.originalFunc(_this, pchInterfaceVersion, peError); + + std::string iface(pchInterfaceVersion); + + if (iface == "IVRServerDriverHost_005") { + if (!IHook::Exists(TrackedDevicePoseUpdatedHook005.name)) { + TrackedDevicePoseUpdatedHook005.CreateHookInObjectVTable(originalInterface, 1, &DetourTrackedDevicePoseUpdated005); + IHook::Register(&TrackedDevicePoseUpdatedHook005); + } + } else if (iface == "IVRServerDriverHost_006") { + if (!IHook::Exists(TrackedDevicePoseUpdatedHook006.name)) { + TrackedDevicePoseUpdatedHook006.CreateHookInObjectVTable(originalInterface, 1, &DetourTrackedDevicePoseUpdated006); + IHook::Register(&TrackedDevicePoseUpdatedHook006); + } + } + + return originalInterface; +} + +void InjectHooks(ServerTrackedDeviceProvider *driver, vr::IVRDriverContext *pDriverContext) { + Driver = driver; + + auto err = MH_Initialize(); + if (err == MH_OK) { + GetGenericInterfaceHook.CreateHookInObjectVTable(pDriverContext, 0, &DetourGetGenericInterface); + IHook::Register(&GetGenericInterfaceHook); + } +} + +void DisableHooks() { + IHook::DestroyAll(); + MH_Uninitialize(); +} \ No newline at end of file diff --git a/src/InterfaceHookInjector.hpp b/src/InterfaceHookInjector.hpp new file mode 100644 index 0000000..bfd478c --- /dev/null +++ b/src/InterfaceHookInjector.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +class ServerTrackedDeviceProvider; + +static void DetourTrackedDevicePoseUpdated(vr::IVRServerDriverHost * _this, uint32_t unWhichDevice, const vr::DriverPose_t & newPose, uint32_t unPoseStructSize); + +void InjectHooks(ServerTrackedDeviceProvider *driver, vr::IVRDriverContext *pDriverContext); +void DisableHooks(); \ No newline at end of file diff --git a/src/ServerTrackedDeviceProvider.cpp b/src/ServerTrackedDeviceProvider.cpp index 719e13c..efb0706 100644 --- a/src/ServerTrackedDeviceProvider.cpp +++ b/src/ServerTrackedDeviceProvider.cpp @@ -1,5 +1,6 @@ #include "ServerTrackedDeviceProvider.hpp" #include "Device.hpp" +#include "InterfaceHookInjector.hpp" #ifdef _MSC_VER #include #endif @@ -22,6 +23,8 @@ vr::EVRInitError ServerTrackedDeviceProvider::Init(vr::IVRDriverContext *pDriver HANDLE pHandle = CreateFileMapping(NULL, NULL, PAGE_READWRITE, NULL, sizeof(SharedDevice), ("MigawariDriver_Device" + std::to_string(i)).c_str()); devices[i] = (SharedDevice*)MapViewOfFile(pHandle, FILE_MAP_ALL_ACCESS, NULL, NULL, sizeof(SharedDevice)); } + + InjectHooks(this, pDriverContext); #endif return vr::VRInitError_None;