From 65a4bcb1d30766631457b180dabd33c18b16e00e Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 28 Jul 2024 21:54:48 +0800 Subject: [PATCH] feat[swift]: cut down Set serialization of swift --- .../serializer/CutDownSetSerializer.java | 44 ++++- .../src/main/resources/swift/ByteBuffer.swift | 177 ++++++++++++++++++ 2 files changed, 211 insertions(+), 10 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java index 6c760ef8..cc14c4db 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -56,7 +56,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeBoolArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeBoolSet({})", objectStr)).append(LS); break; case Lua: @@ -89,7 +89,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeByteSet({})", objectStr)).append(LS); break; case Lua: @@ -122,7 +122,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeShortSet({})", objectStr)).append(LS); break; case Lua: @@ -155,7 +155,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeIntSet({})", objectStr)).append(LS); break; case Lua: @@ -188,7 +188,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeLongSet({})", objectStr)).append(LS); break; case Lua: @@ -218,7 +218,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeFloatSet({})", objectStr)).append(LS); break; case Lua: @@ -249,7 +249,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeDoubleSet({})", objectStr)).append(LS); break; case Lua: @@ -283,7 +283,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case GdScript: builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS); break; - case Kotlin, Scala, Python, Ruby: + case Kotlin, Scala, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeStringSet({})", objectStr)).append(LS); break; case Lua: @@ -334,7 +334,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Java: builder.append(StringUtils.format("buffer.writePacketSet({}, (short) {});", objectStr, protocolId)).append(LS); break; - case Rust, Golang, Protobuf: + case Rust, Swift, Golang, Protobuf: default: flag = false; } @@ -362,6 +362,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readBoolSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readBoolSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readBoolArray()", set)).append(LS); break; @@ -408,6 +411,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readByteSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readByteSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS); break; @@ -454,6 +460,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readShortSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readShortSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS); break; @@ -500,6 +509,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readIntSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readIntSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS); break; @@ -546,6 +558,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readLongSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readLongSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS); break; @@ -589,6 +604,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readFloatSet($1);", set, EnhanceUtils.byteBufUtils)); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readFloatArray()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS); break; @@ -633,6 +651,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Set {} = {}.readDoubleSet($1);", set, EnhanceUtils.byteBufUtils)); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readDoubleArray()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS); break; @@ -680,6 +701,9 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readStringSet();", set)).append(LS); break; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readStringSet()", set)).append(LS); + break; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS); break; @@ -760,7 +784,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case JavaScript, EcmaScript, TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS); break; - case Rust, Golang, Protobuf: + case Rust, Swift, Golang, Protobuf: default: flag = false; } diff --git a/protocol/src/main/resources/swift/ByteBuffer.swift b/protocol/src/main/resources/swift/ByteBuffer.swift index b5d4d1db..63448252 100644 --- a/protocol/src/main/resources/swift/ByteBuffer.swift +++ b/protocol/src/main/resources/swift/ByteBuffer.swift @@ -569,4 +569,181 @@ class ByteBuffer { } return array } + + func writeBoolSet(_ set: Set) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeBool(ele) + } + } + } + + func readBoolSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeByte(ele) + } + } + } + + func readByteSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeShort(ele) + } + } + } + + func readShortSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeInt(ele) + } + } + } + + func readIntSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeLong(ele) + } + } + } + + func readLongSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeFloat(ele) + } + } + } + + func readFloatSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeDouble(ele) + } + } + } + + func readDoubleSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..) { + if (set.isEmpty) { + writeInt(0) + } else { + writeInt(set.count) + for ele in set { + writeString(ele) + } + } + } + + func readStringSet() -> Set { + let size = readInt() + var set = Set() + if (size > 0) { + for _ in 0..