Improve Packet Extractor

Optimize packets.json
- Reduce lines from 1394 -> 256
- Reduces File size 23KB-> 6.9KB
This commit is contained in:
Snowiiii 2024-11-14 18:52:47 +01:00
parent 987aaad3e9
commit ac31d1c83b
14 changed files with 365 additions and 1444 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,14 @@
package de.snowii.extractor.extractors
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import de.snowii.extractor.Extractor
import io.netty.buffer.ByteBuf
import net.minecraft.network.NetworkPhase
import net.minecraft.network.NetworkState
import net.minecraft.network.listener.PacketListener
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.listener.ClientPacketListener
import net.minecraft.network.listener.ServerCrashSafePacketListener
import net.minecraft.network.packet.PacketType
import net.minecraft.network.state.*
import net.minecraft.server.MinecraftServer
@ -18,32 +20,85 @@ class Packets : Extractor.Extractor {
}
override fun extract(server: MinecraftServer): JsonElement {
val packetsJson = JsonArray()
val packetsJson = JsonObject()
serializeFactory(HandshakeStates.C2S_FACTORY, packetsJson)
serializeFactory(QueryStates.C2S_FACTORY, packetsJson)
serializeFactory(QueryStates.S2C_FACTORY, packetsJson)
serializeFactory(LoginStates.C2S_FACTORY, packetsJson)
serializeFactory(LoginStates.S2C_FACTORY, packetsJson)
serializeFactory(ConfigurationStates.C2S_FACTORY, packetsJson)
serializeFactory(ConfigurationStates.S2C_FACTORY, packetsJson)
serializeFactory(PlayStateFactories.C2S, packetsJson)
serializeFactory(PlayStateFactories.S2C, packetsJson)
val serverBound = arrayOf(
QueryStates.S2C_FACTORY,
LoginStates.S2C_FACTORY,
ConfigurationStates.S2C_FACTORY,
PlayStateFactories.S2C
)
val clientBound = arrayOf(
HandshakeStates.C2S_FACTORY,
QueryStates.C2S_FACTORY,
LoginStates.C2S_FACTORY,
ConfigurationStates.C2S_FACTORY,
PlayStateFactories.C2S
)
val serverBoundJson = serializeServerBound(serverBound)
val clientBoundJson = serializeClientBound(clientBound)
packetsJson.add("serverbound", serverBoundJson)
packetsJson.add("clientbound", clientBoundJson)
return packetsJson
}
private fun <T : PacketListener?, B : ByteBuf?> serializeFactory(
factory: NetworkState.Factory<T, B>,
json: JsonArray
) {
private fun serializeServerBound(
packets: Array<NetworkState.Factory<out ClientPacketListener, out PacketByteBuf>>
): JsonObject {
val statusArray = JsonObject()
val loginArray = JsonObject()
val configArray = JsonObject()
val playArray = JsonObject()
for (factory in packets) {
factory.forEachPacketType { type: PacketType<*>, i: Int ->
val packetJson = JsonObject()
packetJson.addProperty("name", type.id().path)
packetJson.addProperty("phase", factory.phase().id)
packetJson.addProperty("side", factory.side().getName())
packetJson.addProperty("id", i)
json.add(packetJson)
when (factory.phase()!!) {
NetworkPhase.HANDSHAKING -> error("Server bound Packet should have no handshake")
NetworkPhase.PLAY -> playArray.addProperty(type.id().path, i)
NetworkPhase.STATUS -> statusArray.addProperty(type.id().path, i)
NetworkPhase.LOGIN -> loginArray.addProperty(type.id().path, i)
NetworkPhase.CONFIGURATION -> configArray.addProperty(type.id().path, i)
}
}
}
val finalJson = JsonObject()
finalJson.add("status", statusArray)
finalJson.add("login", loginArray)
finalJson.add("config", configArray)
finalJson.add("play", playArray)
return finalJson
}
private fun serializeClientBound(
packets: Array<NetworkState.Factory<out ServerCrashSafePacketListener, out ByteBuf>>
): JsonObject {
val handshakeArray = JsonObject()
val statusArray = JsonObject()
val loginArray = JsonObject()
val configArray = JsonObject()
val playArray = JsonObject()
for (factory in packets) {
factory.forEachPacketType { type: PacketType<*>, i: Int ->
when (factory.phase()!!) {
NetworkPhase.HANDSHAKING -> handshakeArray.addProperty(type.id().path, i)
NetworkPhase.PLAY -> playArray.addProperty(type.id().path, i)
NetworkPhase.STATUS -> statusArray.addProperty(type.id().path, i)
NetworkPhase.LOGIN -> loginArray.addProperty(type.id().path, i)
NetworkPhase.CONFIGURATION -> configArray.addProperty(type.id().path, i)
}
}
}
val finalJson = JsonObject()
finalJson.add("handshake", handshakeArray)
finalJson.add("status", statusArray)
finalJson.add("login", loginArray)
finalJson.add("config", configArray)
finalJson.add("play", playArray)
return finalJson
}
}

View File

