From 621bc31f488d77a0f5d15ae2ecab8167a306de87 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Sat, 11 Jan 2025 16:20:31 +0100 Subject: [PATCH] parse entity pose at compile time --- assets/entity_pose.json | 1 + pumpkin-data/build/build.rs | 2 ++ pumpkin-data/build/entity_pose.rs | 21 +++++++++++++++++++++ pumpkin-data/src/lib.rs | 4 ++++ pumpkin-entity/src/lib.rs | 1 - pumpkin-entity/src/pose.rs | 22 ---------------------- pumpkin/src/entity/mod.rs | 3 ++- pumpkin/src/world/mod.rs | 4 ++-- 8 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 assets/entity_pose.json create mode 100644 pumpkin-data/build/entity_pose.rs delete mode 100644 pumpkin-entity/src/pose.rs diff --git a/assets/entity_pose.json b/assets/entity_pose.json new file mode 100644 index 000000000..7f4d1ab0c --- /dev/null +++ b/assets/entity_pose.json @@ -0,0 +1 @@ +["STANDING","FALL_FLYING","SLEEPING","SWIMMING","SPIN_ATTACK","CROUCHING","LONG_JUMPING","DYING","CROAKING","USING_TONGUE","SITTING","ROARING","SNIFFING","EMERGING","DIGGING","SLIDING","SHOOTING","INHALING"] \ No newline at end of file diff --git a/pumpkin-data/build/build.rs b/pumpkin-data/build/build.rs index 83f87d75e..3748b6ea4 100644 --- a/pumpkin-data/build/build.rs +++ b/pumpkin-data/build/build.rs @@ -6,6 +6,7 @@ use proc_macro2::{Span, TokenStream}; use syn::Ident; mod chunk_status; +mod entity_pose; mod game_event; mod packet; mod particle; @@ -21,6 +22,7 @@ pub fn main() { write_generated_file(chunk_status::build(), "chunk_status.rs"); write_generated_file(game_event::build(), "game_event.rs"); write_generated_file(sound_category::build(), "sound_category.rs"); + write_generated_file(entity_pose::build(), "entity_pose.rs"); } pub fn array_to_tokenstream(array: Vec) -> TokenStream { diff --git a/pumpkin-data/build/entity_pose.rs b/pumpkin-data/build/entity_pose.rs new file mode 100644 index 000000000..ac1eaca86 --- /dev/null +++ b/pumpkin-data/build/entity_pose.rs @@ -0,0 +1,21 @@ +use proc_macro2::TokenStream; +use quote::quote; + +use crate::array_to_tokenstream; + +pub(crate) fn build() -> TokenStream { + println!("cargo:rerun-if-changed=assets/entity_pose.json"); + + let sound_categories: Vec = + serde_json::from_str(include_str!("../../assets/entity_pose.json")) + .expect("Failed to parse entity_pose.json"); + let variants = array_to_tokenstream(sound_categories); + + quote! { + #[derive(Clone, Copy)] + #[repr(u8)] + pub enum EntityPose { + #variants + } + } +} diff --git a/pumpkin-data/src/lib.rs b/pumpkin-data/src/lib.rs index 1ffd43534..221f6e02c 100644 --- a/pumpkin-data/src/lib.rs +++ b/pumpkin-data/src/lib.rs @@ -25,3 +25,7 @@ pub mod game_event { pub mod sound_category { include!(concat!(env!("OUT_DIR"), "/sound_category.rs")); } + +pub mod entity_pose { + include!(concat!(env!("OUT_DIR"), "/entity_pose.rs")); +} diff --git a/pumpkin-entity/src/lib.rs b/pumpkin-entity/src/lib.rs index b820b1009..414085ebb 100644 --- a/pumpkin-entity/src/lib.rs +++ b/pumpkin-entity/src/lib.rs @@ -1,4 +1,3 @@ pub mod entity_type; -pub mod pose; pub type EntityId = i32; diff --git a/pumpkin-entity/src/pose.rs b/pumpkin-entity/src/pose.rs deleted file mode 100644 index c750821cc..000000000 --- a/pumpkin-entity/src/pose.rs +++ /dev/null @@ -1,22 +0,0 @@ -#[derive(Clone, Copy)] -#[repr(i32)] -pub enum EntityPose { - Standing = 0, - FallFlying, - Sleeping, - Swimming, - SpinAttack, - Crouching, - LongJumping, - Dying, - Croaking, - UsingTongue, - Sitting, - Roaring, - Sniffing, - Emerging, - Digging, - Sliding, - Shooting, - Inhaling, -} diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index 6e46b193e..6a22718f4 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -2,7 +2,8 @@ use core::f32; use std::sync::{atomic::AtomicBool, Arc}; use crossbeam::atomic::AtomicCell; -use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId}; +use pumpkin_data::entity_pose::EntityPose; +use pumpkin_entity::{entity_type::EntityType, EntityId}; use pumpkin_protocol::{ client::play::{CHeadRot, CSetEntityMetadata, CTeleportEntity, CUpdateEntityRot, Metadata}, codec::var_int::VarInt, diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index a8501631d..5aa0fa508 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -11,8 +11,8 @@ use crate::{ }; use level_time::LevelTime; use pumpkin_config::BasicConfiguration; -use pumpkin_data::{sound::Sound, sound_category::SoundCategory}; -use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId}; +use pumpkin_data::{entity_pose::EntityPose, sound::Sound, sound_category::SoundCategory}; +use pumpkin_entity::{entity_type::EntityType, EntityId}; use pumpkin_protocol::client::play::{CBlockUpdate, CRespawn, CSoundEffect, CWorldEvent}; use pumpkin_protocol::{ client::play::CLevelEvent,