1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

registry macro

This commit is contained in:
mat 2022-08-27 15:07:38 -05:00
parent c3924d47bb
commit 4f209ee622
6 changed files with 148 additions and 36 deletions

16
Cargo.lock generated
View file

@ -230,6 +230,13 @@ dependencies = [
"uuid",
]
[[package]]
name = "azalea-registry"
version = "0.1.0"
dependencies = [
"registry-macros",
]
[[package]]
name = "azalea-world"
version = "0.1.0"
@ -1138,6 +1145,15 @@ version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]]
name = "registry-macros"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "resolv-conf"
version = "0.7.0"

View file

@ -14,6 +14,7 @@ members = [
"azalea-block",
"azalea-entity",
"azalea-buf",
"azalea-registry",
]
[profile.release]

View file

@ -1,8 +1,9 @@
[package]
edition = "2021"
name = "azalea-registry"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
registry-macros = {path = "./registry-macros"}

View file

@ -0,0 +1,14 @@
[package]
edition = "2021"
name = "registry-macros"
version = "0.1.0"
[lib]
proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
proc-macro2 = "1.0.39"
quote = "1.0.18"
syn = "1.0.95"

View file

@ -0,0 +1,112 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{
self, braced,
parse::{Parse, ParseStream, Result},
parse_macro_input,
punctuated::Punctuated,
Ident, LitStr, Token,
};
struct RegistryItem {
name: Ident,
id: String,
}
struct Registry {
name: Ident,
items: Vec<RegistryItem>,
}
impl Parse for RegistryItem {
// Air => "minecraft:air"
fn parse(input: ParseStream) -> Result<Self> {
let name = input.parse()?;
input.parse::<Token![=>]>()?;
let id = input.parse::<LitStr>()?.value();
Ok(RegistryItem { name, id })
}
}
impl Parse for Registry {
fn parse(input: ParseStream) -> Result<Self> {
// Block, {
// Air => "minecraft:air",
// Stone => "minecraft:stone"
// }
let name = input.parse()?;
let _ = input.parse::<Token![,]>()?;
let content;
braced!(content in input);
let items: Punctuated<RegistryItem, Token![,]> =
content.parse_terminated(RegistryItem::parse)?;
Ok(Registry {
name,
items: items.into_iter().collect(),
})
}
}
#[proc_macro]
pub fn registry(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Registry);
let name = input.name;
let mut generated = quote! {};
// enum Block {
// Air = 0,
// Stone,
// }
let mut enum_items = quote! {};
for (i, item) in input.items.iter().enumerate() {
let name = &item.name;
let protocol_id = i as u32;
enum_items.extend(quote! {
#name = #protocol_id,
});
}
generated.extend(quote! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum #name {
#enum_items
}
});
let max_id = input.items.len() as u32;
generated.extend(quote! {
impl #name {
/// Transmutes a u32 to a #name.
///
/// # Safety
/// The `id` should be at most #max_id.
#[inline]
pub unsafe fn from_u32_unchecked(id: u32) -> Self {
std::mem::transmute::<u32, #name>(id)
}
#[inline]
pub fn is_valid_id(id: u32) -> bool {
id <= #max_id
}
}
});
generated.extend(quote! {
impl TryFrom<u32> for #name {
type Error = ();
/// Safely converts a state id to a block state.
fn try_from(id: u32) -> Result<Self, Self::Error> {
if Self::is_valid_id(id) {
Ok(unsafe { Self::from_u32_unchecked(id) })
} else {
Err(())
}
}
}
});
generated.into()
}

View file

@ -1,38 +1,6 @@
use registry_macros::registry;
registry!(Block, {
Air => "minecraft:air",
Stone => "minecraft:stone"
})
// enum Block {
// Air = 0,
// Stone,
// }
// impl Block {
// /// Transmutes a u32 to a Block.
// ///
// /// # Safety
// /// The `id` should be at most {}.
// #[inline]
// pub unsafe fn from_u32_unchecked(id: u32) -> Self {
// mem::transmute::<u32, Block>(id)
// }
// #[inline]
// pub fn is_valid_id(id: u32) -> bool {
// id <= 100
// }
// }
// impl TryFrom<u32> for Block {
// type Error = ();
// /// Safely converts a state id to a block state.
// fn try_from(id: u32) -> Result<Self, Self::Error> {
// if Self::is_valid_state(state_id) {
// Ok(unsafe { Self::from_u32_unsafe(state_id) })
// } else {
// Err(())
// }
// }
// }
});