-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPhotonRoomManager.cs
More file actions
592 lines (460 loc) · 18.9 KB
/
PhotonRoomManager.cs
File metadata and controls
592 lines (460 loc) · 18.9 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using Photon.Pun;
using Photon.Realtime;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using PeerStateValue = ExitGames.Client.Photon.PeerStateValue;
using Hashtable = ExitGames.Client.Photon.Hashtable;
/// <summary>
/// Manages Photon Room creation and maintenance, including custom data synchronized (shared) to the Room.
/// </summary>
[RequireComponent(typeof(PhotonView))]
[MetaCodeSample("SpaceSharing")]
public class PhotonRoomManager : MonoBehaviourPunCallbacks
{
//
// Public interface
public delegate void RoomDataUpdated(Guid groupId, Guid[] roomIds, Pose? floorPose, bool isLocal);
public delegate void CustomDataUpdated(object boxedData, bool isLocal);
public static event RoomDataUpdated OnRoomDataUpdated
{
add
{
if (value is null)
return;
if (TryGetRoomData(out var group, out var rooms, out var floorPose, out bool isLocal))
value(group, rooms, floorPose, isLocal);
s_OnRoomDataUpdated += value;
}
remove => s_OnRoomDataUpdated -= value;
}
public static event CustomDataUpdated OnCustomDataUpdated
{
add
{
if (value is null)
return;
if (TryGetCustomData(out var boxedData, out bool isLocal))
value(boxedData, isLocal);
s_OnCustomDataUpdated += value;
}
remove => s_OnCustomDataUpdated -= value;
}
public static string CurrentRoomName
=> PhotonNetwork.InRoom ? PhotonNetwork.CurrentRoom.Name
: PhotonNetwork.InLobby ? $"{PhotonNetwork.CurrentLobby.Name} (lobby)"
: "(none)";
public static bool CheckConnection(bool tryReconnect, bool logOnFail)
{
if (PhotonNetwork.IsConnected)
return true;
var peerState = PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState;
bool reconnecting = peerState == PeerStateValue.Connecting ||
(tryReconnect && (PhotonNetwork.Reconnect() ||
PhotonNetwork.ConnectUsingSettings()));
if (!logOnFail)
return false;
const string kPhotonNotConnected = "PhotonNetwork was disconnected";
const string kNextStepsMsg = "Check your wifi, restart the scene/app, or check logcat for causes";
if (reconnecting)
Sampleton.Warn($"{kPhotonNotConnected}. Reconnecting now...\n(Status update should log here in a few moments.)");
else if (tryReconnect)
Sampleton.Error($"{kPhotonNotConnected}. The attempt to reconnect FAILED.\n{kNextStepsMsg}.");
else
Sampleton.Error($"{kPhotonNotConnected}. {kNextStepsMsg}.");
return false;
}
public static bool TryJoinLobby()
{
if (!CheckConnection(tryReconnect: false, logOnFail: false))
return false;
s_LastLobby = new TypedLobby(Sampleton.CurrentScene.name, LobbyType.Default);
return PhotonNetwork.JoinLobby(s_LastLobby);
}
public static bool TryJoinRoom(string roomName)
{
var kRoomOptions = new RoomOptions()
{
IsVisible = true,
IsOpen = true,
BroadcastPropsChangeToAll = true,
MaxPlayers = 0, // no defined limit
EmptyRoomTtl = 60000, // 1 minute
PlayerTtl = 600000, // 10 minutes
};
return PhotonNetwork.JoinOrCreateRoom(roomName, kRoomOptions, s_LastLobby);
}
/// <param name="roomUuids">
/// Note: The first element is expected to be the Anchor.Uuid of the host's "main" (or current) MRUKRoom.
/// </param>
/// <param name="floorPose">
/// If provided and not null, must be the world pose of the floor anchor corresponding to the first MRUKRoom
/// UUID in <paramref name="roomUuids"/> (from the publisher's perspective). This will allow MRUK to
/// automatically perform colocated world alignment, enabling even non-anchor GameObjects to appear in the
/// "correct" poses in the shared mixed reality space for all clients.
/// </param>
public static void PublishRoomData(Guid groupUuid, ICollection<Guid> roomUuids, Pose? floorPose = null)
{
if (floorPose is null)
Sampleton.Log($"{nameof(PublishRoomData)}: 1 group, {roomUuids.Count} room(s)");
else
Sampleton.Log($"{nameof(PublishRoomData)}: 1 group, {roomUuids.Count} room(s), 1 pose");
if (roomUuids.Count == 0)
{
Sampleton.Warn($"- (skipped ~ 0 rooms)");
return;
}
PrepareRoomPropertyUpdate(out var room, out var newProps, out var expected, out int version);
var roomArr = new Guid[roomUuids.Count];
roomUuids.CopyTo(roomArr, 0);
newProps[k_PropKeyRoomIds] = roomArr;
newProps[k_PropKeyGroupId] = groupUuid;
if (floorPose.HasValue)
newProps[k_PropKeyHostPose] = floorPose.Value;
else if (version > 0)
newProps[k_PropKeyHostPose] = null;
if (room.SetCustomProperties(newProps, expected))
s_LastVersion = version;
else
Sampleton.Error("- ERR: Photon room.SetCustomProperties failed! (possible concurrency failure)");
}
/// <param name="boxedData">
/// MUST be serializable by Photon!
/// </param>
public static void PublishCustomData(object boxedData)
{
Sampleton.Log($"{nameof(PublishCustomData)}: {nameof(boxedData)}<{boxedData.SafeTypeName()}>");
PrepareRoomPropertyUpdate(out var room, out var newProps, out var expected, out int version);
newProps[k_PropKeyCustomData] = boxedData;
if (room.SetCustomProperties(newProps, expected))
s_LastVersion = version;
else
Sampleton.Error("- ERR: Photon room.SetCustomProperties failed! (possible concurrency failure)");
}
//
// Constants
const string k_PropKeyVersion = "ver"; // int
const string k_PropKeySender = "src"; // string
const string k_PropKeyGroupId = "group"; // System.Guid (works thanks to our PhotonExtensions.cs)
const string k_PropKeyRoomIds = "rooms"; // System.Guid[] (^) (first Guid = host's main room)
const string k_PropKeyHostPose = "pose"; // UnityEngine.Pose (^^) (can be omitted)
const string k_PropKeyCustomData = "data"; // any (listeners to OnCustomDataUpdated decide how to handle)
//
// Fields
static TypedLobby s_LastLobby;
static string s_LastRoomName;
static int s_LastVersion = -1;
static RoomDataUpdated s_OnRoomDataUpdated = LogOnRoomDataUpdated;
static CustomDataUpdated s_OnCustomDataUpdated = LogOnCustomDataUpdated;
//
// impl.
static void PrepareRoomPropertyUpdate(out Room room, out Hashtable newProps, out Hashtable expectedProps, out int currentVersion)
{
room = PhotonNetwork.CurrentRoom;
Assert.IsNotNull(room, "PhotonNetwork.CurrentRoom");
currentVersion = 0;
expectedProps = null;
if (room.CustomProperties.TryGetValue(k_PropKeyVersion, out var box) && box is int v)
{
expectedProps = new Hashtable
{
[k_PropKeyVersion] = currentVersion = v,
};
}
newProps = new Hashtable
{
[k_PropKeyVersion] = currentVersion + 1,
[k_PropKeySender] = $"{PhotonNetwork.LocalPlayer}",
};
}
static void LogOnRoomDataUpdated(Guid groupId, Guid[] roomIds, Pose? floorPose, bool isLocal)
{
Assert.IsNotNull(Sampleton.PhotonRoomManager, "Sampleton.PhotonRoomManager");
if (!PhotonNetwork.InRoom ||
!PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(k_PropKeySender, out var box) ||
box is not string updatedBy)
{
updatedBy = "(unknown)";
}
Sampleton.Log(
$"{nameof(OnRoomDataUpdated)}: {nameof(updatedBy)}: {updatedBy}\n" +
$" {nameof(isLocal)}: {isLocal}\n" +
$" {nameof(groupId)}: {groupId.Brief()}\n" +
$" {nameof(roomIds)}.Length: {roomIds.Length}, {nameof(roomIds)}[0]: {roomIds[0].Brief()}\n" +
$" {nameof(floorPose)}: {(floorPose is null ? "null" : floorPose.Value.position.ToString("F2"))}"
// (logging just the floorPose's position is enough to get an idea of it)
);
}
static void LogOnCustomDataUpdated(object boxedData, bool isLocal)
{
Assert.IsNotNull(Sampleton.PhotonRoomManager, "Sampleton.PhotonRoomManager");
if (!PhotonNetwork.InRoom ||
!PhotonNetwork.CurrentRoom.CustomProperties.TryGetValue(k_PropKeySender, out var box) ||
box is not string updatedBy)
{
updatedBy = "(unknown)";
}
Sampleton.Log(
$"{nameof(OnCustomDataUpdated)}: {nameof(updatedBy)}: {updatedBy}\n" +
$" {nameof(isLocal)}: {isLocal}\n" +
$" {nameof(boxedData)}: <{boxedData.SafeTypeName()}>"
);
}
static bool TryGetRoomData(out Guid groupId, out Guid[] roomIds, out Pose? floorPose, out bool isLocal, Hashtable props = null)
{
groupId = Guid.Empty;
roomIds = Array.Empty<Guid>();
floorPose = null;
isLocal = false;
if (props is null)
{
var room = PhotonNetwork.CurrentRoom;
if (room is null)
return false;
props = room.CustomProperties;
}
if (!props.TryGetValue(k_PropKeyGroupId, out var box) || box is not Guid groupUuid || groupUuid == Guid.Empty)
return false;
groupId = groupUuid;
if (!props.TryGetValue(k_PropKeyRoomIds, out box) || box is not Guid[] roomArray || roomArray.Length == 0)
return false;
roomIds = roomArray;
if (props.TryGetValue(k_PropKeyHostPose, out box) && box is Pose pose)
floorPose = pose;
if (props.TryGetValue(k_PropKeyVersion, out box) && box is int version)
isLocal = version == s_LastVersion;
return true;
}
static bool TryGetCustomData(out object boxedData, out bool isLocal, Hashtable props = null)
{
isLocal = false;
if (props is null)
{
var room = PhotonNetwork.CurrentRoom;
if (room is null)
{
boxedData = null;
return false;
}
props = room.CustomProperties;
}
if (!props.TryGetValue(k_PropKeyCustomData, out boxedData) || boxedData is null)
return false;
if (props.TryGetValue(k_PropKeyVersion, out var box) && box is int version)
isLocal = version == s_LastVersion;
return true;
}
//
// MonoBehaviourPunCallbacks impl.
#region [Monobehaviour Methods]
public override void OnEnable()
{
base.OnEnable();
PhotonNetwork.ConnectUsingSettings();
}
public override void OnDisable()
{
base.OnDisable();
s_OnRoomDataUpdated = LogOnRoomDataUpdated;
s_OnCustomDataUpdated = LogOnCustomDataUpdated;
OnApplicationQuit();
}
IEnumerator OnApplicationPause(bool pause)
{
if (pause)
{
StopAllCoroutines();
yield break;
}
yield return null;
while (Application.internetReachability == NetworkReachability.NotReachable)
yield return null;
// get the "low-level" peer state since it's what's checked before we try [re]connecting to Photon:
var peerState = PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState;
Sampleton.Log($">> PhotonNetwork is {peerState}... (frame={Time.frameCount})");
var ui = Sampleton.BaseUI;
if (PhotonNetwork.InRoom)
{
if (ui)
ui.DisplayRoomPanel();
yield break;
}
if (ui)
ui.DisplayLobbyPanel();
if (PhotonNetwork.InLobby)
yield break;
if (peerState == PeerStateValue.Connecting)
yield break;
if (PhotonNetwork.ReconnectAndRejoin())
{
Sampleton.Log($">> PhotonNetwork.ReconnectAndRejoin()");
yield break;
}
if (TryJoinLobby())
{
Sampleton.Log($">> PhotonNetwork.JoinLobby()");
yield break;
}
if (PhotonNetwork.ConnectUsingSettings())
{
Sampleton.Log($">> PhotonNetwork.ConnectUsingSettings()");
yield break;
}
yield return null;
switch (PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState)
{
case PeerStateValue.Disconnected:
case PeerStateValue.Disconnecting:
Sampleton.Error($">> ERR: PhotonNetwork failed to (re)connect after app resumed.");
break;
}
}
void OnApplicationQuit()
{
if (PhotonNetwork.InRoom)
{
// Call LeaveRoom(false) explicitly, so intentionally
// leaving doesn't make your "inactive" slot linger:
PhotonNetwork.LeaveRoom(becomeInactive: false);
}
PhotonNetwork.Disconnect();
}
#endregion [Monobehaviour Methods]
#region [Photon Callbacks]
public override void OnConnectedToMaster()
{
Sampleton.Log($"Photon::OnConnectedToMaster, CloudRegion='{PhotonNetwork.CloudRegion}'");
TryJoinLobby();
}
public override void OnJoinedLobby()
{
Sampleton.Log($"Photon::OnJoinedLobby: {PhotonNetwork.CurrentLobby}");
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
{
ui.NotifyLobbyAvailable(true);
ui.DisplayLobbyPanel();
}
}
public override void OnLeftLobby()
{
Sampleton.Log($"Photon::OnLeftLobby");
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
ui.NotifyLobbyAvailable(false);
}
public override void OnDisconnected(DisconnectCause cause)
{
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
{
ui.NotifyRoomListUpdate(null);
}
switch (cause)
{
case DisconnectCause.DisconnectByServerLogic:
case DisconnectCause.DisconnectByDisconnectMessage:
case DisconnectCause.DnsExceptionOnConnect:
case DisconnectCause.ServerAddressInvalid:
case DisconnectCause.InvalidRegion:
case DisconnectCause.InvalidAuthentication:
case DisconnectCause.AuthenticationTicketExpired:
case DisconnectCause.CustomAuthenticationFailed:
case DisconnectCause.OperationNotAllowedInCurrentState:
case DisconnectCause.DisconnectByOperationLimit:
case DisconnectCause.MaxCcuReached:
Sampleton.Error($"Photon:OnDisconnected: {cause}\n- will NOT attempt to automatically ReconnectAndRejoin().");
return;
case DisconnectCause.Exception:
case DisconnectCause.ExceptionOnConnect:
case DisconnectCause.ClientTimeout:
case DisconnectCause.ServerTimeout:
case DisconnectCause.DisconnectByServerReasonUnknown:
Sampleton.Warn($"Photon::OnDisconnected: {cause}\n+ attempting auto ReconnectAndRejoin() in 2 seconds...");
Sampleton.DelayCall(2f, () => _ = PhotonNetwork.ReconnectAndRejoin() || PhotonNetwork.ConnectUsingSettings());
return;
default:
case DisconnectCause.None:
case DisconnectCause.DisconnectByClientLogic:
Sampleton.Log($"Photon::OnDisconnected: {cause}");
return;
}
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
string room = s_LastRoomName;
s_LastRoomName = null;
if (!CheckConnection(tryReconnect: false, logOnFail: true))
return;
if (returnCode == ErrorCode.GameDoesNotExist && !string.IsNullOrEmpty(room) && TryJoinRoom(room))
{
Sampleton.Warn(
$"Photon::OnJoinRoomFailed: \"{message}\" ({returnCode})\n" +
$"+ Creating a new \"{room}\"..."
);
return;
}
Sampleton.Error($"Photon::OnJoinRoomFailed: \"{message}\" ({returnCode})");
if (!PhotonNetwork.InLobby)
TryJoinLobby();
else if (Sampleton.GetActiveUI(out var ui))
ui.DisplayLobbyPanel();
}
public override void OnJoinedRoom()
{
var room = PhotonNetwork.CurrentRoom;
Sampleton.Log($"Photon::OnJoinedRoom: {room.Name}");
s_LastRoomName = room.Name;
s_LastVersion = -1; // rejoining should trigger On*DataUpdated even if the data was originally local
OnRoomPropertiesUpdate(room.CustomProperties);
if (Sampleton.GetActiveUI(out var ui))
ui.DisplayRoomPanel();
}
public override void OnLeftRoom()
{
Sampleton.Log("Photon::OnLeftRoom"); // mainly for log consistency
s_LastRoomName = null;
s_LastVersion = -1;
if (Sampleton.GetActiveUI(out var ui))
ui.DisplayLobbyPanel();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Sampleton.Log($"Photon::OnPlayerEnteredRoom: {newPlayer}");
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
ui.NotifyRoomUsersUpdated();
}
public override void OnPlayerLeftRoom(Player otherPlayer)
{
if (otherPlayer.IsInactive)
{
Sampleton.Log($"Photon::OnPlayerLeftRoom: {otherPlayer} has gone inactive.");
return;
}
Sampleton.Log($"Photon::OnPlayerLeftRoom: {otherPlayer} has left.");
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
ui.NotifyRoomUsersUpdated();
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
if (Sampleton.BaseUI is LocalSpaceSharingUI ui)
ui.NotifyRoomListUpdate(roomList);
}
public override void OnRoomPropertiesUpdate(Hashtable changedProps)
{
Assert.IsNotNull(s_OnRoomDataUpdated, "s_OnRoomDataUpdated should never be null");
Assert.IsNotNull(s_OnCustomDataUpdated, "s_OnCustomDataUpdated should never be null");
if (changedProps != PhotonNetwork.CurrentRoom.CustomProperties) // intentional reference comparison
Sampleton.Log($"Photon::OnRoomPropertiesUpdate({string.Join(",", changedProps.Keys)})");
if (TryGetRoomData(out var group, out var rooms, out var floorPose, out bool isLocal, props: changedProps))
{
s_OnRoomDataUpdated(group, rooms, floorPose, isLocal);
}
if (TryGetCustomData(out var boxedData, out isLocal, props: changedProps))
{
s_OnCustomDataUpdated(boxedData, isLocal);
}
}
#endregion [Photon Callbacks]
}