-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
233 lines (201 loc) · 5.78 KB
/
Program.cs
File metadata and controls
233 lines (201 loc) · 5.78 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using Rug.Osc;
using VRC.OSCQuery;
namespace EyeHeightPersistence
{
public class Config
{
public string IP { get; set; } = "127.0.0.1";
public int ListeningPort { get; set; } = 9001;
public int SendingPort { get; set; } = 9000;
public bool OSCQuery { get; set; } = true;
public int ChangeDelayMS { get; set; } = 200;
public bool RelativeMode { get; set; } = true;
public float HeightTolerance { get; set; } = 0.2f;
public bool ToleranceFailBehavior { get; set; } = true;
}
class Program
{
static Config config;
static OscReceiver receiver;
static OscSender sender;
static OSCQueryService oscQuery;
static volatile bool isPaused = false;
static volatile bool isChanging = false;
static string lastAvatarId = "";
static float lastEyeHeight = 1f;
static float lastScaleFactorInverse = 1f;
static float currentEyeHeight = 1f;
static float currentScaleFactorInverse = 1f;
static async Task Main(string[] args)
{
Console.WriteLine("Eye Height Persistence Starting");
Console.WriteLine("Version: {VERSION_PLACEHOLDER}\n");
LoadConfig();
if (config.OSCQuery)
{
Console.WriteLine("Using OSCQuery");
int tcpPort = Extensions.GetAvailableTcpPort();
int udpPort = Extensions.GetAvailableUdpPort();
oscQuery = new OSCQueryServiceBuilder()
.WithServiceName("Eye Height Persistence")
.WithUdpPort(udpPort)
.WithTcpPort(tcpPort)
.WithDefaults()
.Build();
oscQuery.AddEndpoint<float>("/avatar/eyeheight", Attributes.AccessValues.ReadWrite);
oscQuery.AddEndpoint<bool>("/avatar/eyeheightscalingallowed", Attributes.AccessValues.ReadOnly);
oscQuery.AddEndpoint<float>("/avatar/parameters/ScaleFactorInverse", Attributes.AccessValues.ReadOnly);
oscQuery.AddEndpoint<string>("/avatar/change", Attributes.AccessValues.ReadOnly);
receiver = new OscReceiver(IPAddress.Parse(config.IP), udpPort);
receiver.Connect();
InitializeSender(config.SendingPort);
oscQuery.OnOscServiceAdded += (profile) =>
{
if (profile.name.Contains("VRChat"))
{
InitializeSender(profile.port);
}
};
}
else
{
receiver = new OscReceiver(IPAddress.Parse(config.IP), config.ListeningPort);
receiver.Connect();
Console.WriteLine($"Listening on {config.ListeningPort}");
InitializeSender(config.SendingPort);
}
Task listenTask = new Task(ListenLoop);
listenTask.Start();
Console.WriteLine("\n");
await Task.Delay(-1);
}
static void InitializeSender(int port)
{
if (sender != null)
{
sender.Dispose();
}
sender = new OscSender(IPAddress.Parse(config.IP), 0, port);
sender.Connect();
Console.WriteLine($"Sending to port {port}");
}
static void LoadConfig()
{
if (File.Exists("config.json"))
{
try
{
config = JsonSerializer.Deserialize<Config>(File.ReadAllText("config.json"));
Console.WriteLine("Config loaded from config.json");
}
catch (Exception ex)
{
Console.WriteLine($"Error loading config.json: {ex.Message}");
config = new Config();
}
}
else
{
config = new Config();
File.WriteAllText("config.json", JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));
}
}
static void ListenLoop()
{
while (receiver.State != OscSocketState.Closed)
{
if (receiver.State == OscSocketState.Connected)
{
OscPacket packet = receiver.Receive();
if (packet is OscMessage message)
{
HandleMessage(message);
}
}
}
}
static void HandleMessage(OscMessage msg)
{
try
{
switch (msg.Address)
{
case "/avatar/eyeheightscalingallowed":
bool _isPaused = !(bool)msg[0];
if (_isPaused != isPaused)
{
isPaused = _isPaused;
Console.WriteLine($"World scaling allowance changed: {!isPaused}");
}
break;
case "/avatar/eyeheight":
currentEyeHeight = (float)msg[0];
if (!isChanging) lastEyeHeight = currentEyeHeight;
break;
case "/avatar/parameters/ScaleFactorInverse":
currentScaleFactorInverse = (float)msg[0];
if (!isChanging) lastScaleFactorInverse = currentScaleFactorInverse;
break;
case "/avatar/change":
string newId = msg[0].ToString();
if (!isChanging)
{
_ = ProcessAvatarChange(newId);
}
break;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing {msg.Address}: {ex.Message}");
}
}
static async Task ProcessAvatarChange(string newId)
{
isChanging = true;
Console.WriteLine($"Avatar Changed");
await Task.Delay(config.ChangeDelayMS);
if (!isPaused)
{
if (!config.RelativeMode)
{
ApplyEyeHeight(lastEyeHeight);
}
else if (newId == lastAvatarId)
{
ApplyEyeHeight(lastEyeHeight);
}
else
{
float prevBaseHeight = lastEyeHeight * lastScaleFactorInverse;
float newBaseHeight = currentEyeHeight * currentScaleFactorInverse;
Console.WriteLine($"Previous Base Height: {prevBaseHeight:F3}, New Base Height: {newBaseHeight:F3}");
if (Math.Abs(prevBaseHeight - newBaseHeight) <= config.HeightTolerance)
{
float targetEyeHeight = newBaseHeight / lastScaleFactorInverse;
ApplyEyeHeight(targetEyeHeight);
}
else
{
if (config.ToleranceFailBehavior) ApplyEyeHeight(lastEyeHeight);
else Console.WriteLine("Base height difference exceeded tolerance.");
}
}
Console.WriteLine();
}
lastAvatarId = newId;
isChanging = false;
}
static void ApplyEyeHeight(float targetHeight)
{
Console.WriteLine($"EyeHeight set to {targetHeight:F3}");
sender.Send(new OscMessage("/avatar/eyeheight", targetHeight));
lastEyeHeight = targetHeight;
}
}
}