mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 23:44:38 +00:00
start work on optimizing block macros
This commit is contained in:
parent
79cf19f93e
commit
4247945df1
3 changed files with 102 additions and 39 deletions
|
@ -426,45 +426,45 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
|
||||||
quote! { BlockState::#block_name_pascal_case }
|
quote! { BlockState::#block_name_pascal_case }
|
||||||
};
|
};
|
||||||
|
|
||||||
let block_struct = quote! {
|
let block_struct = quote! {
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct #block_struct_name {
|
pub struct #block_struct_name {
|
||||||
#block_struct_fields
|
#block_struct_fields
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Block for #block_struct_name {
|
impl Block for #block_struct_name {
|
||||||
fn behavior(&self) -> BlockBehavior {
|
fn behavior(&self) -> BlockBehavior {
|
||||||
#block_behavior
|
#block_behavior
|
||||||
}
|
}
|
||||||
fn id(&self) -> &'static str {
|
fn id(&self) -> &'static str {
|
||||||
#block_id
|
#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 From<#block_struct_name> for BlockState {
|
block_structs.extend(block_struct);
|
||||||
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;
|
let last_state_id = (state_id - 1) as u32;
|
||||||
let mut generated = quote! {
|
let mut generated = quote! {
|
||||||
#property_enums
|
#property_enums
|
||||||
|
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub enum BlockState {
|
pub enum BlockState {
|
||||||
#block_state_enum_variants
|
#block_state_enum_variants
|
||||||
}
|
}
|
||||||
|
@ -476,21 +476,28 @@ pub fn make_block_states(input: TokenStream) -> TokenStream {
|
||||||
#last_state_id
|
#last_state_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for BlockState {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
// having a big match statement here would take up 700kb
|
||||||
|
f.write_str("BlockState")
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
generated.extend(quote! {
|
generated.extend(quote! {
|
||||||
#block_structs
|
#block_structs
|
||||||
|
|
||||||
impl From<BlockState> for Box<dyn Block> {
|
impl From<BlockState> for Box<dyn Block> {
|
||||||
fn from(b: BlockState) -> Self {
|
fn from(b: BlockState) -> Self {
|
||||||
let b = b as usize;
|
let b = b as usize;
|
||||||
match b {
|
match b {
|
||||||
#from_state_to_block_match
|
#from_state_to_block_match
|
||||||
_ => panic!("Invalid block state: {}", b),
|
_ => panic!("Invalid block state: {}", b),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
generated.into()
|
generated.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,4 +63,10 @@ mod tests {
|
||||||
assert!(BlockState::try_from(BlockState::max_state()).is_ok());
|
assert!(BlockState::try_from(BlockState::max_state()).is_ok());
|
||||||
assert!(BlockState::try_from(BlockState::max_state() + 1).is_err());
|
assert!(BlockState::try_from(BlockState::max_state() + 1).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_blockstate() {
|
||||||
|
let box_block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air);
|
||||||
|
assert_eq!(box_block.id(), "air");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ pub struct BitSet {
|
||||||
data: Vec<u64>,
|
data: Vec<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ADDRESS_BITS_PER_WORD: usize = 6;
|
||||||
|
|
||||||
// the Index trait requires us to return a reference, but we can't do that
|
// the Index trait requires us to return a reference, but we can't do that
|
||||||
impl BitSet {
|
impl BitSet {
|
||||||
pub fn new(size: usize) -> Self {
|
pub fn new(size: usize) -> Self {
|
||||||
|
@ -18,6 +20,54 @@ impl BitSet {
|
||||||
pub fn index(&self, index: usize) -> bool {
|
pub fn index(&self, index: usize) -> bool {
|
||||||
(self.data[index / 64] & (1u64 << (index % 64))) != 0
|
(self.data[index / 64] & (1u64 << (index % 64))) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// private static int wordIndex(int bitIndex) {
|
||||||
|
// return bitIndex >> ADDRESS_BITS_PER_WORD;
|
||||||
|
// }
|
||||||
|
pub fn word_index(bit_index: usize) -> usize {
|
||||||
|
bit_index >> ADDRESS_BITS_PER_WORD
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_from_to(&mut self, from: usize, to: usize) {
|
||||||
|
assert!(from <= to);
|
||||||
|
assert!(to <= self.data.len() * 64);
|
||||||
|
assert!(to > 0);
|
||||||
|
|
||||||
|
if from == to {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// int startWordIndex = wordIndex(fromIndex);
|
||||||
|
// if (startWordIndex >= wordsInUse)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
// int endWordIndex = wordIndex(toIndex - 1);
|
||||||
|
// if (endWordIndex >= wordsInUse) {
|
||||||
|
// toIndex = length();
|
||||||
|
// endWordIndex = wordsInUse - 1;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// long firstWordMask = WORD_MASK << fromIndex;
|
||||||
|
// long lastWordMask = WORD_MASK >>> -toIndex;
|
||||||
|
// if (startWordIndex == endWordIndex) {
|
||||||
|
// // Case 1: One word
|
||||||
|
// words[startWordIndex] &= ~(firstWordMask & lastWordMask);
|
||||||
|
// } else {
|
||||||
|
// // Case 2: Multiple words
|
||||||
|
// // Handle first word
|
||||||
|
// words[startWordIndex] &= ~firstWordMask;
|
||||||
|
|
||||||
|
// // Handle intermediate words, if any
|
||||||
|
// for (int i = startWordIndex+1; i < endWordIndex; i++)
|
||||||
|
// words[i] = 0;
|
||||||
|
|
||||||
|
// // Handle last word
|
||||||
|
// words[endWordIndex] &= ~lastWordMask;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// recalculateWordsInUse();
|
||||||
|
// checkInvariants();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl McBufReadable for BitSet {
|
impl McBufReadable for BitSet {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue