Skip to content

Commit

Permalink
feat[csharp]: csharp support compatible field
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 17, 2023
1 parent 099d511 commit cdff688
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("csharp/IProtocolRegistration.cs"
, "csharp/Buffer/ByteBuffer.cs"
, "csharp/Buffer/LittleEndianByteBuffer.cs"
, "csharp/Buffer/BigEndianByteBuffer.cs");
Expand All @@ -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<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);
}

/**
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ public static void createProtocolManager(List<IProtocolRegistration> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public static void createProtocolManager(List<IProtocolRegistration> 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);

}

Expand Down
21 changes: 7 additions & 14 deletions protocol/src/main/resources/csharp/Buffer/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -879,7 +872,7 @@ public void WritePacketArray<T>(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]);
}
}
}
Expand Down Expand Up @@ -1169,7 +1162,7 @@ public void WritePacketList<T>(List<T> 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]);
}
}
}
Expand Down Expand Up @@ -1419,7 +1412,7 @@ public void WritePacketSet<T>(HashSet<T> set, short protocolId)
WriteInt(set.Count);
foreach (var element in set)
{
protocolRegistration.Write(this, (IProtocol) element);
protocolRegistration.Write(this, element);
}
}
}
Expand Down Expand Up @@ -1556,7 +1549,7 @@ public void WriteIntPacketMap<T>(Dictionary<int, T> map, short protocolId)
foreach (var element in map)
{
WriteInt(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
Expand Down Expand Up @@ -1695,7 +1688,7 @@ public void WriteLongPacketMap<T>(Dictionary<long, T> map, short protocolId)
foreach (var element in map)
{
WriteLong(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
Expand Down Expand Up @@ -1834,7 +1827,7 @@ public void WriteStringPacketMap<T>(Dictionary<string, T> map, short protocolId)
foreach (var element in map)
{
WriteString(element.Key);
protocolRegistration.Write(this, (IProtocol) element.Value);
protocolRegistration.Write(this, element.Value);
}
}
}
Expand All @@ -1860,7 +1853,7 @@ public Dictionary<string, T> ReadStringPacketMap<T>(short protocolId)
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)
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/main/resources/csharp/IProtocolRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, short> protocolIdMap = new Dictionary<Type, short>();


Expand All @@ -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");
Expand All @@ -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();
// 写入协议号
Expand All @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions protocol/src/main/resources/csharp/ProtocolTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit cdff688

Please sign in to comment.