forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblock_data.rs
More file actions
205 lines (167 loc) · 5.48 KB
/
block_data.rs
File metadata and controls
205 lines (167 loc) · 5.48 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use super::WriteExt;
use byteorder::{LittleEndian, WriteBytesExt};
use failure::Error;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
/// The block state ID to use when a block
/// in the native file was not found
/// in the input file. This would happen
/// when the input file is an older version
/// than the native version.
pub const DEFAULT_STATE_ID: u16 = 1; // Stone
/// Deserializable struct representing a block
/// data report from Vanilla.
#[derive(Clone, Debug, Deserialize, Deref, DerefMut)]
pub struct BlockReport {
#[serde(flatten)]
pub blocks: IndexMap<String, Block>,
}
/// A block entry in the data report.
#[derive(Clone, Debug, Deserialize)]
pub struct Block {
pub states: Vec<State>,
pub properties: Option<BlockProperties>,
}
/// List of block properties.
#[derive(Clone, Debug, Deserialize, Deref, DerefMut)]
pub struct BlockProperties {
#[serde(flatten)]
pub props: HashMap<String, Vec<String>>,
}
/// A block state from the data report.
#[derive(Clone, Debug, Deserialize)]
pub struct State {
pub id: u16,
#[serde(default)]
pub default: bool,
pub properties: Option<StateProperties>,
}
/// Properties of a block state from the data report.
#[derive(Clone, Debug, Deserialize, Deref, DerefMut, Default)]
pub struct StateProperties {
#[serde(flatten)]
pub props: HashMap<String, String>,
}
pub fn generate_mappings_file(
input: &str,
output: &str,
native_input: &str,
proto: u32,
version: &str,
) -> Result<(), Error> {
info!(
"Generating mappings file {} using input report {} and native report {}",
output, input, native_input
);
let in_file = File::open(input)?;
let out_file = File::create(output)?;
let native_file = File::open(native_input)?;
info!("Parsing data files");
let report: BlockReport = serde_json::from_reader(BufReader::new(&in_file))?;
let native_report: BlockReport = serde_json::from_reader(BufReader::new(&native_file))?;
info!("Parsing successful");
let mut out = BufWriter::new(&out_file);
// Write header to output file
// See block_format.md
write_header(&mut out, version, proto, false)?;
// Go through native block types and attempt
// to find corresponding state ID in report.
// If it doesn't exist, just set to `DEFAULT_STATE_ID`.
let mut state_bufs = vec![];
for (string_id, block) in &native_report.blocks {
for state in &block.states {
let mut state_buf = vec![];
let props = state.properties.clone().unwrap_or_default();
let props = props.props;
// Try to find corresponding state ID, defaulting to `DEFAULT_STATE_ID`
let state_id = find_state_in_report(&report, string_id.as_str(), &props)
.unwrap_or(DEFAULT_STATE_ID);
state_buf.write_u16::<LittleEndian>(state.id)?; // Native ID
state_buf.write_u16::<LittleEndian>(state_id)?;
state_bufs.push(state_buf);
}
}
out.write_u32::<LittleEndian>(state_bufs.len() as u32)?;
for buf in state_bufs {
out.write_all(&buf)?;
}
out.flush()?;
info!("Mappings file generated successfully");
Ok(())
}
pub fn generate_native_mappings_file(
input: &str,
output: &str,
proto: u32,
version: &str,
) -> Result<(), Error> {
info!(
"Generating native mappings file {} using input report {}",
output, input
);
let in_file = File::open(input)?;
let out_file = File::create(output)?;
info!("Parsing data file");
let report: BlockReport = serde_json::from_reader(BufReader::new(&in_file))?;
info!("Parsing successful");
let mut out = BufWriter::new(&out_file);
write_header(&mut out, version, proto, true)?;
let mut count = 0;
let mut buf = vec![];
// Go through blocks and write to mappings
// file.
for (block_name, block) in &report.blocks {
for state in &block.states {
// Write name
buf.write_string(block_name.as_str())?;
// Write properties
let len = {
if let Some(props) = state.properties.as_ref() {
props.props.len()
} else {
0
}
};
buf.write_u32::<LittleEndian>(len as u32)?;
if let Some(props) = state.properties.as_ref() {
for (name, value) in &props.props {
buf.write_string(name.as_str())?;
buf.write_string(value.as_str())?;
}
}
// Write ID
buf.write_u16::<LittleEndian>(state.id)?;
count += 1;
}
}
out.write_u32::<LittleEndian>(count)?;
out.write_all(&buf)?;
info!("Mappings file generated successfully");
Ok(())
}
fn find_state_in_report(
report: &BlockReport,
name: &str,
props: &HashMap<String, String>,
) -> Option<u16> {
let block = report.blocks.get(name)?;
let state = block.states.iter().find(|state| match &state.properties {
None => props.is_empty(),
Some(state_props) => props == &state_props.props,
})?;
Some(state.id)
}
fn write_header<W: Write>(
out: &mut W,
version: &str,
proto: u32,
native: bool,
) -> Result<(), Error> {
out.write_all(b"FEATHER_BLOCK_DATA_FILE")?;
out.write_string(version)?;
out.write_u32::<LittleEndian>(proto)?;
out.write_u8(native as u8)?;
Ok(())
}