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 package de.snowii.extractor.extractors
import com.google.gson.JsonArray
import com.google.gson.JsonElement import com.google.gson.JsonElement
import com.google.gson.JsonObject import com.google.gson.JsonObject
import de.snowii.extractor.Extractor import de.snowii.extractor.Extractor
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import net.minecraft.network.NetworkPhase
import net.minecraft.network.NetworkState 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.packet.PacketType
import net.minecraft.network.state.* import net.minecraft.network.state.*
import net.minecraft.server.MinecraftServer import net.minecraft.server.MinecraftServer
@ -18,32 +20,85 @@ class Packets : Extractor.Extractor {
} }
override fun extract(server: MinecraftServer): JsonElement { override fun extract(server: MinecraftServer): JsonElement {
val packetsJson = JsonArray() val packetsJson = JsonObject()
serializeFactory(HandshakeStates.C2S_FACTORY, packetsJson) val serverBound = arrayOf(
serializeFactory(QueryStates.C2S_FACTORY, packetsJson) QueryStates.S2C_FACTORY,
serializeFactory(QueryStates.S2C_FACTORY, packetsJson) LoginStates.S2C_FACTORY,
serializeFactory(LoginStates.C2S_FACTORY, packetsJson) ConfigurationStates.S2C_FACTORY,
serializeFactory(LoginStates.S2C_FACTORY, packetsJson) PlayStateFactories.S2C
serializeFactory(ConfigurationStates.C2S_FACTORY, packetsJson) )
serializeFactory(ConfigurationStates.S2C_FACTORY, packetsJson)
serializeFactory(PlayStateFactories.C2S, packetsJson) val clientBound = arrayOf(
serializeFactory(PlayStateFactories.S2C, packetsJson) 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 return packetsJson
} }
private fun <T : PacketListener?, B : ByteBuf?> serializeFactory(
factory: NetworkState.Factory<T, B>, private fun serializeServerBound(
json: JsonArray 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 -> factory.forEachPacketType { type: PacketType<*>, i: Int ->
val packetJson = JsonObject() when (factory.phase()!!) {
packetJson.addProperty("name", type.id().path) NetworkPhase.HANDSHAKING -> error("Server bound Packet should have no handshake")
packetJson.addProperty("phase", factory.phase().id) NetworkPhase.PLAY -> playArray.addProperty(type.id().path, i)
packetJson.addProperty("side", factory.side().getName()) NetworkPhase.STATUS -> statusArray.addProperty(type.id().path, i)
packetJson.addProperty("id", i) NetworkPhase.LOGIN -> loginArray.addProperty(type.id().path, i)
json.add(packetJson) 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; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Packet { pub struct Packets {
name: String, serverbound: HashMap<String, HashMap<String, u8>>,
phase: String, clientbound: HashMap<String, HashMap<String, u8>>,
side: String,
id: u16,
} }
static PACKETS: LazyLock<HashMap<String, u16>> = LazyLock::new(|| { static PACKETS: LazyLock<Packets> = LazyLock::new(|| {
serde_json::from_str::<Vec<Packet>>(include_str!("../../assets/packets.json")) serde_json::from_str(include_str!("../../assets/packets.json"))
.expect("Could not parse packets.json registry.") .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 { pub(crate) fn packet_clientbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string(); let input_string = item.to_string();
let packet_name = input_string.trim_matches('"'); let packet_name = input_string.trim_matches('"');
let packet_name_split: Vec<&str> = packet_name.split(":").collect();
let id = PACKETS let phase = PACKETS
.get(&format!("clientbound:{}", packet_name)) .serverbound
.expect("Invalid Packet"); .get(packet_name_split[0])
.expect("Invalid Phase");
let id = phase
.get(packet_name_split[1])
.expect("Invalid Packet name");
quote! { #id } quote! { #id }
} }
pub(crate) fn packet_serverbound(item: TokenStream) -> proc_macro2::TokenStream { pub(crate) fn packet_serverbound(item: TokenStream) -> proc_macro2::TokenStream {
let input_string = item.to_string(); let input_string = item.to_string();
let packet_name = input_string.trim_matches('"'); let packet_name = input_string.trim_matches('"');
let packet_name_split: Vec<&str> = packet_name.split(":").collect();
let id = PACKETS let phase = PACKETS
.get(&format!("serverbound:{}", packet_name)) .clientbound
.expect("Invalid Packet"); .get(packet_name_split[0])
.expect("Invalid Phase");
let id = phase
.get(packet_name_split[1])
.expect("Invalid Packet name");
quote! { #id } quote! { #id }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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