parse sound category at compile time
This commit is contained in:
parent
82386a57c4
commit
c95b27eafb
1
assets/sound_category.json
Normal file
1
assets/sound_category.json
Normal file
@ -0,0 +1 @@
|
||||
["MASTER","MUSIC","RECORDS","WEATHER","BLOCKS","HOSTILE","NEUTRAL","PLAYERS","AMBIENT","VOICE"]
|
||||
@ -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;
|
||||
|
||||
@ -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");
|
||||
@ -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) {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)]
|
||||
|
||||
@ -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)]
|
||||
|
||||
@ -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)]
|
||||
|
||||
20
pumpkin-data/build/sound_category.rs
Normal file
20
pumpkin-data/build/sound_category.rs
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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"));
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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::{
|
||||
@ -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::{
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user