Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
HapticDeviceManager.cs
Go to the documentation of this file.
1/* ​
2* Copyright (c) 2023 Go Touch VR SAS. All rights reserved. ​
3* ​
4*/
5
6using System.Collections.Generic;
7using System.Linq;
8
10
11
13{
14
15 internal static class LinqExtension
16 {
17
18 internal static void ForEach<T>(this IEnumerable<T> enumeration, System.Action<T> action)
19 {
20 foreach (T item in enumeration)
21 {
22 action(item);
23 }
24 }
25
26 }
27
28 internal static class HapticDeviceManager
29 {
30
31 private static Dictionary<System.Type, object> m_haptic_providers = new Dictionary<System.Type, object>();
32
33 public static void DeviceInitLoop()
34 {
35 ReflectionNames.GetCompatibleAssemblies().ForEach(assembly => {
36 assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IHapticProvider))).ForEach(hapticProviderType => {
37 object instance = System.Activator.CreateInstance(hapticProviderType);
38 if (instance == null)
39 {
40 return;
41 }
42
43 //if Init exists and is successful, adds it to the providers
44 if ((bool)hapticProviderType.GetMethod(ReflectionNames.INIT_PROVIDER_METHOD_NAME)?.Invoke(instance, null))
45 {
46 m_haptic_providers.Add(hapticProviderType, instance);
47 }
48 });
49 }); //Get from AssemblyCsharp and Interhaptics Provider
50 }
51
52 public static void DeviceRenderLoop()
53 {
54 m_haptic_providers.ForEach(provider => {
55 //Check if device is present
56 if ((bool)provider.Key.GetMethod(ReflectionNames.IS_PRESENT_PROVIDER_METHOD_NAME)?.Invoke(provider.Value, null))
57 {
58 //Send haptics to the device
59 provider.Key.GetMethod(ReflectionNames.RENDER_HAPTICS_PROVIDER_METHOD_NAME)?.Invoke(provider.Value, null);
60 }
61 });
62 }
63
64 public static void DeviceCleanLoop()
65 {
66 m_haptic_providers.ForEach(provider => {
67 //Clean device COM
68 provider.Key.GetMethod(ReflectionNames.CLEAN_PROVIDER_METHOD_NAME)?.Invoke(provider.Value, null);
69 });
70 }
71
72 }
73
74}