Skip to content

Commit

Permalink
remove build from gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jan 10, 2025
1 parent 45a591b commit 7b205fe
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Temporary Items

## Java/Gradle Stuff
.gradle
**/build/
!src/**/build/
.kotlin

Expand Down
37 changes: 37 additions & 0 deletions pumpkin-data/build/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{env, fs, path::Path, process::Command};

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

mod packet;
mod particle;
mod screen;
mod sound;

pub fn main() {
write_generated_file(packet::build(), "packet.rs");
write_generated_file(screen::build(), "screen.rs");
write_generated_file(particle::build(), "particle.rs");
write_generated_file(sound::build(), "sound.rs");
}

pub fn write_generated_file(content: TokenStream, out_file: &str) {
let out_dir = env::var_os("OUT_DIR").expect("failed to get OUT_DIR env var");
let path = Path::new(&out_dir).join(out_file);
let code = content.to_string();

fs::write(&path, code).expect("Faile to write to fs");

// Try to format the output for debugging purposes.
// Doesn't matter if rustfmt is unavailable.
let _ = Command::new("rustfmt").arg(path).output();
}

pub fn ident<I: AsRef<str>>(s: I) -> Ident {
let s = s.as_ref().trim();

// Parse the ident from a str. If the string is a Rust keyword, stick an
// underscore in front.
syn::parse_str::<Ident>(s)
.unwrap_or_else(|_| Ident::new(format!("_{s}").as_str(), Span::call_site()))
}
50 changes: 50 additions & 0 deletions pumpkin-data/build/packet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashMap;

use proc_macro2::TokenStream;
use quote::quote;
use serde::Deserialize;

use crate::ident;

#[derive(Deserialize)]
pub struct Packets {
serverbound: HashMap<String, Vec<String>>,
clientbound: HashMap<String, Vec<String>>,
}

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

let packets: Packets = serde_json::from_str(include_str!("../../assets/packets.json"))
.expect("Failed to parse packets.json");
let serverbound_consts = parse_packets(packets.serverbound);
let clientbound_consts = parse_packets(packets.clientbound);

quote!(
pub mod serverbound {
#serverbound_consts
}

pub mod clientbound {
#clientbound_consts
}
)
}

pub(crate) fn parse_packets(packets: HashMap<String, Vec<String>>) -> proc_macro2::TokenStream {
let mut consts = TokenStream::new();

for packet in packets {
let phase = packet.0;

for (id, packet_name) in packet.1.iter().enumerate() {
let packet_id = id as i32;
let name = format!("{phase}_{packet_name}").to_uppercase();
let name = ident(name);
consts.extend([quote! {
pub const #name: i32 = #packet_id;
}]);
}
}
consts
}
29 changes: 29 additions & 0 deletions pumpkin-data/build/particle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;

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

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

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)]
pub enum Particle {
#variants
}
}
}
29 changes: 29 additions & 0 deletions pumpkin-data/build/screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;

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();

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)]
pub enum WindowType {
#variants
}
}
}
29 changes: 29 additions & 0 deletions pumpkin-data/build/sound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use heck::ToPascalCase;
use proc_macro2::TokenStream;
use quote::quote;

use crate::ident;

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

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

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

variants.extend([quote! {
#name = #id,
}]);
}
quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum Sound {
#variants
}
}
}

0 comments on commit 7b205fe

Please sign in to comment.