Azalea registry (#20)

* make azalea-registry crate

* add trait feature to az-block

* registr

* registry macro

* impl Display for registry things

* registries
This commit is contained in:
mat 2022-08-27 20:31:21 -05:00 committed by GitHub
parent 029ae0e567
commit b8228a0360
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 5156 additions and 55 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

@ -9,3 +9,7 @@ version = "0.1.0"
[dependencies]
block-macros = {path = "./block-macros"}
[features]
default = ["trait"]
trait = []

View file

@ -6,3 +6,5 @@ There's two main things here, the `BlockState` enum and the `Block` trait.
`BlockState` is a simple enum with every possible block state as variant, and `Block` is a heavier trait which lets you access information about a block more easily.
Every block is a struct that implements `Block`. You can freely convert between `BlockState` and `Block` with .into().
If you don't want the `Block` trait, set default-features to false.

View file

@ -426,41 +426,43 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
quote! { BlockState::#block_name_pascal_case }
};
let block_struct = quote! {
#[derive(Debug)]
pub struct #block_struct_name {
#block_struct_fields
}
impl Block for #block_struct_name {
fn behavior(&self) -> BlockBehavior {
#block_behavior
if cfg!(feature = "trait") {
let block_struct = quote! {
#[derive(Debug)]
pub struct #block_struct_name {
#block_struct_fields
}
fn id(&self) -> &'static str {
#block_id
}
}
impl From<#block_struct_name> for BlockState {
fn from(b: #block_struct_name) -> Self {
#from_block_to_state_match
}
}
impl Default for #block_struct_name {
fn default() -> Self {
Self {
#block_default_fields
impl Block for #block_struct_name {
fn behavior(&self) -> BlockBehavior {
#block_behavior
}
fn id(&self) -> &'static str {
#block_id
}
}
}
};
block_structs.extend(block_struct);
impl From<#block_struct_name> for BlockState {
fn from(b: #block_struct_name) -> Self {
#from_block_to_state_match
}
}
impl Default for #block_struct_name {
fn default() -> Self {
Self {
#block_default_fields
}
}
}
};
block_structs.extend(block_struct);
}
}
let last_state_id = (state_id - 1) as u32;
quote! {
let mut generated = quote! {
#property_enums
#[repr(u32)]
@ -469,18 +471,6 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#block_state_enum_variants
}
#block_structs
impl From<BlockState> for Box<dyn Block> {
fn from(b: BlockState) -> Self {
let b = b as usize;
match b {
#from_state_to_block_match
_ => panic!("Invalid block state: {}", b),
}
}
}
impl BlockState {
/// Returns the highest possible state
#[inline]
@ -488,7 +478,23 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
#last_state_id
}
}
};
if cfg!(feature = "trait") {
generated.extend(quote! {
#block_structs
impl From<BlockState> for Box<dyn Block> {
fn from(b: BlockState) -> Self {
let b = b as usize;
match b {
#from_state_to_block_match
_ => panic!("Invalid block state: {}", b),
}
}
}
});
}
.into()
generated.into()
}

View file

@ -1,6 +1,8 @@
#[cfg(feature = "trait")]
mod behavior;
mod blocks;
#[cfg(feature = "trait")]
pub use behavior::BlockBehavior;
pub use blocks::*;

View file

@ -110,8 +110,7 @@ impl From<OptionalGameType> for Option<GameType> {
impl McBufReadable for OptionalGameType {
fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let id = i8::read_from(buf)?;
GameType::from_optional_id(id)
.ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 })
GameType::from_optional_id(id).ok_or(BufReadError::UnexpectedEnumVariant { id: id as i32 })
}
}

View file

@ -0,0 +1,9 @@
[package]
edition = "2021"
name = "azalea-registry"
version = "0.1.0"
# 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,3 @@
# Azalea Registry
Minecraft has a concept called "registries", which are primarily used to determine ids in the protocol. The contents of this crate are automatically generated using Minecraft's built-in data generator.

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,131 @@
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(())
}
}
}
});
// Display that uses registry ids
let mut display_items = quote! {};
for item in input.items.iter() {
let name = &item.name;
let id = &item.id;
display_items.extend(quote! {
Self::#name => write!(f, #id),
});
}
generated.extend(quote! {
impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#display_items
}
}
}
});
generated.into()
}

4850
azalea-registry/src/lib.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
azalea-block = {path = "../azalea-block"}
azalea-block = {path = "../azalea-block", default-features = false}
azalea-buf = {path = "../azalea-buf"}
azalea-core = {path = "../azalea-core"}
azalea-entity = {path = "../azalea-entity"}

