-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPositionalAudio.cs
527 lines (444 loc) · 18.5 KB
/
PositionalAudio.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using BepInEx;
using BepInEx.Unity.IL2CPP;
using UnityEngine;
using System.IO.MemoryMappedFiles;
using System.Text;
using BepInEx.Configuration;
using Agents;
using GTFO.API;
using SNetwork;
using System.Runtime.InteropServices;
using System.Collections.Concurrent;
using Player;
namespace PositionalAudio
{
public class OpenPAConfig
{
public static ConfigEntry<bool> configTalkState;
public static ConfigEntry<bool> configVerbose;
public static ConfigEntry<int> configIntensity { get; set; }
}
[BepInPlugin("net.devante.gtfo.positionalaudio", "PositionalAudio", "2.0.0")]
public class Plugin : BasePlugin
{
mumblelib.MumbleLinkFile mumbleLink;
private Timer gameStateCheckTimer;
private Timer reportingTaskTimer;
string endData;
public bool isPlayerInLevel = false;
Thread clientThread;
public bool gameStarted = false;
private volatile bool clientNoise = false;
public override void Load()
{
OpenPAConfig.configTalkState = Config.Bind("TalkState", "Enabled", false, "Whether or not the game should tap into the TalkState plugin for Mumble.");
OpenPAConfig.configIntensity = Config.Bind("TalkState", "Refresh Rate", 120, "The amount of time in milliseconds that the plugin will check for TalkState changes. 120 is a good sweetspot, but you can lower this if it's not precise enough. You could also up it if your host has a bad CPU, since hosts will be a bit more stressed out in this process. I would stay between 30ms and 240ms.");
OpenPAConfig.configVerbose = Config.Bind("Verbose", "Enabled", false, "Enables debug logs in the BepInEx console. Can get very spammy, but useful for debugging.");
LevelAPI.OnEnterLevel += CheckIfPlayerIsInLevel; // open event call
SendDebugLog($"Plugin is loaded!", false);
// Set up a timer to periodically check the game state every 5 seconds
gameStateCheckTimer = new Timer(CheckGameState, null, System.TimeSpan.Zero, System.TimeSpan.FromSeconds(3));
}
public override bool Unload()
{
#pragma warning disable SYSLIB0006 // Type or member is obsolete
clientThread.Abort(); // Close Async Thread to prevent crash on close.
#pragma warning restore SYSLIB0006 // Type or member is obsolete
SendDebugLog($"Plugin is unloading...", false);
return base.Unload();
}
public struct ClientCharID
{
public int PlayerID;
}
public unsafe void CheckGameState(object state)
{
var cState = GameStateManager.CurrentStateName.ToString();
if (gameStarted == true)
{
if (cState != "InLevel" && cState == "Inactive")
{
Unload();
}
}
if (cState == "Generating" || cState == "InLevel")
{
// await Task.Delay(1000);
SendDebugLog($"Game is now in the 'Generating' OR 'InLevel' state. ({cState})", false);
// Run Mumble Setup
mumbleLink = mumblelib.MumbleLinkFile.CreateOrOpen();
mumblelib.Frame* frame = mumbleLink.FramePtr();
frame->SetName("GTFO");
frame->uiVersion = 2;
string id = RandomString(16);
SendDebugLog($"Setting Mumble ID to {id}", false);
frame->SetID(id);
SendDebugLog($"Setting context to InLevel", false);
frame->SetContext("InLevel");
SendDebugLog("Mumble Link Shared Memory Initialized", false);
if (OpenPAConfig.configTalkState.Value == true) // Check config, if TalkState support is enabled, start TalkStatePinger.
{
PlayerStatus.PlayerStartedTalking(0, false);
PlayerStatus.PlayerStatusChanged += PlayerStatusChangedHandler;
HostSync();
FindTalkState();
SendDebugLog("TalkState Shared Memory Initialized", false);
gameStarted = true;
}
// Stop the game state check timer
gameStateCheckTimer.Change(Timeout.Infinite, Timeout.Infinite);
// Start the ReportingTask every second
reportingTaskTimer = new Timer(FixedUpdated, null, System.TimeSpan.Zero, System.TimeSpan.FromMilliseconds(24));
}
else
{
// Important for debugging in development builds.
SendDebugLog($"Currently not in level. Reattempting.. ({cState})", true);
}
}
// All log messages get sent down here.
// false == normal log
// true == debug/verbose log (only shown to user if config value is true)
public void SendDebugLog(string msg, bool verbose)
{
if (verbose == true) { if (OpenPAConfig.configVerbose.Value == true) { Log.LogInfo(msg); }
} else { Log.LogInfo(msg); }
}
// Might combine this with SendDebugLog later.
public void SendErrorLog(string msg, bool verbose)
{
if (verbose == true)
{
if (OpenPAConfig.configVerbose.Value == true) { Log.LogError(msg); }
} else { Log.LogError(msg); }
}
private static System.Random random = new System.Random();
private string RandomString(int len)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, len)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
public void CheckIfPlayerIsInLevel()
{
SendDebugLog($"Player connected to level.", true);
isPlayerInLevel = true;
}
private unsafe void FixedUpdated(object state)
{
// Set Current GameState.
var cState = GameStateManager.CurrentStateName.ToString();
// Player closed the game.
if (cState != "InLevel" && cState == "Inactive") // Could result in unexpected game closes
{
Unload();
}
// Check if Player left the expedition to prevent game crashing.
if (cState != "Generating" && cState != "ReadyToStopElevatorRide" && cState != "StopElevatorRide" && cState != "ReadyToStartLevel" && cState != "InLevel")
{
Log.LogWarning($"Expedition Aborted, Closing Link Connection.");
// Stop sending data to Mumble
reportingTaskTimer.Change(Timeout.Infinite, Timeout.Infinite);
// Start checking Gamestate again
gameStateCheckTimer = new Timer(CheckGameState, null, System.TimeSpan.Zero, System.TimeSpan.FromSeconds(3));
// Close Mumble Link Connection
mumbleLink.Dispose();
mumbleLink = null;
isPlayerInLevel = false;
return;
}
if (isPlayerInLevel == false)
{
// SendErrorLog($"Player is not in level yet! Holding...", true); // --- VERY spammy, even for verbose lol.
return;
}
// Added this to allow player to join mid game. EnterLevel Event fires before PlayerAgent is created, thus leading to a crash.
// This should prevent that, and give it enough time to process the PlayerAgent.
if (Player.PlayerManager.HasLocalPlayerAgent() == false) {
SendErrorLog("No PlayerAgent.", true);
return;
}
// Execute the code to get player variables and output them to the console
PlayerAgent character = Player.PlayerManager.GetLocalPlayerAgent();
var position = character.EyePosition - new Vector3(0, 1, 0);
var ucam = character.FPSCamera;
if (character != null && ucam != null && cState != null)
{
// SendDebugLog($"Everything is set. (!= null).");
if (mumbleLink == null)
{
SendDebugLog($"Initializing Load(). (mumbleLink == null).", false);
Load();
}
mumblelib.Frame* frame = mumbleLink.FramePtr();
if (ucam.Position != null)
{
frame->fCameraPosition[0] = ucam.Position.x;
frame->fCameraPosition[1] = ucam.Position.y;
frame->fCameraPosition[2] = ucam.Position.z;
}
if (ucam.Forward != null)
{
frame->fCameraFront[0] = ucam.Forward.x;
frame->fCameraFront[1] = ucam.Forward.y;
frame->fCameraFront[2] = ucam.Forward.z;
}
if (position != null && GameStateManager.CurrentStateName.ToString() == "InLevel")
{
frame->fAvatarPosition[0] = position.x;
frame->fAvatarPosition[1] = position.y;
frame->fAvatarPosition[2] = position.z;
}
else if (character.Forward == null || GameStateManager.CurrentStateName.ToString() != "InLevel") // If Player is null, use camera position instead.
{
//SendDebugLog($"Character is null, sending camera position instead. {ucam.Position}");
frame->fAvatarPosition[0] = ucam.Position.x;
frame->fAvatarPosition[1] = ucam.Position.y;
frame->fAvatarPosition[2] = ucam.Position.z;
}
if (character.Forward != null && GameStateManager.CurrentStateName.ToString() == "InLevel")
{
// SendDebugLog($"Character is set. {character.Forward.x}, {character.Forward.y}, {character.Forward.z}");
frame->fAvatarFront[0] = character.Forward.x;
frame->fAvatarFront[1] = character.Forward.y;
frame->fAvatarFront[2] = character.Forward.z;
}
else if (character.Forward == null || GameStateManager.CurrentStateName.ToString() != "InLevel") // If Player is null, use camera forward instead.
{
// SendDebugLog($"Character is null OR GSM is not InLevel, sending camera forward instead. {ucam.Forward.x}, {ucam.Forward.y}, {ucam.Forward.z}");
frame->fAvatarFront[0] = ucam.Forward.x;
frame->fAvatarFront[1] = ucam.Forward.y;
frame->fAvatarFront[2] = ucam.Forward.z;
}
frame->uiTick++;
}
else
{
if (mumbleLink != null)
{
SendDebugLog($"Closing Link Connection.", false);
mumbleLink.Dispose();
mumbleLink = null;
isPlayerInLevel = false;
return;
}
Log.LogError($"An error has occurred.");
}
}
public unsafe void FindTalkState()
{
// Set Current GameState.
var cState = GameStateManager.CurrentStateName.ToString();
clientThread = new(ReadMemoryMappedFile);
clientThread.Start();
SendDebugLog($"Initiated TalkState!", false);
if (cState != "Generating" && cState != "ReadyToStopElevatorRide" && cState != "StopElevatorRide" && cState != "ReadyToStartLevel" && cState != "InLevel")
{
SendDebugLog($"Expedition Aborted, Closing TalkState Connection.", false);
isPlayerInLevel = false;
// Stop this thread.
clientThread.Join();
return;
}
//ReadMemoryMappedFile();
void ReadMemoryMappedFile()
{
while (isPlayerInLevel == false)
{
SendErrorLog($"Player is not in level yet! Holding...", true);
Thread.Sleep(5000); // Check every 5 seconds.
}
// Mid-Joiners Rejoyce.
while (Player.PlayerManager.HasLocalPlayerAgent() == false)
{
SendErrorLog("No PlayerAgent.", true);
}
PlayerAgent character = Player.PlayerManager.GetLocalPlayerAgent();
bool sendStartOnce = false;
bool sendStopOnce = false;
HashSet<string> excludedStates = new HashSet<string> { "Generating", "ReadyToStopElevatorRide", "StopElevatorRide", "ReadyToStartLevel", "InLevel" };
int intensity = OpenPAConfig.configIntensity.Value;
const string memoryMappedFileName = "posaudio_mumlink";
try // Check if MemoryMappedFile is set. (aka if Mumble is open or not)
{
using (MemoryMappedFile.OpenExisting(memoryMappedFileName))
{
SendDebugLog("TalkState Enabled, and MemoryMappedFile found, continuing with operation.", true);
}
}
catch (FileNotFoundException)
{
SendErrorLog("TalkState enabled, but MemoryMappedFile could not be found. Disabling TalkState for this session.", false);
return;
}
catch (Exception e)
{
SendErrorLog("An error occurred while initializing TalkState MMF. Aborting...", false);
return;
}
// Continue with initialization
const int dataSize = 1024;
SendDebugLog($"Initiated RMMF!", false);
while (true)
{
if (!excludedStates.Contains(GameStateManager.CurrentStateName.ToString()))
{
break;
}
using (var mmf = MemoryMappedFile.OpenExisting(memoryMappedFileName))
{
using (var accessor = mmf.CreateViewAccessor(0, dataSize))
{
byte[] dataBytes = new byte[dataSize];
accessor.ReadArray(0, dataBytes, 0, dataSize);
string receivedData = Encoding.UTF8.GetString(dataBytes).TrimEnd('\0');
// SendDebugLog($"Received Data: {receivedData}");
if (SNet.LocalPlayer.IsMaster == true) // Is declared as the host.
{
// HOST START
if (receivedData == "Talking")
{
character.Noise = Agent.NoiseType.Walk;
SendDebugLog($"Sending WALK type to localplayeragent.", true);
}
}
else // Is declared as the client.
{
if (receivedData != "Talking") // Send stop signal
{
if (sendStopOnce == false) // hasn't been stopped yet.
{
SendDebugLog($"Sending stop signal for '{SteamManager.LocalPlayerName.ToString()}'.", true);
NetworkAPI.InvokeEvent<ClientSendData>("Client_Status", new ClientSendData
{
clientSlot = Player.PlayerManager.GetLocalPlayerSlotIndex(),
clientStatus = false, // False == Not Talking
});
sendStopOnce = true;
sendStartOnce = false;
}
} else if (receivedData == "Talking") // is currently talking
{
if (sendStartOnce == false) // hasn't been started yet.
{
SendDebugLog($"Sending talk signal for '{character.PlayerName}'!", true);
NetworkAPI.InvokeEvent<ClientSendData>("Client_Status", new ClientSendData
{
clientSlot = Player.PlayerManager.GetLocalPlayerSlotIndex(),
clientStatus = true, // True == Talking
});
sendStartOnce = true;
sendStopOnce = false;
}
}
sendStartOnce = false;
}
}
}
Thread.Sleep(intensity); // Sleep for 120 milliseconds.
// Adjust update frequency if CPU performance is bad.
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct ClientSendData
{
public int clientSlot;
public bool clientStatus;
}
public void HostSync()
{
SendDebugLog($"Checking HostSync...", true);
if (NetworkAPI.IsEventRegistered("Client_Status") == true) { return; } // prevent opening more than one event handler.
NetworkAPI.RegisterEvent<ClientSendData>("Client_Status", (senderId, packet) =>
{
if (SNet.LocalPlayer.IsMaster == true)
{
// Run host sync code, this is fired when a client sends a message.
SendDebugLog($"Message received from {senderId}: {packet.clientSlot}, {packet.clientStatus}.", true);
// run code here
if (packet.clientStatus == true)
{
PlayerStatus.PlayerStartedTalking(packet.clientSlot, true);
}
else
{
PlayerStatus.PlayerStoppedTalking(packet.clientSlot);
}
}
SendDebugLog($"Triggered NetworkEvent.", true);
});
SendDebugLog($"Registered NetworkEvent.", true);
}
public void PlayerStatusChangedHandler(object sender, PlayerStatusChangedEvent e)
{
int cSlot = e.PlayerID;
PlayerAgent clientAgent = null;
if (e.IsTalking)
{
SendDebugLog($"Player {e.PlayerID} started talking.", true);
bool trygetresult = Player.PlayerManager.TryGetPlayerAgent(ref cSlot, out clientAgent);
if (trygetresult)
{
clientAgent.Noise = Agent.NoiseType.Walk;
}
}
else
{
SendDebugLog($"Player {e.PlayerID} stopped talking.", true);
}
}
public interface LinkFileFactory
{
LinkFile Open();
}
public interface LinkFile : System.IDisposable
{
uint UIVersion { set; }
void Tick();
Vector3 CharacterPosition { set; }
Vector3 CharacterForward { set; }
Vector3 CharacterTop { set; }
string Name { set; }
Vector3 CameraPosition { set; }
Vector3 CameraForward { set; }
Vector3 CameraTop { set; }
string ID { set; }
string Context { set; }
string Description { set; }
}
}
public static class PlayerStatus
{
private static ConcurrentDictionary<int, bool> playerStatus = new ConcurrentDictionary<int, bool>();
public static event EventHandler<PlayerStatusChangedEvent> PlayerStatusChanged;
public static void PlayerStartedTalking(int playerID, bool isTalking)
{
playerStatus.AddOrUpdate(playerID, isTalking, (key, oldValue) => isTalking);
// Raise changed event
OnPlayerStatusChanged(new PlayerStatusChangedEvent(playerID, isTalking));
}
public static void PlayerStoppedTalking(int playerID)
{
bool removed = playerStatus.TryRemove(playerID, out _);
}
public static List<int> GetPlayersTalking()
{
return playerStatus.Where(kv => kv.Value).Select(kv => kv.Key).ToList();
}
private static void OnPlayerStatusChanged(PlayerStatusChangedEvent e)
{
PlayerStatusChanged?.Invoke(null, e);
}
}
public class PlayerStatusChangedEvent : EventArgs
{
public int PlayerID { get; }
public bool IsTalking { get; }
public PlayerStatusChangedEvent(int playerID, bool isTalking)
{
PlayerID = playerID;
IsTalking = isTalking;
}
}
}