-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,6 @@ Temporary Items | |
|
||
## Java/Gradle Stuff | ||
.gradle | ||
**/build/ | ||
!src/**/build/ | ||
.kotlin | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |