-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathmain.rs
More file actions
134 lines (122 loc) · 3.94 KB
/
main.rs
File metadata and controls
134 lines (122 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! This program is used to generate a bunch of code, including block state ID mappings
//! and corresponding Rust code. It reads from vanilla block.json
//! files. See `block_format.md` for more information.
#![forbid(unsafe_code, warnings)]
#[macro_use]
extern crate serde;
#[macro_use]
extern crate derive_deref;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate log;
#[macro_use]
extern crate quote;
mod biome;
mod block_data;
mod item;
mod item_to_block;
mod rust;
mod util;
pub use block_data::{
Block, BlockProperties, BlockReport, State, StateProperties, DEFAULT_STATE_ID,
};
use byteorder::{LittleEndian, WriteBytesExt};
use clap::App;
use failure::Error;
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::quote;
use std::fs::File;
use std::io::{BufReader, Write};
use std::process::exit;
use std::str::FromStr;
use syn::export::Span;
use syn::Ident;
fn main() {
simple_logger::init_with_level(log::Level::Info).unwrap();
if let Err(e) = run() {
error!("An error occurred: {}", e);
exit(1);
}
}
fn run() -> Result<(), Error> {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
match matches.subcommand_name() {
Some("block-mappings") => {
let args = matches.subcommand_matches("block-mappings").unwrap();
block_data::generate_mappings_file(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
args.value_of("native").unwrap(),
u32::from_str(args.value_of("proto").unwrap())?,
args.value_of("ver").unwrap(),
)?;
}
Some("native-block-mappings") => {
let args = matches.subcommand_matches("native-block-mappings").unwrap();
block_data::generate_native_mappings_file(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
u32::from_str(args.value_of("proto").unwrap())?,
args.value_of("ver").unwrap(),
)?;
}
Some("block-rust") => {
let args = matches.subcommand_matches("block-rust").unwrap();
rust::generate_rust_code(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
)?;
}
Some("item-mappings") => {
let args = matches.subcommand_matches("item-mappings").unwrap();
item::generate_mappings_file(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
)?;
}
Some("item-rust") => {
let args = matches.subcommand_matches("item-rust").unwrap();
item::generate_rust(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
)?;
}
Some("items-to-blocks") => {
let args = matches.subcommand_matches("items-to-blocks").unwrap();
item_to_block::generate_mappings(
args.value_of("blocks").unwrap(),
args.value_of("items").unwrap(),
args.value_of("output").unwrap(),
)?;
}
Some("biomes") => {
let args = matches.subcommand_matches("biomes").unwrap();
biome::generate_rust(
args.value_of("input").unwrap(),
args.value_of("output").unwrap(),
)?;
}
Some(s) => {
error!("Invalid subcommand {}", s);
return Ok(());
}
None => {
error!("No subcommand specified");
return Ok(());
}
}
Ok(())
}
pub trait WriteExt {
fn write_string(&mut self, x: &str) -> std::io::Result<()>;
}
impl<W: Write> WriteExt for W {
fn write_string(&mut self, x: &str) -> std::io::Result<()> {
self.write_u32::<LittleEndian>(x.len() as u32)?;
self.write_all(x.as_bytes())?;
Ok(())
}
}