resource: update block type code generator for Rust

This commit is contained in:
Matthias Schiffer 2023-02-07 22:09:14 +01:00
parent 6379472282
commit 718ecf5909
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
6 changed files with 6399 additions and 12 deletions

21
Cargo.lock generated
View file

@ -102,6 +102,26 @@ version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "enumflags2"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e75d4cd21b95383444831539909fbb14b9dc3fdceb2a6f5d36577329a1f55ccb"
dependencies = [
"enumflags2_derive",
]
[[package]]
name = "enumflags2_derive"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f58dc3c5e468259f19f2d46304a6b28f1c3d034442e14b322d2b850e36f6d5ae"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.2.8" version = "0.2.8"
@ -210,6 +230,7 @@ dependencies = [
"anyhow", "anyhow",
"bytemuck", "bytemuck",
"clap", "clap",
"enumflags2",
"fastnbt", "fastnbt",
"flate2", "flate2",
"itertools", "itertools",

View file

@ -11,6 +11,7 @@ default-run = "minedmap"
anyhow = "1.0.68" anyhow = "1.0.68"
bytemuck = "1.13.0" bytemuck = "1.13.0"
clap = { version = "4.1.4", features = ["derive"] } clap = { version = "4.1.4", features = ["derive"] }
enumflags2 = "0.7.5"
fastnbt = "2.3.2" fastnbt = "2.3.2"
flate2 = "1.0.25" flate2 = "1.0.25"
itertools = "0.10.5" itertools = "0.10.5"

View file

@ -6,7 +6,7 @@ import sys
if len(sys.argv) != 3: if len(sys.argv) != 3:
sys.exit('Usage: extract.py <colors.json> <BlockType.inc.cpp>') sys.exit('Usage: extract.py <colors.json> <block_types.rs>')
with open(sys.argv[1]) as f: with open(sys.argv[1]) as f:
colors = json.load(f) colors = json.load(f)
@ -14,29 +14,33 @@ with open(sys.argv[1]) as f:
output = {} output = {}
with open(sys.argv[2], 'w') as f: with open(sys.argv[2], 'w') as f:
print('use enumflags2::make_bitflags;', file=f);
print('', file=f)
print('use super::*;', file=f)
print('', file=f)
print('pub const BLOCK_TYPES: &[(&str, BlockType)] = &[', file=f)
for name, info in colors.items(): for name, info in colors.items():
flags = [] flags = []
if info['opaque']: if info['opaque']:
flags.append('BLOCK_OPAQUE') flags.append('Opaque')
if info['grass']: if info['grass']:
flags.append('BLOCK_GRASS') flags.append('Grass')
if info['foliage']: if info['foliage']:
flags.append('BLOCK_FOLIAGE') flags.append('Foliage')
if info['birch']: if info['birch']:
flags.append('BLOCK_BIRCH') flags.append('Birch')
if info['spruce']: if info['spruce']:
flags.append('BLOCK_SPRUCE') flags.append('Spruce')
if info['water']: if info['water']:
flags.append('BLOCK_WATER') flags.append('Water')
if flags: flags = 'make_bitflags!(BlockFlags::{' + '|'.join(flags) + '})'
flags = '|'.join(flags)
else:
flags = '0'
print('{"%s", {%s, {%u, %u, %u}}},' % ( print('\t("%s", BlockType { flags: %s, color: BlockColor(%u, %u, %u) }),' % (
name, name,
flags, flags,
info['color']['r'], info['color']['r'],
info['color']['g'], info['color']['g'],
info['color']['b'], info['color']['b'],
), file=f) ), file=f)
print('];', file=f)

View file

@ -1,3 +1,4 @@
pub mod io; pub mod io;
pub mod resource;
pub mod types; pub mod types;
pub mod world; pub mod world;

6327
src/resource/block_types.rs Normal file

File diff suppressed because it is too large Load diff

33
src/resource/mod.rs Normal file
View file

@ -0,0 +1,33 @@
mod block_types;
use std::collections::HashMap;
use enumflags2::{bitflags, BitFlags};
#[bitflags]
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BlockFlags {
Opaque,
Grass,
Foliage,
Birch,
Spruce,
Water,
}
#[derive(Debug, Clone, Copy)]
pub struct BlockColor(pub u8, pub u8, pub u8);
#[derive(Debug, Clone, Copy)]
pub struct BlockType {
pub flags: BitFlags<BlockFlags>,
pub color: BlockColor,
}
pub fn get_block_types() -> HashMap<String, BlockType> {
block_types::BLOCK_TYPES
.iter()
.map(|(k, v)| (String::from(*k), *v))
.collect()
}