Skip to content

Commit 705ff6c

Browse files
committed
A slight refactor to make it easier to support v2 and legacy params
1 parent cdf0ba5 commit 705ff6c

5 files changed

Lines changed: 98 additions & 39 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using VRCFaceTracking.Core.Params.Data;
2+
3+
namespace ETVRTrackingModule.ExpressionStrategies;
4+
5+
public interface IExpressionMapper
6+
{
7+
public static string GetParamToMap(string oscAddress)
8+
{
9+
var oscUrlSplit = oscAddress.Split("/");
10+
return oscUrlSplit[^1];
11+
}
12+
13+
public void handleOSCMessage(OSCMessage message);
14+
public void UpdateVRCFTEyeData( ref UnifiedEyeData eyeData, ref UnifiedExpressionShape[] eyeShapes);
15+
}

ExpressionStrategies/V1Mapper.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using VRCFaceTracking.Core.Params.Data;
2+
3+
namespace ETVRTrackingModule.ExpressionStrategies;
4+
5+
public class V1Mapper : IExpressionMapper
6+
{
7+
private Dictionary<string, float> parameterValues = new()
8+
{
9+
{ "RightEyeLidExpandedSqueeze", 0f }, // no fucking idea if this should be EyeWide bullfuck
10+
{ "LeftEyeLidExpandedSqueeze", 0f },
11+
{ "LeftEyeX", 0f },
12+
{ "RightEyeX", 0f },
13+
{ "EyesY", 0f },
14+
};
15+
16+
private string[] eyeExpressions = new[]
17+
{
18+
"RightEyeLidExpandedSqueeze",
19+
"LeftEyeLidExpandedSqueeze"
20+
};
21+
22+
public void handleOSCMessage(OSCMessage message)
23+
{
24+
string paramToMap = IExpressionMapper.GetParamToMap(message.address);
25+
if (parameterValues.ContainsKey(paramToMap))
26+
{
27+
parameterValues[paramToMap] = message.value;
28+
}
29+
}
30+
31+
public void UpdateVRCFTEyeData(ref UnifiedEyeData eyeData, ref UnifiedExpressionShape[] eyeShapes)
32+
{
33+
throw new NotImplementedException();
34+
}
35+
}

ExpressionStrategies/V2Mapper.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using VRCFaceTracking.Core.Params.Data;
2+
3+
namespace ETVRTrackingModule.ExpressionStrategies;
4+
5+
public class V2Mapper : IExpressionMapper
6+
{
7+
private Dictionary<string, float> parameterValues = new()
8+
{
9+
{ "EyeX", 0f },
10+
{ "EyeLeftX", 0f },
11+
{ "EyeRightX", 0f },
12+
{ "EyeLeftY", 0f },
13+
{ "EyeRightY", 0f },
14+
{ "EyeLid", 0f },
15+
{ "EyeLidLeft", 0f },
16+
{ "EyeLidRight", 0f },
17+
};
18+
19+
20+
public void handleOSCMessage(OSCMessage message)
21+
{
22+
string paramToMap = IExpressionMapper.GetParamToMap(message.address);
23+
if (parameterValues.ContainsKey(paramToMap))
24+
{
25+
parameterValues[paramToMap] = message.value;
26+
}
27+
}
28+
29+
public void UpdateVRCFTEyeData(ref UnifiedEyeData eyeData, ref UnifiedExpressionShape[] eyeShapes)
30+
{
31+
throw new NotImplementedException();
32+
}
33+
}

ExpressionsMapper.cs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,40 @@
1-

1+
using ETVRTrackingModule.ExpressionStrategies;
22
using Microsoft.Extensions.Logging;
3-
using VRCFaceTracking.Core.Params.Data;
4-
using VRCFaceTracking.Core.Params.Expressions;
3+
using VRCFaceTracking;
54

65
namespace ETVRTrackingModule
76
{
87
public class ExpressionsMapper
98
{
10-
private Dictionary<string, float> parameterValues = new()
11-
{
12-
// v1, legacy support
13-
{ "RightEyeLidExpandedSqueeze", 0f },
14-
{ "LeftEyeLidExpandedSqueeze", 0f },
15-
{ "LeftEyeX", 0f },
16-
{ "RightEyeX", 0f },
17-
{ "EyesY", 0f },
18-
// v2
19-
{ "EyeX", 0f },
20-
{ "EyeLeftX", 0f },
21-
{ "EyeRightX", 0f },
22-
{ "EyeLeftY", 0f },
23-
{ "EyeRightY", 0f },
24-
{ "EyeLid", 0f },
25-
{ "EyeLidLeft", 0f },
26-
{ "EyeLidRight", 0f },
27-
};
28-
9+
private IExpressionMapper _mappingStrategy;
10+
2911
ILogger _logger;
3012
public ExpressionsMapper(ILogger logger)
3113
{
3214
_logger = logger;
15+
_mappingStrategy = new V2Mapper();
3316
}
3417
public void MapMessage(OSCMessage msg)
3518
{
3619
if (!msg.success)
3720
return;
38-
if (IsV2Param(msg))
21+
22+
var nextStrategy = IsV2Param(msg) ? (IExpressionMapper) new V2Mapper() : new V1Mapper();
23+
if (_mappingStrategy.GetType() != nextStrategy.GetType())
3924
{
40-
string paramToMap = GetParamToMap(msg.address);
41-
if (parameterValues.ContainsKey(paramToMap))
42-
{
43-
parameterValues[paramToMap] = msg.value;
44-
}
25+
_mappingStrategy = nextStrategy;
4526
}
27+
_mappingStrategy.handleOSCMessage(msg);
4628
}
4729

4830
private bool IsV2Param(OSCMessage oscMessage)
4931
{
5032
return oscMessage.address.Contains("/v2/");
5133
}
5234

53-
private static string GetParamToMap(string oscAddress)
54-
{
55-
var oscUrlSplit = oscAddress.Split("/");
56-
return oscUrlSplit[^1];
57-
}
58-
5935
public void UpdateVRCFTEyeData()
6036
{
37+
_mappingStrategy.UpdateVRCFTEyeData(ref UnifiedTracking.Data.Eye, ref UnifiedTracking.Data.Shapes);
6138
}
6239
}
6340
}

OSCManager.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,26 @@ public class OSCManager
2626
private ExpressionsMapper _expressionMapper;
2727

2828
private int _receivingPort;
29-
private const int _defaultPort = 8889;
30-
private const int connectionTimeout = 10000;
29+
private const int DefaultPort = 8889;
30+
private const int ConnectionTimeout = 10000;
3131

3232
public OSCManager(ILogger iLogger, ExpressionsMapper expressionsMapper, int? port = null) {
3333
_logger = iLogger;
3434
_expressionMapper = expressionsMapper;
35-
_receivingPort = port ?? _defaultPort;
35+
_receivingPort = port ?? DefaultPort;
3636
_receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3737

3838
try
3939
{
4040
// we should make this a setting, how do settings work in vrcft?
4141
_receiver.Bind(new IPEndPoint(IPAddress.Loopback, _receivingPort));
42-
_receiver.ReceiveTimeout = connectionTimeout;
42+
_receiver.ReceiveTimeout = ConnectionTimeout;
4343
State = OSCState.CONNECTED;
4444
}
4545
catch (Exception e)
4646
{
4747
_logger.LogError(e.ToString());
4848
State = OSCState.ERROR;
49-
5049
}
5150

5251
_literningThread = new Thread(OSCListen);

0 commit comments

Comments
 (0)