DESERIALIZATION-FIX-COMMIT

This commit is contained in:
AnloGames 2024-08-19 19:40:03 +03:00
parent 10089f8a79
commit 332680a8eb
14 changed files with 115 additions and 33 deletions

View file

@ -24,6 +24,7 @@ using ZoFo.GameCore.GUI;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace ZoFo.GameCore namespace ZoFo.GameCore
@ -144,17 +145,17 @@ namespace ZoFo.GameCore
{ {
GameObject created_gameObject; GameObject created_gameObject;
if ((update as UpdateGameObjectCreated).GameObjectType == "EntittyForAnimationTests") 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") 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); players.Add(created_gameObject as Player);
gameObjects.Add(created_gameObject); gameObjects.Add(created_gameObject);
} }
if ((update as UpdateGameObjectCreated).GameObjectType == "Ammo") 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") 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); (gameObjects.Last() as Entity).SetIdByClient((update as UpdateGameObjectCreated).IdEntity);
@ -169,7 +170,7 @@ namespace ZoFo.GameCore
{ {
var ent = FindEntityById(update.IdEntity); var ent = FindEntityById(update.IdEntity);
ent.position = (update as UpdatePosition).NewPosition; ent.position = (update as UpdatePosition).NewPosition.GetVector2();
DebugHUD.Instance.Log("newPosition " + ent.position); DebugHUD.Instance.Log("newPosition " + ent.position);
} }
} }

View file

@ -45,7 +45,7 @@ public class SelectModeMenu : AbstractGUI
AppManager.Instance.SetClient(client); AppManager.Instance.SetClient(client);
server.CreateRoom(false); server.CreateRoom(false);
client.JoinYourself(server.MyIp.Port); client.JoinYourself(server.MyIp.Port);
AppManager.Instance.ChangeState(GameState.HostPlaying); //AppManager.Instance.ChangeState(GameState.HostPlaying);
AppManager.Instance.SetGUI(new HUD()); AppManager.Instance.SetGUI(new HUD());
//server.CreateRoom(1); //server.CreateRoom(1);

View file

@ -12,6 +12,7 @@ using ZoFo.GameCore.GameObjects.Entities;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.Graphics; using ZoFo.GameCore.Graphics;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.CollisionManager 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.X = (int)entity.position.X;
entity.graphicsComponent.ObjectDrawRectangle.Y = (int)entity.position.Y; 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 AppManager.Instance.debugHud.Set("testPos", entity.position.ToString()); //TODO remove
entity.velocity = Vector2.Zero; entity.velocity = Vector2.Zero;
} }

View file

