diff --git a/BeatTogether.DedicatedServer.Messaging/Extensions/BinaryReadWriteExtension.cs b/BeatTogether.DedicatedServer.Messaging/Extensions/BinaryReadWriteExtension.cs index 449b7af8..b8d83c08 100644 --- a/BeatTogether.DedicatedServer.Messaging/Extensions/BinaryReadWriteExtension.cs +++ b/BeatTogether.DedicatedServer.Messaging/Extensions/BinaryReadWriteExtension.cs @@ -8,10 +8,10 @@ public static class BinaryReadWriteExtension // Token: 0x06000069 RID: 105 RVA: 0x00002ED8 File Offset: 0x000010D8 public static void Write(this BinaryWriter binaryWriter, Color color) { - binaryWriter.Write(color.r); - binaryWriter.Write(color.g); - binaryWriter.Write(color.b); - binaryWriter.Write(color.a); + binaryWriter.Write(color.R); + binaryWriter.Write(color.G); + binaryWriter.Write(color.B); + binaryWriter.Write(color.A); } // Token: 0x0600006A RID: 106 RVA: 0x00002F0A File Offset: 0x0000110A diff --git a/BeatTogether.DedicatedServer.Messaging/Models/AvatarData.cs b/BeatTogether.DedicatedServer.Messaging/Models/AvatarData.cs index 4af80334..fa02963e 100644 --- a/BeatTogether.DedicatedServer.Messaging/Models/AvatarData.cs +++ b/BeatTogether.DedicatedServer.Messaging/Models/AvatarData.cs @@ -49,18 +49,18 @@ public AvatarData(string headTopId, Color headTopPrimaryColor, Color headTopSeco public void ReadFrom(ref SpanBuffer reader) { HeadTopId = reader.ReadString(); - HeadTopPrimaryColor = reader.ReadColor(); - HandsColor = reader.ReadColor(); + HeadTopPrimaryColor.ReadFrom(ref reader); + HandsColor.ReadFrom(ref reader); ClothesId = reader.ReadString(); - ClothesPrimaryColor = reader.ReadColor(); - ClothesSecondaryColor = reader.ReadColor(); - ClothesDetailColor = reader.ReadColor(); + ClothesPrimaryColor.ReadFrom(ref reader); + ClothesSecondaryColor.ReadFrom(ref reader); + ClothesDetailColor.ReadFrom(ref reader); reader.SkipBytes(8); EyesId = reader.ReadString(); MouthId = reader.ReadString(); - GlassesColor = reader.ReadColor(); - FacialHairColor = reader.ReadColor(); - HeadTopSecondaryColor = reader.ReadColor(); + GlassesColor.ReadFrom(ref reader); + FacialHairColor.ReadFrom(ref reader); + HeadTopSecondaryColor.ReadFrom(ref reader); GlassesId = reader.ReadString(); FacialHairId = reader.ReadString(); HandsId = reader.ReadString(); @@ -70,19 +70,19 @@ public void ReadFrom(ref SpanBuffer reader) public void WriteTo(ref SpanBuffer writer) { writer.WriteString(HeadTopId); - writer.WriteColor(HeadTopPrimaryColor); - writer.WriteColor(HandsColor); + HeadTopPrimaryColor.WriteTo(ref writer); + HandsColor.WriteTo(ref writer); writer.WriteString(ClothesId); - writer.WriteColor(ClothesPrimaryColor); - writer.WriteColor(ClothesSecondaryColor); - writer.WriteColor(ClothesDetailColor); + ClothesPrimaryColor.WriteTo(ref writer); + ClothesSecondaryColor.WriteTo(ref writer); + ClothesDetailColor.WriteTo(ref writer); writer.WriteColor(new Color()); writer.WriteColor(new Color()); writer.WriteString(EyesId); writer.WriteString(MouthId); - writer.WriteColor(GlassesColor); - writer.WriteColor(FacialHairColor); - writer.WriteColor(HeadTopSecondaryColor); + GlassesColor.WriteTo(ref writer); + FacialHairColor.WriteTo(ref writer); + HeadTopSecondaryColor.WriteTo(ref writer); writer.WriteString(GlassesId); writer.WriteString(FacialHairId); writer.WriteString(HandsId); diff --git a/BeatTogether.DedicatedServer.Messaging/Models/Color.cs b/BeatTogether.DedicatedServer.Messaging/Models/Color.cs index dd8f78e5..53770e5c 100644 --- a/BeatTogether.DedicatedServer.Messaging/Models/Color.cs +++ b/BeatTogether.DedicatedServer.Messaging/Models/Color.cs @@ -2,38 +2,48 @@ using BeatTogether.LiteNetLib.Util; using System.Drawing; using System; +using Serilog; namespace BeatTogether.DedicatedServer.Messaging.Models { public sealed class Color : INetSerializable { - public float r { get; set; } - public float g { get; set; } - public float b { get; set; } - public float a { get; set; } + public float R { get; set; } + public float G { get; set; } + public float B { get; set; } + public float A { get; set; } + + private static readonly ILogger _logger = Log.ForContext(); public Color(float r, float g, float b, float a) { - this.r = r; - this.g = g; - this.b = b; - this.a = a; + _logger.Debug($"Creating color with R: {r} G: {g} B: {b} A: {a}"); + R = r; + G = g; + B = b; + A = a; } public Color() { } public static implicit operator Color(System.Drawing.Color c) { - return new Color(Round(Clamp(c.R) * 255f), Round(Clamp(c.G) * 255f), Round(Clamp(c.B) * 255f), Round(Clamp(c.A) * 255f)); + Color temp = new Color(Clamp(c.R / 255f), Clamp(c.G / 255f), Clamp(c.B / 255f), Clamp(c.A / 255f)); + _logger.Debug($"Converted Drawing.Color"); + _logger.Debug($"Before convert {c.R} {c.G} {c.B} {c.A}"); + _logger.Debug($"After conversion {Clamp(c.R / 255f)} {Clamp(c.G / 255f)} {Clamp(c.B / 255f)} {Clamp(c.A / 255f)}"); + return temp; } public static implicit operator System.Drawing.Color(Color c) { - return System.Drawing.Color.FromArgb((int)(c.a / 255f), (int)(c.r / 255f), (int)(c.g / 255f), (int)(c.b / 255f)); + _logger.Debug($"Converted A: {c.A} R: {c.R} G: {c.G} B: {c.B} to A: {Round(c.A * 255f)} R: {Round(c.R * 255f)} G: {Round(c.G * 255f)} B: {Round(c.B * 255f)}"); + _logger.Debug($"Conversion without rounding A: {c.A * 255f} R: {c.R * 255f} G: {c.G * 255f} B: {c.B * 255f}"); + return System.Drawing.Color.FromArgb(Round(c.A * 255f), Round(c.R * 255f), Round(c.G * 255f), Round(c.B * 255f)); } - public static float Round(float f) + public static int Round(float f) { - return (float)Math.Round((double)f); + return (int)Math.Round((double)f); } public static float Clamp(float value) @@ -60,18 +70,19 @@ public static float Clamp(float value) } public void ReadFrom(ref SpanBuffer reader) { - r = reader.ReadFloat32(); - g = reader.ReadFloat32(); - b = reader.ReadFloat32(); - a = reader.ReadFloat32(); + //Maybe cast to int? + R = Clamp(reader.ReadUInt8() / 255f); + G = Clamp(reader.ReadUInt8() / 255f); + B = Clamp(reader.ReadUInt8() / 255f); + A = Clamp(reader.ReadUInt8() / 255f); } public void WriteTo(ref SpanBuffer writer) { - writer.WriteFloat32(r); - writer.WriteFloat32(g); - writer.WriteFloat32(b); - writer.WriteFloat32(a); + writer.WriteUInt8((byte)Round(R * 255f)); + writer.WriteUInt8((byte)Round(G * 255f)); + writer.WriteUInt8((byte)Round(B * 255f)); + writer.WriteUInt8((byte)Round(A * 255f)); } } } diff --git a/BeatTogether.DedicatedServer.Messaging/Packets/PlayerAvatarPacket.cs b/BeatTogether.DedicatedServer.Messaging/Packets/PlayerAvatarPacket.cs index 0e3b429a..06972e3b 100644 --- a/BeatTogether.DedicatedServer.Messaging/Packets/PlayerAvatarPacket.cs +++ b/BeatTogether.DedicatedServer.Messaging/Packets/PlayerAvatarPacket.cs @@ -14,11 +14,6 @@ public sealed class PlayerAvatarPacket : IVersionedNetSerializable { public MultiplayerAvatarsData PlayerAvatar { get; set; } = new(); - public void ReadFrom(ref SpanBuffer reader) - { - PlayerAvatar.ReadFrom(ref reader); - } - public void ReadFrom(ref SpanBuffer reader, Version version) { if (version < ClientVersions.NewPacketVersion) @@ -30,27 +25,20 @@ public void ReadFrom(ref SpanBuffer reader, Version version) } else { - ReadFrom(ref reader); + PlayerAvatar.ReadFrom(ref reader); } } - public void WriteTo(ref SpanBuffer writer) - { - PlayerAvatar.WriteTo(ref writer); - } - public void WriteTo(ref SpanBuffer writer, Version version) { if (version < ClientVersions.NewPacketVersion) { - if (PlayerAvatar.AvatarsData is null) - PlayerAvatar.AvatarsData = new(); - PlayerAvatar.AvatarsData.FirstOrDefault().CreateAvatarData().WriteTo(ref writer); + PlayerAvatar.AvatarsData.LastOrDefault().CreateAvatarData().WriteTo(ref writer); return; } else { - WriteTo(ref writer); + PlayerAvatar.WriteTo(ref writer); } } }