public OpenVRInject(RemoteHooking.IContext context, String channelName) { // Get reference to IPC to host application // Note: any methods called or events triggered against _interface will execute in the host process. Interface = RemoteHooking.IpcConnectClient <PSInterface>(channelName); RunButton = Interface.RunButton; Hand = Interface.Hand; ButtonType = Interface.ButtonType; Interface.Write("Intialized"); }
public void ReceivedNewViveBindings(PStrafeButtonType touch, Valve.VR.EVRButtonId button, PStrafeHand hand) { if (Iface == null) { return; } try { var handNames = Enum.GetNames(typeof(PStrafeHand)).ToList(); log.Info(touch); log.Info(hand); log.Info(button); Iface.ButtonType = touch; Iface.Hand = hand; Iface.RunButton = button; } catch (Exception ex) { throw (ex); } }
/// <summary> /// Called by EasyHook to begin any hooking etc in the target process /// </summary> /// <param name="InContext"></param> /// <param name="InArg1"></param> public void Run(RemoteHooking.IContext InContext, String InArg1) { Interface.Write("Running in target."); int pid = RemoteHooking.GetCurrentProcessId(); try { // NOTE: We are now already running within the target process // We want to hook each method of we are interested in IntPtr pGetControllerState = IntPtr.Zero; IntPtr pGetControllerStateWithPose = IntPtr.Zero; IntPtr pPollNextEvent = IntPtr.Zero; IntPtr pPollNextEventWithPose = IntPtr.Zero; // TODO: Find out version of openvr dll, which will determine the correct index of the function // ver = vr::IVRSystem_Version; returns string // string matching determines which enum to use if (RemoteHooking.IsX64Process(pid)) { /* * EVRInitError error = EVRInitError.None; * OpenVR.Init(ref error); * if(error == EVRInitError.None) * { * Func<in uint, VRControllerState_t, in uint, out bool> ptr = null; * ptr = OpenVR.System.GetControllerState; * } */ Interface.Write("64 bit process"); pGetControllerState = GetIVRSystemFunctionAddress64((short)OpenVRFunctionIndex.GetControllerState, (int)OpenVRFunctionIndex.Count); pGetControllerStateWithPose = GetIVRSystemFunctionAddress64((short)OpenVRFunctionIndex.GetControllerStateWithPose, (int)OpenVRFunctionIndex.Count); pPollNextEvent = GetIVRSystemFunctionAddress64((short)OpenVRFunctionIndex.PollNextEvent, (int)OpenVRFunctionIndex.Count); pPollNextEventWithPose = GetIVRSystemFunctionAddress64((short)OpenVRFunctionIndex.PollNextEventWithPose, (int)OpenVRFunctionIndex.Count); } else { Interface.Write("32 bit process"); pGetControllerState = GetIVRSystemFunctionAddress32((short)OpenVRFunctionIndex.GetControllerState, (int)OpenVRFunctionIndex.Count); pGetControllerStateWithPose = GetIVRSystemFunctionAddress32((short)OpenVRFunctionIndex.GetControllerStateWithPose, (int)OpenVRFunctionIndex.Count); pPollNextEvent = GetIVRSystemFunctionAddress32((short)OpenVRFunctionIndex.PollNextEvent, (int)OpenVRFunctionIndex.Count); pPollNextEventWithPose = GetIVRSystemFunctionAddress32((short)OpenVRFunctionIndex.PollNextEventWithPose, (int)OpenVRFunctionIndex.Count); } if ((pGetControllerState == IntPtr.Zero && pGetControllerStateWithPose == IntPtr.Zero) || (pPollNextEvent == IntPtr.Zero || pPollNextEventWithPose == IntPtr.Zero)) { throw new VRNotInitializedException("No runtime installed, no HMD present, version mismatch, or other error."); } Interface.Write("GetControllerState function pointer: " + (pGetControllerState).ToString()); Interface.Write("GetControllerStateWithPose function pointer: " + (pGetControllerStateWithPose).ToString()); Interface.Write("PollNextEventEvent function pointer: " + (pPollNextEvent).ToString()); Interface.Write("PollNextEventWithPose function pointer: " + (pPollNextEventWithPose).ToString()); GetControllerStatePtr = pGetControllerState; GetControllerStateHook = LocalHook.Create( pGetControllerState, new vr_GetControllerStateDelegate(GetControllerState_Hooked), this); GetControllerStateWithPosePtr = pGetControllerStateWithPose; GetControllerStateWithPoseHook = LocalHook.Create( pGetControllerStateWithPose, new vr_GetControllerStateWithPoseDelegate(GetControllerStateWithPose_Hooked), this); PollNextEventPtr = pPollNextEvent; PollNextEventHook = LocalHook.Create( pPollNextEvent, new vr_PollNextEventDelegate(PollNextEvent_Hooked), this); PollNextEventWithPosePtr = pPollNextEventWithPose; PollNextEventWithPoseHook = LocalHook.Create( pPollNextEventWithPose, new vr_PollNextEventWithPoseDelegate(PollNextEventWithPose_Hooked), this); /* * Don't forget that all hooks will start deactivated... * The following ensures that all threads are intercepted: * Note: you must do this for each hook. */ GetControllerStateHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 }); GetControllerStateWithPoseHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 }); PollNextEventHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 }); PollNextEventWithPoseHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 }); } catch (Exception e) { /* * We should notify our host process about this error... */ Interface.Write("Error: "); Interface.ReportError(pid, e); return; } Interface.Write("DLL installed"); Interface.IsInstalled(pid); // Wait for host process termination... try { ChosenDeviceIndex = 0; // base station while (ChosenDeviceIndex == 0) { if (!Interface.Installed) { Interface.Write("Need to uninstall hooks!"); break; } // wait until controller comes on? if (RemoteHooking.IsX64Process(pid)) { LeftHandIndex = GetLeftHandIndex64(); RightHandIndex = GetRightHandIndex64(); } else { LeftHandIndex = GetLeftHandIndex32(); RightHandIndex = GetRightHandIndex32(); } Interface.Write("Left hand " + LeftHandIndex); Interface.Write("Right hand " + RightHandIndex); ChosenDeviceIndex = Interface.Hand == PStrafeHand.Left ? LeftHandIndex : RightHandIndex; Thread.Sleep(300); } while (true) { Thread.Sleep(10); if (!Interface.Installed) { break; } bool running = Interface.UserIsRunning; if (running != UserRunning) { // look for interface changes (keybinding, user is running, etc) RunButton = Interface.RunButton; ButtonType = Interface.ButtonType; ChosenDeviceIndex = Interface.Hand == PStrafeHand.Left ? LeftHandIndex : RightHandIndex; UserRunning = running; // create event for vive controller MyEvent = new ButtonEvent() { Queued = true, ShouldPress = ButtonType == PStrafeButtonType.Press, ShouldTouch = true }; } } } catch { // NET Remoting will raise an exception if host is unreachable Interface.ReportError(pid, new Exception("Host unreachable?")); } finally { // Note: this will probably not get called if the target application closes before the // host application. Interface.Write("Remove and cleanup hooks"); Cleanup(); } }