@ -1,3 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlTypes; using System.Data.SqlTypes;
@ -10,6 +12,7 @@ using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates; using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
namespace ZoFo.GameCore.GameManagers.NetworkManager namespace ZoFo.GameCore.GameManagers.NetworkManager
@ -47,7 +50,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
{ {
Datagramm Datagramm = new Datagramm(); Datagramm Datagramm = new Datagramm();
Datagramm.updateDatas = updates; 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); socket.SendTo(bytes, sendingEP);
} }
@ -60,7 +63,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public void AnalyzeData(string data) public void AnalyzeData(string data)
{ {
Datagramm Dgramm = JsonSerializer.Deserialize<Datagramm>(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<bool>();
Dgramm.DatagrammId = token["DatagrammId"].ToObject<int>();
Dgramm.updateDatas = GetSentUpdates(token["updateDatas"]);
if (Dgramm.isImportant) if (Dgramm.isImportant)
{ {
if (Dgramm.DatagrammId == currentServerDatagrammId + 1) if (Dgramm.DatagrammId == currentServerDatagrammId + 1)
@ -85,11 +94,72 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
} }
} }
public List<UpdateData> GetSentUpdates(JToken updatesToken)
{
List<UpdateData> data = new List<UpdateData>();
JArray updateDatas = updatesToken as JArray;
UpdateData update = new UpdateData();
foreach (JObject token in updateDatas.Children())
{
switch (token["UpdateType"].ToObject<string>())
{
case "UpdateAnimation":
update = token.ToObject<UpdateAnimation>();
data.Add(update);
break;
case "UpdateEntityHealth":
update = token.ToObject<UpdateEntityHealth>();
data.Add(update);
break;
case "UpdateGameEnded":
update = token.ToObject<UpdateGameEnded>();
data.Add(update);
break;
case "UpdateGameObjectCreated":
update = token.ToObject<UpdateGameObjectCreated>();
data.Add(update);
break;
case "UpdateGameObjectDeleted":
update = token.ToObject<UpdateGameObjectDeleted>();
data.Add(update);
break;
case "UpdateInteraction":
update = token.ToObject<UpdateInteraction>();
data.Add(update);
break;
case "UpdateInteractionReady":
update = token.ToObject<UpdateInteractionReady>();
data.Add(update);
break;
case "UpdateLoot":
update = token.ToObject<UpdateLoot>();
data.Add(update);
break;
case "UpdatePlayerParametrs":
update = token.ToObject<UpdatePlayerParametrs>();
data.Add(update);
break;
case "UpdatePosition":
update = token.ToObject<UpdatePosition>();
data.Add(update);
break;
case "UpdateTileCreated":
update = token.ToObject<UpdateTileCreated>();
data.Add(update);
break;
}
}
return data;
}
public void SendAcknowledgement(int DatagrammId) public void SendAcknowledgement(int DatagrammId)
{ {
Datagramm Dgramm = new Datagramm() { DatagrammId = 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); byte[] buffer = Encoding.UTF8.GetBytes(data);
socket.SendTo(buffer, sendingEP); socket.SendTo(buffer, sendingEP);
@ -110,11 +180,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
} }
#endregion #endregion
public void StopConnection()
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
#region Join #region Join
/// <summary> /// <summary>
/// приложение пытается подключиться к комнате /// приложение пытается подключиться к комнате
@ -124,6 +189,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public void JoinRoom(string ip, int port) // multyplayer public void JoinRoom(string ip, int port) // multyplayer
{ {
sendingEP = new IPEndPoint(IPAddress.Parse(ip), port); sendingEP = new IPEndPoint(IPAddress.Parse(ip), port);
SendData(); SendData();
Thread listen = new Thread(StartListening); Thread listen = new Thread(StartListening);
listen.IsBackground = true; listen.IsBackground = true;
@ -173,7 +239,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
byte[] correctedBuffer = new byte[size]; byte[] correctedBuffer = new byte[size];
Array.Copy(buffer, correctedBuffer, size); Array.Copy(buffer, correctedBuffer, size);
data = Encoding.UTF8.GetString(correctedBuffer); data = Encoding.UTF8.GetString(correctedBuffer);
AnalyzeData(data); GetDataSent(data);
} }
} }
} }

View file

@ -9,10 +9,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
{ {
public class SerializablePoint public class SerializablePoint
{ {
public int X; public int X { get; set; }
public int Y; public int Y { get; set; }
public SerializablePoint(Point point) { X = point.X; Y = point.Y;} public SerializablePoint(Point point) { X = point.X; Y = point.Y;}
public SerializablePoint() { }
public Point GetPoint() { return new Point(X, Y);} public Point GetPoint() { return new Point(X, Y);}
} }
} }

View file

