From cdff6881473bc9f401af04a12778f24dc5e65074 Mon Sep 17 00:00:00 2001 From: godotg Date: Tue, 17 Oct 2023 16:25:50 +0800 Subject: [PATCH] feat[csharp]: csharp support compatible field --- .../generate/GenerateProtocolFile.java | 2 +- .../serializer/csharp/GenerateCsUtils.java | 23 +++++++++++++++---- .../serializer/lua/GenerateLuaUtils.java | 8 +++---- .../typescript/GenerateTsUtils.java | 6 ++--- .../resources/csharp/Buffer/ByteBuffer.cs | 21 ++++++----------- .../resources/csharp/IProtocolRegistration.cs | 4 ++-- ...lManager.cs => ProtocolManagerTemplate.cs} | 8 +++---- .../main/resources/csharp/ProtocolTemplate.cs | 14 +++++++---- 8 files changed, 50 insertions(+), 36 deletions(-) rename protocol/src/main/resources/csharp/{ProtocolManager.cs => ProtocolManagerTemplate.cs} (82%) diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index a7866b68c..793f73232 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -133,7 +133,7 @@ public static void generate(GenerateOperation generateOperation) throws IOExcept // 生成C#协议 if (generateLanguages.contains(CodeLanguage.CSharp)) { GenerateCsUtils.init(generateOperation); - GenerateCsUtils.createProtocolManager(); + GenerateCsUtils.createProtocolManager(allSortedGenerateProtocols); for (var protocolRegistration : allSortedGenerateProtocols) { GenerateCsUtils.createCsProtocolFile((ProtocolRegistration) protocolRegistration); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java index 6d5924ba4..7bcf66597 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java @@ -19,6 +19,7 @@ import com.zfoo.protocol.generate.GenerateProtocolNote; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.model.Pair; +import com.zfoo.protocol.registration.IProtocolRegistration; import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.serializer.CodeLanguage; @@ -37,6 +38,7 @@ import static com.zfoo.protocol.util.FileUtils.LS; import static com.zfoo.protocol.util.StringUtils.TAB; +import static com.zfoo.protocol.util.StringUtils.TAB_ASCII; /** * @author godotg @@ -87,9 +89,8 @@ public static void clear() { /** * 生成协议依赖的工具类 */ - public static void createProtocolManager() throws IOException { - var list = List.of("csharp/ProtocolManager.cs" - , "csharp/IProtocolRegistration.cs" + public static void createProtocolManager(List protocolList) throws IOException { + var list = List.of("csharp/IProtocolRegistration.cs" , "csharp/Buffer/ByteBuffer.cs" , "csharp/Buffer/LittleEndianByteBuffer.cs" , "csharp/Buffer/BigEndianByteBuffer.cs"); @@ -99,6 +100,20 @@ public static void createProtocolManager() throws IOException { var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "csharp/"))); FileUtils.writeInputStreamToFile(createFile, fileInputStream); } + + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("csharp/ProtocolManagerTemplate.cs"); + var csBuilder = new StringBuilder(); + var initList = new ArrayList(); + for (var protocol : protocolList) { + var protocolId = protocol.protocolId(); + var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.GdScript); + csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("protocols[{}] = new {}Registration();", protocolId, protocolName, path)).append(LS); + csBuilder.append(TAB + TAB + TAB).append(StringUtils.format("protocolIdMap[typeof({})] = {};", protocolName, protocolId, path)).append(LS); + } + var initProtocols = StringUtils.joinWith(StringUtils.COMMA + LS, initList.toArray()); + protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, csBuilder.toString().trim(), initProtocols); + FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.cs")), protocolManagerTemplate, true); } /** @@ -120,7 +135,7 @@ public static void createCsProtocolFile(ProtocolRegistration registration) throw var readObject = readObject(registration); protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim() , protocolClazzName, valueOfMethod.getKey().trim(), protocolClazzName, valueOfMethod.getValue().trim() - , protocolId, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, writeObject.trim() + , protocolClazzName, protocolId, protocolClazzName, protocolClazzName, writeObject.trim() , protocolClazzName, protocolClazzName, readObject.trim()); var outputPath = StringUtils.format("{}/{}/{}.cs" diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java index ce4d9c021..3ec490c25 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java @@ -91,16 +91,16 @@ public static void createProtocolManager(List protocolLis var protocolBuilder = new StringBuilder(); for (var protocol : protocolList) { var protocolId = protocol.protocolId(); - var name = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); var path = GenerateProtocolPath.getCapitalizeProtocolPath(protocolId); if (StringUtils.isBlank(path)) { - fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"LuaProtocol.{}\")", name, name)).append(LS); + fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"LuaProtocol.{}\")", protocolName, protocolName)).append(LS); } else { fieldBuilder.append(TAB).append(StringUtils.format("local {} = require(\"LuaProtocol.{}.{}\")" - , name, path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD), name)).append(LS); + , protocolName, path.replaceAll(StringUtils.SLASH, StringUtils.PERIOD), protocolName)).append(LS); } - protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = {}", protocolId, name)).append(LS); + protocolBuilder.append(TAB).append(StringUtils.format("protocols[{}] = {}", protocolId, protocolName)).append(LS); } protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, StringUtils.EMPTY_JSON, StringUtils.EMPTY_JSON, fieldBuilder.toString().trim(), protocolBuilder.toString().trim()); FileUtils.writeStringToFile(new File(StringUtils.format("{}/{}", protocolOutputRootPath, "ProtocolManager.lua")), protocolManagerTemplate, true); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/typescript/GenerateTsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/typescript/GenerateTsUtils.java index 7ccabb8af..52e16f36f 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/typescript/GenerateTsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/typescript/GenerateTsUtils.java @@ -101,10 +101,10 @@ public static void createProtocolManager(List protocolLis var initProtocolBuilder = new StringBuilder(); for (var protocol : protocolList) { var protocolId = protocol.protocolId(); - var name = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName(); var path = GenerateProtocolPath.protocolAbsolutePath(protocolId, CodeLanguage.TypeScript); - importBuilder.append(StringUtils.format("import {} from './{}';", name, path)).append(LS); - initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, name)).append(LS); + importBuilder.append(StringUtils.format("import {} from './{}';", protocolName, path)).append(LS); + initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName)).append(LS); } diff --git a/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs b/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs index a74796dae..373ea8a79 100644 --- a/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs +++ b/protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs @@ -603,13 +603,6 @@ public string GetString(byte[] value) return Encoding.UTF8.GetString(value, 0, value.Length); } - public bool WritePacketFlag(IProtocol packet) - { - bool flag = packet == null; - WriteBool(!flag); - return flag; - } - public void WriteBooleanArray(bool[] array) { if ((array == null) || (array.Length == 0)) @@ -879,7 +872,7 @@ public void WritePacketArray(T[] array, short protocolId) int length = array.Length; for (int index = 0; index < length; index++) { - protocolRegistration.Write(this, (IProtocol) array[index]); + protocolRegistration.Write(this, array[index]); } } } @@ -1169,7 +1162,7 @@ public void WritePacketList(List list, short protocolId) int length = list.Count; for (int index = 0; index < length; index++) { - protocolRegistration.Write(this, (IProtocol) list[index]); + protocolRegistration.Write(this, list[index]); } } } @@ -1419,7 +1412,7 @@ public void WritePacketSet(HashSet set, short protocolId) WriteInt(set.Count); foreach (var element in set) { - protocolRegistration.Write(this, (IProtocol) element); + protocolRegistration.Write(this, element); } } } @@ -1556,7 +1549,7 @@ public void WriteIntPacketMap(Dictionary map, short protocolId) foreach (var element in map) { WriteInt(element.Key); - protocolRegistration.Write(this, (IProtocol) element.Value); + protocolRegistration.Write(this, element.Value); } } } @@ -1695,7 +1688,7 @@ public void WriteLongPacketMap(Dictionary map, short protocolId) foreach (var element in map) { WriteLong(element.Key); - protocolRegistration.Write(this, (IProtocol) element.Value); + protocolRegistration.Write(this, element.Value); } } } @@ -1834,7 +1827,7 @@ public void WriteStringPacketMap(Dictionary map, short protocolId) foreach (var element in map) { WriteString(element.Key); - protocolRegistration.Write(this, (IProtocol) element.Value); + protocolRegistration.Write(this, element.Value); } } } @@ -1860,7 +1853,7 @@ public Dictionary ReadStringPacketMap(short protocolId) public void WritePacket(T packet, short protocolId) { IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId); - protocolRegistration.Write(this, (IProtocol) packet); + protocolRegistration.Write(this, packet); } public T ReadPacket(short protocolId) diff --git a/protocol/src/main/resources/csharp/IProtocolRegistration.cs b/protocol/src/main/resources/csharp/IProtocolRegistration.cs index 210ef6f1c..3898f8324 100644 --- a/protocol/src/main/resources/csharp/IProtocolRegistration.cs +++ b/protocol/src/main/resources/csharp/IProtocolRegistration.cs @@ -4,9 +4,9 @@ public interface IProtocolRegistration { short ProtocolId(); - void Write(ByteBuffer buffer, IProtocol packet); + void Write(ByteBuffer buffer, object packet); - IProtocol Read(ByteBuffer buffer); + object Read(ByteBuffer buffer); } } \ No newline at end of file diff --git a/protocol/src/main/resources/csharp/ProtocolManager.cs b/protocol/src/main/resources/csharp/ProtocolManagerTemplate.cs similarity index 82% rename from protocol/src/main/resources/csharp/ProtocolManager.cs rename to protocol/src/main/resources/csharp/ProtocolManagerTemplate.cs index 40af018b9..03c480bc1 100644 --- a/protocol/src/main/resources/csharp/ProtocolManager.cs +++ b/protocol/src/main/resources/csharp/ProtocolManagerTemplate.cs @@ -8,7 +8,7 @@ public class ProtocolManager public static readonly short MAX_PROTOCOL_NUM = short.MaxValue; - private static readonly IProtocolRegistration[] protocolList = new IProtocolRegistration[MAX_PROTOCOL_NUM]; + private static readonly IProtocolRegistration[] protocols = new IProtocolRegistration[MAX_PROTOCOL_NUM]; private static readonly Dictionary protocolIdMap = new Dictionary(); @@ -19,7 +19,7 @@ public static void InitProtocol() public static IProtocolRegistration GetProtocol(short protocolId) { - var protocol = protocolList[protocolId]; + var protocol = protocols[protocolId]; if (protocol == null) { throw new Exception("[protocolId:" + protocolId + "] not exist"); @@ -28,7 +28,7 @@ public static IProtocolRegistration GetProtocol(short protocolId) return protocol; } - public static void Write(ByteBuffer buffer, IProtocol packet) + public static void Write(ByteBuffer buffer, object packet) { var protocolId = packet.ProtocolId(); // 写入协议号 @@ -38,7 +38,7 @@ public static void Write(ByteBuffer buffer, IProtocol packet) GetProtocol(protocolId).Write(buffer, packet); } - public static IProtocol Read(ByteBuffer buffer) + public static object Read(ByteBuffer buffer) { var protocolId = buffer.ReadShort(); return GetProtocol(protocolId).Read(buffer); diff --git a/protocol/src/main/resources/csharp/ProtocolTemplate.cs b/protocol/src/main/resources/csharp/ProtocolTemplate.cs index 34177d408..536d3e232 100644 --- a/protocol/src/main/resources/csharp/ProtocolTemplate.cs +++ b/protocol/src/main/resources/csharp/ProtocolTemplate.cs @@ -24,24 +24,30 @@ public short ProtocolId() return {}; } - public void Write(ByteBuffer buffer, IProtocol packet) + public void Write(ByteBuffer buffer, object packet) { - if (buffer.WritePacketFlag(packet)) + if (packet == null) { + buffer.WriteInt(0); return; } {} message = ({}) packet; {} } - public IProtocol Read(ByteBuffer buffer) + public object Read(ByteBuffer buffer) { - if (!buffer.ReadBool()) + int length = buffer.ReadInt(); + if (length == 0) { return null; } + int beforeReadIndex = buffer.ReadOffset(); {} packet = new {}(); {} + if (length > 0) { + buffer.SetReadOffset(beforeReadIndex + length); + } return packet; } }