This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 888
Expand file tree
/
Copy pathMyMultiplayerClient.cs
More file actions
886 lines (731 loc) · 29.9 KB
/
Copy pathMyMultiplayerClient.cs
File metadata and controls
886 lines (731 loc) · 29.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
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
#region Using
using Sandbox.Engine.Networking;
using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer;
using Sandbox.Graphics.GUI;
using SteamSDK;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using VRage;
using VRage.Utils;
using VRage.Trace;
using VRage.Library.Utils;
using VRage.Network;
using VRage.Replication;
using VRage.Library.Collections;
using Sandbox.Game.Entities;
using VRage.Game;
#endregion
namespace Sandbox.Engine.Multiplayer
{
class MyMultiplayerClient : MyMultiplayerBase, IReplicationClientCallback
{
#region Fields
string m_worldName;
MyGameModeEnum m_gameMode;
float m_inventoryMultiplier;
float m_assemblerMultiplier;
float m_refineryMultiplier;
float m_welderMultiplier;
float m_grinderMultiplier;
string m_hostName;
ulong m_worldSize;
int m_appVersion;
int m_membersLimit;
string m_dataHash;
MyMultiplayerBattleData m_battleData;
List<ulong> m_members = new List<ulong>();
MemberCollection m_membersCollection;
Dictionary<ulong, MyConnectedClientData> m_memberData = new Dictionary<ulong, MyConnectedClientData>();
public Action OnJoin;
private bool m_clientJoined = false;
#endregion
#region Properties
public override bool IsServer { get { return false; } }
public override string WorldName
{
get { return m_worldName; }
set { m_worldName = value; }
}
public override MyGameModeEnum GameMode
{
get { return m_gameMode; }
set { m_gameMode = value; }
}
public override float InventoryMultiplier
{
get { return m_inventoryMultiplier; }
set { m_inventoryMultiplier = value; }
}
public override float AssemblerMultiplier
{
get { return m_assemblerMultiplier; }
set { m_assemblerMultiplier = value; }
}
public override float RefineryMultiplier
{
get { return m_refineryMultiplier; }
set { m_refineryMultiplier = value; }
}
public override float WelderMultiplier
{
get { return m_welderMultiplier; }
set { m_welderMultiplier = value; }
}
public override float GrinderMultiplier
{
get { return m_grinderMultiplier; }
set { m_grinderMultiplier = value; }
}
public override string HostName
{
get { return m_hostName; }
set { m_hostName = value; }
}
public override ulong WorldSize
{
get { return m_worldSize; }
set { m_worldSize = value; }
}
public override int AppVersion
{
get { return m_appVersion; }
set { m_appVersion = value; }
}
public override string DataHash
{
get { return m_dataHash; }
set { m_dataHash = value; }
}
public override int MaxPlayers
{
get { return 65536; }
}
public override int ModCount
{
get;
protected set;
}
private List<MyObjectBuilder_Checkpoint.ModItem> m_mods = new List<MyObjectBuilder_Checkpoint.ModItem>();
public override List<MyObjectBuilder_Checkpoint.ModItem> Mods
{
get { return m_mods; }
set
{
m_mods = value;
ModCount = m_mods.Count;
}
}
private int m_viewDistance;
public override int ViewDistance
{
get { return m_viewDistance; }
set { m_viewDistance = value; }
}
public override bool Scenario
{
get;
set;
}
public override string ScenarioBriefing
{
get;
set;
}
public override DateTime ScenarioStartTime
{
get;
set;
}
public override bool Battle
{
get;
set;
}
public override float BattleRemainingTime
{
get { return m_battleData.BattleRemainingTime; }
set { m_battleData.BattleRemainingTime = value; }
}
public override bool BattleCanBeJoined
{
get { return m_battleData.BattleCanBeJoined; }
set { m_battleData.BattleCanBeJoined = value; }
}
public override ulong BattleWorldWorkshopId
{
get { return m_battleData.BattleWorldWorkshopId; }
set { m_battleData.BattleWorldWorkshopId = value; }
}
public override int BattleFaction1MaxBlueprintPoints
{
get { return m_battleData.BattleFaction1MaxBlueprintPoints; }
set { m_battleData.BattleFaction1MaxBlueprintPoints = value; }
}
public override int BattleFaction2MaxBlueprintPoints
{
get { return m_battleData.BattleFaction2MaxBlueprintPoints; }
set { m_battleData.BattleFaction2MaxBlueprintPoints = value; }
}
public override int BattleFaction1BlueprintPoints
{
get { return m_battleData.BattleFaction1BlueprintPoints; }
set { m_battleData.BattleFaction1BlueprintPoints = value; }
}
public override int BattleFaction2BlueprintPoints
{
get { return m_battleData.BattleFaction2BlueprintPoints; }
set { m_battleData.BattleFaction2BlueprintPoints = value; }
}
public override int BattleMapAttackerSlotsCount
{
get { return m_battleData.BattleMapAttackerSlotsCount; }
set { m_battleData.BattleMapAttackerSlotsCount = value; }
}
public override long BattleFaction1Id
{
get { return m_battleData.BattleFaction1Id; }
set { m_battleData.BattleFaction1Id = value; }
}
public override long BattleFaction2Id
{
get { return m_battleData.BattleFaction2Id; }
set { m_battleData.BattleFaction2Id = value; }
}
public override int BattleFaction1Slot
{
get { return m_battleData.BattleFaction1Slot; }
set { m_battleData.BattleFaction1Slot = value; }
}
public override int BattleFaction2Slot
{
get { return m_battleData.BattleFaction2Slot; }
set { m_battleData.BattleFaction2Slot = value; }
}
public override bool BattleFaction1Ready
{
get { return m_battleData.BattleFaction1Ready; }
set { m_battleData.BattleFaction1Ready = value; }
}
public override bool BattleFaction2Ready
{
get { return m_battleData.BattleFaction2Ready; }
set { m_battleData.BattleFaction2Ready = value; }
}
public override int BattleTimeLimit
{
get { return m_battleData.BattleTimeLimit; }
set { m_battleData.BattleTimeLimit = value; }
}
public GameServerItem Server { get; private set; }
#endregion
public new MyReplicationClient ReplicationLayer { get { return (MyReplicationClient)base.ReplicationLayer; } }
internal MyMultiplayerClient(GameServerItem server, MySyncLayer syncLayer)
: base(syncLayer)
{
m_membersCollection = new MemberCollection(m_members);
Server = server;
ServerId = server.SteamID;
SyncLayer.TransportLayer.IsBuffering = true;
SyncLayer.RegisterClientEvents(this);
SetReplicationLayer(new MyReplicationClient(this, CreateClientState()));
syncLayer.TransportLayer.Register(MyMessageId.SERVER_DATA, ReplicationLayer.ProcessServerData);
syncLayer.TransportLayer.Register(MyMessageId.REPLICATION_CREATE, OnReplicationCreate);
syncLayer.TransportLayer.Register(MyMessageId.REPLICATION_DESTROY, OnReplicationDestroy);
syncLayer.TransportLayer.Register(MyMessageId.SERVER_STATE_SYNC, ReplicationLayer.ProcessStateSync);
syncLayer.TransportLayer.Register(MyMessageId.RPC, ReplicationLayer.ProcessEvent);
syncLayer.TransportLayer.Register(MyMessageId.REPLICATION_STREAM_BEGIN, OnReplicationBeginCreate);
syncLayer.TransportLayer.Register(MyMessageId.JOIN_RESULT, OnJoinResult);
syncLayer.TransportLayer.Register(MyMessageId.WORLD_DATA, OnWorldData);
syncLayer.TransportLayer.Register(MyMessageId.WORLD_BATTLE_DATA, OnWorldBattleData);
syncLayer.TransportLayer.Register(MyMessageId.CLIENT_CONNNECTED,OnClientConnected);
ClientJoined += MyMultiplayerClient_ClientJoined;
ClientLeft += MyMultiplayerClient_ClientLeft;
HostLeft += MyMultiplayerClient_HostLeft;
Peer2Peer.ConnectionFailed += Peer2Peer_ConnectionFailed;
Peer2Peer.SessionRequest += Peer2Peer_SessionRequest;
m_battleData = new MyMultiplayerBattleData(this);
}
void OnReplicationBeginCreate(MyPacket packet)
{
while (MyEntities.HasEntitiesToDelete())
{
MyEntities.DeleteRememberedEntities();
}
ReplicationLayer.ProcessReplicationCreateBegin(packet);
}
void OnReplicationCreate(MyPacket packet)
{
while (MyEntities.HasEntitiesToDelete())
{
MyEntities.DeleteRememberedEntities();
}
ReplicationLayer.ProcessReplicationCreate(packet);
}
void OnReplicationDestroy(MyPacket packet)
{
ReplicationLayer.ProcessReplicationDestroy(packet);
}
void OnWorldData(MyPacket packet)
{
ServerDataMsg msg = ReplicationLayer.OnWorldData(packet);
OnServerData(ref msg);
}
void OnWorldBattleData(MyPacket packet)
{
ServerBattleDataMsg msg = ReplicationLayer.OnWorldBattleData(packet);
OnServerBattleData(ref msg);
}
void OnJoinResult(MyPacket packet)
{
JoinResultMsg msg = ReplicationLayer.OnJoinResult(packet);
OnUserJoined(ref msg);
}
void OnClientConnected(MyPacket packet)
{
ConnectedClientDataMsg msg = ReplicationLayer.OnClientConnected(packet);
OnConnectedClient(ref msg);
}
public override void Dispose()
{
CloseClient();
base.Dispose();
}
#region ReplicationClient
void IReplicationClientCallback.SendClientUpdate(BitStream stream)
{
SyncLayer.TransportLayer.SendMessage(MyMessageId.CLIENT_UPDATE, stream, true, new EndpointId(Sync.ServerId));
}
void IReplicationClientCallback.SendEvent(BitStream stream, bool reliable)
{
SyncLayer.TransportLayer.SendMessage(MyMessageId.RPC, stream, reliable, new EndpointId(Sync.ServerId));
}
void IReplicationClientCallback.SendReplicableReady(BitStream stream)
{
SyncLayer.TransportLayer.SendMessage(MyMessageId.REPLICATION_READY, stream, true, new EndpointId(Sync.ServerId));
}
void IReplicationClientCallback.SendConnectRequest(BitStream stream)
{
SyncLayer.TransportLayer.SendMessage(MyMessageId.CLIENT_CONNNECTED, stream, true, new EndpointId(Sync.ServerId));
}
#endregion
void MyMultiplayerClient_HostLeft()
{
m_clientJoined = false;
CloseClient();
MyGuiScreenMainMenu.UnloadAndExitToMenu();
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: MyTexts.Get(MyCommonTexts.MultiplayerErrorServerHasLeft)));
}
void Peer2Peer_SessionRequest(ulong remoteUserId)
{
if (IsClientKickedOrBanned(remoteUserId)) return;
if (IsServer || remoteUserId == ServerId)
{
Peer2Peer.AcceptSession(remoteUserId);
}
}
void MyMultiplayerClient_ClientLeft(ulong user, ChatMemberStateChangeEnum stateChange)
{
if (user == ServerId)
{
RaiseHostLeft();
return;
}
if (m_members.Contains(user))
{
m_members.Remove(user);
MySandboxGame.Log.WriteLineAndConsole("Player disconnected: " + MySteam.API.Friends.GetPersonaName(user) + " (" + user + ")");
MyTrace.Send(TraceWindow.Multiplayer, "Player disconnected: " + stateChange.ToString());
if (MySandboxGame.IsGameReady && Sync.MyId != user)
{
var clientLeft = new MyHudNotification(MyCommonTexts.NotificationClientDisconnected, 5000, level: MyNotificationLevel.Important);
clientLeft.SetTextFormatArguments(MySteam.API.Friends.GetPersonaName(user));
MyHud.Notifications.Add(clientLeft);
}
//Peer2Peer.CloseSession(user);
}
m_memberData.Remove(user);
}
void MyMultiplayerClient_ClientJoined(ulong user)
{
if (m_members.Contains(user)) return;
m_members.Add(user);
}
void Peer2Peer_ConnectionFailed(ulong remoteUserId, P2PSessionErrorEnum error)
{
MyTrace.Send(TraceWindow.Multiplayer, "Peer2Peer_ConnectionFailed (" + remoteUserId.ToString() + ")");
if (remoteUserId == ServerId)
{
RaiseHostLeft();
}
}
public override bool IsCorrectVersion()
{
return m_appVersion == MyFinalBuildConstants.APP_VERSION;
}
public override MyDownloadWorldResult DownloadWorld()
{
MyTrace.Send(TraceWindow.Multiplayer, "World request sent");
MyDownloadWorldResult ret = new MyDownloadWorldResult(MyMultiplayer.WorldDownloadChannel, ServerId, this);
MyControlWorldRequestMsg msg = new MyControlWorldRequestMsg();
SendControlMessage(ServerId, ref msg);
return ret;
}
public override void KickClient(ulong client)
{
MyControlKickClientMsg msg = new MyControlKickClientMsg();
msg.KickedClient = client;
SendControlMessage(ServerId, ref msg);
}
public override void BanClient(ulong client, bool ban)
{
MyControlBanClientMsg msg = new MyControlBanClientMsg();
msg.BannedClient = client;
msg.Banned = ban;
SendControlMessage(ServerId, ref msg);
}
private void CloseClient()
{
MyTrace.Send(TraceWindow.Multiplayer, "Multiplayer client closed");
MyControlDisconnectedMsg msg = new MyControlDisconnectedMsg();
msg.Client = Sync.MyId;
SendControlMessage(ServerId, ref msg);
OnJoin = null;
//TODO: Any better way? P2P needs to be closed from both sides. If closed right after Send, message
//can stay not sent.
Thread.Sleep(200);
//WARN: If closed here, previous control message probably not come
Peer2Peer.CloseSession(ServerId);
Peer2Peer.ConnectionFailed -= Peer2Peer_ConnectionFailed;
Peer2Peer.SessionRequest -= Peer2Peer_SessionRequest;
}
public override MemberCollection Members
{
get { return m_membersCollection; }
}
public override int MemberCount
{
get { return m_members.Count; }
}
public override ulong GetMemberByIndex(int memberIndex)
{
return m_members[memberIndex];
}
public override ulong LobbyId
{
get { return 0; }
}
public override int MemberLimit
{
get { return m_membersLimit; }
set { m_membersLimit = value; }
}
public override bool IsAdmin(ulong steamID)
{
if (m_memberData.ContainsKey(steamID))
return m_memberData[steamID].IsAdmin;
return false;
}
public override ulong GetOwner()
{
return ServerId;
}
public override void SetOwner(ulong owner)
{
System.Diagnostics.Debug.Fail("Cannot change owner");
}
public override LobbyTypeEnum GetLobbyType()
{
return LobbyTypeEnum.Public;
}
public override void SetLobbyType(LobbyTypeEnum type)
{
}
public override void SetMemberLimit(int limit)
{
m_membersLimit = limit;
}
protected override void OnChatMessage(ref ChatMsg msg)
{
RaiseChatMessageReceived(msg.Author, msg.Text, ChatEntryTypeEnum.ChatMsg);
}
public override void SendChatMessage(string text)
{
ChatMsg msg = new ChatMsg();
msg.Text = text;
msg.Author = Sync.MyId;
OnChatMessage(ref msg);
SendChatMessage(ref msg);
}
void OnServerData(ref ServerDataMsg msg)
{
m_worldName = msg.WorldName;
m_gameMode = msg.GameMode;
m_inventoryMultiplier = msg.InventoryMultiplier;
m_assemblerMultiplier = msg.AssemblerMultiplier;
m_refineryMultiplier = msg.RefineryMultiplier;
m_welderMultiplier = msg.WelderMultiplier;
m_grinderMultiplier = msg.GrinderMultiplier;
m_hostName = msg.HostName;
m_worldSize = msg.WorldSize;
m_appVersion = msg.AppVersion;
m_membersLimit = msg.MembersLimit;
m_dataHash = msg.DataHash;
}
void OnServerBattleData(ref ServerBattleDataMsg msg)
{
Battle = true;
m_worldName = msg.WorldName;
m_gameMode = msg.GameMode;
m_hostName = msg.HostName;
m_worldSize = msg.WorldSize;
m_appVersion = msg.AppVersion;
m_membersLimit = msg.MembersLimit;
m_dataHash = msg.DataHash;
m_battleData.LoadData(msg.BattleData);
}
void OnUserJoined(ref JoinResultMsg msg)
{
if (msg.JoinResult == JoinResult.OK)
{
if (OnJoin != null)
{
OnJoin();
OnJoin = null;
m_clientJoined = true;
}
}
else if (msg.JoinResult == JoinResult.NotInGroup)
{
MyGuiScreenMainMenu.UnloadAndExitToMenu();
Dispose();
ulong groupId = Server.GetGameTagByPrefixUlong("groupId");
string groupName = MySteam.API.Friends.GetClanName(groupId);
var messageBox = MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: new StringBuilder(string.Format(
MyTexts.GetString(MyCommonTexts.MultiplayerErrorNotInGroup), groupName)),
buttonType: MyMessageBoxButtonsType.YES_NO);
messageBox.ResultCallback = delegate(MyGuiScreenMessageBox.ResultEnum result)
{
if (result == MyGuiScreenMessageBox.ResultEnum.YES)
{
MySteam.API.OpenOverlayUser(groupId);
};
};
MyGuiSandbox.AddScreen(messageBox);
}
else if (msg.JoinResult == JoinResult.BannedByAdmins)
{
MyGuiScreenMainMenu.UnloadAndExitToMenu();
Dispose();
ulong admin = msg.Admin;
if (admin != 0)
{
var messageBox = MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: MyTexts.Get(MyCommonTexts.MultiplayerErrorBannedByAdminsWithDialog),
buttonType: MyMessageBoxButtonsType.YES_NO);
messageBox.ResultCallback = delegate(MyGuiScreenMessageBox.ResultEnum result)
{
if (result == MyGuiScreenMessageBox.ResultEnum.YES)
{
MySteam.API.OpenOverlayUser(admin);
};
};
MyGuiSandbox.AddScreen(messageBox);
}
else
{
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: MyTexts.Get(MyCommonTexts.MultiplayerErrorBannedByAdmins)));
}
}
else
{
MyStringId resultText = MyCommonTexts.MultiplayerErrorConnectionFailed;
switch (msg.JoinResult)
{
case JoinResult.AlreadyJoined:
resultText = MyCommonTexts.MultiplayerErrorAlreadyJoined;
break;
case JoinResult.ServerFull:
resultText = MyCommonTexts.MultiplayerErrorServerFull;
break;
case JoinResult.SteamServersOffline:
resultText = MyCommonTexts.MultiplayerErrorSteamServersOffline;
break;
case JoinResult.TicketInvalid:
resultText = MyCommonTexts.MultiplayerErrorTicketInvalid;
break;
case JoinResult.GroupIdInvalid:
resultText = MyCommonTexts.MultiplayerErrorGroupIdInvalid;
break;
case JoinResult.TicketCanceled:
resultText = MyCommonTexts.MultiplayerErrorTicketCanceled;
break;
case JoinResult.TicketAlreadyUsed:
resultText = MyCommonTexts.MultiplayerErrorTicketAlreadyUsed;
break;
case JoinResult.LoggedInElseWhere:
resultText = MyCommonTexts.MultiplayerErrorLoggedInElseWhere;
break;
case JoinResult.NoLicenseOrExpired:
resultText = MyCommonTexts.MultiplayerErrorNoLicenseOrExpired;
break;
case JoinResult.UserNotConnected:
resultText = MyCommonTexts.MultiplayerErrorUserNotConnected;
break;
case JoinResult.VACBanned:
resultText = MyCommonTexts.MultiplayerErrorVACBanned;
break;
case JoinResult.VACCheckTimedOut:
resultText = MyCommonTexts.MultiplayerErrorVACCheckTimedOut;
break;
default:
System.Diagnostics.Debug.Fail("Unknown JoinResult");
break;
}
Dispose();
MyGuiScreenMainMenu.UnloadAndExitToMenu();
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: MyTexts.Get(resultText)));
return;
}
}
public void LoadMembersFromWorld(List<MyObjectBuilder_Client> clients)
{
if (clients == null) return;
foreach (var client in clients)
{
Debug.Assert(!m_members.Contains(client.SteamId), "Member already present!");
Debug.Assert(!m_memberData.ContainsKey(client.SteamId), "Member already present!");
m_memberData.Add(client.SteamId, new MyConnectedClientData() { Name = client.Name, IsAdmin = client.IsAdmin });
RaiseClientJoined(client.SteamId);
}
}
public override string GetMemberName(ulong steamUserID)
{
MyConnectedClientData clientData;
m_memberData.TryGetValue(steamUserID, out clientData);
return clientData.Name;
}
protected void OnConnectedClient(ref ConnectedClientDataMsg msg)
{
MySandboxGame.Log.WriteLineAndConsole("Client connected: " + msg.Name + " (" + msg.SteamID.ToString() + ")");
MyTrace.Send(TraceWindow.Multiplayer, "Client connected");
if (MySandboxGame.IsGameReady && msg.SteamID != ServerId && Sync.MyId != msg.SteamID && msg.Join)
{
var clientConnected = new MyHudNotification(MyCommonTexts.NotificationClientConnected, 5000, level: MyNotificationLevel.Important);
clientConnected.SetTextFormatArguments(msg.Name);
MyHud.Notifications.Add(clientConnected);
}
m_memberData[msg.SteamID] = new MyConnectedClientData()
{
Name = msg.Name,
IsAdmin = msg.IsAdmin
};
RaiseClientJoined(msg.SteamID);
}
public void SendPlayerData(string clientName)
{
ConnectedClientDataMsg msg = new ConnectedClientDataMsg();
msg.SteamID = Sync.MyId;
msg.Name = clientName;
msg.Join = true;
var buffer = new byte[1024];
uint length;
uint ticketHandle; // TODO: Store handle and end auth session on end
if (!MySteam.API.GetAuthSessionTicket(out ticketHandle, buffer, out length))
{
MyGuiScreenMainMenu.UnloadAndExitToMenu();
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
messageText: MyTexts.Get(MyCommonTexts.MultiplayerErrorConnectionFailed)));
return;
}
msg.Token = new byte[length];
Array.Copy(buffer, msg.Token, length);
ReplicationLayer.SendClientConnected(ref msg);
}
protected override void OnClientKick(ref MyControlKickClientMsg data, ulong sender)
{
if (data.KickedClient == Sync.MyId)
{
// We don't want to send disconnect message because the clients will disconnect the client automatically upon receiving on the MyControlKickClientMsg
m_clientJoined = false;
Dispose();
MyGuiScreenMainMenu.ReturnToMainMenu();
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionKicked),
messageText: MyTexts.Get(MyCommonTexts.MessageBoxTextYouHaveBeenKicked)));
}
else
{
AddKickedClient(data.KickedClient);
RaiseClientLeft(data.KickedClient, ChatMemberStateChangeEnum.Kicked);
}
}
protected override void OnClientBan(ref MyControlBanClientMsg data, ulong sender)
{
if (data.BannedClient == Sync.MyId && data.Banned == true)
{
// We don't want to send disconnect message because the clients will disconnect the client automatically upon receiving on the MyControlBanClientMsg
m_clientJoined = false;
Dispose();
MyGuiScreenMainMenu.ReturnToMainMenu();
MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionKicked),
messageText: MyTexts.Get(MyCommonTexts.MessageBoxTextYouHaveBeenBanned)));
}
else
{
if (data.Banned) AddBannedClient(data.BannedClient);
else RemoveBannedClient(data.BannedClient);
if (m_members.Contains(data.BannedClient) && data.Banned == true)
{
RaiseClientLeft(data.BannedClient, ChatMemberStateChangeEnum.Banned);
}
}
}
protected override void OnPing(ref MyControlPingMsg data, ulong sender)
{
MyNetworkStats.Static.OnPingSuccess();
}
public override void StartProcessingClientMessagesWithEmptyWorld()
{
// Add server client - needed for processing messages, before StartProcessingClientMessages
if (!Sync.Clients.HasClient(ServerId))
Sync.Clients.AddClient(ServerId);
base.StartProcessingClientMessagesWithEmptyWorld();
// Set local client before LoadDataComponents.
if (Sync.Clients.LocalClient == null)
Sync.Clients.SetLocalSteamId(Sync.MyId, createLocalClient: !Sync.Clients.HasClient(Sync.MyId));
}
public override void OnAllMembersData(ref AllMembersDataMsg msg)
{
// Setup members and clients
if (msg.Clients != null)
{
foreach (var client in msg.Clients)
{
if (!m_memberData.ContainsKey(client.SteamId))
m_memberData.Add(client.SteamId, new MyConnectedClientData() { Name = client.Name, IsAdmin = client.IsAdmin });
if (!m_members.Contains(client.SteamId))
m_members.Add(client.SteamId);
if (!Sync.Clients.HasClient(client.SteamId))
Sync.Clients.AddClient(client.SteamId);
}
}
// Setup identities, players, factions
ProcessAllMembersData(ref msg);
}
}
}