From b28cd708d2fc2079d757e639f2ef2b413ebb1753 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 28 Jul 2024 22:20:45 +0800 Subject: [PATCH] feat[swift]: cut down Map serialization of swift --- .../serializer/CutDownMapSerializer.java | 60 ++++- .../src/main/resources/swift/ByteBuffer.swift | 249 ++++++++++++++++++ 2 files changed, 297 insertions(+), 12 deletions(-) diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java index e9f0c71a..0b92fc35 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -59,7 +59,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeIntIntMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -86,7 +86,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeIntLongMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -113,7 +113,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeIntStringMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -138,7 +138,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeIntPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, protocolId)).append(LS); return true; case Lua: @@ -169,7 +169,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeLongIntMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -196,7 +196,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeLongLongMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeLongLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -223,7 +223,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeLongStringMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -248,7 +248,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeLongPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, protocolId)).append(LS); return true; case Lua: @@ -279,7 +279,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeStringIntMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS); return true; case Lua: @@ -306,7 +306,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeStringLongMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS); return true; case Lua: @@ -333,7 +333,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("buffer.writeStringStringMap(&{});", objectStr)).append(LS); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS); return true; case Lua: @@ -358,7 +358,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("{}.writeStringPacketMap($1, (Map){}, {});", EnhanceUtils.byteBufUtils, objectStr, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return true; - case Kotlin, Scala, GdScript, Python, Ruby: + case Kotlin, Scala, GdScript, Python, Ruby, Swift: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, protocolId)).append(LS); return true; case Lua: @@ -405,6 +405,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readIntIntMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readIntIntMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS); return map; @@ -448,6 +451,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readIntLongMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readIntLongMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS); return map; @@ -491,6 +497,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readIntStringMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readIntStringMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS); return map; @@ -533,6 +542,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readIntPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readIntPacketMap({}) as! Dictionary", map, protocolId, protocolName)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, protocolId)).append(LS); return map; @@ -579,6 +591,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readLongIntMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readLongIntMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS); return map; @@ -622,6 +637,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readLongLongMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readLongLongMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS); return map; @@ -665,6 +683,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readLongStringMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readLongStringMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS); return map; @@ -707,6 +728,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readLongPacketMap({}) as! Dictionary", map, protocolId, protocolName)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, protocolId)).append(LS); return map; @@ -754,6 +778,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readStringIntMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readStringIntMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS); return map; @@ -797,6 +824,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readStringLongMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readStringLongMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS); return map; @@ -840,6 +870,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Rust: builder.append(StringUtils.format("let {} = buffer.readStringStringMap();", map)).append(LS); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readStringStringMap()", map)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS); return map; @@ -882,6 +915,9 @@ public class CutDownMapSerializer implements ICutDownSerializer { case Enhance: builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(protocolId))); return map; + case Swift: + builder.append(StringUtils.format("let {} = buffer.readStringPacketMap({}) as! Dictionary", map, protocolId, protocolName)).append(LS); + return map; case GdScript: builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS); return map; diff --git a/protocol/src/main/resources/swift/ByteBuffer.swift b/protocol/src/main/resources/swift/ByteBuffer.swift index 63448252..5ee9665f 100644 --- a/protocol/src/main/resources/swift/ByteBuffer.swift +++ b/protocol/src/main/resources/swift/ByteBuffer.swift @@ -746,4 +746,253 @@ class ByteBuffer { return set } + func writeIntIntMap(_ map: Dictionary) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeInt(value) + } + } + } + + func readIntIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeLong(value) + } + } + } + + func readIntLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writeString(value) + } + } + } + + func readIntStringMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeInt(key) + writePacket(value, protocolId) + } + } + } + + func readIntPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writeInt(value) + } + } + } + + func readLongIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writeLong(value) + } + } + } + + func readLongLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeLong(key) + writePacket(value, protocolId) + } + } + } + + func readLongPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writeInt(value) + } + } + } + + func readStringIntMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writeLong(value) + } + } + } + + func readStringLongMap() -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0.., _ protocolId: Int) { + if (map.isEmpty) { + writeInt(0) + } else { + writeInt(map.count) + for (key, value) in map { + writeString(key) + writePacket(value, protocolId) + } + } + } + + func readStringPacketMap(_ protocolId: Int) -> Dictionary { + let size = readInt() + var map = Dictionary() + if (size > 0) { + for _ in 0..