feat[swift]: cut down Map serialization of swift

This commit is contained in:
godotg
2024-07-28 22:20:45 +08:00
parent 65a4bcb1d3
commit b28cd708d2
2 changed files with 297 additions and 12 deletions
@@ -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<Int, {}>", 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<Int64, {}>", 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<String, {}>", map, protocolId, protocolName)).append(LS);
return map;
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, protocolId)).append(LS);
return map;
@@ -746,4 +746,253 @@ class ByteBuffer {
return set
}
func writeIntIntMap(_ map: Dictionary<Int, Int>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeInt(key)
writeInt(value)
}
}
}
func readIntIntMap() -> Dictionary<Int, Int> {
let size = readInt()
var map = Dictionary<Int, Int>()
if (size > 0) {
for _ in 0..<size {
let key = readInt()
let value = readInt()
map[key] = value
}
}
return map
}
func writeIntLongMap(_ map: Dictionary<Int, Int64>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeInt(key)
writeLong(value)
}
}
}
func readIntLongMap() -> Dictionary<Int, Int64> {
let size = readInt()
var map = Dictionary<Int, Int64>()
if (size > 0) {
for _ in 0..<size {
let key = readInt()
let value = readLong()
map[key] = value
}
}
return map
}
func writeIntStringMap(_ map: Dictionary<Int, String>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeInt(key)
writeString(value)
}
}
}
func readIntStringMap() -> Dictionary<Int, String> {
let size = readInt()
var map = Dictionary<Int, String>()
if (size > 0) {
for _ in 0..<size {
let key = readInt()
let value = readString()
map[key] = value
}
}
return map
}
func writeIntPacketMap(_ map: Dictionary<Int, Any>, _ 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<Int, Any> {
let size = readInt()
var map = Dictionary<Int, Any>()
if (size > 0) {
for _ in 0..<size {
let key = readInt()
let value = readPacket(protocolId)
map[key] = value
}
}
return map
}
func writeLongIntMap(_ map: Dictionary<Int64, Int>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeLong(key)
writeInt(value)
}
}
}
func readLongIntMap() -> Dictionary<Int64, Int> {
let size = readInt()
var map = Dictionary<Int64, Int>()
if (size > 0) {
for _ in 0..<size {
let key = readLong()
let value = readInt()
map[key] = value
}
}
return map
}
func writeLongLongMap(_ map: Dictionary<Int64, Int64>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeLong(key)
writeLong(value)
}
}
}
func readLongLongMap() -> Dictionary<Int64, Int64> {
let size = readInt()
var map = Dictionary<Int64, Int64>()
if (size > 0) {
for _ in 0..<size {
let key = readLong()
let value = readLong()
map[key] = value
}
}
return map
}
func writeLongPacketMap(_ map: Dictionary<Int64, Any>, _ 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<Int64, Any> {
let size = readInt()
var map = Dictionary<Int64, Any>()
if (size > 0) {
for _ in 0..<size {
let key = readLong()
let value = readPacket(protocolId)
map[key] = value
}
}
return map
}
func writeStringIntMap(_ map: Dictionary<String, Int>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeString(key)
writeInt(value)
}
}
}
func readStringIntMap() -> Dictionary<String, Int> {
let size = readInt()
var map = Dictionary<String, Int>()
if (size > 0) {
for _ in 0..<size {
let key = readString()
let value = readInt()
map[key] = value
}
}
return map
}
func writeStringLongMap(_ map: Dictionary<String, Int64>) {
if (map.isEmpty) {
writeInt(0)
} else {
writeInt(map.count)
for (key, value) in map {
writeString(key)
writeLong(value)
}
}
}
func readStringLongMap() -> Dictionary<String, Int64> {
let size = readInt()
var map = Dictionary<String, Int64>()
if (size > 0) {
for _ in 0..<size {
let key = readString()
let value = readLong()
map[key] = value
}
}
return map
}
func writeStringPacketMap(_ map: Dictionary<String, Any>, _ 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<String, Any> {
let size = readInt()
var map = Dictionary<String, Any>()
if (size > 0) {
for _ in 0..<size {
let key = readString()
let value = readPacket(protocolId)
map[key] = value
}
}
return map
}
}