From 65459b609cbfa8e78b9092797d2ac770051729c6 Mon Sep 17 00:00:00 2001 From: Mushroom Date: Tue, 13 Apr 2021 20:00:17 +0100 Subject: [PATCH] Add player username syncing --- NebulaClient/MultiplayerClientSession.cs | 2 +- NebulaHost/MultiplayerHostSession.cs | 2 +- .../Session/HandshakeRequestProcessor.cs | 3 +++ NebulaModel/DataStructures/PlayerData.cs | 8 ++++++-- NebulaModel/Packets/Session/HandshakeRequest.cs | 4 +++- NebulaWorld/RemotePlayerModel.cs | 4 +++- NebulaWorld/SimulatedWorld.cs | 9 ++++----- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/NebulaClient/MultiplayerClientSession.cs b/NebulaClient/MultiplayerClientSession.cs index d6cb51955..c81082203 100644 --- a/NebulaClient/MultiplayerClientSession.cs +++ b/NebulaClient/MultiplayerClientSession.cs @@ -100,7 +100,7 @@ private void ClientSocket_OnOpen(object sender, System.EventArgs e) serverConnection = new NebulaConnection(clientSocket, serverEndpoint, PacketProcessor); IsConnected = true; //TODO: Maybe some challenge-response authentication mechanism? - SendPacket(new HandshakeRequest(CryptoUtils.GetPublicKey(CryptoUtils.GetOrCreateUserCert()))); + SendPacket(new HandshakeRequest(CryptoUtils.GetPublicKey(CryptoUtils.GetOrCreateUserCert()), GameMain.data.account.userName)); } private void ClientSocket_OnClose(object sender, CloseEventArgs e) diff --git a/NebulaHost/MultiplayerHostSession.cs b/NebulaHost/MultiplayerHostSession.cs index 52c761709..c8c401ab2 100644 --- a/NebulaHost/MultiplayerHostSession.cs +++ b/NebulaHost/MultiplayerHostSession.cs @@ -65,7 +65,7 @@ public void StartServer(int port, bool loadSaveFile = false) LocalPlayer.IsMasterClient = true; // TODO: Load saved player info here - LocalPlayer.SetPlayerData(new PlayerData(PlayerManager.GetNextAvailablePlayerId(), GameMain.localPlanet?.id ?? -1, new Float3(1.0f, 0.6846404f, 0.243137181f))); + LocalPlayer.SetPlayerData(new PlayerData(PlayerManager.GetNextAvailablePlayerId(), GameMain.localPlanet?.id ?? -1, new Float3(1.0f, 0.6846404f, 0.243137181f), AccountData.me.userName)); } private void StopServer() diff --git a/NebulaHost/PacketProcessors/Session/HandshakeRequestProcessor.cs b/NebulaHost/PacketProcessors/Session/HandshakeRequestProcessor.cs index 406e2a7fb..9a246ff7a 100644 --- a/NebulaHost/PacketProcessors/Session/HandshakeRequestProcessor.cs +++ b/NebulaHost/PacketProcessors/Session/HandshakeRequestProcessor.cs @@ -59,6 +59,9 @@ public void ProcessPacket(HandshakeRequest packet, NebulaConnection conn) playerManager.SavedPlayerData.Add(clientCertHash, player.Data); } + // Add the username to the player data + player.Data.Username = packet.Username; + // Make sure that each player that is currently in the game receives that a new player as join so they can create its RemotePlayerCharacter PlayerData pdata = player.Data.CreateCopyWithoutMechaData(); // Remove inventory from mecha data foreach (Player activePlayer in playerManager.GetConnectedPlayers()) diff --git a/NebulaModel/DataStructures/PlayerData.cs b/NebulaModel/DataStructures/PlayerData.cs index 27d857906..c6482103f 100644 --- a/NebulaModel/DataStructures/PlayerData.cs +++ b/NebulaModel/DataStructures/PlayerData.cs @@ -6,6 +6,7 @@ namespace NebulaModel.DataStructures [RegisterNestedType] public class PlayerData : INetSerializable { + public string Username { get; set; } public ushort PlayerId { get; set; } public int LocalPlanetId { get; set; } public Float3 Color { get; set; } @@ -16,10 +17,11 @@ public class PlayerData : INetSerializable public MechaData Mecha { get; set; } public PlayerData() { } - public PlayerData(ushort playerId, int localPlanetId, Float3 color, Float3 localPlanetPosition = new Float3(), Double3 position = new Double3(), Float3 rotation = new Float3(), Float3 bodyRotation = new Float3()) + public PlayerData(ushort playerId, int localPlanetId, Float3 color, string username = null, Float3 localPlanetPosition = new Float3(), Double3 position = new Double3(), Float3 rotation = new Float3(), Float3 bodyRotation = new Float3()) { PlayerId = playerId; LocalPlanetId = localPlanetId; + Username = username ?? $"Player {playerId}"; LocalPlanetPosition = localPlanetPosition; Color = color; UPosition = position; @@ -30,6 +32,7 @@ public PlayerData() { } public void Serialize(NetDataWriter writer) { + writer.Put(Username); writer.Put(PlayerId); writer.Put(LocalPlanetId); Color.Serialize(writer); @@ -42,6 +45,7 @@ public void Serialize(NetDataWriter writer) public void Deserialize(NetDataReader reader) { + Username = reader.GetString(); PlayerId = reader.GetUShort(); LocalPlanetId = reader.GetInt(); Color = reader.GetFloat3(); @@ -55,7 +59,7 @@ public void Deserialize(NetDataReader reader) public PlayerData CreateCopyWithoutMechaData() { - return new PlayerData(PlayerId, LocalPlanetId, Color, LocalPlanetPosition, UPosition, Rotation, BodyRotation); + return new PlayerData(PlayerId, LocalPlanetId, Color, Username, LocalPlanetPosition, UPosition, Rotation, BodyRotation); } } } diff --git a/NebulaModel/Packets/Session/HandshakeRequest.cs b/NebulaModel/Packets/Session/HandshakeRequest.cs index 5ccf59c15..0ae7b9571 100644 --- a/NebulaModel/Packets/Session/HandshakeRequest.cs +++ b/NebulaModel/Packets/Session/HandshakeRequest.cs @@ -2,14 +2,16 @@ { public class HandshakeRequest { + public string Username { get; set; } public string ModVersion { get; set; } public int GameVersionSig { get; set; } public byte[] ClientCert { get; set; } public HandshakeRequest() { } - public HandshakeRequest(byte[] clientCert) + public HandshakeRequest(byte[] clientCert, string username) { + this.Username = username; this.ModVersion = Config.ModVersion.ToString(); this.GameVersionSig = GameConfig.gameVersion.sig; this.ClientCert = clientCert; diff --git a/NebulaWorld/RemotePlayerModel.cs b/NebulaWorld/RemotePlayerModel.cs index e91933512..d799018e7 100644 --- a/NebulaWorld/RemotePlayerModel.cs +++ b/NebulaWorld/RemotePlayerModel.cs @@ -9,6 +9,7 @@ public class RemotePlayerModel { const int PLAYER_PROTO_ID = 1; + public string Username { get; } public ushort PlayerId { get; } public Transform PlayerTransform { get; set; } public Transform PlayerModelTransform { get; set; } @@ -22,7 +23,7 @@ public class RemotePlayerModel public Player PlayerInstance { get; set; } public Mecha MechaInstance { get; set; } - public RemotePlayerModel(ushort playerId) + public RemotePlayerModel(ushort playerId, string username) { // Spawn remote player model by cloning the player prefab and replacing local player script by remote player ones. string playerPrefabPath = LDB.players.Select(PLAYER_PROTO_ID).PrefabPath; @@ -53,6 +54,7 @@ public RemotePlayerModel(ushort playerId) MechaInstance.Init(PlayerInstance); PlayerId = playerId; + Username = username; } public void Destroy() diff --git a/NebulaWorld/SimulatedWorld.cs b/NebulaWorld/SimulatedWorld.cs index b5a79167f..d033d6d2e 100644 --- a/NebulaWorld/SimulatedWorld.cs +++ b/NebulaWorld/SimulatedWorld.cs @@ -7,7 +7,6 @@ using NebulaWorld.Factory; using NebulaWorld.MonoBehaviours.Remote; using NebulaWorld.Trash; -using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; @@ -80,7 +79,7 @@ public static void SpawnRemotePlayerModel(PlayerData playerData) { if (!remotePlayersModels.ContainsKey(playerData.PlayerId)) { - RemotePlayerModel model = new RemotePlayerModel(playerData.PlayerId); + RemotePlayerModel model = new RemotePlayerModel(playerData.PlayerId, playerData.Username); remotePlayersModels.Add(playerData.PlayerId, model); } @@ -333,7 +332,7 @@ public static void RenderPlayerNameTagsOnStarmap(UIStarmap starmap) { // Make an instance of the "Icarus" text to represent the other player name nameText = playerModel.StarmapNameText = GameObject.Instantiate(starmap_playerNameText, starmap_playerNameText.transform.parent); - nameText.text = $"Player {playerModel.PlayerId}"; + nameText.text = $"{ playerModel.Username }{ playerModel.PlayerId }"; nameText.gameObject.SetActive(true); // Make another copy of it, but just replace it with a point to represent their location @@ -351,7 +350,7 @@ public static void RenderPlayerNameTagsOnStarmap(UIStarmap starmap) adjustedVector = planet.uPosition; // Add the local position of the player - Vector3 localPlanetPosition = playerModel.Movement.GetLastPosition().LocalPlanetPosition.ToUnity(); + Vector3 localPlanetPosition = playerModel.Movement.GetLastPosition().LocalPlanetPosition.ToVector3(); adjustedVector += (VectorLF3)Maths.QRotate(planet.runtimeRotation, localPlanetPosition); // Scale as required @@ -423,7 +422,7 @@ public static void RenderPlayerNameTagsInGame() TextMesh textMesh = playerNameText.AddComponent(); // Set the text to be their name - textMesh.text = $"Player {playerModel.PlayerId}"; + textMesh.text = $"{ playerModel.Username }{ playerModel.PlayerId }"; // Align it to be centered below them textMesh.anchor = TextAnchor.UpperCenter; // Copy the font over from the sail indicator