@ -10,7 +10,6 @@ using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
{ {
[Serializable] [Serializable]
[JsonSerializable(typeof(SerializableRectangle))]
public class SerializableRectangle public class SerializableRectangle
{ {
public SerializablePoint Size { get; set; } public SerializablePoint Size { get; set; }

View file

@ -2,15 +2,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
{ {
[Serializable]
public class SerializableVector2 public class SerializableVector2
{ {
public float X; public float X { get; set; }
public float Y; public float Y { get; set; }
public SerializableVector2(Vector2 vector) public SerializableVector2(Vector2 vector)
{ {
X = vector.X; X = vector.X;

View file

@ -13,6 +13,7 @@ using System.Text.Json.Serialization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates; using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager namespace ZoFo.GameCore.GameManagers.NetworkManager
{ {
@ -64,13 +65,18 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
string hostName = Dns.GetHostName(); // Retrive the Name of HOST string hostName = Dns.GetHostName(); // Retrive the Name of HOST
var ipList = Dns.GetHostEntry(hostName).AddressList; var ipList = Dns.GetHostEntry(hostName).AddressList;
var ipV4List = new List<IPAddress>();
foreach (var ip in ipList) foreach (var ip in ipList)
{ {
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 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; return IPAddress.Loopback;
} }
public void SetIsMultiplayer(bool isMultiplayer) public void SetIsMultiplayer(bool isMultiplayer)
@ -119,6 +125,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
sendedData.Add(impDgramm); sendedData.Add(impDgramm);
foreach (Datagramm Dgramm in sendedData) foreach (Datagramm Dgramm in sendedData)
{ {
string impData = JsonSerializer.Serialize(Dgramm); string impData = JsonSerializer.Serialize(Dgramm);
byte[] impBuffer = Encoding.UTF8.GetBytes(impData); byte[] impBuffer = Encoding.UTF8.GetBytes(impData);
foreach (EndPoint sendingEP in clientsEP) foreach (EndPoint sendingEP in clientsEP)

View file

@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates
public class Datagramm public class Datagramm
{ {
public int DatagrammId { get; set; } public int DatagrammId { get; set; }
public List<UpdateData> updateDatas { get; set; }
public bool isImportant { get; set; } public bool isImportant { get; set; }
public List<UpdateData> updateDatas { get; set; }
} }
} }

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{ {
@ -12,9 +13,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
/// </summary> /// </summary>
public class UpdateGameObjectCreated : UpdateData public class UpdateGameObjectCreated : UpdateData
{ {
public UpdateGameObjectCreated() { UpdateType = "UpdateGameObjectCreated"; } public UpdateGameObjectCreated() { UpdateType = "UpdateGameObjectCreated"; isImportant = true; }
public string GameObjectType;
public string GameObjectId; public string GameObjectType { get; set; }
public Vector2 position;
public string GameObjectId { get; set; }
public SerializableVector2 position { get; set; }
} }
} }

View file

@ -11,7 +11,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
/// </summary> /// </summary>
public class UpdateGameObjectDeleted : UpdateData public class UpdateGameObjectDeleted : UpdateData
{ {
public UpdateGameObjectDeleted() { UpdateType = "UpdateGameObjectDeleted"; } public UpdateGameObjectDeleted() { UpdateType = "UpdateGameObjectDeleted"; isImportant = false; }
public string GameObjectType; public string GameObjectType;
} }
} }

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities; using ZoFo.GameCore.GameObjects.Entities.LivingEntities;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
@ -15,6 +16,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{ {
public UpdatePosition() { UpdateType = "UpdatePosition"; } public UpdatePosition() { UpdateType = "UpdatePosition"; }
public Vector2 NewPosition { get; set; } public SerializableVector2 NewPosition { get; set; }
} }
} }

View file

@ -17,8 +17,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
/// </summary> /// </summary>
public class UpdateTileCreated : UpdateData public class UpdateTileCreated : UpdateData
{ {
public UpdateTileCreated() { UpdateType = "UpdateTileCreated"; } public UpdateTileCreated() { UpdateType = "UpdateTileCreated"; isImportant = true; }
public Texture2D TextureTile { get; set; } [JsonInclude]
public SerializableVector2 Position { get; set; } public SerializableVector2 Position { get; set; }
public SerializablePoint Size { get; set; } public SerializablePoint Size { get; set; }
public SerializableRectangle sourceRectangle { get; set; } public SerializableRectangle sourceRectangle { get; set; }

View file

@ -182,12 +182,12 @@ namespace ZoFo.GameCore
if (gameObject is Entity entity) if (gameObject is Entity entity)
{ {
AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, IdEntity = entity.Id, AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, IdEntity = entity.Id,
position = gameObject.position}); position = new SerializableVector2(gameObject.position)});
collisionManager.Register(entity.collisionComponent); collisionManager.Register(entity.collisionComponent);
} }
else else
AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name, AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name,
position = gameObject.position position = new SerializableVector2(gameObject.position)
}); });