/
PhotonNetwork.cs
1384 lines (1276 loc) · 44.3 KB
/
PhotonNetwork.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using ExitGames.Client.Photon;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public static class PhotonNetwork
{
private static bool _mAutomaticallySyncScene = false;
private static bool autoJoinLobbyField = true;
public static bool InstantiateInRoomOnly = true;
private static bool isOfflineMode = false;
internal static int lastUsedViewSubId = 0;
internal static int lastUsedViewSubIdStatic = 0;
public static PhotonLogLevel logLevel = PhotonLogLevel.ErrorsOnly;
private static bool m_autoCleanUpPlayerObjects = true;
private static bool m_isMessageQueueRunning = true;
internal static List<int> manuallyAllocatedViewIds = new List<int>();
public static readonly int MAX_VIEW_IDS = 0x3e8;
internal static NetworkingPeer networkingPeer;
private static Room offlineModeRoom = null;
public static EventCallback OnEventCall;
internal static readonly PhotonHandler photonMono;
public static ServerSettings PhotonServerSettings = ((ServerSettings)Resources.Load("PhotonServerSettings", typeof(ServerSettings)));
public static float precisionForFloatSynchronization = 0.01f;
public static float precisionForQuaternionSynchronization = 1f;
public static float precisionForVectorSynchronization = 9.9E-05f;
private static int sendInterval = 50;
private static int sendIntervalOnSerialize = 100;
public static HashSet<GameObject> SendMonoMessageTargets;
public const string serverSettingsAssetFile = "PhotonServerSettings";
public const string serverSettingsAssetPath = "Assets/Photon Unity Networking/Resources/PhotonServerSettings.asset";
public static bool UseNameServer = true;
public static bool UsePrefabCache = true;
public const string versionPUN = "1.28";
static PhotonNetwork()
{
Application.runInBackground = true;
GameObject obj2 = new GameObject();
photonMono = obj2.AddComponent<PhotonHandler>();
obj2.name = "PhotonMono";
obj2.hideFlags = HideFlags.HideInHierarchy;
networkingPeer = new NetworkingPeer(photonMono, string.Empty, ConnectionProtocol.Udp);
CustomTypes.Register();
}
private static int[] AllocateSceneViewIDs(int countOfNewViews)
{
int[] numArray = new int[countOfNewViews];
for (int i = 0; i < countOfNewViews; i++)
{
numArray[i] = AllocateViewID(0);
}
return numArray;
}
public static int AllocateViewID()
{
int item = AllocateViewID(player.ID);
manuallyAllocatedViewIds.Add(item);
return item;
}
private static int AllocateViewID(int ownerId)
{
if (ownerId != 0)
{
int lastUsedViewSubId = PhotonNetwork.lastUsedViewSubId;
int num2 = ownerId * MAX_VIEW_IDS;
for (int j = 1; j < MAX_VIEW_IDS; j++)
{
lastUsedViewSubId = (lastUsedViewSubId + 1) % MAX_VIEW_IDS;
if (lastUsedViewSubId != 0)
{
int key = lastUsedViewSubId + num2;
if (!networkingPeer.photonViewList.ContainsKey(key) && !manuallyAllocatedViewIds.Contains(key))
{
PhotonNetwork.lastUsedViewSubId = lastUsedViewSubId;
return key;
}
}
}
throw new Exception(
String.Format("AllocateViewID() failed. User {0} is out of subIds, as all viewIDs are used.", ownerId));
}
int lastUsedViewSubIdStatic = PhotonNetwork.lastUsedViewSubIdStatic;
int num6 = ownerId * MAX_VIEW_IDS;
for (int i = 1; i < MAX_VIEW_IDS; i++)
{
lastUsedViewSubIdStatic = (lastUsedViewSubIdStatic + 1) % MAX_VIEW_IDS;
if (lastUsedViewSubIdStatic != 0)
{
int num8 = lastUsedViewSubIdStatic + num6;
if (!networkingPeer.photonViewList.ContainsKey(num8))
{
PhotonNetwork.lastUsedViewSubIdStatic = lastUsedViewSubIdStatic;
return num8;
}
}
}
throw new Exception(
String.Format("AllocateViewID() failed. Room (user {0}) is out of subIds, as all room viewIDs are used.",
ownerId));
}
public static bool CloseConnection(PhotonPlayer kickPlayer)
{
if (!VerifyCanUseNetwork())
{
return false;
}
if (!player.isMasterClient)
{
Debug.LogError("CloseConnection: Only the masterclient can kick another player.");
return false;
}
if (kickPlayer == null)
{
Debug.LogError("CloseConnection: No such player connected!");
return false;
}
RaiseEventOptions raiseEventOptions = new RaiseEventOptions
{
TargetActors = new int[] { kickPlayer.ID }
};
return networkingPeer.OpRaiseEvent(0xcb, null, true, raiseEventOptions);
}
public static bool ConnectToBestCloudServer(string gameVersion)
{
if (PhotonServerSettings == null)
{
Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: PhotonServerSettings");
return false;
}
if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
{
return ConnectUsingSettings(gameVersion);
}
networkingPeer.IsInitialConnect = true;
networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);
CloudRegionCode bestRegionCodeInPreferences = PhotonHandler.BestRegionCodeInPreferences;
if (bestRegionCodeInPreferences != CloudRegionCode.none)
{
Debug.Log("Best region found in PlayerPrefs. Connecting to: " + bestRegionCodeInPreferences);
return networkingPeer.ConnectToRegionMaster(bestRegionCodeInPreferences);
}
return networkingPeer.ConnectToNameServer();
}
public static bool ConnectToMaster(string masterServerAddress, int port, string appID, string gameVersion)
{
if (networkingPeer.PeerState != PeerStateValue.Disconnected)
{
Debug.LogWarning("ConnectToMaster() failed. Can only connect while in state 'Disconnected'. Current state: " + networkingPeer.PeerState);
return false;
}
if (offlineMode)
{
offlineMode = false;
Debug.LogWarning("ConnectToMaster() disabled the offline mode. No longer offline.");
}
if (!isMessageQueueRunning)
{
isMessageQueueRunning = true;
Debug.LogWarning("ConnectToMaster() enabled isMessageQueueRunning. Needs to be able to dispatch incoming messages.");
}
networkingPeer.SetApp(appID, gameVersion);
networkingPeer.IsUsingNameServer = false;
networkingPeer.IsInitialConnect = true;
networkingPeer.MasterServerAddress = masterServerAddress + ":" + port;
return networkingPeer.Connect(networkingPeer.MasterServerAddress, ServerConnection.MasterServer);
}
public static bool ConnectUsingSettings(string gameVersion)
{
if (PhotonServerSettings == null)
{
Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: PhotonServerSettings");
return false;
}
SwitchToProtocol(PhotonServerSettings.Protocol);
networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);
if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
{
offlineMode = true;
return true;
}
if (offlineMode)
{
Debug.LogWarning("ConnectUsingSettings() disabled the offline mode. No longer offline.");
}
offlineMode = false;
isMessageQueueRunning = true;
networkingPeer.IsInitialConnect = true;
if (PhotonServerSettings.HostType == ServerSettings.HostingOption.SelfHosted)
{
networkingPeer.IsUsingNameServer = false;
networkingPeer.MasterServerAddress = PhotonServerSettings.ServerAddress + ":" + PhotonServerSettings.ServerPort;
return networkingPeer.Connect(networkingPeer.MasterServerAddress, ServerConnection.MasterServer);
}
if (PhotonServerSettings.HostType == ServerSettings.HostingOption.BestRegion)
{
return ConnectToBestCloudServer(gameVersion);
}
return networkingPeer.ConnectToRegionMaster(PhotonServerSettings.PreferredRegion);
}
public static bool CreateRoom(string roomName)
{
return CreateRoom(roomName, null, null);
}
public static bool CreateRoom(string roomName, RoomOptions roomOptions, TypedLobby typedLobby)
{
if (offlineMode)
{
if (offlineModeRoom != null)
{
Debug.LogError("CreateRoom failed. In offline mode you still have to leave a room to enter another.");
return false;
}
offlineModeRoom = new Room(roomName, roomOptions);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom, new object[0]);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom, new object[0]);
return true;
}
if ((networkingPeer.server == ServerConnection.MasterServer) && connectedAndReady)
{
return networkingPeer.OpCreateGame(roomName, roomOptions, typedLobby);
}
Debug.LogError("CreateRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.");
return false;
}
public static bool CreateRoom(string roomName, bool isVisible, bool isOpen, int maxPlayers)
{
RoomOptions roomOptions = new RoomOptions
{
isVisible = isVisible,
isOpen = isOpen,
maxPlayers = maxPlayers
};
return CreateRoom(roomName, roomOptions, null);
}
[Obsolete("Use overload with RoomOptions and TypedLobby parameters.")]
public static bool CreateRoom(string roomName, bool isVisible, bool isOpen, int maxPlayers, Hashtable customRoomProperties, string[] propsToListInLobby)
{
RoomOptions roomOptions = new RoomOptions
{
isVisible = isVisible,
isOpen = isOpen,
maxPlayers = maxPlayers,
customRoomProperties = customRoomProperties,
customRoomPropertiesForLobby = propsToListInLobby
};
return CreateRoom(roomName, roomOptions, null);
}
public static void Destroy(PhotonView targetView)
{
if (targetView != null)
{
networkingPeer.RemoveInstantiatedGO(targetView.gameObject, !inRoom);
}
else
{
Debug.LogError("Destroy(targetPhotonView) failed, cause targetPhotonView is null.");
}
}
public static void Destroy(GameObject targetGo)
{
networkingPeer.RemoveInstantiatedGO(targetGo, !inRoom);
}
public static void DestroyAll()
{
if (isMasterClient)
{
networkingPeer.DestroyAll(false);
}
else
{
Debug.LogError("Couldn't call DestroyAll() as only the master client is allowed to call this.");
}
}
public static void DestroyPlayerObjects(PhotonPlayer targetPlayer, bool local = false)
{
if (targetPlayer == null)
{
Debug.LogError("DestroyPlayerObjects() failed, cause parameter 'targetPlayer' was null.");
}
if ((targetPlayer != player) && (targetPlayer.ID != player.ID))
{
DestroyPlayerObjects(targetPlayer.ID, local);
}
}
public static void DestroyPlayerObjects(int targetPlayerId, bool local = false)
{
if (VerifyCanUseNetwork() && (targetPlayerId != player.ID))
{
if (player.isMasterClient || (targetPlayerId == player.ID))
{
if (local)
{
networkingPeer.SendDestroyOfAllTo(targetPlayerId);
}
networkingPeer.DestroyPlayerObjects(targetPlayerId, false);
}
else
{
Debug.LogError("DestroyPlayerObjects() failed, cause players can only destroy their own GameObjects. A Master Client can destroy anyone's. This is master: " + isMasterClient);
}
}
}
public static void Disconnect()
{
if (offlineMode)
{
offlineMode = false;
offlineModeRoom = null;
networkingPeer.states = PeerStates.Disconnecting;
networkingPeer.OnStatusChanged(StatusCode.Disconnect);
}
else if (networkingPeer != null)
{
networkingPeer.Disconnect();
}
}
public static void FetchServerTimestamp()
{
networkingPeer.FetchServerTimestamp();
}
public static bool FindFriends(string[] friendsToFind)
{
return (((networkingPeer != null) && !isOfflineMode) && networkingPeer.OpFindFriends(friendsToFind));
}
public static int GetPing()
{
return networkingPeer.RoundTripTime;
}
public static RoomInfo[] GetRoomList()
{
if (!offlineMode && (networkingPeer != null))
{
return networkingPeer.mGameListCopy;
}
return new RoomInfo[0];
}
[Obsolete("Used for compatibility with Unity networking only. Encryption is automatically initialized while connecting.")]
public static void InitializeSecurity()
{
}
public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group)
{
return Instantiate(prefabName, position, rotation, @group, null);
}
public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
{
GameObject obj2;
if (!connected || (InstantiateInRoomOnly && !inRoom))
{
UnityEngine.Debug.LogError(string.Concat(new object[] { "Failed to Instantiate prefab: ", prefabName, ". Client should be in a room. Current connectionStateDetailed: ", connectionStatesDetailed }));
return null;
}
if (prefabName == "TITAN_VER3.1")
{
obj2 = Cach.TITAN_VER3_1 != null ? Cach.TITAN_VER3_1 : Cach.TITAN_VER3_1 = (GameObject)Resources.Load("TITAN_VER3.1");
}
else if (prefabName == "hook")
{
obj2 = Cach.hook != null ? Cach.hook : Cach.hook = (GameObject)Resources.Load("hook");
}
else if (prefabName == "AOTTG_HERO 1")
{
obj2 = Cach.AOTTG_HERO_1 != null ? Cach.AOTTG_HERO_1 : Cach.AOTTG_HERO_1 = (GameObject)Resources.Load("AOTTG_HERO 1");
}
else if (prefabName.StartsWith("RCAsset/"))
{
obj2 = CyanMod.CacheGameResources.RCLoad<GameObject>(prefabName.Substring(8));
}
else
{
obj2 = CyanMod.CacheGameResources.Load<GameObject>(prefabName);
}
if (obj2 == null)
{
UnityEngine.Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
return null;
}
if (obj2.GetComponent<PhotonView>() == null)
{
UnityEngine.Debug.LogError("Failed to Instantiate prefab:" + prefabName + ". Prefab must have a PhotonView component.");
return null;
}
if (isMasterClient && InRoomChat.IsPausedFlayer && (prefabName == "FX/flareBullet2") && Time.timeScale > 0.9f)
{
FengGameManagerMKII.instance.FlayerPauseRecompil();
}
int[] viewIDs = new int[obj2.GetComponentsInChildren<PhotonView>(true).Length];
for (int i = 0; i < viewIDs.Length; i++)
{
viewIDs[i] = AllocateViewID(player.ID);
}
return networkingPeer.DoInstantiate(networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, false), networkingPeer.mLocalActor, obj2);
}
public static GameObject InstantiateSceneObject(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
{
if (!connected || (InstantiateInRoomOnly && !inRoom))
{
UnityEngine.Debug.LogError(string.Concat(new object[] { "Failed to Instantiate prefab: ", prefabName, ". Client should be in a room. Current connectionStateDetailed: ", connectionStatesDetailed }));
return null;
}
if (!isMasterClient)
{
UnityEngine.Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Client is not the MasterClient in this room.");
return null;
}
GameObject resourceGameObject = CyanMod.CacheGameResources.Load<GameObject>(prefabName);
if (resourceGameObject == null)
{
UnityEngine.Debug.LogError("Failed to InstantiateSceneObject prefab: " + prefabName + ". Verify the Prefab is in a Resources folder (and not in a subfolder)");
return null;
}
if (resourceGameObject.GetComponent<PhotonView>() == null)
{
UnityEngine.Debug.LogError("Failed to InstantiateSceneObject prefab:" + prefabName + ". Prefab must have a PhotonView component.");
return null;
}
int[] viewIDs = AllocateSceneViewIDs(resourceGameObject.GetComponentsInChildren<PhotonView>(true).Length);
if (viewIDs == null)
{
UnityEngine.Debug.LogError(string.Concat(new object[] { "Failed to InstantiateSceneObject prefab: ", prefabName, ". No ViewIDs are free to use. Max is: ", (short)0x3e8 }));
return null;
}
return networkingPeer.DoInstantiate(networkingPeer.SendInstantiate(prefabName, position, rotation, group, viewIDs, data, true), networkingPeer.mLocalActor, resourceGameObject);
}
public static void InternalCleanPhotonMonoFromSceneIfStuck()
{
PhotonHandler[] handlerArray = UnityEngine.Object.FindObjectsOfType(typeof(PhotonHandler)) as PhotonHandler[];
if ((handlerArray != null) && (handlerArray.Length > 0))
{
Debug.Log("Cleaning up hidden PhotonHandler instances in scene. Please save it. This is not an issue.");
foreach (PhotonHandler handler in handlerArray)
{
handler.gameObject.hideFlags = HideFlags.None;
if ((handler.gameObject != null) && (handler.gameObject.name == "PhotonMono"))
{
UnityEngine.Object.DestroyImmediate(handler.gameObject);
}
UnityEngine.Object.DestroyImmediate(handler);
}
}
}
public static bool JoinLobby()
{
return JoinLobby(null);
}
public static bool JoinLobby(TypedLobby typedLobby)
{
bool flag;
if (!connected || (Server != ServerConnection.MasterServer))
{
return false;
}
if (typedLobby == null)
{
typedLobby = TypedLobby.Default;
}
if (flag = networkingPeer.OpJoinLobby(typedLobby))
{
networkingPeer.lobby = typedLobby;
}
return flag;
}
public static bool JoinOrCreateRoom(string roomName, RoomOptions roomOptions, TypedLobby typedLobby)
{
if (offlineMode)
{
if (offlineModeRoom != null)
{
Debug.LogError("JoinOrCreateRoom failed. In offline mode you still have to leave a room to enter another.");
return false;
}
offlineModeRoom = new Room(roomName, roomOptions);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom, new object[0]);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom, new object[0]);
return true;
}
if ((networkingPeer.server == ServerConnection.MasterServer) && connectedAndReady)
{
if (string.IsNullOrEmpty(roomName))
{
Debug.LogError("JoinOrCreateRoom failed. A roomname is required. If you don't know one, how will you join?");
return false;
}
return networkingPeer.OpJoinRoom(roomName, roomOptions, typedLobby, true);
}
Debug.LogError("JoinOrCreateRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.");
return false;
}
public static bool JoinRandomRoom()
{
return JoinRandomRoom(null, 0, MatchmakingMode.FillRoom, null, null);
}
public static bool JoinRandomRoom(Hashtable expectedCustomRoomProperties, byte expectedMaxPlayers)
{
return JoinRandomRoom(expectedCustomRoomProperties, expectedMaxPlayers, MatchmakingMode.FillRoom, null, null);
}
public static bool JoinRandomRoom(Hashtable expectedCustomRoomProperties, byte expectedMaxPlayers, MatchmakingMode matchingType, TypedLobby typedLobby, string sqlLobbyFilter)
{
if (offlineMode)
{
if (offlineModeRoom != null)
{
Debug.LogError("JoinRandomRoom failed. In offline mode you still have to leave a room to enter another.");
return false;
}
offlineModeRoom = new Room("offline room", null);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom, new object[0]);
return true;
}
if ((networkingPeer.server == ServerConnection.MasterServer) && connectedAndReady)
{
Hashtable target = new Hashtable();
target.MergeStringKeys(expectedCustomRoomProperties);
if (expectedMaxPlayers > 0)
{
target[(byte)0xff] = expectedMaxPlayers;
}
return networkingPeer.OpJoinRandomRoom(target, 0, null, matchingType, typedLobby, sqlLobbyFilter);
}
Debug.LogError("JoinRandomRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.");
return false;
}
public static bool JoinRoom(string roomName)
{
if (offlineMode)
{
if (offlineModeRoom != null)
{
Debug.LogError("JoinRoom failed. In offline mode you still have to leave a room to enter another.");
return false;
}
offlineModeRoom = new Room(roomName, null);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom, new object[0]);
return true;
}
if ((networkingPeer.server == ServerConnection.MasterServer) && connectedAndReady)
{
if (string.IsNullOrEmpty(roomName))
{
Debug.LogError("JoinRoom failed. A roomname is required. If you don't know one, how will you join?");
return false;
}
return networkingPeer.OpJoinRoom(roomName, null, null, false);
}
Debug.LogError("JoinRoom failed. Client is not on Master Server or not yet ready to call operations. Wait for callback: OnJoinedLobby or OnConnectedToMaster.");
return false;
}
[Obsolete("Use overload with roomOptions and TypedLobby parameter.")]
public static bool JoinRoom(string roomName, bool createIfNotExists)
{
if (((connectionStatesDetailed != PeerStates.Joining) && (connectionStatesDetailed != PeerStates.Joined)) && (connectionStatesDetailed != PeerStates.ConnectedToGameserver))
{
if (room == null)
{
if (roomName != string.Empty)
{
if (offlineMode)
{
offlineModeRoom = new Room(roomName, null);
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom, new object[0]);
return true;
}
return networkingPeer.OpJoinRoom(roomName, null, null, createIfNotExists);
}
Debug.LogError("JoinRoom aborted: You must specifiy a room name!");
}
else
{
Debug.LogError("JoinRoom aborted: You are already in a room!");
}
}
else
{
Debug.LogError("JoinRoom aborted: You can only join a room while not currently connected/connecting to a room.");
}
return false;
}
public static bool LeaveLobby()
{
return ((connected && (Server == ServerConnection.MasterServer)) && networkingPeer.OpLeaveLobby());
}
public static bool LeaveRoom()
{
if (offlineMode)
{
offlineModeRoom = null;
NetworkingPeer.SendMonoMessage(PhotonNetworkingMessage.OnLeftRoom, new object[0]);
return true;
}
if (room == null)
{
Debug.LogWarning("PhotonNetwork.room is null. You don't have to call LeaveRoom() when you're not in one. State: " + connectionStatesDetailed);
}
return networkingPeer.OpLeave();
}
public static void LoadLevel(int levelNumber)
{
networkingPeer.SetLevelInPropsIfSynced(levelNumber);
isMessageQueueRunning = false;
networkingPeer.loadingLevelAndPausedNetwork = true;
Application.LoadLevel(levelNumber);
}
public static void LoadLevel(string levelName)
{
networkingPeer.SetLevelInPropsIfSynced(levelName);
isMessageQueueRunning = false;
networkingPeer.loadingLevelAndPausedNetwork = true;
Application.LoadLevel(levelName);
}
public static void NetworkStatisticsReset()
{
networkingPeer.TrafficStatsReset();
}
public static string NetworkStatisticsToString()
{
if ((networkingPeer != null) && !offlineMode)
{
return networkingPeer.VitalStatsToString(false);
}
return "Offline or in OfflineMode. No VitalStats available.";
}
public static void OverrideBestCloudServer(CloudRegionCode region)
{
PhotonHandler.BestRegionCodeInPreferences = region;
}
public static bool RaiseEvent(byte eventCode, object eventContent, bool sendReliable, RaiseEventOptions options)
{
if (inRoom && (eventCode < 0xff))
{
return networkingPeer.OpRaiseEvent(eventCode, eventContent, sendReliable, options);
}
Debug.LogWarning("RaiseEvent() failed. Your event is not being sent! Check if your are in a Room and the eventCode must be less than 200 (0..199).");
return false;
}
public static void RefreshCloudServerRating()
{
throw new NotImplementedException("not available at the moment");
}
public static void RemoveRPCs(PhotonPlayer targetPlayer)
{
if (VerifyCanUseNetwork())
{
if (!targetPlayer.isLocal && !isMasterClient)
{
Debug.LogError("Error; Only the MasterClient can call RemoveRPCs for other players.");
}
else
{
networkingPeer.OpCleanRpcBuffer(targetPlayer.ID);
}
}
}
public static void RemoveRPCs(PhotonView targetPhotonView)
{
if (VerifyCanUseNetwork())
{
networkingPeer.CleanRpcBufferIfMine(targetPhotonView);
}
}
public static void RemoveRPCsInGroup(int targetGroup)
{
if (VerifyCanUseNetwork())
{
networkingPeer.RemoveRPCsInGroup(targetGroup);
}
}
internal static void RPC(PhotonView view, string methodName, PhotonPlayer targetPlayer, params object[] parameters)
{
if (VerifyCanUseNetwork())
{
if (room == null)
{
Debug.LogWarning("Cannot send RPCs in Lobby, only processed locally");
}
else
{
if (player == null)
{
Debug.LogError("Error; Sending RPC to player null! Aborted \"" + methodName + "\"");
}
if (networkingPeer != null)
{
networkingPeer.RPC(view, methodName, targetPlayer, parameters);
}
else
{
Debug.LogWarning("Could not execute RPC " + methodName + ". Possible scene loading in progress?");
}
}
}
}
internal static void RPC(PhotonView view, string methodName, PhotonTargets target, params object[] parameters)
{
if (VerifyCanUseNetwork())
{
if (room == null)
{
Debug.LogWarning("Cannot send RPCs in Lobby! RPC dropped.");
}
else if (networkingPeer != null)
{
networkingPeer.RPC(view, methodName, target, parameters);
}
else
{
Debug.LogWarning("Could not execute RPC " + methodName + ". Possible scene loading in progress?");
}
}
}
public static void SendOutgoingCommands()
{
if (VerifyCanUseNetwork())
{
while (networkingPeer.SendOutgoingCommands())
{
}
}
}
public static void SetLevelPrefix(short prefix)
{
if (VerifyCanUseNetwork())
{
networkingPeer.SetLevelPrefix(prefix);
}
}
public static bool SetMasterClient(PhotonPlayer masterClientPlayer)
{
return networkingPeer.SetMasterClient(masterClientPlayer.ID, true);
}
public static void SetPlayerCustomProperties(Hashtable customProperties)
{
if (customProperties == null)
{
customProperties = new Hashtable();
foreach (object obj2 in player.customProperties.Keys)
{
customProperties[(string)obj2] = null;
}
}
if ((room != null) && room.isLocalClientInside)
{
player.SetCustomProperties(customProperties);
}
else
{
player.InternalCacheProperties(customProperties);
}
}
public static void SetReceivingEnabled(int group, bool enabled)
{
if (VerifyCanUseNetwork())
{
networkingPeer.SetReceivingEnabled(group, enabled);
}
}
public static void SetReceivingEnabled(int[] enableGroups, int[] disableGroups)
{
if (VerifyCanUseNetwork())
{
networkingPeer.SetReceivingEnabled(enableGroups, disableGroups);
}
}
public static void SetSendingEnabled(int group, bool enabled)
{
if (VerifyCanUseNetwork())
{
networkingPeer.SetSendingEnabled(group, enabled);
}
}
public static void SetSendingEnabled(int[] enableGroups, int[] disableGroups)
{
if (VerifyCanUseNetwork())
{
networkingPeer.SetSendingEnabled(enableGroups, disableGroups);
}
}
public static void SwitchToProtocol(ConnectionProtocol cp)
{
if (networkingPeer.UsedProtocol != cp)
{
try
{
networkingPeer.Disconnect();
networkingPeer.StopThread();
}
catch
{
}
networkingPeer = new NetworkingPeer(photonMono, string.Empty, cp);
Debug.Log("Protocol switched to: " + cp);
}
}
public static void UnAllocateViewID(int viewID)
{
manuallyAllocatedViewIds.Remove(viewID);
if (networkingPeer.photonViewList.ContainsKey(viewID))
{
Debug.LogWarning(
String.Format("Unallocated manually used viewID: {0} but found it used still in a PhotonView: {1}",
viewID, networkingPeer.photonViewList[viewID]));
}
}
private static bool VerifyCanUseNetwork()
{
if (connected)
{
return true;
}
Debug.LogError("Cannot send messages when not connected. Either connect to Photon OR use offline mode!");
return false;
}
public static bool WebRpc(string name, object parameters)
{
return networkingPeer.WebRpc(name, parameters);
}
public static AuthenticationValues AuthValues
{
get
{
if (networkingPeer != null)
{
return networkingPeer.CustomAuthenticationValues;
}
return null;
}
set
{
if (networkingPeer != null)
{
networkingPeer.CustomAuthenticationValues = value;
}
}
}
public static bool autoCleanUpPlayerObjects
{
get { return m_autoCleanUpPlayerObjects; }
set
{
if (room != null)
{
Debug.LogError("Setting autoCleanUpPlayerObjects while in a room is not supported.");
}
else
{
m_autoCleanUpPlayerObjects = value;
}
}
}
public static bool autoJoinLobby
{
get { return autoJoinLobbyField; }
set
{
autoJoinLobbyField = value;
}
}
public static bool automaticallySyncScene
{
get { return _mAutomaticallySyncScene; }
set
{
_mAutomaticallySyncScene = value;
if (_mAutomaticallySyncScene && (room != null))
{
networkingPeer.LoadLevelIfSynced();
}
}
}
public static bool connected
{
get
{
if (offlineMode)
{
return true;
}
if (networkingPeer == null)
{
return false;
}
return (((!networkingPeer.IsInitialConnect && (networkingPeer.states != PeerStates.PeerCreated)) && ((networkingPeer.states != PeerStates.Disconnected) && (networkingPeer.states != PeerStates.Disconnecting))) && (networkingPeer.states != PeerStates.ConnectingToNameServer));
}
}
public static bool connectedAndReady
{
get
{
if (!connected)
{
return false;
}
if (!offlineMode)
{
switch (connectionStatesDetailed)
{
case PeerStates.ConnectingToGameserver:
case PeerStates.Joining:
case PeerStates.Leaving:
case PeerStates.ConnectingToMasterserver:
case PeerStates.Disconnecting:
case PeerStates.Disconnected:
case PeerStates.ConnectingToNameServer:
case PeerStates.Authenticating:
case PeerStates.PeerCreated:
return false;
}
}
return true;
}
}
public static bool connecting
{
get { return (networkingPeer.IsInitialConnect && !offlineMode); }
}
public static ConnectionState connectionState
{
get
{
if (offlineMode)
{
return ConnectionState.Connected;
}
if (networkingPeer == null)
{
return ConnectionState.Disconnected;
}
PeerStateValue peerState = networkingPeer.PeerState;
switch (peerState)
{
case PeerStateValue.Disconnected:
return ConnectionState.Disconnected;