@ -5,37 +5,41 @@ use quote::quote;
use serde::Deserialize;
#[derive(Deserialize)]
pub struct Packet {
name: String,
phase: String,
side: String,
id: u16,
pub struct Packets {
serverbound: HashMap<String, HashMap<String, u8>>,
clientbound: HashMap<String, HashMap<String, u8>>,
}
static PACKETS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Packet>>(include_str!("../../assets/packets.json"))
static PACKETS: LazyLock<Packets> = LazyLock::new(|| {
serde_json::from_str(include_str!("../../assets/packets.json"))
.expect("Could not parse packets.json registry.")
.into_iter()
.map(|val| (format!("{}:{}:{}", val.side, val.phase, val.name), val.id))
.collect()
});
pub(crate) fn packet_clientbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string();
let packet_name = input_string.trim_matches('"');
let id = PACKETS
.get(&format!("clientbound:{}", packet_name))
.expect("Invalid Packet");
let packet_name_split: Vec<&str> = packet_name.split(":").collect();
let phase = PACKETS
.serverbound
.get(packet_name_split[0])
.expect("Invalid Phase");
let id = phase
.get(packet_name_split[1])
.expect("Invalid Packet name");
quote! { #id }
}
pub(crate) fn packet_serverbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string();
let packet_name = input_string.trim_matches('"');
let packet_name_split: Vec<&str> = packet_name.split(":").collect();
let id = PACKETS
.get(&format!("serverbound:{}", packet_name))
.expect("Invalid Packet");
let phase = PACKETS
.clientbound
.get(packet_name_split[0])
.expect("Invalid Phase");
let id = phase
.get(packet_name_split[1])
.expect("Invalid Packet name");
quote! { #id }
}

View File

@ -4,7 +4,7 @@ use pumpkin_macros::client_packet;
use serde::Serialize;
#[derive(Serialize)]
#[client_packet("configuration:resource_pack_push")]
#[client_packet("config:resource_pack_push")]
pub struct CConfigAddResourcePack<'a> {
uuid: uuid::Uuid,
url: &'a str,

View File

@ -1,7 +1,7 @@
use pumpkin_macros::client_packet;
#[derive(serde::Serialize)]
#[client_packet("configuration:disconnect")]
#[client_packet("config:disconnect")]
pub struct CConfigDisconnect<'a> {
reason: &'a str,
}

View File

@ -3,7 +3,7 @@ use pumpkin_macros::client_packet;
use crate::Identifier;
#[derive(serde::Serialize)]
#[client_packet("configuration:cookie_request")]
#[client_packet("config:cookie_request")]
pub struct CCookieRequest {
key: Identifier,
}

View File

@ -1,7 +1,7 @@
use pumpkin_macros::client_packet;
#[derive(serde::Serialize)]
#[client_packet("configuration:finish_configuration")]
#[client_packet("config:finish_configuration")]
pub struct CFinishConfig {}
impl Default for CFinishConfig {

View File

@ -2,7 +2,7 @@ use pumpkin_macros::client_packet;
use crate::{bytebuf::ByteBuffer, ClientPacket, KnownPack};
#[client_packet("configuration:select_known_packs")]
#[client_packet("config:select_known_packs")]
pub struct CKnownPacks<'a> {
known_packs: &'a [KnownPack<'a>],
}

View File

@ -2,7 +2,7 @@ use pumpkin_macros::client_packet;
use serde::Serialize;
#[derive(Serialize)]
#[client_packet("configuration:custom_payload")]
#[client_packet("config:custom_payload")]
pub struct CPluginMessage<'a> {
channel: &'a str,
data: &'a [u8],

View File

@ -2,7 +2,7 @@ use pumpkin_macros::client_packet;
use crate::{bytebuf::ByteBuffer, ClientPacket};
#[client_packet("configuration:registry_data")]
#[client_packet("config:registry_data")]
pub struct CRegistryData<'a> {
registry_id: &'a str,
entries: &'a [RegistryEntry<'a>],

View File

@ -1,5 +1,5 @@
use pumpkin_macros::server_packet;
#[derive(serde::Deserialize)]
#[server_packet("configuration:finish_configuration")]
#[server_packet("config:finish_configuration")]
pub struct SAcknowledgeFinishConfig {}

View File

@ -3,7 +3,7 @@ use pumpkin_macros::server_packet;
use crate::VarInt;
#[derive(serde::Deserialize)]
#[server_packet("configuration:client_information")]
#[server_packet("config:client_information")]
pub struct SClientInformationConfig {
pub locale: String, // 16
pub view_distance: i8,

View File

@ -3,7 +3,7 @@ use pumpkin_macros::server_packet;
use crate::VarInt;
#[derive(serde::Deserialize)]
#[server_packet("configuration:select_known_packs")]
#[server_packet("config:select_known_packs")]
pub struct SKnownPacks {
pub known_pack_count: VarInt,
// known_packs: &'a [KnownPack]

View File

@ -5,7 +5,7 @@ use crate::{
Identifier, ServerPacket,
};
#[server_packet("configuration:custom_payload")]
#[server_packet("config:custom_payload")]
pub struct SPluginMessage {
pub channel: Identifier,
pub data: Vec<u8>,