public static void JoinGame(Lobby lobby, bool requestData = true)
        {
            // Data not received
            if (requestData && String.IsNullOrEmpty(lobby.GetLobbyData(MyMultiplayer.AppVersionTag)))
            {
                var helper = new MyLobbyHelper(lobby);
                helper.OnSuccess += (l) => JoinGame(l, false);
                if (helper.RequestData())
                    return;
            }

            if (!JoinGameTest(lobby))
                return;

            if (MyMultiplayerLobby.GetLobbyScenario(lobby))
            {
                MyJoinGameHelper.JoinScenarioGame(lobby.LobbyId);
            }
            else if (MyFakes.ENABLE_BATTLE_SYSTEM && MyMultiplayerLobby.GetLobbyBattle(lobby))
            {
                bool canBeJoined = MyMultiplayerLobby.GetLobbyBattleCanBeJoined(lobby);
                // Check also valid faction ids in battle lobby.
                long faction1Id = MyMultiplayerLobby.GetLobbyBattleFaction1Id(lobby);
                long faction2Id = MyMultiplayerLobby.GetLobbyBattleFaction2Id(lobby);

                if (canBeJoined && faction1Id != 0 && faction2Id != 0)
                    MyJoinGameHelper.JoinBattleGame(lobby.LobbyId);
            }
            else
            {
                JoinGame(lobby.LobbyId);
            }
        }
        private static bool JoinGameTest(Lobby lobby)
        {
            if (!lobby.IsValid)
                return false;

            if (lobby.GetLobbyType() == LobbyTypeEnum.FriendsOnly && !MySteam.API.Friends.HasFriend(lobby.GetOwner()))
            {
                MyGuiSandbox.Show(MyCommonTexts.OnlyFriendsCanJoinThisGame);
                return false;
            }
            if (!MyMultiplayerLobby.IsLobbyCorrectVersion(lobby))
            {
                var formatString = MyTexts.GetString(MyCommonTexts.MultiplayerError_IncorrectVersion);
                var myVersion = MyBuildNumbers.ConvertBuildNumberFromIntToString(MyFinalBuildConstants.APP_VERSION);
                var serverVersion = MyBuildNumbers.ConvertBuildNumberFromIntToString(MyMultiplayerLobby.GetLobbyAppVersion(lobby));
                MyGuiSandbox.Show(new StringBuilder(String.Format(formatString, myVersion, serverVersion)));
                return false;
            }
            if (MyFakes.ENABLE_MP_DATA_HASHES && !MyMultiplayerLobby.HasSameData(lobby))
            {
                MyGuiSandbox.Show(MyCommonTexts.MultiplayerError_DifferentData);
                MySandboxGame.Log.WriteLine("Different game data when connecting to server. Local hash: " + MyDataIntegrityChecker.GetHashBase64() + ", server hash: " + MyMultiplayerLobby.GetDataHash(lobby));
                return false;
            }
            return true;
        }
 void JoinGame_LobbyUpdate(bool success, Lobby lobby, ulong memberOrLobby)
 {
     if (lobby.LobbyId == m_lobby.LobbyId)
     {
         SteamAPI.Instance.Matchmaking.LobbyDataUpdate -= m_dataUpdateHandler;
         var handler = OnSuccess;
         if (handler != null) handler(lobby);
     }
 }
        public static void JoinBattleGame(Lobby lobby, bool requestData = true)
        {
            // Data not received
            if (requestData && String.IsNullOrEmpty(lobby.GetLobbyData(MyMultiplayer.AppVersionTag)))
            {
                var helper = new MyLobbyHelper(lobby);
                helper.OnSuccess += (l) => JoinBattleGame(l, false);
                if (helper.RequestData())
                    return;
            }

            if (!JoinGameTest(lobby))
                return;

            JoinBattleGame(lobby.LobbyId);
        }
 public static bool GetLobbyBattleCanBeJoined(Lobby lobby)
 {
     return GetLobbyBool(MyMultiplayer.BattleCanBeJoinedTag, lobby, false);
 }
 public static bool GetLobbyBattle(Lobby lobby)
 {
     return GetLobbyBool(MyMultiplayer.BattleTag, lobby, false);
 }
 public static bool GetLobbyScenario(Lobby lobby)
 {
     return GetLobbyBool(MyMultiplayer.ScenarioTag, lobby, false);
 }
        public static List<MyObjectBuilder_Checkpoint.ModItem> GetLobbyMods(Lobby lobby)
        {
            var modsCount = GetLobbyModCount(lobby);
            var mods = new List<MyObjectBuilder_Checkpoint.ModItem>(modsCount);
            for (int i = 0; i < modsCount; ++i)
            {
                string modInfo = lobby.GetLobbyData(MyMultiplayer.ModItemTag + i);

                var index = modInfo.IndexOf("_");
                if (index != -1)
                {
                    ulong publishedFileId = 0;
                    ulong.TryParse(modInfo.Substring(0, index), out publishedFileId);
                    var name = modInfo.Substring(index + 1);
                    mods.Add(new MyObjectBuilder_Checkpoint.ModItem(name, publishedFileId, name));
                }
                else
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to parse mod details from LobbyData. '{0}'", modInfo));
                }

            }
            return mods;
        }
        public static bool HasSameData(Lobby lobby)
        {
            string remoteHash = GetDataHash(lobby);

            // If the data hash is not set, the server does not want to check the data consistency
            if (remoteHash == "") return true;

            if (remoteHash == MyDataIntegrityChecker.GetHashBase64()) return true;

            return false;
        }
 public static int GetLobbyAppVersion(Lobby lobby)
 {
     int result;
     return int.TryParse(lobby.GetLobbyData(MyMultiplayer.AppVersionTag), out result) ? result : 0;
 }
        public static ulong GetLobbyWorldSize(Lobby lobby)
        {
            var s = lobby.GetLobbyData(MyMultiplayer.WorldSizeTag);
            if (!string.IsNullOrEmpty(s))
                return Convert.ToUInt64(s);

            return 0;
        }
        internal MyMultiplayerLobby(Lobby lobby, MySyncLayer syncLayer)
            : base(syncLayer, new EndpointId(Sync.MyId))
        {
            Lobby = lobby;
            ServerId = Lobby.GetOwner();

            SyncLayer.RegisterClientEvents(this);

            HostName = MySteam.UserName;
           
            Debug.Assert(IsServer, "Wrong object created");

            MySteam.API.Matchmaking.LobbyChatUpdate += Matchmaking_LobbyChatUpdate;
            MySteam.API.Matchmaking.LobbyChatMsg += Matchmaking_LobbyChatMsg;
            ClientLeft += MyMultiplayerLobby_ClientLeft;
            AcceptMemberSessions();
        }
        void Matchmaking_LobbyChatUpdate(Lobby lobby, ulong changedUser, ulong makingChangeUser, ChatMemberStateChangeEnum stateChange)
        {
            //System.Diagnostics.Debug.Assert(MySession.Static != null);

            if (lobby.LobbyId == Lobby.LobbyId)
            {
                if (stateChange == ChatMemberStateChangeEnum.Entered)
                {
                    MySandboxGame.Log.WriteLineAndConsole("Player entered: " + MySteam.API.Friends.GetPersonaName(changedUser) + " (" + changedUser + ")");
                    MyTrace.Send(TraceWindow.Multiplayer, "Player entered");
                    Peer2Peer.AcceptSession(changedUser);

                    RaiseClientJoined(changedUser);

                    if (MySandboxGame.IsGameReady && changedUser != ServerId)
                    {
                        var playerJoined = new MyHudNotification(MySpaceTexts.NotificationClientConnected, 5000, level: MyNotificationLevel.Important);
                        playerJoined.SetTextFormatArguments(MySteam.API.Friends.GetPersonaName(changedUser));
                        MyHud.Notifications.Add(playerJoined);
                    }
                }
                else
                {
                    // Kicked client can be already removed from Clients
                    if (Sync.Clients.HasClient(changedUser))
                        RaiseClientLeft(changedUser, stateChange);

                    if (changedUser == ServerId)
                    {
                        MyTrace.Send(TraceWindow.Multiplayer, "Host left: " + stateChange.ToString());
                        RaiseHostLeft();

                        MyGuiScreenMainMenu.UnloadAndExitToMenu();
                        MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
                            messageCaption: MyTexts.Get(MySpaceTexts.MessageBoxCaptionError),
                            messageText: MyTexts.Get(MySpaceTexts.MultiplayerErrorServerHasLeft)));

                        // Set new server
                        //ServerId = Lobby.GetOwner();

                        //if (ServerId == Sync.MyId)
                        //{
                        //    Lobby.SetLobbyData(HostNameTag, Sync.MyName);
                        //}
                    }
                    else if (MySandboxGame.IsGameReady)
                    {
                        var playerLeft = new MyHudNotification(MySpaceTexts.NotificationClientDisconnected, 5000, level: MyNotificationLevel.Important);
                        playerLeft.SetTextFormatArguments(MySteam.API.Friends.GetPersonaName(changedUser));
                        MyHud.Notifications.Add(playerLeft);
                    }
                }
            }
        }
        internal MyMultiplayerLobby(Lobby lobby, MySyncLayer syncLayer)
            : base(syncLayer)
        {
            Lobby = lobby;
            ServerId = Lobby.GetOwner();

            SyncLayer.RegisterClientEvents(this);

            if (IsServer)
            {
                HostName = MySteam.UserName;
            }
            else
            {
                SyncLayer.TransportLayer.IsBuffering = true;
            }
            MySteam.API.Matchmaking.LobbyChatUpdate += Matchmaking_LobbyChatUpdate;
            MySteam.API.Matchmaking.LobbyChatMsg += Matchmaking_LobbyChatMsg;
            ClientLeft += MyMultiplayerLobby_ClientLeft;
            AcceptMemberSessions();
        }
        void Matchmaking_LobbyChatUpdate(Lobby lobby, ulong changedUser, ulong makingChangeUser, ChatMemberStateChangeEnum stateChange)
        {
            //System.Diagnostics.Debug.Assert(MySession.Static != null);

            if (lobby.LobbyId == Lobby.LobbyId)
            {
                if (stateChange == ChatMemberStateChangeEnum.Entered)
                {
                    MySandboxGame.Log.WriteLineAndConsole("Player entered: " + MySteam.API.Friends.GetPersonaName(changedUser) + " (" + changedUser + ")");
                    MyTrace.Send(TraceWindow.Multiplayer, "Player entered");
                    Peer2Peer.AcceptSession(changedUser);

                    // When some clients connect at the same time then some of them can have already added clients 
                    // (see function MySyncLayer.RegisterClientEvents which registers all Members in Lobby).
                    if (Sync.Clients == null || !Sync.Clients.HasClient(changedUser))
                    {
                        RaiseClientJoined(changedUser);

                        // Battles - send all clients, identities, players, factions as first message to client
                        if ((Battle || Scenario) && changedUser != Sync.MyId)
                        {
                            SendAllMembersDataToClient(changedUser);
                        }
                    }

                    if (MySandboxGame.IsGameReady && changedUser != ServerId)
                    {
                        // Player is able to connect to the battle which already started - player is then kicked and we do not want to show connected message in HUD.
                        bool showMsg = true;
                        if (MyFakes.ENABLE_BATTLE_SYSTEM && MySession.Static != null && MySession.Static.Battle && !BattleCanBeJoined)
                            showMsg = false;

                        if (showMsg)
                        {
                            var playerJoined = new MyHudNotification(MyCommonTexts.NotificationClientConnected, 5000, level: MyNotificationLevel.Important);
                            playerJoined.SetTextFormatArguments(MySteam.API.Friends.GetPersonaName(changedUser));
                            MyHud.Notifications.Add(playerJoined);
                        }
                    }
                }
                else
                {
                    // Kicked client can be already removed from Clients
                    if (Sync.Clients == null || Sync.Clients.HasClient(changedUser))
                        RaiseClientLeft(changedUser, stateChange);

                    if (changedUser == ServerId)
                    {
                        MyTrace.Send(TraceWindow.Multiplayer, "Host left: " + stateChange.ToString());
                        RaiseHostLeft();

                        MyGuiScreenMainMenu.UnloadAndExitToMenu();
                        MyGuiSandbox.AddScreen(MyGuiSandbox.CreateMessageBox(
                            messageCaption: MyTexts.Get(MyCommonTexts.MessageBoxCaptionError),
                            messageText: MyTexts.Get(MyCommonTexts.MultiplayerErrorServerHasLeft)));

                        // Set new server
                        //ServerId = Lobby.GetOwner();

                        //if (ServerId == Sync.MyId)
                        //{
                        //    Lobby.SetLobbyData(HostNameTag, Sync.MyName);
                        //}
                    }
                    else if (MySandboxGame.IsGameReady)
                    {
                        var playerLeft = new MyHudNotification(MyCommonTexts.NotificationClientDisconnected, 5000, level: MyNotificationLevel.Important);
                        playerLeft.SetTextFormatArguments(MySteam.API.Friends.GetPersonaName(changedUser));
                        MyHud.Notifications.Add(playerLeft);
                    }
                }
            }
        }
 public static bool GetLobbyBool(string key, Lobby lobby, bool defValue)
 {
     bool val;
     if (bool.TryParse(lobby.GetLobbyData(key), out val))
         return val;
     else
         return defValue;
 }
 void Matchmaking_LobbyChatMsg(Lobby lobby, ulong steamUserID, byte chatEntryTypeIn, uint chatID)
 {
     string messageText;
     ChatEntryTypeEnum chatEntryType;
     GetChatMessage((int)chatID, out messageText, out chatEntryType);
     RaiseChatMessageReceived(steamUserID, messageText, chatEntryType);
 }
 public static string GetLobbyHostName(Lobby lobby)
 {
     return lobby.GetLobbyData(MyMultiplayer.HostNameTag);
 }
 public static bool IsLobbyCorrectVersion(Lobby lobby)
 {
     return GetLobbyAppVersion(lobby) == MyFinalBuildConstants.APP_VERSION;
 }
 public static string GetDataHash(Lobby lobby)
 {
     return lobby.GetLobbyData(MyMultiplayer.DataHashTag);
 }
 public static MyGameModeEnum GetLobbyGameMode(Lobby lobby)
 {
     int val;
     if (int.TryParse(lobby.GetLobbyData(MyMultiplayer.GameModeTag), out val))
         return (MyGameModeEnum)val;
     else
         return MyGameModeEnum.Creative;
 }
 public static int GetLobbyModCount(Lobby lobby)
 {
     return GetLobbyInt(MyMultiplayer.ModCountTag, lobby, 0);
 }
 public static float GetLobbyFloat(string key, Lobby lobby, float defValue)
 {
     float val;
     if (float.TryParse(lobby.GetLobbyData(key), NumberStyles.Float, CultureInfo.InvariantCulture, out val))
         return val;
     else
         return defValue;
 }
 public static int GetLobbyViewDistance(Lobby lobby)
 {
     return GetLobbyInt(MyMultiplayer.ViewDistanceTag, lobby, 20000);
 }
 public static int GetLobbyInt(string key, Lobby lobby, int defValue)
 {
     int val;
     if (int.TryParse(lobby.GetLobbyData(key), NumberStyles.Integer, CultureInfo.InvariantCulture, out val))
         return val;
     else
         return defValue;
 }
 public static string GetLobbyScenarioBriefing(Lobby lobby)
 {
     return lobby.GetLobbyData(MyMultiplayer.ScenarioBriefingTag);
 }
 public static DateTime GetLobbyDateTime(string key, Lobby lobby, DateTime defValue)
 {
     DateTime val;
     if (DateTime.TryParse(lobby.GetLobbyData(key), CultureInfo.InvariantCulture, DateTimeStyles.None, out val))
         return val;
     else
         return defValue;
 }
 public static float GetLobbyBattleRemainingTime(Lobby lobby)
 {
     return GetLobbyFloat(MyMultiplayer.BattleRemainingTimeTag, lobby, 0);
 }
 public static ulong GetLobbyULong(string key, Lobby lobby, ulong defValue)
 {
     ulong val;
     if (ulong.TryParse(lobby.GetLobbyData(key), out val))
         return val;
     else
         return defValue;
 }
 public static long GetLobbyBattleFaction2Id(Lobby lobby)
 {
     return GetLobbyLong(MyMultiplayer.BattleFaction2IdTag, lobby, 0);
 }
Exemple #31
0
        public static void Join(ulong lobbyId, CompletedDelegate <LobbyEnterInfo> callback)
        {
            Lobby lobbyId2 = new Lobby(lobbyId);

            lobbyId2.Join(callback);
        }