/// <summary> /// Called once we have connected to the server, /// at this point we want to send a connect request packet /// to the server /// </summary> private void ListenerOnPeerConnectedEvent(NetPeer peer) { // Get the major.minor version Version version = Assembly.GetAssembly(typeof(Client)).GetName().Version; string versionString = $"{version.Major}.{version.Minor}"; // Build the connection request ConnectionRequestCommand connectionRequest = new ConnectionRequestCommand { GameVersion = BuildConfig.applicationVersion, ModCount = PluginManager.instance.modCount, ModVersion = versionString, Password = Config.Password, Username = Config.Username, DLCBitMask = DLCHelper.GetOwnedDLCs() }; _logger.Info("Sending connection request to server..."); Command.SendToServer(connectionRequest); }
protected override void Handle(ConnectionResultCommand command) { // We only want this message while connecting if (MultiplayerManager.Instance.CurrentClient.Status != ClientStatus.Connecting) { return; } // If we are allowed to connect if (command.Success) { // Log and set that we are connected. Log.Info("Successfully connected to server. Downloading world..."); MultiplayerManager.Instance.CurrentClient.ClientPlayer = new Player(); MultiplayerManager.Instance.CurrentClient.Status = ClientStatus.Downloading; MultiplayerManager.Instance.CurrentClient.ClientId = command.ClientId; } else { Log.Info($"Could not connect: {command.Reason}"); MultiplayerManager.Instance.CurrentClient.ConnectionMessage = command.Reason; MultiplayerManager.Instance.CurrentClient.Disconnect(); if (command.Reason.Contains("DLC")) // No other way to detect if we should display the box { DLCHelper.DLCComparison compare = DLCHelper.Compare(command.DLCBitMask, DLCHelper.GetOwnedDLCs()); ThreadHelper.dispatcher.Dispatch(() => { MessagePanel panel = PanelManager.ShowPanel <MessagePanel>(); panel.DisplayDlcMessage(compare); }); } } }
public void HandleOnServer(ConnectionRequestCommand command, NetPeer peer) { Log.Info("Received connection request."); string joiningVersion = command.GameVersion; string appVersion = BuildConfig.applicationVersion; MatchVersionString(ref joiningVersion); MatchVersionString(ref appVersion); // Check to see if the game versions match if (joiningVersion != appVersion) { Log.Info($"Connection rejected: Game versions {joiningVersion} (client) and {appVersion} (server) differ."); Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = $"Client and server have different game versions. Client: {joiningVersion}, Server: {appVersion}." }); return; } // Check to see if the mod version matches Version version = Assembly.GetAssembly(typeof(Client)).GetName().Version; string versionString = $"{version.Major}.{version.Minor}"; if (command.ModVersion != versionString) { Log.Info($"Connection rejected: Mod versions {command.ModVersion} (client) and {versionString} (server) differ."); Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = $"Client and server have different CSM Mod versions. Client: {command.ModVersion}, Server: {versionString}." }); return; } // Check the client username to see if anyone on the server already have a username bool hasExistingPlayer = MultiplayerManager.Instance.PlayerList.Contains(command.Username); if (hasExistingPlayer) { Log.Info($"Connection rejected: Username {command.Username} already in use."); Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "This username is already in use." }); return; } // Check the password to see if it matches (only if the server has provided a password). if (!string.IsNullOrEmpty(MultiplayerManager.Instance.CurrentServer.Config.Password)) { if (command.Password != MultiplayerManager.Instance.CurrentServer.Config.Password) { Log.Warn("Connection rejected: Invalid password provided!"); Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "Invalid password for this server." }); return; } } SteamHelper.DLC_BitMask dlcMask = DLCHelper.GetOwnedDLCs(); // Check both client have the same DLCs enabled if (!command.DLCBitMask.Equals(dlcMask)) { Log.Info($"Connection rejected: DLC bit mask {command.DLCBitMask} (client) and {dlcMask} (server) differ."); Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "DLCs don't match", DLCBitMask = dlcMask }); return; } // Check that no other player is currently connecting bool clientJoining = false; foreach (Player p in MultiplayerManager.Instance.CurrentServer.ConnectedPlayers.Values) { if (p.Status != ClientStatus.Connected) { clientJoining = true; } } if (clientJoining) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "A client is already joining", DLCBitMask = dlcMask }); return; } // Add the new player as a connected player Player newPlayer = new Player(peer, command.Username); MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[peer.Id] = newPlayer; // Send the result command Command.SendToClient(peer, new ConnectionResultCommand { Success = true, ClientId = peer.Id }); PrepareWorldLoad(newPlayer); MultiplayerManager.Instance.CurrentServer.HandlePlayerConnect(newPlayer); }
protected override void Handle(ConnectionResultCommand command) { // We only want this message while connecting if (MultiplayerManager.Instance.CurrentClient.Status != ClientStatus.Connecting) { return; } // If we are allowed to connect if (command.Success) { // Log and set that we are connected. _logger.Info("Successfully connected to server. Downloading world..."); MultiplayerManager.Instance.CurrentClient.Status = ClientStatus.Downloading; MultiplayerManager.Instance.CurrentClient.ClientId = command.ClientId; } else { _logger.Info($"Could not connect: {command.Reason}"); MultiplayerManager.Instance.CurrentClient.ConnectionMessage = command.Reason; MultiplayerManager.Instance.CurrentClient.Disconnect(); if (command.DLCBitMask != SteamHelper.DLC_BitMask.None) { DLCHelper.DLCComparison compare = DLCHelper.Compare(command.DLCBitMask, DLCHelper.GetOwnedDLCs()); if (compare.ClientMissing != SteamHelper.DLC_BitMask.None) { ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Error, $"You are missing the following DLCs: {compare.ClientMissing}"); } if (compare.ServerMissing != SteamHelper.DLC_BitMask.None) { ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Error, $"The server doesn't have the following DLCs: {compare.ServerMissing}"); } ChatLogPanel.PrintGameMessage(ChatLogPanel.MessageType.Normal, "DLCs can be disabled via checkbox in Steam"); } } }
public static void CreateOrUpdateMultiplayerButton() { Log.Info("Creating multiplayer button..."); // Find pause menu view. UIPanel pauseUiPanel = UIView.GetAView()?.FindUIComponent("Menu") as UIPanel; if (pauseUiPanel == null) { return; } // Find button. UIButton _multiplayerButton = UIView.GetAView().FindUIComponent("Multiplayer") as UIButton; // Find divider. UIPanel _multiplayerDivider = UIView.GetAView().FindUIComponent("multiplayerDivider") as UIPanel; // Add the button & divider if it does not exist and assign // the click event. if (_multiplayerButton == null) { // Add multiplayer button. _multiplayerButton = (UIButton)pauseUiPanel.AddUIComponent(typeof(UIButton)); // Add multiplayer divider. _multiplayerDivider = (UIPanel)pauseUiPanel.AddUIComponent(typeof(UIPanel)); // Respond to button click. _multiplayerButton.eventClick += (component, param) => { // Close pause menu. ReflectionHelper.Call(new PauseMenu(), "Resume"); // Open host game menu if not in multiplayer session, else open connection panel if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.None) { PanelManager.TogglePanel <HostGamePanel>(); // Display warning if DLCs or other mods are enabled if (DLCHelper.GetOwnedDLCs() != SteamHelper.DLC_BitMask.None || Singleton <PluginManager> .instance.enabledModCount > 1) { MessagePanel msgPanel = PanelManager.ShowPanel <MessagePanel>(); msgPanel.DisplayContentWarning(); } } else { PanelManager.TogglePanel <ConnectionPanel>(); } }; } // Set multiplayer button properties. _multiplayerButton.name = "Multiplayer"; _multiplayerButton.text = "MULTIPLAYER"; _multiplayerButton.width = 310; _multiplayerButton.height = 57; _multiplayerButton.textScale = 1.25f; _multiplayerButton.normalBgSprite = "ButtonMenu"; _multiplayerButton.disabledBgSprite = "ButtonMenuDisabled"; _multiplayerButton.hoveredBgSprite = "ButtonMenuHovered"; _multiplayerButton.focusedBgSprite = "ButtonMenuFocused"; _multiplayerButton.pressedBgSprite = "ButtonMenuPressed"; _multiplayerButton.textColor = new Color32(255, 255, 255, 255); _multiplayerButton.disabledTextColor = new Color32(7, 7, 7, 255); _multiplayerButton.hoveredTextColor = new Color32(7, 132, 255, 255); _multiplayerButton.focusedTextColor = new Color32(255, 255, 255, 255); _multiplayerButton.pressedTextColor = new Color32(30, 30, 44, 255); // Set multiplayer divider properties. _multiplayerDivider.height = 7; _multiplayerDivider.name = "multiplayerDivider"; // Enable button sounds. _multiplayerButton.playAudioEvents = true; }
public override void OnLevelLoaded(LoadMode mode) { base.OnLevelLoaded(mode); if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.Client) { MultiplayerManager.Instance.CurrentClient.Status = ClientStatus.Connected; Command.SendToServer(new ClientLevelLoadedCommand()); } UIView uiView = UIView.GetAView(); // Add the chat log uiView.AddUIComponent(typeof(ChatLogPanel)); _multiplayerButton = (UIButton)uiView.AddUIComponent(typeof(UIButton)); _multiplayerButton.text = "Multiplayer"; _multiplayerButton.width = 150; _multiplayerButton.height = 40; _multiplayerButton.normalBgSprite = "ButtonMenu"; _multiplayerButton.disabledBgSprite = "ButtonMenuDisabled"; _multiplayerButton.hoveredBgSprite = "ButtonMenuHovered"; _multiplayerButton.focusedBgSprite = "ButtonMenuFocused"; _multiplayerButton.pressedBgSprite = "ButtonMenuPressed"; _multiplayerButton.textColor = new Color32(255, 255, 255, 255); _multiplayerButton.disabledTextColor = new Color32(7, 7, 7, 255); _multiplayerButton.hoveredTextColor = new Color32(7, 132, 255, 255); _multiplayerButton.focusedTextColor = new Color32(255, 255, 255, 255); _multiplayerButton.pressedTextColor = new Color32(30, 30, 44, 255); // Enable button sounds. _multiplayerButton.playAudioEvents = true; // Place the button. _multiplayerButton.transformPosition = new Vector3(-1.45f, 0.97f); // Respond to button click. _multiplayerButton.eventClick += (component, param) => { // Open host game menu if not in multiplayer session, else open connection panel if (MultiplayerManager.Instance.CurrentRole == MultiplayerRole.None) { PanelManager.TogglePanel <HostGamePanel>(); // Display warning if DLCs or other mods are enabled if (DLCHelper.GetOwnedDLCs() != SteamHelper.DLC_BitMask.None || Singleton <PluginManager> .instance.enabledModCount > 1) { MessagePanel msgPanel = PanelManager.ShowPanel <MessagePanel>(); msgPanel.DisplayContentWarning(); } } else { PanelManager.TogglePanel <ConnectionPanel>(); } _multiplayerButton.Unfocus(); }; }
public void HandleOnServer(ConnectionRequestCommand command, NetPeer peer) { // Check to see if the game versions match if (command.GameVersion != BuildConfig.applicationVersion) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = $"Client and server have different game versions. Client: {command.GameVersion}, Server: {BuildConfig.applicationVersion}." }); return; } // Check to see if the mod version matches Version version = Assembly.GetAssembly(typeof(Client)).GetName().Version; string versionString = $"{version.Major}.{version.Minor}"; if (command.ModVersion != versionString) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = $"Client and server have different CSM Mod versions. Client: {command.ModVersion}, Server: {versionString}." }); return; } // Check the client username to see if anyone on the server already have a username bool hasExistingPlayer = MultiplayerManager.Instance.PlayerList.Contains(command.Username); if (hasExistingPlayer) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "This username is already in use." }); return; } // Check the password to see if it matches (only if the server has provided a password). if (!string.IsNullOrEmpty(MultiplayerManager.Instance.CurrentServer.Config.Password)) { if (command.Password != MultiplayerManager.Instance.CurrentServer.Config.Password) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "Invalid password for this server." }); return; } } // Check both client have the same DLCs enabled if (!command.DLCBitMask.Equals(DLCHelper.GetOwnedDLCs())) { Command.SendToClient(peer, new ConnectionResultCommand { Success = false, Reason = "DLCs don't match", DLCBitMask = DLCHelper.GetOwnedDLCs() }); return; } // Add the new player as a connected player Player newPlayer = new Player(peer, command.Username); MultiplayerManager.Instance.CurrentServer.ConnectedPlayers[peer.Id] = newPlayer; // Get a serialized version of the server world to send to the player. if (command.RequestWorld) { // Get the world byte[] world = WorldManager.GetWorld(); // Send the result command Command.SendToClient(peer, new ConnectionResultCommand { Success = true, ClientId = peer.Id, World = world }); } else { // Send the result command Command.SendToClient(peer, new ConnectionResultCommand { Success = true, ClientId = peer.Id }); } MultiplayerManager.Instance.CurrentServer.HandlePlayerConnect(newPlayer); }