Skip to content

Commit

Permalink
Add Array to Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Newspicel committed Jan 8, 2023
1 parent e99daa1 commit b39ac5b
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 15 deletions.
52 changes: 41 additions & 11 deletions scripts/packet_generate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ def generate_array(self, field_name: str, array: dict, infos: dict, round: int =
"extra_classes": []
}

if "countType" not in array:
return clazz

count_type = array["countType"]
array_type = array["type"]
array_type_type = type(array_type)
Expand Down Expand Up @@ -597,19 +600,22 @@ def generate_option_container(self, parent_field_name: str, container: list, inf
if field_sub_type_name == "array":
print("Not supported yet")
elif field_sub_type_name == "switch":
print(field_type)
return_value = self.generate_switch(field_name, field_type, infos)
self.add_to_clazz_field(return_value, clazz)
pass
elif field_sub_type_name == "bitfield":
print("Not supported yet")
else:
print(field_sub_type_name)
# raise Exception("Not supported")
raise Exception(f"Not supported yet: {field_sub_type_name}")
else:
raise Exception("Not supported")
raise Exception(f"Not supported yet: {field_type_type}")

return clazz

def generate_switch(self, field_name: str, switch: dict, infos: dict) -> dict:
if type(switch) is list:
switch = switch[1]

clazz = {
"fields": [],
"serialize": [],
Expand All @@ -625,6 +631,9 @@ def generate_switch(self, field_name: str, switch: dict, infos: dict) -> dict:
info_serialize_value_var_type = infos["serialize_value_var_type"] if "serialize_value_var_type" in infos else "value"

compare_to_field = switch["compareTo"].replace("../", "")
if "/" in compare_to_field:
return clazz

compare_to_field_type = ""
compare_to_field_var_name = camel_case(compare_to_field)
compare_to_field_var_name_with_value = f"{info_serialize_value_var_type}.{compare_to_field_var_name}"
Expand Down Expand Up @@ -663,6 +672,9 @@ def generate_switch(self, field_name: str, switch: dict, infos: dict) -> dict:
if field_value == ['buffer', {'countType': 'varint'}]:
field_value = "buffer"

if field_value == ['option', ['buffer', {'countType': 'varint'}]]:
field_value = ['option', 'buffer']

field_type_type = type(field_value)

if field_type_type is str:
Expand Down Expand Up @@ -700,9 +712,32 @@ def generate_switch(self, field_name: str, switch: dict, infos: dict) -> dict:
serialize += [f"{field} -> if ({info_serialize_value_var_type}.{boolean_field_var_name}!!) {info_serialize_var_name}.{kotlin_type['serialize'] % (info_serialize_value_var_type + '.' + field_var_name + '!!')}"]
continue

# Array?
if field_value[0] == "array":
return_value = self.generate_array(field_name, field_value[1], infos, 1)
if len(return_value["fields"]) > 0:
fields_build = return_value["fields"][0].split(", // ", 1)
fields_build_second = f" // {fields_build[1]}" if len(return_value["fields"]) > 1 else ""
fields_build = fields_build[0] + f"?,{fields_build_second}"

if fields_build not in clazz["fields"]:
clazz["fields"] += [fields_build]
clazz["other_imports"] += return_value["other_imports"]
clazz["extra_classes"] += return_value["extra_classes"]
clazz["var_list"] += return_value["var_list"]
clazz["docs"] += return_value["docs"]

deserialize_str = return_value['deserialize'][0].split(" = ", 1)[1]
serialize_build = return_value['serialize'][0].split(") {", 1)
serialize_str = serialize_build[0] + "!!) {" + serialize_build[1]

deserialize += [f"{field} -> {deserialize_str}"]
serialize += [f"{field} -> {serialize_str}"]
continue

other_switch = True
other_switch_value = field_value
if field_value[0] == "container" or field_value[0] == "array":
if field_value[0] == "container":
for field_container in field_value[1]:
inside = False
for it in other_switch_value_dict:
Expand Down Expand Up @@ -750,16 +785,12 @@ def generate_switch(self, field_name: str, switch: dict, infos: dict) -> dict:

return_value = self.generate_switch(field_name, build_other_switch, infos)
self.add_to_clazz_field(return_value, clazz)
elif other_switch_value_type == "array":
print("Not supported yet")
elif other_switch_value_type == "option":
other_switch_value_type = other_switch_value[1][0]
if other_switch_value_type == "container":
for field in other_switch_value_dict:
field_info = field['value']
field_keys = field['keys']
if type(field_info) is str or field_info == "{'countType': 'varint'}":
continue

if type(field_info) is list:
for field_sub in field_info:
Expand Down Expand Up @@ -876,7 +907,7 @@ def generate_field(self, field_name: str, field, infos: dict, round: int = 0) ->
field_type = field[0]
field = field[1]
if field_type == "array":
pass
print("Option array not supported yet")
elif field_type == "container":
return_value = self.generate_option_container(field_name, field, infos)
else:
Expand All @@ -886,7 +917,6 @@ def generate_field(self, field_name: str, field, infos: dict, round: int = 0) ->
elif field_type == "buffer":
buffer_count_type = field["countType"]
if buffer_count_type == "varint":
print("Buffer varint")
return_value = self.generate_basic_type(field_name, "buffer", infos)
else:
raise Exception("Not supported")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ data class DeclareRecipesPacket(
"minecraft:stonecutting" -> arrayInput.readString()
else -> null
}
val ingredients = when (type) {
else -> null
}
val result = when (type) {
else -> null
}
Expand All @@ -37,6 +40,9 @@ data class DeclareRecipesPacket(
"minecraft:crafting_shaped" -> arrayInput.readVarInt()
else -> null
}
val ingredients = when (type) {
else -> null
}
val ingredient = when (type) {
else -> null
}
Expand Down Expand Up @@ -66,6 +72,9 @@ data class DeclareRecipesPacket(
when (arrayValue.type) {
else -> {}
}
when (arrayValue.type) {
else -> {}
}
when (arrayValue.type) {
"minecraft:crafting_shaped" -> arrayOutput.writeVarInt(arrayValue.width!!)
else -> {}
Expand All @@ -83,6 +92,9 @@ data class DeclareRecipesPacket(
when (arrayValue.type) {
else -> {}
}
when (arrayValue.type) {
else -> {}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import java.util.UUID
* @param hasUnsignedContent unsignedContent is present
* @param unsignedContent unsignedContent
* @param filterType filterType
* @param filterTypeMask filterTypeMask
* @param type type
* @param networkName networkName
* @param hasNetworkTargetName networkTargetName is present
Expand All @@ -36,6 +37,7 @@ data class PlayerChatPacket(
val hasUnsignedContent: Boolean,
val unsignedContent: String?,
val filterType: Int, // varint
val filterTypeMask: List<Long>?,
val type: Int, // varint
val networkName: String,
val hasNetworkTargetName: Boolean,
Expand All @@ -53,12 +55,16 @@ data class PlayerChatPacket(
val hasUnsignedContent = input.readBoolean()
val unsignedContent = if (hasUnsignedContent) input.readString() else null
val filterType = input.readVarInt()
val filterTypeMask = when (filterType) {
2 -> input.readVarIntArray { arrayInput -> arrayInput.readLong() }
else -> null
}
val type = input.readVarInt()
val networkName = input.readString()
val hasNetworkTargetName = input.readBoolean()
val networkTargetName = if (hasNetworkTargetName) input.readString() else null

return PlayerChatPacket(senderUuid, headerSignature, plainMessage, hasFormattedMessage, formattedMessage, timestamp, salt, hasUnsignedContent, unsignedContent, filterType, type, networkName, hasNetworkTargetName, networkTargetName)
return PlayerChatPacket(senderUuid, headerSignature, plainMessage, hasFormattedMessage, formattedMessage, timestamp, salt, hasUnsignedContent, unsignedContent, filterType, filterTypeMask, type, networkName, hasNetworkTargetName, networkTargetName)
}

override fun serialize(output: MinecraftProtocolSerializeInterface<*>, value: PlayerChatPacket) {
Expand All @@ -72,6 +78,10 @@ data class PlayerChatPacket(
output.writeBoolean(value.hasUnsignedContent)
if (value.hasUnsignedContent) output.writeString(value.unsignedContent!!)
output.writeVarInt(value.filterType)
when (value.filterType) {
2 -> output.writeVarIntArray(value.filterTypeMask!!) { arrayValue, arrayOutput -> arrayOutput.writeLong(arrayValue) }
else -> {}
}
output.writeVarInt(value.type)
output.writeString(value.networkName)
output.writeBoolean(value.hasNetworkTargetName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ data class PlayerInfoPacket(
0 -> arrayInput.readString()
else -> null
}
val properties = when (action) {
0 -> arrayInput.readVarIntArray { arrayInput1 ->
val name = arrayInput1.readString()
val value = arrayInput1.readString()
val hasSignature = arrayInput1.readBoolean()
val signature = if (hasSignature) arrayInput1.readString() else null

return@readVarIntArray PlayerInfoPacketProperties(name, value, hasSignature, signature)
}
else -> null
}
val gamemode = when (action) {
0 -> arrayInput.readVarInt()
1 -> arrayInput.readVarInt()
Expand Down Expand Up @@ -54,8 +65,24 @@ data class PlayerInfoPacket(
0 -> if (hasTimestamp!!) arrayInput.readLong() else null
else -> null
}
val hasPublicKey = when (action) {
0 -> arrayInput.readBoolean()
else -> null
}
val publicKey = when (action) {
0 -> if (hasPublicKey!!) arrayInput.readVarIntByteArray() else null
else -> null
}
val hasSignature = when (action) {
0 -> arrayInput.readBoolean()
else -> null
}
val signature = when (action) {
0 -> if (hasSignature!!) arrayInput.readVarIntByteArray() else null
else -> null
}

return@readVarIntArray PlayerInfoPacketData(uuid, name, gamemode, ping, hasDisplayName, displayName, hasTimestamp, timestamp)
return@readVarIntArray PlayerInfoPacketData(uuid, name, properties, gamemode, ping, hasDisplayName, displayName, hasTimestamp, timestamp, hasPublicKey, publicKey, hasSignature, signature)
}

return PlayerInfoPacket(action, data)
Expand All @@ -70,6 +97,17 @@ data class PlayerInfoPacket(
0 -> arrayOutput.writeString(arrayValue.name!!)
else -> {}
}
when (value.action) {
0 ->
arrayOutput.writeVarIntArray(arrayValue.properties!!) { arrayValue1, arrayOutput1 ->
arrayOutput1.writeString(arrayValue1.name)
arrayOutput1.writeString(arrayValue1.value)
arrayOutput1.writeBoolean(arrayValue1.hasSignature)
if (arrayValue1.hasSignature) arrayOutput1.writeString(arrayValue1.signature!!)
}

else -> {}
}
when (value.action) {
0 -> arrayOutput.writeVarInt(arrayValue.gamemode!!)
1 -> arrayOutput.writeVarInt(arrayValue.gamemode!!)
Expand Down Expand Up @@ -98,30 +136,71 @@ data class PlayerInfoPacket(
0 -> if (arrayValue.hasTimestamp!!) arrayOutput.writeLong(arrayValue.timestamp!!)
else -> {}
}
when (value.action) {
0 -> arrayOutput.writeBoolean(arrayValue.hasPublicKey!!)
else -> {}
}
when (value.action) {
0 -> if (arrayValue.hasPublicKey!!) arrayOutput.writeVarIntByteArray(arrayValue.publicKey!!)
else -> {}
}
when (value.action) {
0 -> arrayOutput.writeBoolean(arrayValue.hasSignature!!)
else -> {}
}
when (value.action) {
0 -> if (arrayValue.hasSignature!!) arrayOutput.writeVarIntByteArray(arrayValue.signature!!)
else -> {}
}
}
}
}
}

/**
* PlayerInfoPacketProperties
*
* @param name name
* @param value value
* @param hasSignature signature is present
* @param signature signature
*/
data class PlayerInfoPacketProperties(
val name: String,
val value: String,
val hasSignature: Boolean,
val signature: String?,
)

/**
* PlayerInfoPacketData
*
* @param uuid uuid
* @param name name
* @param properties list of PlayerInfoPacketProperties
* @param gamemode gamemode
* @param ping ping
* @param hasDisplayName hasDisplayName
* @param displayName displayName
* @param hasTimestamp hasTimestamp
* @param timestamp timestamp
* @param hasPublicKey hasPublicKey
* @param publicKey publicKey
* @param hasSignature hasSignature
* @param signature signature
*/
data class PlayerInfoPacketData(
val uuid: UUID,
val name: String?,
val properties: List<PlayerInfoPacketProperties>?,
val gamemode: Int?, // varint
val ping: Int?, // varint
val hasDisplayName: Boolean?,
val displayName: String?,
val hasTimestamp: Boolean?,
val timestamp: Long?,
val hasPublicKey: Boolean?,
val publicKey: ByteArray?,
val hasSignature: Boolean?,
val signature: ByteArray?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.layercraft.packetlib.serialization.MinecraftProtocolSerializeInterface
* @param formatting formatting
* @param prefix prefix
* @param suffix suffix
* @param players players
* @see <a href="https://wiki.vg/index.php?title=Protocol&oldid=17873#Update_Teams">https://wiki.vg/Protocol#Update_Teams</a>
*/

Expand All @@ -30,6 +31,7 @@ data class TeamsPacket(
val formatting: Int?, // varint
val prefix: String?,
val suffix: String?,
val players: List<String>?,
) : ClientBoundPacket {
companion object : PacketSerializer<TeamsPacket> {
override fun deserialize(input: MinecraftProtocolDeserializeInterface<*>): TeamsPacket {
Expand Down Expand Up @@ -70,8 +72,14 @@ data class TeamsPacket(
2 -> input.readString()
else -> null
}
val players = when (mode.toInt()) {
0 -> input.readVarIntArray { arrayInput -> arrayInput.readString() }
3 -> input.readVarIntArray { arrayInput -> arrayInput.readString() }
4 -> input.readVarIntArray { arrayInput -> arrayInput.readString() }
else -> null
}

return TeamsPacket(team, mode, name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix)
return TeamsPacket(team, mode, name, friendlyFire, nameTagVisibility, collisionRule, formatting, prefix, suffix, players)
}

override fun serialize(output: MinecraftProtocolSerializeInterface<*>, value: TeamsPacket) {
Expand Down Expand Up @@ -112,6 +120,12 @@ data class TeamsPacket(
2 -> output.writeString(value.suffix!!)
else -> {}
}
when (value.mode.toInt()) {
0 -> output.writeVarIntArray(value.players!!) { arrayValue, arrayOutput -> arrayOutput.writeString(arrayValue) }
3 -> output.writeVarIntArray(value.players!!) { arrayValue, arrayOutput -> arrayOutput.writeString(arrayValue) }
4 -> output.writeVarIntArray(value.players!!) { arrayValue, arrayOutput -> arrayOutput.writeString(arrayValue) }
else -> {}
}
}
}
}
Loading

0 comments on commit b39ac5b

Please sign in to comment.