feat[csharp]: csharp support compatible field

This commit is contained in:
godotg
2023-10-17 16:25:50 +08:00
parent 099d5118b3
commit cdff688147
8 changed files with 50 additions and 36 deletions
@@ -133,7 +133,7 @@ public abstract class GenerateProtocolFile {
// 生成C#协议
if (generateLanguages.contains(CodeLanguage.CSharp)) {
GenerateCsUtils.init(generateOperation);
GenerateCsUtils.createProtocolManager();
GenerateCsUtils.createProtocolManager(allSortedGenerateProtocols);
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateCsUtils.createCsProtocolFile((ProtocolRegistration) protocolRegistration);
}
@@ -19,6 +19,7 @@ import com.zfoo.protocol.generate.GenerateProtocolFile;
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 java.util.Map;
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 abstract class GenerateCsUtils {
/**
* 生成协议依赖的工具类
*/
public static void createProtocolManager() throws IOException {
var list = List.of("csharp/ProtocolManager.cs"
, "csharp/IProtocolRegistration.cs"
public static void createProtocolManager(List<IProtocolRegistration> 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 abstract class GenerateCsUtils {
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<String>();
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 abstract class GenerateCsUtils {
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"
@@ -91,16 +91,16 @@ public abstract class GenerateLuaUtils {
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);
@@ -101,10 +101,10 @@ public abstract class GenerateTsUtils {
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);
}
@@ -603,13 +603,6 @@ namespace zfoocs
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 @@ namespace zfoocs
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 @@ namespace zfoocs
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 @@ namespace zfoocs
WriteInt(set.Count);
foreach (var element in set)
{
protocolRegistration.Write(this, (IProtocol) element);
protocolRegistration.Write(this, element);
}
}
}
@@ -1556,7 +1549,7 @@ namespace zfoocs
foreach (var element in map)
{
WriteInt(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1695,7 +1688,7 @@ namespace zfoocs
foreach (var element in map)
{
WriteLong(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1834,7 +1827,7 @@ namespace zfoocs
foreach (var element in map)
{
WriteString(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
@@ -1860,7 +1853,7 @@ namespace zfoocs
public void WritePacket<T>(T packet, short protocolId)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
protocolRegistration.Write(this, (IProtocol) packet);
protocolRegistration.Write(this, packet);
}
public T ReadPacket<T>(short protocolId)
@@ -4,9 +4,9 @@
{
short ProtocolId();
void Write(ByteBuffer buffer, IProtocol packet);
void Write(ByteBuffer buffer, object packet);
IProtocol Read(ByteBuffer buffer);
object Read(ByteBuffer buffer);
}
}
@@ -8,7 +8,7 @@ namespace zfoocs
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<Type, short> protocolIdMap = new Dictionary<Type, short>();
@@ -19,7 +19,7 @@ namespace zfoocs
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 @@ namespace zfoocs
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 @@ namespace zfoocs
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);
@@ -24,24 +24,30 @@ namespace zfoocs
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;
}
}