diff --git a/Godot Project/Demos/Chat/Chat.tscn b/Godot Project/Demos/Chat/Chat.tscn index 0ad99d1..889f31e 100644 --- a/Godot Project/Demos/Chat/Chat.tscn +++ b/Godot Project/Demos/Chat/Chat.tscn @@ -64,4 +64,4 @@ unique_name_in_owner = true visible = false [connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer/ChatMessageField" to="." method="OnChatTextEditChanged"] -[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer/SendButton" to="." method="OnChatTextEditChanged"] +[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer/SendButton" to="." method="OnSendButtonPressed"] diff --git a/Godot Project/Demos/Chat/ChatJsonSourceGenContext.cs b/Godot Project/Demos/Chat/ChatJsonSourceGenContext.cs new file mode 100644 index 0000000..9819955 --- /dev/null +++ b/Godot Project/Demos/Chat/ChatJsonSourceGenContext.cs @@ -0,0 +1,14 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Godot; + +[JsonSourceGenerationOptions( + WriteIndented = true, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + IncludeFields = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull +)] +[JsonSerializable(typeof(ChatMessagePacket))] +public partial class ChatJsonSourceGenContext : JsonSerializerContext +{ +} diff --git a/Godot Project/Demos/Chat/ChatMessagePacket.cs b/Godot Project/Demos/Chat/ChatMessagePacket.cs index ee342ae..826a3e0 100644 --- a/Godot Project/Demos/Chat/ChatMessagePacket.cs +++ b/Godot Project/Demos/Chat/ChatMessagePacket.cs @@ -6,11 +6,11 @@ public class ChatMessagePacket public static ChatMessagePacket FromJSON(string json) { - return PacketSerializer.FromJSON(json); + return ChatPacketSerializer.FromJSON(json); } public string ToJSON() { - return PacketSerializer.ToJSON(this); + return ChatPacketSerializer.ToJSON(this); } } diff --git a/Godot Project/Demos/Chat/ChatPacketSerializer.cs b/Godot Project/Demos/Chat/ChatPacketSerializer.cs new file mode 100644 index 0000000..30d4636 --- /dev/null +++ b/Godot Project/Demos/Chat/ChatPacketSerializer.cs @@ -0,0 +1,23 @@ +using System.Text.Json; + +public interface ChatPacketSerializer +{ + /// + /// Deserializes (/Parse) from JSON + /// + /// The JSON string to parse + /// An instance of this T class + public static T FromJSON(string json) where T : class + { + return JsonSerializer.Deserialize(json, typeof(T), ChatJsonSourceGenContext.Default) as T; + } + + /// + /// Serializes this instance to JSON + /// + /// This T as JSON + public static string ToJSON(object o) + { + return JsonSerializer.Serialize(o, o.GetType(), ChatJsonSourceGenContext.Default); + } +} diff --git a/Godot Project/Demos/Game/GameJsonSourceGenContext.cs b/Godot Project/Demos/Game/GameJsonSourceGenContext.cs new file mode 100644 index 0000000..1544520 --- /dev/null +++ b/Godot Project/Demos/Game/GameJsonSourceGenContext.cs @@ -0,0 +1,17 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Godot; + +[JsonSourceGenerationOptions( + WriteIndented = true, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + IncludeFields = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull +)] +[JsonSerializable(typeof(GamePacket))] +[JsonSerializable(typeof(GamePacketInput))] +[JsonSerializable(typeof(GamePacketPlayer))] +[JsonSerializable(typeof(GamePacketType))] +public partial class GameJsonSourceGenContext : JsonSerializerContext +{ +} diff --git a/Godot Project/Demos/Game/GamePacket.cs b/Godot Project/Demos/Game/GamePacket.cs index a78cd90..7d3355d 100644 --- a/Godot Project/Demos/Game/GamePacket.cs +++ b/Godot Project/Demos/Game/GamePacket.cs @@ -13,24 +13,24 @@ public GamePacket(GamePacketType type, string innerJSON) } public GamePacket(GamePacketType type, object inner) - : this(type, PacketSerializer.ToJSON(inner)) + : this(type, GamePacketSerializer.ToJSON(inner)) { } - public T InnerAs() + public T InnerAs() where T : class { - return PacketSerializer.FromJSON(InnerJSON); + return GamePacketSerializer.FromJSON(InnerJSON); } public static GamePacket FromJSON(string json) { - return PacketSerializer.FromJSON(json); + return GamePacketSerializer.FromJSON(json); } public string ToJSON() { - return PacketSerializer.ToJSON(this); + return GamePacketSerializer.ToJSON(this); } - + public override string ToString() { return $"GamePacket@{Type}"; diff --git a/Godot Project/Demos/Game/GamePacketSerializer.cs b/Godot Project/Demos/Game/GamePacketSerializer.cs new file mode 100644 index 0000000..e80ad9a --- /dev/null +++ b/Godot Project/Demos/Game/GamePacketSerializer.cs @@ -0,0 +1,23 @@ +using System.Text.Json; + +public interface GamePacketSerializer +{ + /// + /// Deserializes (/Parse) from JSON + /// + /// The JSON string to parse + /// An instance of this T class + public static T FromJSON(string json) where T : class + { + return JsonSerializer.Deserialize(json, typeof(T), GameJsonSourceGenContext.Default) as T; + } + + /// + /// Serializes this instance to JSON + /// + /// This T as JSON + public static string ToJSON(object o) + { + return JsonSerializer.Serialize(o, o.GetType(), GameJsonSourceGenContext.Default); + } +} \ No newline at end of file diff --git a/Godot Project/Demos/VideoCall/GZIP.cs b/Godot Project/Demos/VideoCall/GZIP.cs index ba5c6a1..915adb5 100644 --- a/Godot Project/Demos/VideoCall/GZIP.cs +++ b/Godot Project/Demos/VideoCall/GZIP.cs @@ -4,40 +4,23 @@ public class GZIP { - public static byte[] Decompress(byte[] input) + public static byte[] Decompress(byte[] data) { - using (var source = new MemoryStream(input)) - { - byte[] lengthBytes = new byte[4]; - source.Read(lengthBytes, 0, 4); + using var compressedStream = new MemoryStream(data); + using var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress); + using var resultStream = new MemoryStream(); - var length = BitConverter.ToInt32(lengthBytes, 0); - using (var decompressionStream = new GZipStream(source, - CompressionMode.Decompress)) - { - var result = new byte[length]; - decompressionStream.Read(result, 0, length); - return result; - } - } + zipStream.CopyTo(resultStream); + return resultStream.ToArray(); } - public static byte[] Compress(byte[] input) + public static byte[] Compress(byte[] data) { - using (var result = new MemoryStream()) + using var result = new MemoryStream(); + using (var compressionStream = new GZipStream(result, CompressionMode.Compress)) { - var lengthBytes = BitConverter.GetBytes(input.Length); - result.Write(lengthBytes, 0, 4); - - using (var compressionStream = new GZipStream(result, - CompressionMode.Compress)) - { - compressionStream.Write(input, 0, input.Length); - compressionStream.Flush(); - - } - return result.ToArray(); + compressionStream.Write(data, 0, data.Length); } + return result.ToArray(); } - } \ No newline at end of file diff --git a/Godot Project/WebRTC Match Maker Godot Project.csproj b/Godot Project/WebRTC Match Maker Godot Project.csproj index 576a181..783fca5 100644 --- a/Godot Project/WebRTC Match Maker Godot Project.csproj +++ b/Godot Project/WebRTC Match Maker Godot Project.csproj @@ -12,5 +12,6 @@ true + \ No newline at end of file diff --git a/Godot Project/addons/match_maker/JsonSourceGenContext.cs b/Godot Project/addons/match_maker/JsonSourceGenContext.cs new file mode 100644 index 0000000..c62de94 --- /dev/null +++ b/Godot Project/addons/match_maker/JsonSourceGenContext.cs @@ -0,0 +1,16 @@ +using System.Text.Json.Serialization; + +[JsonSourceGenerationOptions( + WriteIndented = true, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + IncludeFields = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull +)] +[JsonSerializable(typeof(MatchMakerResponse))] +[JsonSerializable(typeof(MatchMakerRequest))] +[JsonSerializable(typeof(MatchMakerUpdate))] +[JsonSerializable(typeof(Packet))] +[JsonSerializable(typeof(PacketType))] +public partial class JsonSourceGenContext : JsonSerializerContext +{ +} diff --git a/Godot Project/addons/match_maker/MatchMakerResponse.cs b/Godot Project/addons/match_maker/MatchMakerResponse.cs index 98dafaa..e8beaa1 100644 --- a/Godot Project/addons/match_maker/MatchMakerResponse.cs +++ b/Godot Project/addons/match_maker/MatchMakerResponse.cs @@ -7,4 +7,4 @@ public class MatchMakerResponse public string ownUUID { get; set; } public string hostUUID { get; set; } public string[] peers { get; set; } -} \ No newline at end of file +} diff --git a/Godot Project/addons/match_maker/Packet.cs b/Godot Project/addons/match_maker/Packet.cs index 91aabe5..4734ea2 100644 --- a/Godot Project/addons/match_maker/Packet.cs +++ b/Godot Project/addons/match_maker/Packet.cs @@ -25,7 +25,7 @@ public class Packet /// public string json { get; set; } - public static T FromJSON(string json) + public static T FromJSON(string json) where T : class { return PacketSerializer.FromJSON(json); } @@ -95,12 +95,7 @@ public MatchMakerRequest ParseMatchMakerRequest() return null; } - var result = JsonSerializer.Deserialize(json, new JsonSerializerOptions() - { - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }); - return result; + return JsonSerializer.Deserialize(json, typeof(MatchMakerRequest), JsonSourceGenContext.Default) as MatchMakerRequest; } /// @@ -117,12 +112,7 @@ public MatchMakerResponse ParseMatchMakerResponse() return null; } - var result = JsonSerializer.Deserialize(json, new JsonSerializerOptions() - { - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }); - return result; + return JsonSerializer.Deserialize(json, typeof(MatchMakerResponse), JsonSourceGenContext.Default) as MatchMakerResponse; } /// @@ -139,11 +129,6 @@ public MatchMakerUpdate ParseMatchMakerUpdate() return null; } - var result = JsonSerializer.Deserialize(json, new JsonSerializerOptions() - { - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }); - return result; + return JsonSerializer.Deserialize(json, typeof(MatchMakerUpdate), JsonSourceGenContext.Default) as MatchMakerUpdate; } } diff --git a/Godot Project/addons/match_maker/PacketSerializer.cs b/Godot Project/addons/match_maker/PacketSerializer.cs index 8ef0b47..2e9782d 100644 --- a/Godot Project/addons/match_maker/PacketSerializer.cs +++ b/Godot Project/addons/match_maker/PacketSerializer.cs @@ -1,5 +1,4 @@ using System.Text.Json; -using System.Text.Json.Serialization; public interface PacketSerializer { @@ -8,16 +7,9 @@ public interface PacketSerializer /// /// The JSON string to parse /// An instance of this T class - public static T FromJSON(string json) + public static T FromJSON(string json) where T : class { - return JsonSerializer.Deserialize(json, new JsonSerializerOptions() - { - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - Converters = { - new JsonStringEnumConverter(), - }, - }); + return JsonSerializer.Deserialize(json, typeof(T), JsonSourceGenContext.Default) as T; } /// @@ -26,13 +18,6 @@ public static T FromJSON(string json) /// This T as JSON public static string ToJSON(object o) { - return JsonSerializer.Serialize(o, new JsonSerializerOptions() - { - IncludeFields = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - Converters = { - new JsonStringEnumConverter(), - }, - }); + return JsonSerializer.Serialize(o, o.GetType(), JsonSourceGenContext.Default); } } \ No newline at end of file diff --git a/Godot Project/addons/match_maker/PacketType.cs b/Godot Project/addons/match_maker/PacketType.cs index 70bf8b0..5fa510e 100644 --- a/Godot Project/addons/match_maker/PacketType.cs +++ b/Godot Project/addons/match_maker/PacketType.cs @@ -1,6 +1,9 @@ +using System.Text.Json.Serialization; + /// /// The type of /// +[JsonConverter(typeof(JsonStringEnumConverter))] public enum PacketType { MatchMakerRequest,