From 6415f697e5751c4034702292d1366240bbae1533 Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Sat, 11 Jan 2025 19:48:18 +0100 Subject: [PATCH] parse Biome enum at compile time --- assets/biome.json | 1 + pumpkin-data/build/biome.rs | 20 +++++++++++++++++++ pumpkin-data/build/build.rs | 2 ++ pumpkin-data/build/entity_pose.rs | 7 +++---- pumpkin-data/build/noise_parmeter.rs | 2 +- pumpkin-data/src/lib.rs | 1 + pumpkin-world/src/biome.rs | 12 +---------- pumpkin-world/src/generation/generator.rs | 2 +- .../implementation/overworld/biome/plains.rs | 2 +- .../generation/implementation/superflat.rs | 2 +- .../src/generation/implementation/test.rs | 2 +- 11 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 assets/biome.json create mode 100644 pumpkin-data/build/biome.rs diff --git a/assets/biome.json b/assets/biome.json new file mode 100644 index 00000000..46891534 --- /dev/null +++ b/assets/biome.json @@ -0,0 +1 @@ +["badlands","bamboo_jungle","basalt_deltas","beach","birch_forest","cherry_grove","cold_ocean","crimson_forest","dark_forest","deep_cold_ocean","deep_dark","deep_frozen_ocean","deep_lukewarm_ocean","deep_ocean","desert","dripstone_caves","end_barrens","end_highlands","end_midlands","eroded_badlands","flower_forest","forest","frozen_ocean","frozen_peaks","frozen_river","grove","ice_spikes","jagged_peaks","jungle","lukewarm_ocean","lush_caves","mangrove_swamp","meadow","mushroom_fields","nether_wastes","ocean","old_growth_birch_forest","old_growth_pine_taiga","old_growth_spruce_taiga","pale_garden","plains","river","savanna","savanna_plateau","small_end_islands","snowy_beach","snowy_plains","snowy_slopes","snowy_taiga","soul_sand_valley","sparse_jungle","stony_peaks","stony_shore","sunflower_plains","swamp","taiga","the_end","the_void","warm_ocean","warped_forest","windswept_forest","windswept_gravelly_hills","windswept_hills","windswept_savanna","wooded_badlands"] \ No newline at end of file diff --git a/pumpkin-data/build/biome.rs b/pumpkin-data/build/biome.rs new file mode 100644 index 00000000..fd092635 --- /dev/null +++ b/pumpkin-data/build/biome.rs @@ -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/biome.json"); + + let biomes: Vec = serde_json::from_str(include_str!("../../assets/biome.json")) + .expect("Failed to parse entity_pose.json"); + let variants = array_to_tokenstream(biomes); + + quote! { + #[derive(Clone, Copy)] + #[repr(u8)] + pub enum Biome { + #variants + } + } +} diff --git a/pumpkin-data/build/build.rs b/pumpkin-data/build/build.rs index 143dc0b2..ce9ee25d 100644 --- a/pumpkin-data/build/build.rs +++ b/pumpkin-data/build/build.rs @@ -5,6 +5,7 @@ use heck::ToPascalCase; use proc_macro2::{Span, TokenStream}; use syn::Ident; +mod biome; mod chunk_status; mod entity_pose; mod entity_type; @@ -31,6 +32,7 @@ pub fn main() { write_generated_file(world_event::build(), "world_event.rs"); write_generated_file(entity_type::build(), "entity_type.rs"); write_generated_file(noise_parmeter::build(), "noise_parmeter.rs"); + write_generated_file(biome::build(), "biome.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 index ac1eaca8..60e4d83b 100644 --- a/pumpkin-data/build/entity_pose.rs +++ b/pumpkin-data/build/entity_pose.rs @@ -6,10 +6,9 @@ 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); + let poses: Vec = serde_json::from_str(include_str!("../../assets/entity_pose.json")) + .expect("Failed to parse entity_pose.json"); + let variants = array_to_tokenstream(poses); quote! { #[derive(Clone, Copy)] diff --git a/pumpkin-data/build/noise_parmeter.rs b/pumpkin-data/build/noise_parmeter.rs index a6d08330..c27e8e4d 100644 --- a/pumpkin-data/build/noise_parmeter.rs +++ b/pumpkin-data/build/noise_parmeter.rs @@ -21,7 +21,7 @@ pub(crate) fn build() -> TokenStream { let mut variants = TokenStream::new(); for (name, paremter) in json.iter() { - let raw_name = name; + let raw_name = format!("minecraft:{name}"); let name = ident(name.to_uppercase()); let first_octave = paremter.first_octave; let amplitudes = &paremter.amplitudes; diff --git a/pumpkin-data/src/lib.rs b/pumpkin-data/src/lib.rs index fce727ed..a9d3c9fb 100644 --- a/pumpkin-data/src/lib.rs +++ b/pumpkin-data/src/lib.rs @@ -16,6 +16,7 @@ pub mod sound { } pub mod chunk { + include!(concat!(env!("OUT_DIR"), "/biome.rs")); include!(concat!(env!("OUT_DIR"), "/noise_parmeter.rs")); include!(concat!(env!("OUT_DIR"), "/chunk_status.rs")); } diff --git a/pumpkin-world/src/biome.rs b/pumpkin-world/src/biome.rs index 337a582c..fbe1f3a7 100644 --- a/pumpkin-world/src/biome.rs +++ b/pumpkin-world/src/biome.rs @@ -1,15 +1,5 @@ use enum_dispatch::enum_dispatch; -use serde::{Deserialize, Serialize}; - -// TODO make this work with the protocol -// Send by the registry -#[derive(Serialize, Deserialize, Clone, Copy)] -#[non_exhaustive] -pub enum Biome { - Plains, - SnowyTiga, - // TODO list all Biomes -} +use pumpkin_data::chunk::Biome; #[derive(Clone)] #[enum_dispatch(BiomeSupplierImpl)] diff --git a/pumpkin-world/src/generation/generator.rs b/pumpkin-world/src/generation/generator.rs index 56bfd4d4..e30ac46a 100644 --- a/pumpkin-world/src/generation/generator.rs +++ b/pumpkin-world/src/generation/generator.rs @@ -1,8 +1,8 @@ use noise::Perlin; +use pumpkin_data::chunk::Biome; use pumpkin_util::math::vector2::Vector2; use pumpkin_util::math::vector3::Vector3; -use crate::biome::Biome; use crate::block::block_state::BlockState; use crate::chunk::{ChunkData, Subchunks}; use crate::coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates}; diff --git a/pumpkin-world/src/generation/implementation/overworld/biome/plains.rs b/pumpkin-world/src/generation/implementation/overworld/biome/plains.rs index 48f5d0e8..1d68db78 100644 --- a/pumpkin-world/src/generation/implementation/overworld/biome/plains.rs +++ b/pumpkin-world/src/generation/implementation/overworld/biome/plains.rs @@ -1,10 +1,10 @@ use noise::Perlin; +use pumpkin_data::chunk::Biome; use pumpkin_macros::block_state; use pumpkin_util::math::vector2::Vector2; use rand::Rng; use crate::{ - biome::Biome, chunk::Subchunks, coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates}, generation::{ diff --git a/pumpkin-world/src/generation/implementation/superflat.rs b/pumpkin-world/src/generation/implementation/superflat.rs index 32f1c737..e419f5c6 100644 --- a/pumpkin-world/src/generation/implementation/superflat.rs +++ b/pumpkin-world/src/generation/implementation/superflat.rs @@ -1,7 +1,7 @@ +use pumpkin_data::chunk::Biome; use pumpkin_util::math::vector2::Vector2; use crate::{ - biome::Biome, block::block_state::BlockState, coordinates::XZBlockCoordinates, generation::{ diff --git a/pumpkin-world/src/generation/implementation/test.rs b/pumpkin-world/src/generation/implementation/test.rs index ac5173d8..525107c3 100644 --- a/pumpkin-world/src/generation/implementation/test.rs +++ b/pumpkin-world/src/generation/implementation/test.rs @@ -5,10 +5,10 @@ use std::{ use dashmap::{DashMap, Entry}; use num_traits::Zero; +use pumpkin_data::chunk::Biome; use pumpkin_util::math::{vector2::Vector2, vector3::Vector3}; use crate::{ - biome::Biome, block::block_state::BlockState, chunk::{ChunkData, Subchunks}, coordinates::{