-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
CyanEmuPlayerManager.cs
543 lines (448 loc) · 17 KB
/
CyanEmuPlayerManager.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
using System;
using System.Collections.Generic;
using UnityEngine;
using VRC.SDKBase;
namespace VRCPrefabs.CyanEmu
{
public class CyanEmuPlayerManager
{
// Player Manager system
private static int masterID = -1;
private static int localPlayerID = -1;
private static int nextPlayerID = 1;
private static readonly Dictionary<VRCPlayerApi, int> playerIDs = new Dictionary<VRCPlayerApi, int>();
private static readonly Dictionary<int, VRCPlayerApi> players = new Dictionary<int, VRCPlayerApi>();
private static readonly List<VRCPlayerApi> waitingPlayers = new List<VRCPlayerApi>();
private static bool networkReady = false;
public static void InitializePlayer(VRCPlayerApi player)
{
int id = nextPlayerID;
player.Log("Assigning player id " + id);
++nextPlayerID;
playerIDs[player] = id;
players[id] = player;
player.AddToList();
if (masterID == -1)
{
player.Log("Player is now master");
masterID = id;
Debug.Assert(player.isMaster, "VRCP_PlayerManager:InitializePlayer Player should be considered master!");
}
else
{
Debug.Assert(!player.isMaster, "VRCP_PlayerManager:InitializePlayer Player should not be considered master!");
}
Debug.Assert(player.playerId == id, "VRCP_PlayerManager:InitializePlayer Player's id does not match assigned id!");
if (networkReady)
{
CyanEmuMain.PlayerJoined(player);
}
else
{
waitingPlayers.Add(player);
}
}
public static void OnNetworkReady()
{
networkReady = true;
foreach (var player in waitingPlayers)
{
CyanEmuMain.PlayerJoined(player);
}
waitingPlayers.Clear();
}
public static VRCPlayerApi CreateNewPlayer(bool local, GameObject playerObj, string name = null)
{
VRCPlayerApi player = new VRCPlayerApi();
player.gameObject = playerObj;
player.displayName = (string.IsNullOrEmpty(name) ? (local ? "Local" : "Remote") +" Player " + nextPlayerID : name);
player.isLocal = local;
InitializePlayer(player);
if (local)
{
localPlayerID = player.playerId;
}
Debug.Assert(player.isLocal == local, "VRCP_PlayerManager:CreateNewPlayer New player does not match local settings!");
return player;
}
public static void RemovePlayer(VRCPlayerApi player)
{
playerIDs.Remove(player);
players.Remove(player.playerId);
player.RemoveFromList();
if (masterID == player.playerId)
{
masterID = -1;
if (VRCPlayerApi.AllPlayers.Count != 0)
{
masterID = VRCPlayerApi.AllPlayers[0].playerId;
}
}
CyanEmuMain.PlayerLeft(player);
}
public static int GetMasterID()
{
return masterID;
}
public static VRCPlayerApi LocalPlayer()
{
return GetPlayerByID(localPlayerID);
}
public static VRCPlayerApi GetPlayerByID(int playerID)
{
players.TryGetValue(playerID, out VRCPlayerApi player);
return player;
}
public static int GetPlayerID(VRCPlayerApi player)
{
if (player == null)
{
return -1;
}
playerIDs.TryGetValue(player, out int playerId);
return playerId;
}
public static bool IsMaster(VRCPlayerApi player)
{
return GetPlayerID(player) == masterID;
}
public static bool IsLocalPlayerMaster()
{
return localPlayerID == masterID;
}
public static void EnablePickups(VRCPlayerApi player, bool enabled)
{
if (!player.isLocal)
{
player.LogWarning("[VRCPlayerAPI.EnablePickups] EnablePickups for remote players will do nothing.");
return;
}
// TODO
}
public static void Immobilize(VRCPlayerApi player, bool immobilized)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.Immobilize] You cannot set remote players Immobilized");
}
player.GetPlayerController().Immobilize(immobilized);
}
public static void TeleportToOrientationLerp(VRCPlayerApi player, Vector3 position, Quaternion rotation, VRC_SceneDescriptor.SpawnOrientation orientation, bool lerp)
{
if (!player.isLocal)
{
player.LogWarning("[VRCPlayerAPI.TeleportTo] Teleporting remote players will do nothing.");
return;
}
// Ignore lerp since there is no networking here
player.GetPlayerController().Teleport(position, rotation, orientation == VRC_SceneDescriptor.SpawnOrientation.AlignRoomWithSpawnPoint);
}
public static void TeleportToOrientation(VRCPlayerApi player, Vector3 position, Quaternion rotation, VRC_SceneDescriptor.SpawnOrientation orientation)
{
TeleportToOrientationLerp(player, position, rotation, VRC_SceneDescriptor.SpawnOrientation.Default, false);
}
public static void TeleportTo(VRCPlayerApi player, Vector3 position, Quaternion rotation)
{
TeleportToOrientationLerp(player, position, rotation, VRC_SceneDescriptor.SpawnOrientation.Default, false);
}
public static void PlayHapticEventInHand(VRCPlayerApi player, VRC_Pickup.PickupHand hand, float f1, float f2, float f3)
{
if (!player.isLocal)
{
player.LogWarning("[VRCPlayerAPI.PlayHapticEventInHand] PlayHapticEventInHand for remote players will do nothing.");
return;
}
// TODO
}
public static void ClaimNetworkControl(VRCPlayerApi player, VRC_ObjectApi obj)
{
// TODO Is this necessary?
}
public static VRCPlayerApi GetPlayerByGameObject(GameObject obj)
{
CyanEmuPlayer player = obj.GetComponentInParent<CyanEmuPlayer>();
if (player != null)
{
return player.player;
}
return null;
}
public static VRC_Pickup GetPickupInHand(VRCPlayerApi player, VRC_Pickup.PickupHand hand)
{
return player.GetPlayerController().GetHeldPickup(hand);
}
public static VRCPlayerApi.TrackingData GetTrackingData(VRCPlayerApi player, VRCPlayerApi.TrackingDataType trackingDataType)
{
return player.GetPlayerController().GetTrackingData(trackingDataType);
}
public static void TakeOwnership(VRCPlayerApi player, GameObject obj)
{
obj.SetOwner(player);
}
public static VRCPlayerApi GetOwner(GameObject obj)
{
ICyanEmuSyncable sync = obj.GetComponent<ICyanEmuSyncable>();
int playerID = sync != null ? sync.GetOwner() : masterID;
if (!players.TryGetValue(playerID, out VRCPlayerApi player))
{
return null;
}
return player;
}
public static bool IsOwner(VRCPlayerApi player, GameObject obj)
{
ICyanEmuSyncable sync = obj.GetComponent<ICyanEmuSyncable>();
int owner = sync == null ? masterID : sync.GetOwner();
return owner == player.playerId;
}
public static float GetRunSpeed(VRCPlayerApi player)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.GetRunSpeed] You cannot get run speed for remote clients!");
}
return player.GetPlayerController().GetRunSpeed();
}
public static void SetRunSpeed(VRCPlayerApi player, float speed)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.SetRunSpeed] You cannot set run speed for remote clients!");
}
player.GetPlayerController().SetRunSpeed(speed);
}
public static float GetStrafeSpeed(VRCPlayerApi player)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.GetStrafeSpeed] You cannot get strafe speed for remote clients!");
}
return player.GetPlayerController().GetStrafeSpeed();
}
public static void SetStrafeSpeed(VRCPlayerApi player, float speed)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.SetStrafeSpeed] You cannot set strafe speed for remote clients!");
}
player.GetPlayerController().SetStrafeSpeed(speed);
}
public static float GetWalkSpeed(VRCPlayerApi player)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.GetWalkSpeed] You cannot get walk speed for remote clients!");
}
return player.GetPlayerController().GetWalkSpeed();
}
public static void SetWalkSpeed(VRCPlayerApi player, float speed)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.SetWalkSpeed] You cannot set walk speed for remote clients!");
}
player.GetPlayerController().SetWalkSpeed(speed);
}
public static float GetJumpImpulse(VRCPlayerApi player)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.GetJumpImpulse] You cannot get jump impulse for remote clients!");
}
return player.GetPlayerController().GetJump();
}
public static void SetJumpImpulse(VRCPlayerApi player, float jump)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.SetJumpImpulse] You cannot set jump impulse for remote clients!");
}
player.GetPlayerController().SetJump(jump);
}
public static float GetGravityStrength(VRCPlayerApi player)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.GetGravityStrength] You cannot get gravity strength for remote clients!");
}
return player.GetPlayerController().GetGravityStrength();
}
public static void SetGravityStrength(VRCPlayerApi player, float gravity)
{
if (!player.isLocal)
{
throw new Exception("[VRCPlayerAPI.SetGravityStrength] You cannot set gravity strength for remote clients!");
}
player.GetPlayerController().SetGravityStrength(gravity);
}
public static Vector3 GetVelocity(VRCPlayerApi player)
{
if (!player.isLocal)
{
return Vector3.zero;
}
return player.GetPlayerController().GetVelocity();
}
public static void SetVelocity(VRCPlayerApi player, Vector3 velocity)
{
if (!player.isLocal)
{
return;
}
player.GetPlayerController().SetVelocity(velocity);
}
public static Vector3 GetPosition(VRCPlayerApi player)
{
return player.gameObject.transform.position;
}
public static Quaternion GetRotation(VRCPlayerApi player)
{
return player.gameObject.transform.rotation;
}
public static bool IsGrounded(VRCPlayerApi player)
{
if (!player.isLocal)
{
// TODO verify remote player values when not grounded.
return true;
}
return player.GetPlayerController().IsGrounded();
}
public static void UseAttachedStation(VRCPlayerApi player)
{
if (!player.isLocal)
{
return;
}
GameObject obj = null;
#if UDON
// This is dumb
obj = VRC.Udon.UdonManager.Instance.currentlyExecuting.gameObject;
#endif
obj?.GetComponent<CyanEmuStationHelper>()?.UseStation();
}
public static void UseLegacyLocomotion(VRCPlayerApi player)
{
if (!player.isLocal)
{
return;
}
player.GetPlayerController().UseLegacyLocomotion();
}
public static Quaternion GetBoneRotation(VRCPlayerApi player, HumanBodyBones bone)
{
return player.gameObject.transform.rotation;
}
public static Vector3 GetBonePosition(VRCPlayerApi player, HumanBodyBones bone)
{
return player.gameObject.transform.position;
}
public static Transform GetBoneTransform(VRCPlayerApi player, HumanBodyBones bone)
{
// TODO
return null;
}
#region Player Tags
public static List<int> GetPlayersWithTag(string tagName, string tagValue)
{
List<int> players = new List<int>();
foreach (var player in VRCPlayerApi.AllPlayers)
{
if (player.GetCyanEmuPlayer().HasTag(tagName, tagValue))
{
players.Add(player.playerId);
}
}
return players;
}
public static void ClearPlayerTag(VRCPlayerApi player)
{
player.GetCyanEmuPlayer().ClearTags();
}
public static void SetPlayerTag(VRCPlayerApi player, string tagName, string tagValue)
{
player.GetCyanEmuPlayer().SetTag(tagName, tagValue);
}
public static string GetPlayerTag(VRCPlayerApi player, string tagName)
{
return player.GetCyanEmuPlayer().GetTag(tagName);
}
public static void ClearSilence(VRCPlayerApi player)
{
// TODO?
}
public static void SetSilencedToUntagged(VRCPlayerApi player, int number, string tagName, string tagValue)
{
// TODO?
}
public static void SetSilencedToTagged(VRCPlayerApi player, int number, string tagName, string tagValue)
{
// TODO?
}
public static void ClearInvisible(VRCPlayerApi player)
{
// TODO?
}
public static void SetInvisibleToUntagged(VRCPlayerApi player, bool invisible, string tagName, string tagValue)
{
// TODO?
}
public static void SetInvisibleToTagged(VRCPlayerApi player, bool invisible, string tagName, string tagValue)
{
// TODO?
}
#endregion
#region Player Audio
public static void SetAvatarAudioVolumetricRadius(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetAvatarAudioNearRadius(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetAvatarAudioFarRadius(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetAvatarAudioGain(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetVoiceLowpass(VRCPlayerApi player, bool value)
{
// TODO?
}
public static void SetVoiceVolumetricRadius(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetVoiceDistanceFar(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetVoiceDistanceNear(VRCPlayerApi player, float value)
{
// TODO?
}
public static void SetVoiceGain(VRCPlayerApi player, float value)
{
// TODO?
}
#endregion
/*
TODO all the interfaces:
public static ClaimNetworkControlDelegate ClaimNetworkControl;
public static GetLookRayDelegate GetLookRay;
public static Action<VRCPlayerApi, RuntimeAnimatorController> _PushAnimations;
public static Action<VRCPlayerApi> _PopAnimations;
public static SetAnimatorBoolDelegate SetAnimatorBool;
public static Func<VRCPlayerApi, bool> _isModeratorDelegate;
public static Func<VRCPlayerApi, bool> _isSuperDelegate;
public static Action<VRCPlayerApi, Color> _SetNamePlateColor;
public static Action<VRCPlayerApi> _RestoreNamePlateColor;
public static Action<VRCPlayerApi, bool> _SetNamePlateVisibility;
public static Action<VRCPlayerApi> _RestoreNamePlateVisibility;
*/
}
}