From 23b3517ae534beb10e73467521d40c1f6c97a740 Mon Sep 17 00:00:00 2001 From: Sunset Mikoto <26019675+SunsetMkt@users.noreply.github.com> Date: Fri, 22 Aug 2025 15:57:28 +0800 Subject: [PATCH] fix: use static files by include_json_static (#1123) (#1146) * fix: use static files by include_json_static (#1123) * chore: add workflow_dispatch to workflows --- .github/workflows/docker.yml | 1 + .github/workflows/rust.yml | 3 ++- .github/workflows/typos.yml | 3 ++- pumpkin-registry/src/lib.rs | 7 +++---- pumpkin-util/src/lib.rs | 9 +++++++++ .../src/generation/feature/configured_features.rs | 7 ++++--- .../src/generation/feature/placed_features.rs | 7 ++++--- pumpkin-world/src/generation/settings.rs | 6 ++++-- pumpkin-world/src/generation/structure/mod.rs | 12 +++++++----- 9 files changed, 36 insertions(+), 19 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 9c9066bd0..9dfb779e5 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -8,6 +8,7 @@ on: push: branches: - "master" + workflow_dispatch: jobs: docker-build-deploy: diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 49a62485a..8428bb70d 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,6 +3,7 @@ name: Cargo Build, Test, and Linting on: push: pull_request: + workflow_dispatch: env: CARGO_TERM_COLOR: always @@ -77,4 +78,4 @@ jobs: - uses: actions/checkout@v5 - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} - uses: Swatinem/rust-cache@v2 - - run: cargo clippy --release --all-targets --all-features \ No newline at end of file + - run: cargo clippy --release --all-targets --all-features diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml index ffd88c379..c97702f6d 100644 --- a/.github/workflows/typos.yml +++ b/.github/workflows/typos.yml @@ -6,6 +6,7 @@ permissions: on: push: pull_request: + workflow_dispatch: env: CLICOLOR: 1 @@ -18,4 +19,4 @@ jobs: - name: Checkout Actions Repository uses: actions/checkout@v5 - name: Spell Check Repo - uses: crate-ci/typos@v1.35.4 \ No newline at end of file + uses: crate-ci/typos@v1.35.4 diff --git a/pumpkin-registry/src/lib.rs b/pumpkin-registry/src/lib.rs index 728a7d9b2..e15bd2a62 100644 --- a/pumpkin-registry/src/lib.rs +++ b/pumpkin-registry/src/lib.rs @@ -16,6 +16,7 @@ use jukebox_song::JukeboxSong; use paint::Painting; use pig::PigVariant; use pumpkin_protocol::java::client::config::RegistryEntry; +use pumpkin_util::include_json_static; use pumpkin_util::resource_location::ResourceLocation; use serde::{Deserialize, Serialize}; use trim_material::TrimMaterial; @@ -40,10 +41,8 @@ mod trim_material; mod trim_pattern; mod wolf; -pub static SYNCED_REGISTRIES: LazyLock = LazyLock::new(|| { - serde_json::from_str(include_str!("../../assets/synced_registries.json")) - .expect("Could not parse synced_registries.json registry.") -}); +pub static SYNCED_REGISTRIES: LazyLock = + LazyLock::new(|| include_json_static!("../../assets/synced_registries.json", SyncedRegistry)); pub struct Registry { pub registry_id: ResourceLocation, diff --git a/pumpkin-util/src/lib.rs b/pumpkin-util/src/lib.rs index c916d6ac1..c58c209fd 100644 --- a/pumpkin-util/src/lib.rs +++ b/pumpkin-util/src/lib.rs @@ -54,6 +54,15 @@ macro_rules! read_data_from_file { }}; } +/// Includes a JSON file on build time. Use this for static files. +#[macro_export] +macro_rules! include_json_static { + ($path:expr, $ty:ty) => {{ + serde_json::from_str::<$ty>(include_str!($path)) + .expect(concat!("Could not parse JSON file: ", $path)) + }}; +} + /// The minimum number of bits required to represent this number #[inline] pub fn encompassing_bits(count: usize) -> u8 { diff --git a/pumpkin-world/src/generation/feature/configured_features.rs b/pumpkin-world/src/generation/feature/configured_features.rs index 718d365d2..0c25baab8 100644 --- a/pumpkin-world/src/generation/feature/configured_features.rs +++ b/pumpkin-world/src/generation/feature/configured_features.rs @@ -3,7 +3,7 @@ use std::{ sync::{Arc, LazyLock}, }; -use pumpkin_util::{math::position::BlockPos, random::RandomGenerator, read_data_from_file}; +use pumpkin_util::{include_json_static, math::position::BlockPos, random::RandomGenerator}; use serde::Deserialize; use crate::{ProtoChunk, level::Level, world::BlockRegistryExt}; @@ -73,8 +73,9 @@ use super::features::{ weeping_vines::WeepingVinesFeature, }; -pub static CONFIGURED_FEATURES: LazyLock> = - LazyLock::new(|| read_data_from_file!("../../../../assets/configured_features.json")); +pub static CONFIGURED_FEATURES: LazyLock> = LazyLock::new( + || include_json_static!("../../../../assets/configured_features.json", HashMap), +); // Yes this may look ugly and you wonder why this is hard coded, but its makes sense to hardcode since we have to add logic for these in code #[derive(Deserialize)] diff --git a/pumpkin-world/src/generation/feature/placed_features.rs b/pumpkin-world/src/generation/feature/placed_features.rs index f6159844b..e53c6faf4 100644 --- a/pumpkin-world/src/generation/feature/placed_features.rs +++ b/pumpkin-world/src/generation/feature/placed_features.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use pumpkin_data::{Block, BlockDirection}; -use pumpkin_util::{HeightMap, read_data_from_file}; +use pumpkin_util::{HeightMap, include_json_static}; use serde::Deserialize; use std::collections::HashMap; use std::iter; @@ -24,8 +24,9 @@ use crate::world::BlockRegistryExt; use super::configured_features::{CONFIGURED_FEATURES, ConfiguredFeature}; -pub static PLACED_FEATURES: LazyLock> = - LazyLock::new(|| read_data_from_file!("../../../../assets/placed_feature.json")); +pub static PLACED_FEATURES: LazyLock> = LazyLock::new( + || include_json_static!("../../../../assets/placed_feature.json", HashMap), +); #[derive(Deserialize)] #[serde(untagged)] diff --git a/pumpkin-world/src/generation/settings.rs b/pumpkin-world/src/generation/settings.rs index 7e761baac..3b34480cb 100644 --- a/pumpkin-world/src/generation/settings.rs +++ b/pumpkin-world/src/generation/settings.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, sync::LazyLock}; -use pumpkin_util::read_data_from_file; +use pumpkin_util::include_json_static; use serde::Deserialize; use crate::{block::BlockStateCodec, dimension::Dimension}; @@ -8,7 +8,9 @@ use crate::{block::BlockStateCodec, dimension::Dimension}; use super::{biome_coords::to_block, height_limit::HeightLimitView, surface::rule::MaterialRule}; pub static GENERATION_SETTINGS: LazyLock> = - LazyLock::new(|| read_data_from_file!("../../../assets/chunk_gen_settings.json")); + LazyLock::new( + || include_json_static!("../../../assets/chunk_gen_settings.json", HashMap), + ); pub fn gen_settings_from_dimension(dimension: &Dimension) -> &GenerationSettings { match dimension { diff --git a/pumpkin-world/src/generation/structure/mod.rs b/pumpkin-world/src/generation/structure/mod.rs index ccb958ee9..6a2525874 100644 --- a/pumpkin-world/src/generation/structure/mod.rs +++ b/pumpkin-world/src/generation/structure/mod.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, sync::LazyLock}; use pumpkin_data::{chunk::Biome, tag::Taggable}; -use pumpkin_util::read_data_from_file; +use pumpkin_util::include_json_static; use serde::Deserialize; use crate::{ @@ -74,8 +74,10 @@ pub struct Structure { biomes: String, } -pub static STRUCTURES: LazyLock> = - LazyLock::new(|| read_data_from_file!("../../../assets/structure.json")); +pub static STRUCTURES: LazyLock> = LazyLock::new( + || include_json_static!("../../../../assets/structures.json", HashMap), +); -pub static STRUCTURE_SETS: LazyLock> = - LazyLock::new(|| read_data_from_file!("../../../assets/structure_set.json")); +pub static STRUCTURE_SETS: LazyLock> = LazyLock::new( + || include_json_static!("../../../../assets/structure_set.json", HashMap), +);