-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyBhapticsTactsuit.cs
149 lines (131 loc) · 5.58 KB
/
MyBhapticsTactsuit.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Bhaptics.Tact;
using bHapticsMusical;
using System.Resources;
using System.Globalization;
using System.Collections;
namespace MyBhapticsTactsuit
{
public class TactsuitVR
{
/* A class that contains the basic functions for the bhaptics Tactsuit, like:
* - A Heartbeat function that can be turned on/off
* - A function to read in and register all .tact patterns in the bHaptics subfolder
* - A logging hook to output to the Melonloader log
* -
* */
public bool suitDisabled = true;
public bool systemInitialized = false;
// Event to start and stop the heartbeat thread
//private static ManualResetEvent HeartBeat_mrse = new ManualResetEvent(false);
// dictionary of all feedback patterns found in the bHaptics directory
public Dictionary<String, String> FeedbackMap = new Dictionary<String, String>();
public Dictionary<String, String> defaultEffects = new Dictionary<String, String>();
public List<string> myEffectStrings = new List<string> { };
#pragma warning disable CS0618 // remove warning that the C# library is deprecated
public HapticPlayer hapticPlayer;
#pragma warning restore CS0618
private static RotationOption defaultRotationOption = new RotationOption(0.0f, 0.0f);
public TactsuitVR()
{
LOG("Initializing suit");
try
{
#pragma warning disable CS0618 // remove warning that the C# library is deprecated
hapticPlayer = new HapticPlayer("bHapticsMusical", "bHapticsMusical", bhapticsPlayerConnected, false);
#pragma warning restore CS0618
//suitDisabled = false;
}
catch { LOG("Suit initialization failed!"); }
RegisterAllTactFiles();
LOG("Starting HeartBeat thread...");
}
private void bhapticsPlayerConnected(bool connected)
{
//LOG("Connection changed " + connected);
suitDisabled = !connected;
}
public void LOG(string logStr)
{
Plugin.Log.Info(logStr);
//Plugin.Log.LogMessage(logStr);
}
void RegisterAllTactFiles()
{
if (suitDisabled) return;
ResourceSet resourceSet = bHapticsMusical.Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
foreach (DictionaryEntry dict in resourceSet)
{
if (dict.Key.ToString().StartsWith("LightEffect"))
{
defaultEffects.Add(dict.Key.ToString(), dict.Value.ToString());
continue;
}
try
{
hapticPlayer.RegisterTactFileStr(dict.Key.ToString(), dict.Value.ToString());
LOG("Pattern registered: " + dict.Key.ToString());
}
catch (Exception e) { LOG(e.ToString()); }
FeedbackMap.Add(dict.Key.ToString(), dict.Value.ToString());
}
// Get location of the compiled assembly and search through "bHaptics" directory and contained patterns
// string configPath = Directory.GetCurrentDirectory() + "\\Plugins\\bHaptics";
string configPath = Path.Combine(IPA.Utilities.UnityGame.UserDataPath, "bHapticsMusical");
//LOG("Path: " + configPath);
if (!Directory.Exists(configPath))
{
Directory.CreateDirectory(configPath);
foreach (KeyValuePair<string, string> dict in defaultEffects)
{
string filePath = Path.Combine(configPath, dict.Key + ".tact");
File.WriteAllText(filePath, dict.Value);
}
}
DirectoryInfo d = new DirectoryInfo(configPath);
FileInfo[] Files = d.GetFiles("*.tact", SearchOption.AllDirectories);
for (int i = 0; i < Files.Length; i++)
{
string filename = Files[i].Name;
string fullName = Files[i].FullName;
string prefix = Path.GetFileNameWithoutExtension(filename);
// LOG("Trying to register: " + prefix + " " + fullName);
if (filename == "." || filename == "..")
continue;
string tactFileStr = File.ReadAllText(fullName);
try
{
hapticPlayer.RegisterTactFileStr(prefix, tactFileStr);
LOG("Light effect pattern registered: " + prefix);
}
catch (Exception e) { LOG("Pattern failed: " + e.ToString()); }
FeedbackMap.Add(prefix, tactFileStr);
myEffectStrings.Add(prefix);
}
systemInitialized = true;
}
public void PlaybackHaptics(String key, float intensity = 1.0f, float duration = 1.0f)
{
//LOG("Trying to play");
if (suitDisabled) return;
if (FeedbackMap.ContainsKey(key))
{
//LOG("ScaleOption");
ScaleOption scaleOption = new ScaleOption(intensity, duration);
//LOG("Submit");
hapticPlayer.SubmitRegistered(key, scaleOption);
}
else
{
LOG("Feedback not registered: " + key);
}
}
public bool IsPlaying(String effect)
{
return IsPlaying(effect);
}
}
}