feat[cpp]: compatible field of inside protocol class

This commit is contained in:
godotg
2023-10-15 17:35:25 +08:00
parent 1c87c12035
commit 98504cb124
2 changed files with 11 additions and 5 deletions
@@ -262,6 +262,9 @@ public abstract class GenerateCppUtils {
serializer.writeObject(cppBuilder, "message->" + field.getName(), 3, field, fieldRegistration);
}
}
if (registration.isCompatible()) {
cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS);
}
return cppBuilder.toString();
}
@@ -275,18 +278,16 @@ public abstract class GenerateCppUtils {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
cppBuilder.append(TAB + TAB + TAB).append(StringUtils.format("if (!buffer.isReadable()) { return packet; }")).append(LS);
}
if (field.isAnnotationPresent(Compatible.class)) {
cppBuilder.append(TAB + TAB + TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = cppSerializer(fieldRegistration.serializer()).readObject(cppBuilder, 4, field, fieldRegistration);
cppBuilder.append(TAB + TAB + TAB);
cppBuilder.append(TAB + TAB + TAB + TAB);
if (ProtocolManager.isProtocolClass(field.getType())) {
cppBuilder.append(StringUtils.format("packet->{} = *{};", field.getName(), compatibleReadObject));
} else {
cppBuilder.append(StringUtils.format("packet->{} = {};", field.getName(), compatibleReadObject));
}
cppBuilder.append(LS).append(TAB + TAB + TAB).append("}").append(LS);
continue;
}
@@ -46,10 +46,15 @@ namespace zfoo {
IProtocol *read(ByteBuffer &buffer) override {
auto *packet = new {}();
if (!buffer.readBool()) {
auto length = buffer.readInt();
if (length == 0) {
return packet;
}
auto beforeReadIndex = buffer.readerIndex();
{}
if (length > 0) {
buffer.readerIndex(beforeReadIndex + length);
}
return packet;
}
};