-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOscManager.cs
217 lines (181 loc) · 8.39 KB
/
OscManager.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using Rug.Osc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace VRC_OSC_ExternallyTrackedObject
{
public class TrackingActiveChangedArgs : EventArgs
{
public bool Active { get; set; }
public bool AvatarKnown { get; set; }
}
public class AvatarChangedArgs : EventArgs
{
public string Id { get; set; } = "";
}
internal class OscManager
{
private static string AVATAR_CHANGE_ADDRESS = "/avatar/change";
private CancellationTokenSource CancelTokenSource = new CancellationTokenSource();
private Thread? currentThread = null;
private Dictionary<string, AvatarConfig>? currentConfig = null;
private string? currentAvatar = null;
private bool currentlyActive = false;
private OscSender? oscSender;
private OscReceiver? oscReceiver;
public EventHandler? TrackingActiveChanged;
public EventHandler? AvatarChanged;
public EventHandler? ThreadCrashed;
public void Start(string inputAddress, int inputPort, string outputAddress, int outputPort, Dictionary<string, AvatarConfig> config)
{
if (oscSender == null)
{
oscSender = new OscSender(System.Net.IPAddress.Parse(outputAddress), 0, outputPort);
}
if (oscReceiver == null)
{
oscReceiver = new OscReceiver(System.Net.IPAddress.Parse(inputAddress), inputPort);
}
currentConfig = config;
//this.currentlyActive = true; // just for testing
oscReceiver.Connect();
oscSender.Connect();
currentThread = new Thread(() => ListenThread());
currentThread.Name = "OscThread";
currentThread.IsBackground = true;
currentThread.Start();
Debug.WriteLine("OSC thread started");
}
public void ListenThread()
{
if (oscReceiver == null || currentConfig == null) throw new Exception("ListenThread was set up incorrectly");
try
{
{
var args = new TrackingActiveChangedArgs()
{
Active = false,
AvatarKnown = false,
};
var handler = TrackingActiveChanged;
handler?.Invoke(this, args);
}
while (oscReceiver.State != OscSocketState.Closed)
{
if (oscReceiver.State == OscSocketState.Connected)
{
OscPacket packet = oscReceiver.Receive();
string? msgStr = packet.ToString();
if (msgStr == null)
{
continue; // skip this packet so we don't crash the OSC thread
}
OscMessage msg;
try
{
msg = OscMessage.Parse(msgStr);
}
catch
{
continue;
}
//Debug.WriteLine(packet.ToString());
if (msg.Address == AVATAR_CHANGE_ADDRESS && msg.Count > 0)
{
currentAvatar = (string)msg[0];
{
var args = new AvatarChangedArgs() { Id = currentAvatar };
var handler = AvatarChanged;
handler?.Invoke(this, args);
}
this.currentlyActive = false;
if (currentConfig.ContainsKey(currentAvatar))
{
// if the activate parameter is set to nothing we are always activated, otherwise we wait for the trigger
this.currentlyActive = (currentConfig[currentAvatar].Parameters.Activate == "");
}
{
var args = new TrackingActiveChangedArgs()
{
Active = this.currentlyActive,
AvatarKnown = currentConfig.ContainsKey(currentAvatar),
};
var handler = TrackingActiveChanged;
handler?.Invoke(this, args);
}
}
else if (currentAvatar != null
&& currentConfig.ContainsKey(currentAvatar)
&& msg.Address == currentConfig[currentAvatar].Parameters.Activate
&& msg.Count > 0)
{
bool activate = (bool)msg[0];
this.currentlyActive = activate;
var args = new TrackingActiveChangedArgs() { Active = activate, AvatarKnown = true };
var handler = TrackingActiveChanged;
handler?.Invoke(this, args);
if (!this.currentlyActive)
{
SendValues(0, 0, 0, 0, 0, 0, true); // reset all values to 0 so that we can reuse those parameters
}
}
// everything else is ignored
}
}
}
catch(Exception e)
{
if (oscReceiver != null && oscReceiver.State == OscSocketState.Connected)
{
MessageBox.Show("OSC thread encountered an unexpected error: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
var handler = ThreadCrashed;
handler?.Invoke(this, new EventArgs());
}
}
}
public void Stop()
{
if (oscReceiver != null)
{
oscReceiver.Close();
//oscReceiver.Dispose();
}
if (oscSender != null)
{
oscSender.Close();
//oscSender.Dispose();
}
//oscReceiver = null;
//oscSender = null;
currentConfig = null;
if (currentThread != null)
{
currentThread.Join();
}
currentThread = null;
}
public void SendValues(float posX, float posY, float posZ, float rotX, float rotY, float rotZ, bool force = false)
{
if (this.oscSender == null || this.oscSender.State != OscSocketState.Connected || this.currentConfig == null)
{
throw new Exception("SendValues was called without the OSC manager being set up");
}
if ((!force && !this.currentlyActive) || currentAvatar == null || !this.currentConfig.ContainsKey(currentAvatar))
{
return; // discard the message to not spam the game with useless messages
}
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.PositionX, posX));
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.PositionY, posY));
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.PositionZ, posZ));
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.RotationX, rotX));
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.RotationY, rotY));
oscSender.Send(new OscMessage(this.currentConfig[currentAvatar].Parameters.RotationZ, rotZ));
//Debug.WriteLine("[OSC] Sending pos=(" + posX + ", " + posY + ", " + posZ + ") rot=(" + rotX + ", " + rotY + ", " + rotZ + ")");
}
}
}