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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
|
#include "stdafx.h"
#include "..\..\..\Minecraft.World\Socket.h"
#include "..\..\..\Minecraft.World\StringHelpers.h"
#include "PlatformNetworkManagerSony.h"
#include "NetworkPlayerSony.h"
#include "..\..\Common\Network\GameNetworkManager.h"
CPlatformNetworkManagerSony *g_pPlatformNetworkManager;
bool CPlatformNetworkManagerSony::IsLocalGame()
{
return m_bIsOfflineGame;
}
bool CPlatformNetworkManagerSony::IsPrivateGame()
{
return m_bIsPrivateGame;
}
bool CPlatformNetworkManagerSony::IsLeavingGame()
{
return m_bLeavingGame;
}
void CPlatformNetworkManagerSony::ResetLeavingGame()
{
m_bLeavingGame = false;
}
void CPlatformNetworkManagerSony::HandleStateChange(SQRNetworkManager::eSQRNetworkManagerState oldState, SQRNetworkManager::eSQRNetworkManagerState newState, bool idleReasonIsSessionFull)
{
static const char * c_apszStateNames[] =
{
"SNM_STATE_INITIALISING",
"SNM_STATE_INITIALISE_FAILED",
"SNM_STATE_IDLE",
"SNM_STATE_HOSTING",
"SNM_STATE_JOINING",
"SNM_STATE_STARTING",
"SNM_STATE_PLAYING",
"SNM_STATE_LEAVING",
"SNM_STATE_ENDING",
};
app.DebugPrintf( "Network State: %s ==> %s\n",
c_apszStateNames[ oldState ],
c_apszStateNames[ newState ] );
if( newState == SQRNetworkManager::SNM_STATE_HOSTING )
{
m_bLeavingGame = false;
m_bLeaveGameOnTick = false;
m_bHostChanged = false;
g_NetworkManager.StateChange_AnyToHosting();
}
else if( newState == SQRNetworkManager::SNM_STATE_JOINING )
{
// 4J Stu - We may be accepting an invite from the DLC menu, so hide the icon
#ifdef __ORBIS__
sceNpCommerceHidePsStoreIcon();
#elif defined __PSVITA__
sceNpCommerce2HidePsStoreIcon();
#endif
m_bLeavingGame = false;
m_bLeaveGameOnTick = false;
m_bHostChanged = false;
g_NetworkManager.StateChange_AnyToJoining();
}
else if( newState == SQRNetworkManager::SNM_STATE_IDLE && oldState == SQRNetworkManager::SNM_STATE_JOINING )
{
if( idleReasonIsSessionFull )
{
g_NetworkManager.StateChange_JoiningToIdle(JOIN_FAILED_SERVER_FULL);
}
else
{
g_NetworkManager.StateChange_JoiningToIdle(JOIN_FAILED_NONSPECIFIC);
}
}
else if( newState == SQRNetworkManager::SNM_STATE_IDLE && oldState == SQRNetworkManager::SNM_STATE_HOSTING )
{
m_bLeavingGame = true;
}
else if( newState == SQRNetworkManager::SNM_STATE_STARTING )
{
m_lastPlayerEventTimeStart = app.getAppTime();
g_NetworkManager.StateChange_AnyToStarting();
}
// Fix for #93148 - TCR 001: BAS Game Stability: Title will crash for the multiplayer client if host of the game will exit during the clients loading to created world.
// 4J Stu - If the client joins just as the host is exiting, then they can skip to leaving without passing through ending
else if( newState == SQRNetworkManager::SNM_STATE_ENDING )
{
g_NetworkManager.StateChange_AnyToEnding( oldState == SQRNetworkManager::SNM_STATE_PLAYING );
// 4J-PB - Only the host can leave here - the clients will hang if m_bLeavingGame is set to true here
if( m_pSQRNet->IsHost() )
{
m_bLeavingGame = true;
}
}
if( newState == SQRNetworkManager::SNM_STATE_IDLE )
{
// On PS3, sometimes we're getting a SNM_STATE_ENDING transition to SNM_STATE_IDLE on joining, because the server context being deleted sets the state away from SNM_STATE_JOINING before we detect
// the cause for the disconnection. This means we don't pick up on the joining->idle transition. Set disconnection reason here too for this case.
if( idleReasonIsSessionFull )
{
app.SetDisconnectReason( DisconnectPacket::eDisconnect_ServerFull );
}
g_NetworkManager.StateChange_AnyToIdle();
}
}
void CPlatformNetworkManagerSony::HandleDataReceived(SQRNetworkPlayer *playerFrom, SQRNetworkPlayer *playerTo, unsigned char *data, unsigned int dataSize)
{
if(m_pSQRNet->GetState() == SQRNetworkManager::SNM_STATE_ENDING)
{
return;
}
if( playerTo->IsHost() )
{
// If we are the host we care who this came from
//app.DebugPrintf( "Pushing data into host read queue for user \"%ls\"\n", pPlayerFrom->GetGamertag());
// Push this data into the read queue for the player that sent it
INetworkPlayer *pPlayerFrom = getNetworkPlayer(playerFrom);
Socket *socket = pPlayerFrom->GetSocket();
if(socket != NULL)
socket->pushDataToQueue(data, dataSize, false);
}
else
{
// If we are not the host the message must have come from the host, so we care more about who it is addressed to
INetworkPlayer *pPlayerTo = getNetworkPlayer(playerTo);
Socket *socket = pPlayerTo->GetSocket();
//app.DebugPrintf( "Pushing data into read queue for user \"%ls\"\n", apPlayersTo[dwPlayer]->GetGamertag());
if(socket != NULL)
socket->pushDataToQueue(data, dataSize);
}
}
void CPlatformNetworkManagerSony::HandlePlayerJoined(SQRNetworkPlayer * pSQRPlayer)
{
const char * pszDescription;
// 4J Stu - We create a fake socket for every where that we need an INBOUND queue of game data. Outbound
// is all handled by QNet so we don't need that. Therefore each client player has one, and the host has one
// for each client player.
bool createFakeSocket = false;
bool localPlayer = false;
NetworkPlayerSony *networkPlayer = (NetworkPlayerSony *)addNetworkPlayer(pSQRPlayer);
if( pSQRPlayer->IsLocal() )
{
localPlayer = true;
if( pSQRPlayer->IsHost() )
{
pszDescription = "local host";
// 4J Stu - No socket for the localhost as it uses a special loopback queue
m_machineSQRPrimaryPlayers.push_back( pSQRPlayer );
}
else
{
pszDescription = "local";
// We need an inbound queue on all local players to receive data from the host
createFakeSocket = true;
}
}
else
{
if( pSQRPlayer->IsHost() )
{
pszDescription = "remote host";
}
else
{
pszDescription = "remote";
// If we are the host, then create a fake socket for every remote player
if( m_pSQRNet->IsHost() )
{
createFakeSocket = true;
}
}
if( m_pSQRNet->IsHost() && !m_bHostChanged )
{
// Do we already have a primary player for this system?
bool systemHasPrimaryPlayer = false;
for(AUTO_VAR(it, m_machineSQRPrimaryPlayers.begin()); it < m_machineSQRPrimaryPlayers.end(); ++it)
{
SQRNetworkPlayer *pQNetPrimaryPlayer = *it;
if( pSQRPlayer->IsSameSystem(pQNetPrimaryPlayer) )
{
systemHasPrimaryPlayer = true;
break;
}
}
if( !systemHasPrimaryPlayer )
m_machineSQRPrimaryPlayers.push_back( pSQRPlayer );
}
}
g_NetworkManager.PlayerJoining( networkPlayer );
if( createFakeSocket == true && !m_bHostChanged )
{
g_NetworkManager.CreateSocket( networkPlayer, localPlayer );
}
#if 0
app.DebugPrintf( "Player 0x%p \"%ls\" joined; %s; voice %i; camera %i.\n",
pSQRPlayer,
pSQRPlayer->GetGamertag(),
pszDescription,
(int) pSQRPlayer->HasVoice(),
(int) pSQRPlayer->HasCamera() );
#endif
if( m_pSQRNet->IsHost() )
{
// 4J-PB - only the host should do this
g_NetworkManager.UpdateAndSetGameSessionData();
SystemFlagAddPlayer( networkPlayer );
}
for( int idx = 0; idx < XUSER_MAX_COUNT; ++idx)
{
if(playerChangedCallback[idx] != NULL)
playerChangedCallback[idx]( playerChangedCallbackParam[idx], networkPlayer, false );
}
if(true) // TODO m_pSQRNet->GetState() == QNET_STATE_GAME_PLAY)
{
int localPlayerCount = 0;
for(unsigned int idx = 0; idx < XUSER_MAX_COUNT; ++idx)
{
if( m_pSQRNet->GetLocalPlayerByUserIndex(idx) != NULL ) ++localPlayerCount;
}
float appTime = app.getAppTime();
// Only record stats for the primary player here
m_lastPlayerEventTimeStart = appTime;
}
}
void CPlatformNetworkManagerSony::HandlePlayerLeaving(SQRNetworkPlayer *pSQRPlayer)
{
//__debugbreak();
app.DebugPrintf( "Player 0x%p leaving.\n",
pSQRPlayer );
INetworkPlayer *networkPlayer = getNetworkPlayer(pSQRPlayer);
if( networkPlayer )
{
// Get our wrapper object associated with this player.
Socket *socket = networkPlayer->GetSocket();
if( socket != NULL )
{
// If we are in game then remove this player from the game as well.
// We may get here either from the player requesting to exit the game,
// in which case we they will already have left the game server, or from a disconnection
// where we then have to remove them from the game server
if( m_pSQRNet->IsHost() && !m_bHostChanged )
{
g_NetworkManager.CloseConnection(networkPlayer);
}
// Free the wrapper object memory.
// TODO 4J Stu - We may still be using this at the point that the player leaves the session.
// We need this as long as the game server still needs to communicate with the player
//delete socket;
networkPlayer->SetSocket( NULL );
}
if( m_pSQRNet->IsHost() && !m_bHostChanged )
{
if( isSystemPrimaryPlayer(pSQRPlayer) )
{
SQRNetworkPlayer *pNewSQRPrimaryPlayer = NULL;
for(unsigned int i = 0; i < m_pSQRNet->GetPlayerCount(); ++i )
{
SQRNetworkPlayer *pSQRPlayer2 = m_pSQRNet->GetPlayerByIndex( i );
if ( pSQRPlayer2 != NULL && pSQRPlayer2 != pSQRPlayer && pSQRPlayer2->IsSameSystem( pSQRPlayer ) )
{
pNewSQRPrimaryPlayer = pSQRPlayer2;
break;
}
}
AUTO_VAR(it, find( m_machineSQRPrimaryPlayers.begin(), m_machineSQRPrimaryPlayers.end(), pSQRPlayer));
if( it != m_machineSQRPrimaryPlayers.end() )
{
m_machineSQRPrimaryPlayers.erase( it );
}
if( pNewSQRPrimaryPlayer != NULL )
m_machineSQRPrimaryPlayers.push_back( pNewSQRPrimaryPlayer );
}
g_NetworkManager.UpdateAndSetGameSessionData( networkPlayer );
SystemFlagRemovePlayer( networkPlayer );
}
g_NetworkManager.PlayerLeaving( networkPlayer );
for( int idx = 0; idx < XUSER_MAX_COUNT; ++idx)
{
if(playerChangedCallback[idx] != NULL)
playerChangedCallback[idx]( playerChangedCallbackParam[idx], networkPlayer, true );
}
if(m_pSQRNet->GetState() == SQRNetworkManager::SNM_STATE_PLAYING)
{
int localPlayerCount = 0;
for(unsigned int idx = 0; idx < XUSER_MAX_COUNT; ++idx)
{
if( m_pSQRNet->GetLocalPlayerByUserIndex(idx) != NULL ) ++localPlayerCount;
}
float appTime = app.getAppTime();
m_lastPlayerEventTimeStart = appTime;
}
removeNetworkPlayer(pSQRPlayer);
}
}
// Update our external data to match the current internal player slots, and resync back out (host only)
void CPlatformNetworkManagerSony::HandleResyncPlayerRequest(SQRNetworkPlayer **aPlayers)
{
m_hostGameSessionData.playerCount = 0;
for(int i = 0; i < SQRNetworkManager::MAX_ONLINE_PLAYER_COUNT; i++ )
{
if( aPlayers[i] )
{
m_hostGameSessionData.players[i] = aPlayers[i]->GetUID();
m_hostGameSessionData.playerCount++;
}
else
{
memset(&m_hostGameSessionData.players[i],0,sizeof(m_hostGameSessionData.players[i]));
}
}
m_pSQRNet->UpdateExternalRoomData();
}
void CPlatformNetworkManagerSony::HandleAddLocalPlayerFailed(int idx)
{
g_NetworkManager.AddLocalPlayerFailed(idx);
}
void CPlatformNetworkManagerSony::HandleDisconnect(bool bLostRoomOnly,bool bPSNSignOut)
{
g_NetworkManager.HandleDisconnect(bLostRoomOnly,bPSNSignOut);
}
void CPlatformNetworkManagerSony::HandleInviteReceived( int userIndex, const SQRNetworkManager::PresenceSyncInfo *pInviteInfo)
{
g_NetworkManager.GameInviteReceived( userIndex, pInviteInfo );
}
extern SQRNetworkManager *testSQRNetworkManager;
bool CPlatformNetworkManagerSony::Initialise(CGameNetworkManager *pGameNetworkManager, int flagIndexSize)
{
// Create a sony network manager, and go online
#ifdef __ORBIS__
m_pSQRNet = new SQRNetworkManager_Orbis(this);
m_pSQRNet->Initialise();
#elif defined __PS3__
m_pSQRNet = new SQRNetworkManager_PS3(this);
m_pSQRNet->Initialise();
#else // __PSVITA__
// m_pSQRNet = new SQRNetworkManager_Vita(this);
m_bUsingAdhocMode = false;
m_pSQRNet_Vita_Adhoc = new SQRNetworkManager_AdHoc_Vita(this);
m_pSQRNet_Vita = new SQRNetworkManager_Vita(this);
m_pSQRNet = m_pSQRNet_Vita;
// 4J-PB - seems we can't initialise both adhoc and psn comms - from Rohan - "having adhoc matching and matching2 library initialised together results in undesired behaviour", but probably having other parts initialised also is 'undesirable'
m_pSQRNet_Vita->Initialise();
if(ProfileManager.IsSignedInPSN(ProfileManager.GetPrimaryPad()))
{
// we're signed into the PSN, but we won't be online yet, force a sign-in online here
m_pSQRNet_Vita->AttemptPSNSignIn(NULL, NULL);
}
#endif
m_pGameNetworkManager = pGameNetworkManager;
m_flagIndexSize = flagIndexSize;
g_pPlatformNetworkManager = this;
for( int i = 0; i < XUSER_MAX_COUNT; i++ )
{
playerChangedCallback[ i ] = NULL;
}
m_bLeavingGame = false;
m_bLeaveGameOnTick = false;
m_bHostChanged = false;
m_bLeaveRoomWhenLeavingGame = true;
m_bSearchPending = false;
m_bIsOfflineGame = false;
m_pSearchParam = NULL;
m_SessionsUpdatedCallback = NULL;
m_searchResultsCount = 0;
m_pSearchResults = NULL;
m_lastSearchStartTime = 0;
// Success!
return true;
}
void CPlatformNetworkManagerSony::Terminate()
{
m_pSQRNet->Terminate();
}
int CPlatformNetworkManagerSony::GetJoiningReadyPercentage()
{
return m_pSQRNet->GetJoiningReadyPercentage();
}
int CPlatformNetworkManagerSony::CorrectErrorIDS(int IDS)
{
// Attempts to remap the following messages to provide something that PS3 TCRs are happier with
//
// IDS_CONNECTION_LOST - "Connection lost"
// IDS_CONNECTION_FAILED - "Connection failed"
// IDS_CONNECTION_LOST_LIVE - "Connection to "PSN" was lost. Exiting to the main menu."
// IDS_CONNECTION_LOST_LIVE_NO_EXIT - "Connection to "PSN" was lost."
// IDS_CONNECTION_LOST_SERVER - "Connection to the server was lost. Exiting to the main menu."
//
// Map to:
//
// IDS_ERROR_NETWORK - "A network error has occurred"
// IDS_ERROR_NETWORK_TITLE - "Network Error"
// IDS_ERROR_NETWORK_EXIT - "A network error has occurred. Exiting to Main Menu."
// IDS_ERROR_PSN_SIGN_OUT - You have been signed out from the "PSN".
// IDS_ERROR_PSN_SIGN_OUT_EXIT - You have been signed out from the "PSN". Exiting to Main Menu
// Determine if we'd prefer to present errors as a signing out issue, rather than a network issue, based on whether we have a network connection at all or not
bool preferSignoutError = false;
int state;
#ifdef __PS3__
int ret = cellNetCtlGetState( &state );
int IPObtainedState = CELL_NET_CTL_STATE_IPObtained;
#elif defined __ORBIS__
int ret = sceNetCtlGetState( &state );
int IPObtainedState = SCE_NET_CTL_STATE_IPOBTAINED;
#elif defined __PSVITA__
int ret = sceNetCtlInetGetState( &state );
int IPObtainedState = SCE_NET_CTL_STATE_IPOBTAINED;
#endif
if( ret == 0 )
{
if( state == IPObtainedState )
{
preferSignoutError = true;
}
}
#ifdef __PSVITA__
// If we're in ad-hoc mode this problem definitely wasn't PSN related
if (usingAdhocMode()) preferSignoutError = false;
#endif
// If we're the host we haven't lost connection to the server
if (IDS == IDS_CONNECTION_LOST_SERVER && g_NetworkManager.IsHost())
{
IDS = IDS_CONNECTION_LOST_LIVE;
}
switch(IDS)
{
case IDS_CONNECTION_LOST:
case IDS_CONNECTION_FAILED:
return IDS_ERROR_NETWORK_TITLE;
case IDS_CONNECTION_LOST_LIVE:
if( preferSignoutError )
{
return IDS_ERROR_PSN_SIGN_OUT_EXIT;
}
else
{
return IDS_ERROR_NETWORK_EXIT;
}
case IDS_CONNECTION_LOST_LIVE_NO_EXIT:
if( preferSignoutError )
{
return IDS_ERROR_PSN_SIGN_OUT;
}
else
{
return IDS_ERROR_NETWORK_TITLE;
}
break;
#ifdef __PSVITA__
case IDS_CONNECTION_LOST_SERVER:
if(preferSignoutError)
{
if(ProfileManager.IsSignedInPSN(ProfileManager.GetPrimaryPad()) == false)
return IDS_ERROR_PSN_SIGN_OUT_EXIT;
}
#endif
default:
return IDS;
}
}
bool CPlatformNetworkManagerSony::isSystemPrimaryPlayer(SQRNetworkPlayer *pSQRPlayer)
{
bool playerIsSystemPrimary = false;
for(AUTO_VAR(it, m_machineSQRPrimaryPlayers.begin()); it < m_machineSQRPrimaryPlayers.end(); ++it)
{
SQRNetworkPlayer *pSQRPrimaryPlayer = *it;
if( pSQRPrimaryPlayer == pSQRPlayer )
{
playerIsSystemPrimary = true;
break;
}
}
return playerIsSystemPrimary;
}
// We call this twice a frame, either side of the render call so is a good place to "tick" things
void CPlatformNetworkManagerSony::DoWork()
{
#if 0
DWORD dwNotifyId;
ULONG_PTR ulpNotifyParam;
while( XNotifyGetNext(
m_notificationListener,
0, // Any notification
&dwNotifyId,
&ulpNotifyParam)
)
{
switch(dwNotifyId)
{
case XN_SYS_SIGNINCHANGED:
app.DebugPrintf("Signinchanged - %d\n", ulpNotifyParam);
break;
case XN_LIVE_INVITE_ACCEPTED:
// ignore these - we're catching them from the game listener, so we can get the one from the dashboard
break;
default:
m_pIQNet->Notify(dwNotifyId,ulpNotifyParam);
break;
}
}
TickSearch();
if( m_bLeaveGameOnTick )
{
m_pIQNet->LeaveGame(m_migrateHostOnLeave);
m_bLeaveGameOnTick = false;
}
m_pIQNet->DoWork();
#else
TickSearch();
if( m_bLeaveGameOnTick )
{
m_pSQRNet->LeaveRoom(m_bLeaveRoomWhenLeavingGame);
m_bLeaveGameOnTick = false;
}
m_pSQRNet->Tick();
#endif
}
int CPlatformNetworkManagerSony::GetPlayerCount()
{
return m_pSQRNet->GetPlayerCount();
}
bool CPlatformNetworkManagerSony::ShouldMessageForFullSession()
{
return false;
}
int CPlatformNetworkManagerSony::GetOnlinePlayerCount()
{
return m_pSQRNet->GetOnlinePlayerCount();
}
int CPlatformNetworkManagerSony::GetLocalPlayerMask(int playerIndex)
{
return 1 << playerIndex;
}
bool CPlatformNetworkManagerSony::AddLocalPlayerByUserIndex( int userIndex )
{
return m_pSQRNet->AddLocalPlayerByUserIndex(userIndex);
}
bool CPlatformNetworkManagerSony::RemoveLocalPlayerByUserIndex( int userIndex )
{
SQRNetworkPlayer *pSQRPlayer = m_pSQRNet->GetLocalPlayerByUserIndex(userIndex);
INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pSQRPlayer);
if(pNetworkPlayer != NULL)
{
Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL )
{
// We can't remove the player from qnet until we have stopped using it to communicate
C4JThread* thread = new C4JThread(&CPlatformNetworkManagerSony::RemovePlayerOnSocketClosedThreadProc, pNetworkPlayer, "RemovePlayerOnSocketClosed");
thread->SetProcessor( CPU_CORE_REMOVE_PLAYER );
thread->Run();
}
else
{
// Safe to remove the player straight away
return m_pSQRNet->RemoveLocalPlayerByUserIndex(userIndex);
}
}
return true;
}
bool CPlatformNetworkManagerSony::IsInStatsEnabledSession()
{
#if 0
DWORD dataSize = sizeof(QNET_LIVE_STATS_MODE);
QNET_LIVE_STATS_MODE statsMode;
m_pIQNet->GetOpt(QNET_OPTION_LIVE_STATS_MODE, &statsMode , &dataSize );
// Use QNET_LIVE_STATS_MODE_AUTO if there is another way to check if stats are enabled or not
bool statsEnabled = statsMode == QNET_LIVE_STATS_MODE_ENABLED;
return m_pIQNet->GetState() != QNET_STATE_IDLE && statsEnabled;
#endif
return true;
}
bool CPlatformNetworkManagerSony::SessionHasSpace(unsigned int spaceRequired /*= 1*/)
{
return m_pSQRNet->SessionHasSpace(spaceRequired);
#if 0
// This function is used while a session is running, so all players trying to join
// should use public slots,
DWORD publicSlots = 0;
DWORD filledPublicSlots = 0;
DWORD privateSlots = 0;
DWORD filledPrivateSlots = 0;
DWORD dataSize = sizeof(DWORD);
m_pIQNet->GetOpt(QNET_OPTION_TOTAL_PUBLIC_SLOTS, &publicSlots, &dataSize );
m_pIQNet->GetOpt(QNET_OPTION_FILLED_PUBLIC_SLOTS, &filledPublicSlots, &dataSize );
m_pIQNet->GetOpt(QNET_OPTION_TOTAL_PRIVATE_SLOTS, &privateSlots, &dataSize );
m_pIQNet->GetOpt(QNET_OPTION_FILLED_PRIVATE_SLOTS, &filledPrivateSlots, &dataSize );
DWORD spaceLeft = (publicSlots - filledPublicSlots) + (privateSlots - filledPrivateSlots);
return spaceLeft >= spaceRequired;
#else
return true;
#endif
}
void CPlatformNetworkManagerSony::SendInviteGUI(int quadrant)
{
m_pSQRNet->SendInviteGUI();
}
bool CPlatformNetworkManagerSony::IsAddingPlayer()
{
return false;
}
bool CPlatformNetworkManagerSony::LeaveGame(bool bMigrateHost)
{
if( m_bLeavingGame ) return true;
m_bLeavingGame = true;
// If we are a client, wait for all client connections to close
// TODO Possibly need to do multiple objects depending on how split screen online works
SQRNetworkPlayer *pSQRPlayer = m_pSQRNet->GetLocalPlayerByUserIndex(g_NetworkManager.GetPrimaryPad());
INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pSQRPlayer);
if(pNetworkPlayer != NULL)
{
Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL )
{
//printf("Waiting for socket closed event\n");
DWORD result = socket->m_socketClosedEvent->WaitForSignal(INFINITE);
// The session might be gone once the socket releases
if( IsInSession() )
{
//printf("Socket closed event has fired\n");
// 4J Stu - Clear our reference to this socket
pSQRPlayer = m_pSQRNet->GetLocalPlayerByUserIndex(g_NetworkManager.GetPrimaryPad());
pNetworkPlayer = getNetworkPlayer(pSQRPlayer);
pNetworkPlayer->SetSocket( NULL );
}
delete socket;
}
else
{
//printf("Socket is already NULL\n");
}
}
// If we are the host wait for the game server to end
if(m_pSQRNet->IsHost() && g_NetworkManager.ServerStoppedValid())
{
m_pSQRNet->EndGame();
g_NetworkManager.ServerStoppedWait();
g_NetworkManager.ServerStoppedDestroy();
}
return _LeaveGame(bMigrateHost, true);
}
bool CPlatformNetworkManagerSony::_LeaveGame(bool bMigrateHost, bool bLeaveRoom)
{
// 4J Stu - Fix for #10490 - TCR 001 BAS Game Stability: When a party of four players leave a world to join another world without saving the title will crash.
// Changed this to make it threadsafe
m_bLeavingGame = true; // Added for Sony platforms but unsure why the 360 doesn't need it - without this, the leaving triggered by this causes the game to respond by leaving again when it transitions to the SNM_STATE_ENDING state
m_bLeaveRoomWhenLeavingGame = bLeaveRoom;
m_bLeaveGameOnTick = true;
m_migrateHostOnLeave = bMigrateHost;
return true;
}
void CPlatformNetworkManagerSony::HostGame(int localUsersMask, bool bOnlineGame, bool bIsPrivate, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/)
{
// #ifdef _XBOX
// 4J Stu - We probably did this earlier as well, but just to be sure!
SetLocalGame( !bOnlineGame );
SetPrivateGame( bIsPrivate );
SystemFlagReset();
// Make sure that the Primary Pad is in by default
localUsersMask |= GetLocalPlayerMask( g_NetworkManager.GetPrimaryPad() );
_HostGame( localUsersMask, publicSlots, privateSlots );
//#endif
}
void CPlatformNetworkManagerSony::_HostGame(int usersMask, unsigned char publicSlots /*= MINECRAFT_NET_MAX_PLAYERS*/, unsigned char privateSlots /*= 0*/)
{
// Start hosting a new game
memset(&m_hostGameSessionData,0,sizeof(m_hostGameSessionData));
m_hostGameSessionData.netVersion = MINECRAFT_NET_VERSION;
m_hostGameSessionData.isJoinable = !IsPrivateGame();
m_hostGameSessionData.isReadyToJoin = false;
m_hostGameSessionData.playerCount = 0;
m_hostGameSessionData.m_uiGameHostSettings = app.GetGameHostOption(eGameHostOption_All);
for( int i = 0; i < SQRNetworkManager::MAX_LOCAL_PLAYER_COUNT; i++ )
{
if( usersMask & ( 1 << i ) )
{
m_hostGameSessionData.playerCount++;
}
}
m_pSQRNet->CreateAndJoinRoom(g_NetworkManager.GetPrimaryPad(),usersMask, &m_hostGameSessionData, sizeof(m_hostGameSessionData), IsLocalGame()); // Should be using: g_NetworkManager.GetLockedProfile() but that isn't being set currently
}
bool CPlatformNetworkManagerSony::_StartGame()
{
#if 0
// Set the options that now allow players to join this game
BOOL enableJip = TRUE; // Must always be true othewise nobody can join the game while in the PLAY state
m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL );
BOOL enableInv = !IsLocalGame();
m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL );
BOOL enablePres = !IsPrivateGame() && !IsLocalGame();
m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL );
return ( m_pIQNet->StartGame() == S_OK );
#else
m_pSQRNet->StartGame();
return true;
#endif
}
int CPlatformNetworkManagerSony::JoinGame(FriendSessionInfo *searchResult, int localUsersMask, int primaryUserIndex)
{
int joinPlayerCount = 0;
for( int i = 0; i < SQRNetworkManager::MAX_LOCAL_PLAYER_COUNT; i++ )
{
if( localUsersMask & ( 1 << i ) )
{
joinPlayerCount++;
}
}
GameSessionData *gameSession = (GameSessionData *)(&searchResult->data);
if( ( gameSession->playerCount + joinPlayerCount ) > SQRNetworkManager::MAX_ONLINE_PLAYER_COUNT )
{
return CGameNetworkManager::JOINGAME_FAIL_SERVER_FULL;
}
if( m_pSQRNet->JoinRoom(&searchResult->searchResult, localUsersMask) )
{
return CGameNetworkManager::JOINGAME_SUCCESS;
}
else
{
return CGameNetworkManager::JOINGAME_FAIL_GENERAL;
}
}
bool CPlatformNetworkManagerSony::SetLocalGame(bool isLocal)
{
if( m_pSQRNet->GetState() == SQRNetworkManager::SNM_STATE_IDLE)
{
#if 0
QNET_SESSIONTYPE sessionType = isLocal ? QNET_SESSIONTYPE_LOCAL : QNET_SESSIONTYPE_LIVE_STANDARD;
m_pIQNet->SetOpt(QNET_OPTION_TYPE_SESSIONTYPE, &sessionType , sizeof QNET_SESSIONTYPE);
// The default value for this is QNET_LIVE_STATS_MODE_AUTO, but that decides based on the players
// in when the game starts. As we may want a non-live player to join the game we cannot have stats enabled
// when we create the sessions. As a result of this, the NotifyWriteStats callback will not be called for
// LIVE players that are connected to LIVE so we write their stats data on a state change.
QNET_LIVE_STATS_MODE statsMode = isLocal ? QNET_LIVE_STATS_MODE_DISABLED : QNET_LIVE_STATS_MODE_ENABLED;
m_pIQNet->SetOpt(QNET_OPTION_LIVE_STATS_MODE, &statsMode , sizeof QNET_LIVE_STATS_MODE);
// Also has a default of QNET_LIVE_PRESENCE_MODE_AUTO as above, although the effects are less of an issue
QNET_LIVE_PRESENCE_MODE presenceMode = isLocal ? QNET_LIVE_PRESENCE_MODE_NOT_ADVERTISED : QNET_LIVE_PRESENCE_MODE_ADVERTISED;
m_pIQNet->SetOpt(QNET_OPTION_LIVE_PRESENCE_MODE, &presenceMode , sizeof QNET_LIVE_PRESENCE_MODE);
#endif
m_bIsOfflineGame = isLocal;
app.DebugPrintf("Setting as local game: %s\n", isLocal ? "yes" : "no" );
}
else
{
app.DebugPrintf("Tried to change session type while not in idle or offline state\n");
}
return true;
}
void CPlatformNetworkManagerSony::SetPrivateGame(bool isPrivate)
{
app.DebugPrintf("Setting as private game: %s\n", isPrivate ? "yes" : "no" );
m_bIsPrivateGame = isPrivate;
}
void CPlatformNetworkManagerSony::RegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam)
{
playerChangedCallback[iPad] = callback;
playerChangedCallbackParam[iPad] = callbackParam;
}
void CPlatformNetworkManagerSony::UnRegisterPlayerChangedCallback(int iPad, void (*callback)(void *callbackParam, INetworkPlayer *pPlayer, bool leaving), void *callbackParam)
{
if(playerChangedCallbackParam[iPad] == callbackParam)
{
playerChangedCallback[iPad] = NULL;
playerChangedCallbackParam[iPad] = NULL;
}
}
void CPlatformNetworkManagerSony::HandleSignInChange()
{
return;
}
bool CPlatformNetworkManagerSony::_RunNetworkGame()
{
#if 0
// We delay actually starting the session so that we know the game server is running by the time the clients try to join
// This does result in a host advantage
HRESULT hr = m_pIQNet->StartGame();
if(FAILED(hr)) return false;
// Set the options that now allow players to join this game
BOOL enableJip = TRUE; // Must always be true othewise nobody can join the game while in the PLAY state
m_pIQNet->SetOpt( QNET_OPTION_JOIN_IN_PROGRESS_ALLOWED, &enableJip, sizeof BOOL );
BOOL enableInv = !IsLocalGame();
m_pIQNet->SetOpt( QNET_OPTION_INVITES_ALLOWED, &enableInv, sizeof BOOL );
BOOL enablePres = !IsPrivateGame() && !IsLocalGame();
m_pIQNet->SetOpt( QNET_OPTION_PRESENCE_JOIN_MODE, &enablePres, sizeof BOOL );
#endif
if( IsHost() )
{
m_pSQRNet->StartGame();
m_hostGameSessionData.isReadyToJoin = true;
m_pSQRNet->UpdateExternalRoomData();
m_pSQRNet->SetPresenceDataStartHostingGame();
}
return true;
}
// Note that this does less than the xbox equivalent as we have HandleResyncPlayerRequest that is called by the underlying SQRNetworkManager when players are added/removed etc., so this
// call is only used to update the game host settings & then do the final push out of the data.
void CPlatformNetworkManagerSony::UpdateAndSetGameSessionData(INetworkPlayer *pNetworkPlayerLeaving /*= NULL*/)
{
if( this->m_bLeavingGame )
return;
m_hostGameSessionData.hostPlayerUID = GetHostPlayer()->GetUID();
#ifdef __PSVITA__
if(usingAdhocMode())
{
m_hostGameSessionData.hostPlayerUID.setForAdhoc();
}
#endif
m_hostGameSessionData.m_uiGameHostSettings = app.GetGameHostOption(eGameHostOption_All);
// If this is called With a pNetworkPlayerLeaving, then the call has ultimately started within SQRNetworkManager::RemoveRemotePlayersAndSync, so we don't need to sync each change
// as that function does a sync at the end of all changes.
if( pNetworkPlayerLeaving == NULL )
{
m_pSQRNet->UpdateExternalRoomData();
}
}
int CPlatformNetworkManagerSony::RemovePlayerOnSocketClosedThreadProc( void* lpParam )
{
INetworkPlayer *pNetworkPlayer = (INetworkPlayer *)lpParam;
Socket *socket = pNetworkPlayer->GetSocket();
if( socket != NULL )
{
//printf("Waiting for socket closed event\n");
socket->m_socketClosedEvent->WaitForSignal(INFINITE);
//printf("Socket closed event has fired\n");
// 4J Stu - Clear our reference to this socket
pNetworkPlayer->SetSocket( NULL );
delete socket;
}
return g_pPlatformNetworkManager->RemoveLocalPlayer( pNetworkPlayer );
}
bool CPlatformNetworkManagerSony::RemoveLocalPlayer( INetworkPlayer *pNetworkPlayer )
{
if( pNetworkPlayer->IsLocal() )
{
return m_pSQRNet->RemoveLocalPlayerByUserIndex( pNetworkPlayer->GetUserIndex() );
}
return true;
}
CPlatformNetworkManagerSony::PlayerFlags::PlayerFlags(INetworkPlayer *pNetworkPlayer, unsigned int count)
{
// 4J Stu - Don't assert, just make it a multiple of 8! This count is calculated from a load of separate values,
// and makes tweaking world/render sizes a pain if we hit an assert here
count = (count + 8 - 1) & ~(8 - 1);
//assert( ( count % 8 ) == 0 );
this->m_pNetworkPlayer = pNetworkPlayer;
this->flags = new unsigned char [ count / 8 ];
memset( this->flags, 0, count / 8 );
this->count = count;
}
CPlatformNetworkManagerSony::PlayerFlags::~PlayerFlags()
{
delete [] flags;
}
// Add a player to the per system flag storage - if we've already got a player from that system, copy its flags over
void CPlatformNetworkManagerSony::SystemFlagAddPlayer(INetworkPlayer *pNetworkPlayer)
{
PlayerFlags *newPlayerFlags = new PlayerFlags( pNetworkPlayer, m_flagIndexSize);
// If any of our existing players are on the same system, then copy over flags from that one
for( unsigned int i = 0; i < m_playerFlags.size(); i++ )
{
if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) )
{
memcpy( newPlayerFlags->flags, m_playerFlags[i]->flags, m_playerFlags[i]->count / 8 );
break;
}
}
m_playerFlags.push_back(newPlayerFlags);
}
// Remove a player from the per system flag storage - just maintains the m_playerFlags vector without any gaps in it
void CPlatformNetworkManagerSony::SystemFlagRemovePlayer(INetworkPlayer *pNetworkPlayer)
{
for( unsigned int i = 0; i < m_playerFlags.size(); i++ )
{
if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer )
{
delete m_playerFlags[i];
m_playerFlags[i] = m_playerFlags.back();
m_playerFlags.pop_back();
return;
}
}
}
void CPlatformNetworkManagerSony::SystemFlagReset()
{
for( unsigned int i = 0; i < m_playerFlags.size(); i++ )
{
delete m_playerFlags[i];
}
m_playerFlags.clear();
}
// Set a per system flag - this is done by setting the flag on every player that shares that system
void CPlatformNetworkManagerSony::SystemFlagSet(INetworkPlayer *pNetworkPlayer, int index)
{
if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return;
if( pNetworkPlayer == NULL ) return;
for( unsigned int i = 0; i < m_playerFlags.size(); i++ )
{
if( pNetworkPlayer->IsSameSystem(m_playerFlags[i]->m_pNetworkPlayer) )
{
m_playerFlags[i]->flags[ index / 8 ] |= ( 128 >> ( index % 8 ) );
}
}
}
// Get value of a per system flag - can be read from the flags of the passed in player as anything else sent to that
// system should also have been duplicated here
bool CPlatformNetworkManagerSony::SystemFlagGet(INetworkPlayer *pNetworkPlayer, int index)
{
if( ( index < 0 ) || ( index >= m_flagIndexSize ) ) return false;
if( pNetworkPlayer == NULL )
{
return false;
}
for( unsigned int i = 0; i < m_playerFlags.size(); i++ )
{
if( m_playerFlags[i]->m_pNetworkPlayer == pNetworkPlayer )
{
return ( ( m_playerFlags[i]->flags[ index / 8 ] & ( 128 >> ( index % 8 ) ) ) != 0 );
}
}
return false;
}
wstring CPlatformNetworkManagerSony::GatherStats()
{
#if 0
return L"Queue messages: " + _toString(((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetSendQueueSize( NULL, QNET_GETSENDQUEUESIZE_MESSAGES ) )
+ L" Queue bytes: " + _toString( ((NetworkPlayerXbox *)GetHostPlayer())->GetQNetPlayer()->GetSendQueueSize( NULL, QNET_GETSENDQUEUESIZE_BYTES ) );
#else
return L"";
#endif
}
wstring CPlatformNetworkManagerSony::GatherRTTStats()
{
#if 0
wstring stats(L"Rtt: ");
wchar_t stat[32];
for(unsigned int i = 0; i < GetPlayerCount(); ++i)
{
SQRNetworkPlayer *pSQRPlayer = ((NetworkPlayerXbox *)GetPlayerByIndex( i ))->GetQNetPlayer();
if(!pSQRPlayer->IsLocal())
{
ZeroMemory(stat,32);
swprintf(stat, 32, L"%d: %d/", i, pSQRPlayer->GetCurrentRtt() );
stats.append(stat);
}
}
return stats;
#else
return L"";
#endif
}
void CPlatformNetworkManagerSony::TickSearch()
{
if( m_bSearchPending )
{
if( !m_pSQRNet->FriendRoomManagerIsBusy() )
{
m_searchResultsCount = m_pSQRNet->FriendRoomManagerGetCount();
delete m_pSearchResults;
m_pSearchResults = new SQRNetworkManager::SessionSearchResult[m_searchResultsCount];
for( int i = 0; i < m_searchResultsCount; i++ )
{
m_pSQRNet->FriendRoomManagerGetRoomInfo(i, &m_pSearchResults[i] );
}
m_bSearchPending = false;
if( m_SessionsUpdatedCallback != NULL ) m_SessionsUpdatedCallback(m_pSearchParam);
}
}
else
{
if( !m_pSQRNet->FriendRoomManagerIsBusy() )
{
// Don't start searches unless we have registered a callback
int searchDelay = MINECRAFT_PS3ROOM_SEARCH_DELAY_MILLISECONDS;
#ifdef __PSVITA__
// in adhoc mode we can keep searching, as the friend list is populated in callbacks
// 4J Stu - Every second seems a bit much as it makes the friend list flash every time it updates. Changed this to 5 seconds.
if( usingAdhocMode())
searchDelay = 5000;
#endif
if( m_SessionsUpdatedCallback != NULL && (m_lastSearchStartTime + searchDelay) < GetTickCount() )
{
if( m_pSQRNet->FriendRoomManagerSearch() )
{
m_bSearchPending = true;
m_lastSearchStartTime = GetTickCount();
}
}
}
}
}
vector<FriendSessionInfo *> *CPlatformNetworkManagerSony::GetSessionList(int iPad, int localPlayers, bool partyOnly)
{
vector<FriendSessionInfo *> *filteredList = new vector<FriendSessionInfo *>();
for( int i = 0; i < m_searchResultsCount; i++ )
{
if( m_pSearchResults[i].m_extData )
{
FriendSessionInfo *newInfo = new FriendSessionInfo();
newInfo->displayLabel = new wchar_t[17];
ZeroMemory(newInfo->displayLabel, sizeof(wchar_t)*17);
// TODO - this mbstowcs shouldn't encounter any non-ascii characters, but I imagine we'll want to actually use the online name here which is UTF-8
mbstowcs(newInfo->displayLabel, m_pSearchResults[i].m_NpId.handle.data, 17);
newInfo->displayLabelLength = strlen(m_pSearchResults[i].m_NpId.handle.data);
newInfo->hasPartyMember = false;
newInfo->searchResult = m_pSearchResults[i];
newInfo->sessionId = m_pSearchResults[i].m_sessionId;
memcpy(&newInfo->data, m_pSearchResults[i].m_extData, sizeof(GameSessionData));
if( ( newInfo->data.isReadyToJoin ) &&
( newInfo->data.isJoinable ) &&
( newInfo->data.netVersion == MINECRAFT_NET_VERSION ) )
{
filteredList->push_back(newInfo);
}
else
{
delete newInfo;
}
}
}
return filteredList;
}
bool CPlatformNetworkManagerSony::GetGameSessionInfo(int iPad, SessionID sessionId, FriendSessionInfo *foundSessionInfo)
{
#if 0
HRESULT hr = E_FAIL;
const XSESSION_SEARCHRESULT *pSearchResult;
const XNQOSINFO * pxnqi;
if( m_currentSearchResultsCount[iPad] > 0 )
{
// Loop through all the results.
for( DWORD dwResult = 0; dwResult < m_currentSearchResultsCount[iPad]; dwResult++ )
{
pSearchResult = &m_pCurrentSearchResults[iPad]->pResults[dwResult];
if(memcmp( &pSearchResult->info.sessionID, &sessionId, sizeof(SessionID) ) != 0) continue;
bool foundSession = false;
FriendSessionInfo *sessionInfo = NULL;
AUTO_VAR(itFriendSession, friendsSessions[iPad].begin());
for(itFriendSession = friendsSessions[iPad].begin(); itFriendSession < friendsSessions[iPad].end(); ++itFriendSession)
{
sessionInfo = *itFriendSession;
if(memcmp( &pSearchResult->info.sessionID, &sessionInfo->sessionId, sizeof(SessionID) ) == 0)
{
sessionInfo->searchResult = *pSearchResult;
sessionInfo->displayLabel = new wchar_t[100];
ZeroMemory( sessionInfo->displayLabel, 100 * sizeof(wchar_t) );
foundSession = true;
break;
}
}
// We received a search result for a session no longer in our list of friends sessions
if(!foundSession) break;
// See if this result was contacted successfully via QoS probes.
pxnqi = &m_pCurrentQoSResult[iPad]->axnqosinfo[dwResult];
if( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_CONTACTED )
{
if(pxnqi->cbData > 0)
{
sessionInfo->data = *(GameSessionData *)pxnqi->pbData;
wstring gamerName = convStringToWstring(sessionInfo->data.hostName);
swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME),L"MWWWWWWWWWWWWWWM");// gamerName.c_str() );
}
else
{
swprintf(sessionInfo->displayLabel,app.GetString(IDS_GAME_HOST_NAME_UNKNOWN));
}
sessionInfo->displayLabelLength = wcslen( sessionInfo->displayLabel );
// If this host wasn't disabled use this one.
if( !( pxnqi->bFlags & XNET_XNQOSINFO_TARGET_DISABLED ) &&
sessionInfo->data.netVersion == MINECRAFT_NET_VERSION &&
sessionInfo->data.isJoinable)
{
foundSessionInfo->data = sessionInfo->data;
if(foundSessionInfo->displayLabel != NULL) delete [] foundSessionInfo->displayLabel;
foundSessionInfo->displayLabel = new wchar_t[100];
memcpy(foundSessionInfo->displayLabel, sessionInfo->displayLabel, 100 * sizeof(wchar_t) );
foundSessionInfo->displayLabelLength = sessionInfo->displayLabelLength;
foundSessionInfo->hasPartyMember = sessionInfo->hasPartyMember;
foundSessionInfo->searchResult = sessionInfo->searchResult;
foundSessionInfo->sessionId = sessionInfo->sessionId;
hr = S_OK;
}
}
}
}
return ( hr == S_OK );
#else
return false;
#endif
}
void CPlatformNetworkManagerSony::SetSessionsUpdatedCallback( void (*SessionsUpdatedCallback)(LPVOID pParam), LPVOID pSearchParam )
{
m_SessionsUpdatedCallback = SessionsUpdatedCallback; m_pSearchParam = pSearchParam;
}
void CPlatformNetworkManagerSony::GetFullFriendSessionInfo( FriendSessionInfo *foundSession, void (* FriendSessionUpdatedFn)(bool success, void *pParam), void *pParam )
{
m_pSQRNet->GetExtDataForRoom( foundSession->sessionId.m_RoomId, &foundSession->data, FriendSessionUpdatedFn, pParam);
}
void CPlatformNetworkManagerSony::ForceFriendsSessionRefresh()
{
app.DebugPrintf("Resetting friends session search data\n");
m_lastSearchStartTime = 0;
m_searchResultsCount = 0;
delete m_pSearchResults;
m_pSearchResults = NULL;
}
INetworkPlayer *CPlatformNetworkManagerSony::addNetworkPlayer(SQRNetworkPlayer *pSQRPlayer)
{
NetworkPlayerSony *pNetworkPlayer = new NetworkPlayerSony(pSQRPlayer);
pSQRPlayer->SetCustomDataValue((ULONG_PTR)pNetworkPlayer);
currentNetworkPlayers.push_back( pNetworkPlayer );
return pNetworkPlayer;
}
void CPlatformNetworkManagerSony::removeNetworkPlayer(SQRNetworkPlayer *pSQRPlayer)
{
INetworkPlayer *pNetworkPlayer = getNetworkPlayer(pSQRPlayer);
for( AUTO_VAR(it, currentNetworkPlayers.begin()); it != currentNetworkPlayers.end(); it++ )
{
if( *it == pNetworkPlayer )
{
currentNetworkPlayers.erase(it);
return;
}
}
}
INetworkPlayer *CPlatformNetworkManagerSony::getNetworkPlayer(SQRNetworkPlayer *pSQRPlayer)
{
return pSQRPlayer ? (INetworkPlayer *)(pSQRPlayer->GetCustomDataValue()) : NULL;
}
INetworkPlayer *CPlatformNetworkManagerSony::GetLocalPlayerByUserIndex(int userIndex )
{
return getNetworkPlayer(m_pSQRNet->GetLocalPlayerByUserIndex(userIndex));
}
INetworkPlayer *CPlatformNetworkManagerSony::GetPlayerByIndex(int playerIndex)
{
return getNetworkPlayer(m_pSQRNet->GetPlayerByIndex(playerIndex));
}
INetworkPlayer * CPlatformNetworkManagerSony::GetPlayerByXuid(PlayerUID xuid)
{
return getNetworkPlayer(m_pSQRNet->GetPlayerByXuid(xuid));
}
INetworkPlayer * CPlatformNetworkManagerSony::GetPlayerBySmallId(unsigned char smallId)
{
return getNetworkPlayer(m_pSQRNet->GetPlayerBySmallId(smallId));
}
INetworkPlayer *CPlatformNetworkManagerSony::GetHostPlayer()
{
return getNetworkPlayer(m_pSQRNet->GetHostPlayer());
}
bool CPlatformNetworkManagerSony::IsHost()
{
return m_pSQRNet->IsHost() && !m_bHostChanged;
}
bool CPlatformNetworkManagerSony::JoinGameFromInviteInfo( int userIndex, int userMask, const INVITE_INFO *pInviteInfo)
{
return m_pSQRNet->JoinRoom( pInviteInfo->m_RoomId, pInviteInfo->m_ServerId, userMask, pInviteInfo );
}
void CPlatformNetworkManagerSony::SetSessionTexturePackParentId( int id )
{
m_hostGameSessionData.texturePackParentId = id;
}
void CPlatformNetworkManagerSony::SetSessionSubTexturePackId( int id )
{
m_hostGameSessionData.subTexturePackId = id;
}
void CPlatformNetworkManagerSony::Notify(int ID, ULONG_PTR Param)
{
#if 0
m_pSQRNet->Notify( ID, Param );
#endif
}
bool CPlatformNetworkManagerSony::IsInSession()
{
return m_pSQRNet->IsInSession();
}
bool CPlatformNetworkManagerSony::IsInGameplay()
{
return m_pSQRNet->GetState() == SQRNetworkManager::SNM_STATE_PLAYING;
}
bool CPlatformNetworkManagerSony::IsReadyToPlayOrIdle()
{
return m_pSQRNet->IsReadyToPlayOrIdle();
}
void CPlatformNetworkManagerSony::SetSQRPresenceInfoFromExtData(SQRNetworkManager::PresenceSyncInfo *presence, void *pExtData, SceNpMatching2RoomId roomId, SceNpMatching2ServerId serverId)
{
GameSessionData *gsd = (GameSessionData *)pExtData;
memcpy(&presence->hostPlayerUID, &gsd->hostPlayerUID, sizeof(GameSessionUID) );
presence->m_RoomId = roomId;
presence->m_ServerId = serverId;
presence->texturePackParentId = gsd->texturePackParentId;
presence->subTexturePackId = gsd->subTexturePackId;
presence->netVersion = gsd->netVersion;
presence->inviteOnly = !gsd->isJoinable;
}
void CPlatformNetworkManagerSony::MallocAndSetExtDataFromSQRPresenceInfo(void **pExtData, SQRNetworkManager::PresenceSyncInfo *presence)
{
GameSessionData *gsd = (GameSessionData *)malloc(sizeof(GameSessionData));
memset(gsd, 0, sizeof(GameSessionData));
if( presence->netVersion != 0 )
{
memcpy(&gsd->hostPlayerUID, &presence->hostPlayerUID, sizeof(GameSessionUID) );
gsd->texturePackParentId = presence->texturePackParentId;
gsd->subTexturePackId = presence->subTexturePackId;
gsd->netVersion = presence->netVersion;
gsd->isJoinable = !presence->inviteOnly;
gsd->isReadyToJoin = true;
}
*pExtData = gsd;
}
#ifdef __PSVITA__
bool CPlatformNetworkManagerSony::setAdhocMode( bool bAdhoc )
{
if(m_bUsingAdhocMode != bAdhoc)
{
m_bUsingAdhocMode = bAdhoc;
if(m_bUsingAdhocMode)
{
// uninit the PSN, and init adhoc
if(m_pSQRNet_Vita->IsInitialised())
{
m_pSQRNet_Vita->UnInitialise();
}
if(m_pSQRNet_Vita_Adhoc->IsInitialised()==false)
{
m_pSQRNet_Vita_Adhoc->Initialise();
}
m_pSQRNet = m_pSQRNet_Vita_Adhoc;
}
else
{
if(m_pSQRNet_Vita_Adhoc->IsInitialised())
{
int ret = sceNetCtlAdhocDisconnect();
// uninit the adhoc, and init psn
m_pSQRNet_Vita_Adhoc->UnInitialise();
}
if(m_pSQRNet_Vita->IsInitialised()==false)
{
m_pSQRNet_Vita->Initialise();
}
m_pSQRNet = m_pSQRNet_Vita;
}
}
return true;
}
void CPlatformNetworkManagerSony::startAdhocMatching( )
{
assert(m_pSQRNet == m_pSQRNet_Vita_Adhoc);
((SQRNetworkManager_AdHoc_Vita*)m_pSQRNet_Vita_Adhoc)->startMatching();
}
bool CPlatformNetworkManagerSony::checkValidInviteData(const INVITE_INFO* pInviteInfo)
{
if(((SQRNetworkManager_Vita*)m_pSQRNet_Vita)->GetHostUID() == pInviteInfo->hostPlayerUID)
{
// we're trying to join a game we're already in, so we just ignore this
return false;
}
else
{
return true;
}
}
#endif // __PSVITA__
|