Skip to content

Commit

Permalink
Merge pull request #189 from SakulFlee/SakulFlee/issue107
Browse files Browse the repository at this point in the history
iOS Support
  • Loading branch information
SakulFlee authored Dec 17, 2024
2 parents b67a613 + 33cb063 commit 00adea9
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Godot Project/Demos/Chat/Chat.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
14 changes: 14 additions & 0 deletions Godot Project/Demos/Chat/ChatJsonSourceGenContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
4 changes: 2 additions & 2 deletions Godot Project/Demos/Chat/ChatMessagePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public class ChatMessagePacket

public static ChatMessagePacket FromJSON(string json)
{
return PacketSerializer.FromJSON<ChatMessagePacket>(json);
return ChatPacketSerializer.FromJSON<ChatMessagePacket>(json);
}

public string ToJSON()
{
return PacketSerializer.ToJSON(this);
return ChatPacketSerializer.ToJSON(this);
}
}
23 changes: 23 additions & 0 deletions Godot Project/Demos/Chat/ChatPacketSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;

public interface ChatPacketSerializer
{
/// <summary>
/// Deserializes (/Parse) from JSON
/// </summary>
/// <param name="json">The JSON string to parse</param>
/// <returns>An instance of this T class</returns>
public static T FromJSON<T>(string json) where T : class
{
return JsonSerializer.Deserialize(json, typeof(T), ChatJsonSourceGenContext.Default) as T;
}

/// <summary>
/// Serializes this instance to JSON
/// </summary>
/// <returns>This T as JSON</returns>
public static string ToJSON(object o)
{
return JsonSerializer.Serialize(o, o.GetType(), ChatJsonSourceGenContext.Default);
}
}
17 changes: 17 additions & 0 deletions Godot Project/Demos/Game/GameJsonSourceGenContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
12 changes: 6 additions & 6 deletions Godot Project/Demos/Game/GamePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>()
public T InnerAs<T>() where T : class
{
return PacketSerializer.FromJSON<T>(InnerJSON);
return GamePacketSerializer.FromJSON<T>(InnerJSON);
}

public static GamePacket FromJSON(string json)
{
return PacketSerializer.FromJSON<GamePacket>(json);
return GamePacketSerializer.FromJSON<GamePacket>(json);
}

public string ToJSON()
{
return PacketSerializer.ToJSON(this);
return GamePacketSerializer.ToJSON(this);
}

public override string ToString()
{
return $"GamePacket@{Type}";
Expand Down
23 changes: 23 additions & 0 deletions Godot Project/Demos/Game/GamePacketSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text.Json;

public interface GamePacketSerializer
{
/// <summary>
/// Deserializes (/Parse) from JSON
/// </summary>
/// <param name="json">The JSON string to parse</param>
/// <returns>An instance of this T class</returns>
public static T FromJSON<T>(string json) where T : class
{
return JsonSerializer.Deserialize(json, typeof(T), GameJsonSourceGenContext.Default) as T;
}

/// <summary>
/// Serializes this instance to JSON
/// </summary>
/// <returns>This T as JSON</returns>
public static string ToJSON(object o)
{
return JsonSerializer.Serialize(o, o.GetType(), GameJsonSourceGenContext.Default);
}
}
39 changes: 11 additions & 28 deletions Godot Project/Demos/VideoCall/GZIP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
1 change: 1 addition & 0 deletions Godot Project/WebRTC Match Maker Godot Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<Content Include="...">
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
<PackageReference Include="System.Net.Security" Version="4.3.2" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions Godot Project/addons/match_maker/JsonSourceGenContext.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
2 changes: 1 addition & 1 deletion Godot Project/addons/match_maker/MatchMakerResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public class MatchMakerResponse
public string ownUUID { get; set; }
public string hostUUID { get; set; }
public string[] peers { get; set; }
}
}
23 changes: 4 additions & 19 deletions Godot Project/addons/match_maker/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Packet
/// </summary>
public string json { get; set; }

public static T FromJSON<T>(string json)
public static T FromJSON<T>(string json) where T : class
{
return PacketSerializer.FromJSON<T>(json);
}
Expand Down Expand Up @@ -95,12 +95,7 @@ public MatchMakerRequest ParseMatchMakerRequest()
return null;
}

var result = JsonSerializer.Deserialize<MatchMakerRequest>(json, new JsonSerializerOptions()
{
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
});
return result;
return JsonSerializer.Deserialize(json, typeof(MatchMakerRequest), JsonSourceGenContext.Default) as MatchMakerRequest;
}

/// <summary>
Expand All @@ -117,12 +112,7 @@ public MatchMakerResponse ParseMatchMakerResponse()
return null;
}

var result = JsonSerializer.Deserialize<MatchMakerResponse>(json, new JsonSerializerOptions()
{
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
});
return result;
return JsonSerializer.Deserialize(json, typeof(MatchMakerResponse), JsonSourceGenContext.Default) as MatchMakerResponse;
}

/// <summary>
Expand All @@ -139,11 +129,6 @@ public MatchMakerUpdate ParseMatchMakerUpdate()
return null;
}

var result = JsonSerializer.Deserialize<MatchMakerUpdate>(json, new JsonSerializerOptions()
{
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
});
return result;
return JsonSerializer.Deserialize(json, typeof(MatchMakerUpdate), JsonSourceGenContext.Default) as MatchMakerUpdate;
}
}
21 changes: 3 additions & 18 deletions Godot Project/addons/match_maker/PacketSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;

public interface PacketSerializer
{
Expand All @@ -8,16 +7,9 @@ public interface PacketSerializer
/// </summary>
/// <param name="json">The JSON string to parse</param>
/// <returns>An instance of this T class</returns>
public static T FromJSON<T>(string json)
public static T FromJSON<T>(string json) where T : class
{
return JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions()
{
IncludeFields = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = {
new JsonStringEnumConverter(),
},
});
return JsonSerializer.Deserialize(json, typeof(T), JsonSourceGenContext.Default) as T;
}

/// <summary>
Expand All @@ -26,13 +18,6 @@ public static T FromJSON<T>(string json)
/// <returns>This T as JSON</returns>
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);
}
}
3 changes: 3 additions & 0 deletions Godot Project/addons/match_maker/PacketType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Text.Json.Serialization;

/// <summary>
/// The type of <see cref="Packet"/>
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter<PacketType>))]
public enum PacketType
{
MatchMakerRequest,
Expand Down

0 comments on commit 00adea9

Please sign in to comment.