From 332680a8eb1f4b9d3db4c8a1d61eb765e064937b Mon Sep 17 00:00:00 2001 From: AnloGames <7383an@gmail.com> Date: Mon, 19 Aug 2024 19:40:03 +0300 Subject: [PATCH] DESERIALIZATION-FIX-COMMIT --- ZoFo/GameCore/Client.cs | 11 +-- ZoFo/GameCore/GUI/SelectModeMenu.cs | 2 +- .../CollisionManager/CollisionManager.cs | 3 +- .../NetworkManager/ClientNetworkManager.cs | 84 +++++++++++++++++-- .../SerializableDTO/SerializablePoint.cs | 5 +- .../SerializableDTO/SerializableRectangle.cs | 1 - .../SerializableDTO/SerializableVector2.cs | 6 +- .../NetworkManager/ServerNetworkManager.cs | 9 +- .../NetworkManager/Updates/Datagramm.cs | 2 +- .../ServerToClient/UpdateGameObjectCreated.cs | 12 ++- .../ServerToClient/UpdateGameObjectDeleted.cs | 2 +- .../Updates/ServerToClient/UpdatePosition.cs | 3 +- .../ServerToClient/UpdateTileCreated.cs | 4 +- ZoFo/GameCore/Server.cs | 4 +- 14 files changed, 115 insertions(+), 33 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 120be4e..4a2d6e1 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -24,6 +24,7 @@ using ZoFo.GameCore.GUI; using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; using Newtonsoft.Json.Linq; using Newtonsoft.Json; namespace ZoFo.GameCore @@ -144,17 +145,17 @@ namespace ZoFo.GameCore { GameObject created_gameObject; if ((update as UpdateGameObjectCreated).GameObjectType == "EntittyForAnimationTests") - gameObjects.Add(new EntittyForAnimationTests((update as UpdateGameObjectCreated).position)); + gameObjects.Add(new EntittyForAnimationTests((update as UpdateGameObjectCreated).position.GetVector2())); if ((update as UpdateGameObjectCreated).GameObjectType == "Player") { - created_gameObject = new Player((update as UpdateGameObjectCreated).position); + created_gameObject = new Player((update as UpdateGameObjectCreated).position.GetVector2()); players.Add(created_gameObject as Player); gameObjects.Add(created_gameObject); } if ((update as UpdateGameObjectCreated).GameObjectType == "Ammo") - gameObjects.Add(new Ammo((update as UpdateGameObjectCreated).position)); + gameObjects.Add(new Ammo((update as UpdateGameObjectCreated).position.GetVector2())); if ((update as UpdateGameObjectCreated).GameObjectType == "Zombie") - gameObjects.Add(new Zombie((update as UpdateGameObjectCreated).position)); + gameObjects.Add(new Zombie((update as UpdateGameObjectCreated).position.GetVector2())); (gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity); @@ -169,7 +170,7 @@ namespace ZoFo.GameCore { var ent = FindEntityById(update.IdEntity); - ent.position = (update as UpdatePosition).NewPosition; + ent.position = (update as UpdatePosition).NewPosition.GetVector2(); DebugHUD.Instance.Log("newPosition " + ent.position); } } diff --git a/ZoFo/GameCore/GUI/SelectModeMenu.cs b/ZoFo/GameCore/GUI/SelectModeMenu.cs index e97d2df..b833447 100644 --- a/ZoFo/GameCore/GUI/SelectModeMenu.cs +++ b/ZoFo/GameCore/GUI/SelectModeMenu.cs @@ -45,7 +45,7 @@ public class SelectModeMenu : AbstractGUI AppManager.Instance.SetClient(client); server.CreateRoom(false); client.JoinYourself(server.MyIp.Port); - AppManager.Instance.ChangeState(GameState.HostPlaying); + //AppManager.Instance.ChangeState(GameState.HostPlaying); AppManager.Instance.SetGUI(new HUD()); //server.CreateRoom(1); diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index 25736fe..60e81b9 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -12,6 +12,7 @@ using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.Graphics; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; namespace ZoFo.GameCore.GameManagers.CollisionManager { @@ -104,7 +105,7 @@ namespace ZoFo.GameCore.GameManagers.CollisionManager entity.graphicsComponent.ObjectDrawRectangle.X = (int)entity.position.X; entity.graphicsComponent.ObjectDrawRectangle.Y = (int)entity.position.Y; - AppManager.Instance.server.AddData(new UpdatePosition() { NewPosition = entity.position, IdEntity = entity.Id }); + AppManager.Instance.server.AddData(new UpdatePosition() { NewPosition = new SerializableVector2(entity.position), IdEntity = entity.Id }); AppManager.Instance.debugHud.Set("testPos", entity.position.ToString()); //TODO remove entity.velocity = Vector2.Zero; } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index a6b1549..0f29333 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data.SqlTypes; @@ -10,6 +12,7 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; using ZoFo.GameCore.GameManagers.NetworkManager.Updates; +using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; namespace ZoFo.GameCore.GameManagers.NetworkManager @@ -47,7 +50,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { Datagramm Datagramm = new Datagramm(); Datagramm.updateDatas = updates; - byte[] bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(Datagramm)); //нужно сериализовать + byte[] bytes = Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(Datagramm)); //нужно сериализовать socket.SendTo(bytes, sendingEP); } @@ -60,7 +63,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void AnalyzeData(string data) { - Datagramm Dgramm = JsonSerializer.Deserialize(data); + JObject jObj = JsonConvert.DeserializeObject(data) as JObject; + JToken token = JToken.FromObject(jObj); + JToken updateDatas = token["updateDatas"]; + Datagramm Dgramm = new Datagramm(); + Dgramm.isImportant = token["isImportant"].ToObject(); + Dgramm.DatagrammId = token["DatagrammId"].ToObject(); + Dgramm.updateDatas = GetSentUpdates(token["updateDatas"]); if (Dgramm.isImportant) { if (Dgramm.DatagrammId == currentServerDatagrammId + 1) @@ -85,11 +94,72 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager } } + + public List GetSentUpdates(JToken updatesToken) + { + List data = new List(); + JArray updateDatas = updatesToken as JArray; + UpdateData update = new UpdateData(); + foreach (JObject token in updateDatas.Children()) + { + switch (token["UpdateType"].ToObject()) + { + case "UpdateAnimation": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateEntityHealth": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateGameEnded": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateGameObjectCreated": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateGameObjectDeleted": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateInteraction": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateInteractionReady": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateLoot": + update = token.ToObject(); + data.Add(update); + break; + case "UpdatePlayerParametrs": + update = token.ToObject(); + data.Add(update); + break; + case "UpdatePosition": + update = token.ToObject(); + data.Add(update); + break; + case "UpdateTileCreated": + update = token.ToObject(); + data.Add(update); + break; + + } + } + + return data; + } + public void SendAcknowledgement(int DatagrammId) { Datagramm Dgramm = new Datagramm() { DatagrammId = DatagrammId }; - string data = JsonSerializer.Serialize(Dgramm); + string data = System.Text.Json.JsonSerializer.Serialize(Dgramm); byte[] buffer = Encoding.UTF8.GetBytes(data); socket.SendTo(buffer, sendingEP); @@ -110,11 +180,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager } #endregion - public void StopConnection() - { - socket.Shutdown(SocketShutdown.Both); - socket.Close(); - } #region Join /// /// приложение пытается подключиться к комнате @@ -124,6 +189,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void JoinRoom(string ip, int port) // multyplayer { sendingEP = new IPEndPoint(IPAddress.Parse(ip), port); + SendData(); Thread listen = new Thread(StartListening); listen.IsBackground = true; @@ -173,7 +239,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager byte[] correctedBuffer = new byte[size]; Array.Copy(buffer, correctedBuffer, size); data = Encoding.UTF8.GetString(correctedBuffer); - AnalyzeData(data); + GetDataSent(data); } } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs index 4480a8c..8db620a 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs @@ -9,10 +9,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO { public class SerializablePoint { - public int X; - public int Y; + public int X { get; set; } + public int Y { get; set; } public SerializablePoint(Point point) { X = point.X; Y = point.Y;} + public SerializablePoint() { } public Point GetPoint() { return new Point(X, Y);} } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs index 991d928..22ea155 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs @@ -10,7 +10,6 @@ using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO { [Serializable] - [JsonSerializable(typeof(SerializableRectangle))] public class SerializableRectangle { public SerializablePoint Size { get; set; } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableVector2.cs b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableVector2.cs index 017ada0..094d849 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableVector2.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableVector2.cs @@ -2,15 +2,17 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices.JavaScript; using System.Text; using System.Threading.Tasks; namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO { + [Serializable] public class SerializableVector2 { - public float X; - public float Y; + public float X { get; set; } + public float Y { get; set; } public SerializableVector2(Vector2 vector) { X = vector.X; diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 9a4d67b..1e38eb5 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -13,6 +13,7 @@ using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; using ZoFo.GameCore.GameManagers.NetworkManager.Updates; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; namespace ZoFo.GameCore.GameManagers.NetworkManager { @@ -64,13 +65,18 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager string hostName = Dns.GetHostName(); // Retrive the Name of HOST var ipList = Dns.GetHostEntry(hostName).AddressList; + var ipV4List = new List(); foreach (var ip in ipList) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { - return ip; + ipV4List.Add(ip); } } + if (ipV4List.Count > 0) + { + return ipV4List[ipV4List.Count - 1]; + } return IPAddress.Loopback; } public void SetIsMultiplayer(bool isMultiplayer) @@ -119,6 +125,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager sendedData.Add(impDgramm); foreach (Datagramm Dgramm in sendedData) { + string impData = JsonSerializer.Serialize(Dgramm); byte[] impBuffer = Encoding.UTF8.GetBytes(impData); foreach (EndPoint sendingEP in clientsEP) diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/Datagramm.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/Datagramm.cs index 0395ebc..8d6317d 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/Datagramm.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/Datagramm.cs @@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates public class Datagramm { public int DatagrammId { get; set; } - public List updateDatas { get; set; } public bool isImportant { get; set; } + public List updateDatas { get; set; } } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs index 8e1b5b6..9188f1c 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectCreated.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient { @@ -12,9 +13,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient /// public class UpdateGameObjectCreated : UpdateData { - public UpdateGameObjectCreated() { UpdateType = "UpdateGameObjectCreated"; } - public string GameObjectType; - public string GameObjectId; - public Vector2 position; + public UpdateGameObjectCreated() { UpdateType = "UpdateGameObjectCreated"; isImportant = true; } + + public string GameObjectType { get; set; } + + public string GameObjectId { get; set; } + + public SerializableVector2 position { get; set; } } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectDeleted.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectDeleted.cs index 25415e1..d149be8 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectDeleted.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateGameObjectDeleted.cs @@ -11,7 +11,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient /// public class UpdateGameObjectDeleted : UpdateData { - public UpdateGameObjectDeleted() { UpdateType = "UpdateGameObjectDeleted"; } + public UpdateGameObjectDeleted() { UpdateType = "UpdateGameObjectDeleted"; isImportant = false; } public string GameObjectType; } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs index 189a299..da2293f 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdatePosition.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; using ZoFo.GameCore.GameObjects.Entities.LivingEntities; namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient @@ -15,6 +16,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient { public UpdatePosition() { UpdateType = "UpdatePosition"; } - public Vector2 NewPosition { get; set; } + public SerializableVector2 NewPosition { get; set; } } } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateTileCreated.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateTileCreated.cs index a009ddc..fe97a10 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateTileCreated.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateTileCreated.cs @@ -17,8 +17,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient /// public class UpdateTileCreated : UpdateData { - public UpdateTileCreated() { UpdateType = "UpdateTileCreated"; } - public Texture2D TextureTile { get; set; } + public UpdateTileCreated() { UpdateType = "UpdateTileCreated"; isImportant = true; } + [JsonInclude] public SerializableVector2 Position { get; set; } public SerializablePoint Size { get; set; } public SerializableRectangle sourceRectangle { get; set; } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index ec3699e..2abf5dd 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -182,12 +182,12 @@ namespace ZoFo.GameCore if (gameObject is Entity entity) { AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, IdEntity = entity.Id, - position = gameObject.position}); + position = new SerializableVector2(gameObject.position)}); collisionManager.Register(entity.collisionComponent); } else AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, - position = gameObject.position + position = new SerializableVector2(gameObject.position) });