Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 19, 2024
2 parents c944853 + ea5388c commit 98d293b
Show file tree
Hide file tree
Showing 50 changed files with 11,584 additions and 11,408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static String formatTemplate(String template, Map<CodeTemplatePlaceholder
if (formatLine.contains(codeTemplatePlaceholder.placeholder)) {
var placeholder = placeholderMap.get(codeTemplatePlaceholder);
if (placeholder == null) {
throw new RunException("placeholder:[{}] not exist, and add [{}] to your placeholderMap", placeholder, placeholder);
throw new RunException("placeholder:[{}] not exist, and add [{}] to your placeholderMap", codeTemplatePlaceholder, codeTemplatePlaceholder);
}
formatLine = formatLine.replace(codeTemplatePlaceholder.placeholder, placeholder);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private String protocol_write_serialization(ProtocolRegistration registration) {
var fieldRegistrations = registration.getFieldRegistrations();
var goBuilder = new StringBuilder();
if (registration.isCompatible()) {
goBuilder.append("var beforeWriteIndex = buffer.WriteOffset()").append(LS);
goBuilder.append("var beforeWriteIndex = buffer.GetWriteOffset()").append(LS);
goBuilder.append(StringUtils.format("buffer.WriteInt({})", registration.getPredictionLength())).append(LS);
} else {
goBuilder.append("buffer.WriteInt(-1)").append(LS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public String fieldDefaultValue(Field field, IFieldRegistration fieldRegistratio
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer:writeBoolean({})", objectStr)).append(LS);
builder.append(StringUtils.format("buffer:writeBool({})", objectStr)).append(LS);
}

@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.localVariableId++;

GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("local {} = buffer:readBoolean()", result)).append(LS);
builder.append(StringUtils.format("local {} = buffer:readBool()", result)).append(LS);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ProtocolManager = preload("res://{}/ProtocolManager.gd")
const ProtocolManager = preload("res://zfoogd/ProtocolManager.gd")

const EMPTY: String = ""

Expand Down Expand Up @@ -39,9 +39,12 @@ func compatibleRead(beforeReadIndex: int, length: int) -> bool:
return length != -1 && getReadOffset() < length + beforeReadIndex

# -------------------------------------------------get/set-------------------------------------------------
func getBuffer() -> StreamPeerBuffer:
return buffer

func setWriteOffset(writeIndex: int) -> void:
if (writeIndex > buffer.get_size()):
var template = "writeIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})"
var template = "writeIndex[{}] out of bounds exception: readOffset: {}, writeOffset: {} (expected: 0 <= readOffset <= writeOffset <= capacity: {})"
printerr(template.format([writeIndex, readOffset, writeOffset, buffer.get_size()], "{}"))
return
writeOffset = writeIndex
Expand All @@ -52,7 +55,7 @@ func getWriteOffset() -> int:

func setReadOffset(readIndex: int) -> void:
if (readIndex > writeOffset):
var template = "readIndex[{}] out of bounds exception: readerIndex: {}, writerIndex: {} (expected: 0 <= readerIndex <= writerIndex <= capacity: {})"
var template = "readIndex[{}] out of bounds exception: readOffset: {}, writeOffset: {} (expected: 0 <= readOffset <= writeOffset <= capacity: {})"
printerr(template.format([readIndex, readOffset, writeOffset, buffer.size()], "{}"))
return
readOffset = readIndex
Expand All @@ -64,15 +67,21 @@ func getReadOffset() -> int:
func isReadable() -> bool:
return writeOffset > readOffset

func toBytes() -> PackedByteArray:
return buffer.data_array.slice(0, writeOffset)

func newInstance(protocolId: int):
return ProtocolManager.newInstance(protocolId)

# -------------------------------------------------write/read-------------------------------------------------
func writePackedByteArray(value: PackedByteArray):
func writeBytes(value: PackedByteArray):
var length: int = value.size()
buffer.put_partial_data(value)
writeOffset += length
pass

func toPackedByteArray() -> PackedByteArray:
return buffer.data_array.slice(0, writeOffset)
func readBytes(length: int) -> PackedByteArray:
return buffer.data_array.slice(0, length)

func writeBool(value: bool) -> void:
var byte: int = 1 if value else 0
Expand Down Expand Up @@ -303,10 +312,7 @@ func readPacket(protocolId):
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
return protocolRegistration.read(self)

func newInstance(protocolId: int):
return ProtocolManager.newInstance(protocolId)

func writeBooleanArray(array):
func writeBoolArray(array):
if (array == null):
writeInt(0)
else:
Expand All @@ -315,7 +321,7 @@ func writeBooleanArray(array):
writeBool(element)
pass

func readBooleanArray() -> Array[bool]:
func readBoolArray() -> Array[bool]:
var array: Array[bool] = []
var size = readInt()
if (size > 0):
Expand Down Expand Up @@ -454,7 +460,8 @@ func writePacketArray(array, protocolId):

func readPacketArray(protocolId):
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
var array = Array([], typeof(protocolRegistration), StringName("RefCounted"), protocolRegistration)
var protocol = ProtocolManager.getProtocolClass(protocolId)
var array = Array([], typeof(protocol), StringName("RefCounted"), protocol)
var size = readInt()
if (size > 0):
for index in range(size):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
${protocol_note}
${protocol_field_definition}

func protocolId() -> int:
return ${protocol_id}

func get_class_name() -> String:
return "${protocol_name}"

func _to_string() -> String:
const jsonTemplate = "${protocol_json}"
var params = [${protocol_to_string}]
return jsonTemplate.format(params, "{}")
27 changes: 6 additions & 21 deletions protocol/src/main/resources/gdscript/ProtocolClassTemplate.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,13 @@ ${protocol_note}
class ${protocol_name}:
${protocol_field_definition}

const PROTOCOL_ID = ${protocol_id}
const PROTOCOL_CLASS_NAME = "${protocol_name}"
func protocolId() -> int:
return ${protocol_id}

func get_class_name() -> String:
return "${protocol_name}"

func _to_string() -> String:
const jsonTemplate = "${protocol_json}"
var params = [${protocol_to_string}]
return jsonTemplate.format(params, "{}")

static func write(buffer, packet):
if (packet == null):
buffer.writeInt(0)
return
${protocol_write_serialization}
pass

static func read(buffer):
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet = buffer.newInstance(PROTOCOL_ID)
${protocol_read_deserialization}
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
return packet
return jsonTemplate.format(params, "{}")
14 changes: 10 additions & 4 deletions protocol/src/main/resources/gdscript/ProtocolManagerTemplate.gd
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
${protocol_imports}

const protocols: Dictionary = {
static var protocols: Dictionary = {}
static var protocolClassMap: Dictionary = {}

static func initProtocol():
${protocol_manager_registrations}
}
pass

static func getProtocol(protocolId: int):
return protocols[protocolId]

static func getProtocolClass(protocolId: int):
return protocolClassMap[protocolId]

static func newInstance(protocolId: int):
var protocol = protocols[protocolId]
var protocol = protocolClassMap[protocolId]
return protocol.new()

static func write(buffer, packet):
var protocolId: int = packet.PROTOCOL_ID
var protocolId: int = packet.protocolId()
buffer.writeShort(protocolId)
var protocol = protocols[protocolId]
protocol.write(buffer, packet)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ${protocol_name}Registration:
func write(buffer: ByteBuffer, packet: Object):
if (packet == null):
buffer.writeInt(0)
return
${protocol_write_serialization}
pass

func read(buffer: ByteBuffer):
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet = buffer.newInstance(${protocol_id})
${protocol_read_deserialization}
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
return packet
27 changes: 2 additions & 25 deletions protocol/src/main/resources/gdscript/ProtocolTemplate.gd
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
const PROTOCOL_ID = ${protocol_id}
const PROTOCOL_CLASS_NAME = "${protocol_name}"
${protocol_imports}
${protocol_note}
${protocol_field_definition}

func _to_string() -> String:
const jsonTemplate = "${protocol_json}"
var params = [${protocol_to_string}]
return jsonTemplate.format(params, "{}")
${protocol_class}

static func write(buffer, packet):
if (packet == null):
buffer.writeInt(0)
return
${protocol_write_serialization}
pass

static func read(buffer):
var length = buffer.readInt()
if (length == 0):
return null
var beforeReadIndex = buffer.getReadOffset()
var packet = buffer.newInstance(PROTOCOL_ID)
${protocol_read_deserialization}
if (length > 0):
buffer.setReadOffset(beforeReadIndex + length)
return packet
${protocol_registration}
6 changes: 5 additions & 1 deletion protocol/src/main/resources/gdscript/ProtocolsTemplate.gd
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
${protocol_class}
${protocol_imports}

${protocol_class}

${protocol_registration}
Loading

0 comments on commit 98d293b

Please sign in to comment.