Skip to content

Commit

Permalink
parse sound category at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jan 11, 2025
1 parent 82386a5 commit c95b27e
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 69 deletions.
1 change: 1 addition & 0 deletions assets/sound_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["MASTER","MUSIC","RECORDS","WEATHER","BLOCKS","HOSTILE","NEUTRAL","PLAYERS","AMBIENT","VOICE"]
16 changes: 16 additions & 0 deletions pumpkin-data/build/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use quote::quote;
use std::{env, fs, path::Path, process::Command};

use heck::ToPascalCase;
use proc_macro2::{Span, TokenStream};
use syn::Ident;

Expand All @@ -9,6 +11,7 @@ mod packet;
mod particle;
mod screen;
mod sound;
mod sound_category;

pub fn main() {
write_generated_file(packet::build(), "packet.rs");
Expand All @@ -17,6 +20,19 @@ pub fn main() {
write_generated_file(sound::build(), "sound.rs");
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");
}

pub fn array_to_tokenstream(array: Vec<String>) -> TokenStream {
let mut variants = TokenStream::new();

for item in array.iter() {
let name = ident(item.to_pascal_case());
variants.extend([quote! {
#name,
}]);
}
variants
}

pub fn write_generated_file(content: TokenStream, out_file: &str) {
Expand Down
13 changes: 3 additions & 10 deletions pumpkin-data/build/game_event.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;
use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/game_event.json");

let game_event: Vec<String> =
let game_events: Vec<String> =
serde_json::from_str(include_str!("../../assets/game_event.json"))
.expect("Failed to parse game_event.json");
let mut variants = TokenStream::new();
let variants = array_to_tokenstream(game_events);

for event in game_event.iter() {
let name = ident(event.to_pascal_case());
variants.extend([quote! {
#name,
}]);
}
quote! {
#[repr(u8)]
pub enum GameEvent {
Expand Down
13 changes: 2 additions & 11 deletions pumpkin-data/build/particle.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;
use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/particles.json");

let particle: Vec<String> = serde_json::from_str(include_str!("../../assets/particles.json"))
.expect("Failed to parse particles.json");
let mut variants = TokenStream::new();
let variants = array_to_tokenstream(particle);

for (id, particle) in particle.iter().enumerate() {
let id = id as u8;
let name = ident(particle.to_pascal_case());

variants.extend([quote! {
#name = #id,
}]);
}
quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
Expand Down
13 changes: 2 additions & 11 deletions pumpkin-data/build/screen.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;
use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/screens.json");

let screens: Vec<String> = serde_json::from_str(include_str!("../../assets/screens.json"))
.expect("Failed to parse screens.json");
let mut variants = TokenStream::new();
let variants = array_to_tokenstream(screens);

for (id, screen) in screens.iter().enumerate() {
let id = id as u8;
let name = ident(screen.to_pascal_case());

variants.extend([quote! {
#name = #id,
}]);
}
quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
Expand Down
13 changes: 2 additions & 11 deletions pumpkin-data/build/sound.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;
use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/sounds.json");

let sound: Vec<String> = serde_json::from_str(include_str!("../../assets/sounds.json"))
.expect("Failed to parse sounds.json");
let mut variants = TokenStream::new();
let variants = array_to_tokenstream(sound);

for (id, sound) in sound.iter().enumerate() {
let id = id as u16;
let name = ident(sound.to_pascal_case());

variants.extend([quote! {
#name = #id,
}]);
}
quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
Expand Down
20 changes: 20 additions & 0 deletions pumpkin-data/build/sound_category.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use proc_macro2::TokenStream;
use quote::quote;

use crate::array_to_tokenstream;

pub(crate) fn build() -> TokenStream {
println!("cargo:rerun-if-changed=assets/sound_category.json");

let sound_categories: Vec<String> =
serde_json::from_str(include_str!("../../assets/sound_category.json"))
.expect("Failed to parse sound_category.json");
let variants = array_to_tokenstream(sound_categories);

quote! {
#[repr(u8)]
pub enum SoundCategory {
#variants
}
}
}
4 changes: 4 additions & 0 deletions pumpkin-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ pub mod chunk_status {
pub mod game_event {
include!(concat!(env!("OUT_DIR"), "/game_event.rs"));
}

pub mod sound_category {
include!(concat!(env!("OUT_DIR"), "/sound_category.rs"));
}
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/c_entity_sound_effect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bytes::BufMut;
use pumpkin_data::packet::clientbound::PLAY_SOUND_ENTITY;
use pumpkin_data::{packet::clientbound::PLAY_SOUND_ENTITY, sound_category::SoundCategory};
use pumpkin_macros::client_packet;

use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundCategory, SoundEvent, VarInt};
use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundEvent, VarInt};

#[client_packet(PLAY_SOUND_ENTITY)]
pub struct CEntitySoundEffect {
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/c_sound_effect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bytes::BufMut;
use pumpkin_data::packet::clientbound::PLAY_SOUND;
use pumpkin_data::{packet::clientbound::PLAY_SOUND, sound_category::SoundCategory};
use pumpkin_macros::client_packet;

use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundCategory, SoundEvent, VarInt};
use crate::{bytebuf::ByteBufMut, ClientPacket, IDOrSoundEvent, SoundEvent, VarInt};

#[client_packet(PLAY_SOUND)]
pub struct CSoundEffect {
Expand Down
13 changes: 0 additions & 13 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ impl TryFrom<VarInt> for ConnectionState {
}
}

pub enum SoundCategory {
Master,
Music,
Records,
Weather,
Blocks,
Hostile,
Neutral,
Players,
Ambient,
Voice,
}

#[derive(Serialize)]
pub struct IDOrSoundEvent {
pub id: VarInt,
Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crossbeam::atomic::AtomicCell;
use pumpkin_config::{ADVANCED_CONFIG, BASIC_CONFIG};
use pumpkin_data::sound::Sound;
use pumpkin_data::{sound::Sound, sound_category::SoundCategory};
use pumpkin_entity::{entity_type::EntityType, EntityId};
use pumpkin_inventory::player::PlayerInventory;
use pumpkin_protocol::server::play::{
Expand All @@ -29,7 +29,7 @@ use pumpkin_protocol::{
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSetPlayerGround, SSwingArm, SUseItem,
SUseItemOn,
},
RawPacket, ServerPacket, SoundCategory,
RawPacket, ServerPacket,
};
use pumpkin_protocol::{client::play::CUpdateTime, codec::var_int::VarInt};
use pumpkin_protocol::{
Expand Down
3 changes: 1 addition & 2 deletions pumpkin/src/net/combat.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::f32::consts::PI;

use pumpkin_data::{particle::Particle, sound::Sound};
use pumpkin_data::{particle::Particle, sound::Sound, sound_category::SoundCategory};
use pumpkin_protocol::{
client::play::{CEntityVelocity, CParticle},
codec::var_int::VarInt,
SoundCategory,
};
use pumpkin_util::math::vector3::Vector3;
use pumpkin_world::item::ItemStack;
Expand Down
7 changes: 2 additions & 5 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ use crate::{
};
use level_time::LevelTime;
use pumpkin_config::BasicConfiguration;
use pumpkin_data::sound::Sound;
use pumpkin_data::{sound::Sound, sound_category::SoundCategory};
use pumpkin_entity::{entity_type::EntityType, pose::EntityPose, EntityId};
use pumpkin_protocol::client::play::{CBlockUpdate, CRespawn, CSoundEffect, CWorldEvent};
use pumpkin_protocol::{
client::play::CLevelEvent,
codec::{identifier::Identifier, var_int::VarInt},
};
use pumpkin_protocol::{
client::play::{CBlockUpdate, CRespawn, CSoundEffect, CWorldEvent},
SoundCategory,
};
use pumpkin_protocol::{
client::play::{
CChunkData, CGameEvent, CLogin, CPlayerInfoUpdate, CRemoveEntities, CRemovePlayerInfo,
Expand Down

0 comments on commit c95b27e

Please sign in to comment.