View file

@ -8,18 +8,8 @@ import lib.utils
version_id = lib.code.version.get_version_id()
lib.extract.get_generator_mod_data(version_id, 'blockCollisionShapes')
# lib.download.get_burger()
# lib.download.get_client_jar(version_id)
# print('Generating data with burger')
# os.system(
# f'cd {lib.utils.get_dir_location("downloads/Burger")} && python munch.py ../client-{version_id}.jar --output ../burger-{version_id}.json --toppings blockstates'
# )
# print('Ok')
mappings = lib.download.get_mappings_for_version(version_id)
block_states_burger = lib.extract.get_block_states_burger(version_id)
ordered_blocks = lib.extract.get_ordered_blocks_burger(version_id)
@ -27,3 +17,7 @@ block_states_report = lib.extract.get_block_states_report(version_id)
lib.code.blocks.generate_blocks(
block_states_burger, block_states_report, ordered_blocks, mappings)
lib.code.utils.fmt()
print('Done!')

16
codegen/genregistries.py Normal file
View file

@ -0,0 +1,16 @@
import lib.code.registry
import lib.code.version
import lib.code.packet
import lib.code.utils
import lib.download
import lib.extract
import lib.utils
version_id = lib.code.version.get_version_id()
registries = lib.extract.get_registries_report(version_id)
lib.code.registry.generate_registries(registries)
lib.code.utils.fmt()
print('Done!')

View file

@ -0,0 +1,32 @@
from typing import Optional
from lib.utils import to_snake_case, upper_first_letter, get_dir_location, to_camel_case
from ..mappings import Mappings
import re
REGISTRIES_DIR = get_dir_location('../azalea-registry/src/lib.rs')
def generate_registries(registries: dict):
code = []
code.append('use registry_macros::registry;')
code.append('')
for registry_name, registry in registries.items():
# registry!(Block, {
# Air => "minecraft:air",
# Stone => "minecraft:stone"
# });
registry_struct_name = to_camel_case(registry_name.split(':')[1])
code.append(f'registry!({registry_struct_name}, {{')
registry_entries = sorted(
registry['entries'].items(), key=lambda x: x[1]['protocol_id'])
for variant_name, _variant in registry_entries:
variant_struct_name = to_camel_case(
variant_name.split(':')[1])
code.append(f'\t{variant_struct_name} => "{variant_name}",')
code.append('});')
code.append('')
with open(REGISTRIES_DIR, 'w') as f:
f.write('\n'.join(code))

View file

@ -24,6 +24,12 @@ def get_block_states_report(version_id: str):
return json.load(f)
def get_registries_report(version_id: str):
generate_data_from_server_jar(version_id)
with open(get_dir_location(f'downloads/generated-{version_id}/reports/registries.json'), 'r') as f:
return json.load(f)
def get_block_states_burger(version_id: str):
burger_data = get_burger_data_for_version(version_id)
return burger_data[0]['blocks']['block']

View file

@ -10,7 +10,8 @@ def to_snake_case(name: str):
def to_camel_case(name: str):
s = re.sub('_([a-z])', lambda m: m.group(1).upper(), name)
s = re.sub('[_ ](\w)', lambda m: m.group(1).upper(),
name.replace('.', '_').replace('/', '_'))
s = upper_first_letter(s)
# if the first character is a number, we need to add an underscore
# maybe we could convert it to the number name (like 2 would become "two")?

View file

@ -1,8 +1,9 @@
from lib.code.packet import fix_state
from lib.utils import PacketIdentifier, group_packets
import lib.code.utils
import lib.code.version
import lib.code.blocks
import lib.code.packet
import lib.code.utils
import lib.download
import lib.extract
import sys
@ -102,6 +103,20 @@ lib.code.version.set_protocol_version(
new_burger_data[0]['version']['protocol'])
lib.code.version.set_version_id(new_version_id)
print('Updated protocol!')
old_ordered_blocks = lib.extract.get_ordered_blocks_burger(old_version_id)
new_ordered_blocks = lib.extract.get_ordered_blocks_burger(new_version_id)
if old_ordered_blocks != new_ordered_blocks:
print('Blocks changed, updating...')
block_states_burger = lib.extract.get_block_states_burger(new_version_id)
block_states_report = lib.extract.get_block_states_report(new_version_id)
lib.code.blocks.generate_blocks(
block_states_burger, block_states_report, old_ordered_blocks, new_mappings)
lib.code.utils.fmt()
print('Done!')