add hook injection, delete linux support

This commit is contained in:
mii443
2024-11-01 18:37:47 +09:00
parent 1d5f93f881
commit af0a6b7025
5 changed files with 80 additions and 17 deletions

View File

@ -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)

View File

@ -2,7 +2,7 @@
#pragma once
#include <MinHook.h>
#include "MinHook.h"
#include <map>
#include <string>
@ -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;
}

View File

@ -0,0 +1,59 @@
#include "Hooking.hpp"
#include "InterfaceHookInjector.hpp"
#include "ServerTrackedDeviceProvider.hpp"
static ServerTrackedDeviceProvider *Driver = nullptr;
static Hook<void*(*)(vr::IVRDriverContext *, const char *, vr::EVRInitError *)>
GetGenericInterfaceHook("IVRDriverContext::GetGenericInterface");
static Hook<void(*)(vr::IVRServerDriverHost *, uint32_t, const vr::DriverPose_t &, uint32_t)>
TrackedDevicePoseUpdatedHook005("IVRServerDriverHost005::TrackedDevicePoseUpdated");
static Hook<void(*)(vr::IVRServerDriverHost *, uint32_t, const vr::DriverPose_t &, uint32_t)>
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();
}

View File

@ -0,0 +1,10 @@
#pragma once
#include <openvr_driver.h>
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();

View File

@ -1,5 +1,6 @@
#include "ServerTrackedDeviceProvider.hpp"
#include "Device.hpp"
#include "InterfaceHookInjector.hpp"
#ifdef _MSC_VER
#include <Windows.h>
#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;